github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/transformer/btcbase/btcbase.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package btcbase 转换基于比特币地址规则的币种
     6  //使用此规则的币种有:BTC、BCH、LTC、ZEC、USDT、 TRC
     7  package btcbase
     8  
     9  import (
    10  	"crypto/sha256"
    11  	"fmt"
    12  
    13  	"github.com/turingchain2020/turingchain/common/address"
    14  
    15  	"github.com/turingchain2020/turingchain/common/crypto"
    16  	"github.com/mr-tron/base58/base58"
    17  	"golang.org/x/crypto/ripemd160"
    18  )
    19  
    20  // btcBaseTransformer 转换基于比特币地址规则的币种实现类
    21  type btcBaseTransformer struct {
    22  	prefix []byte //版本号前缀
    23  }
    24  
    25  // Base58ToByte base58转字节形式
    26  func (t btcBaseTransformer) Base58ToByte(str string) (bin []byte, err error) {
    27  	bin, err = base58.Decode(str)
    28  	return
    29  }
    30  
    31  // ByteToBase58 字节形式转base58编码
    32  func (t btcBaseTransformer) ByteToBase58(bin []byte) (str string) {
    33  	str = base58.Encode(bin)
    34  	return
    35  }
    36  
    37  //TODO: 根据私钥类型进行判断,选择输出压缩或非压缩公钥
    38  
    39  // PrivKeyToPub 32字节私钥生成压缩格式公钥
    40  func (t btcBaseTransformer) PrivKeyToPub(keyTy uint32, priv []byte) (pub []byte, err error) {
    41  	if len(priv) != 32 && len(priv) != 64 {
    42  		return nil, fmt.Errorf("invalid priv key byte")
    43  	}
    44  	//pub = secp256k1.PubkeyFromSeckey(priv)
    45  
    46  	edcrypto, err := crypto.New(crypto.GetName(int(keyTy)))
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	edkey, err := edcrypto.PrivKeyFromBytes(priv[:])
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return edkey.PubKey().Bytes(), nil
    55  
    56  }
    57  
    58  //checksum: first four bytes of double-SHA256.
    59  func checksum(input []byte) (cksum [4]byte) {
    60  	h := sha256.New()
    61  	_, err := h.Write(input)
    62  	if err != nil {
    63  		return
    64  	}
    65  	intermediateHash := h.Sum(nil)
    66  	h.Reset()
    67  	_, err = h.Write(intermediateHash)
    68  	if err != nil {
    69  		return
    70  	}
    71  	finalHash := h.Sum(nil)
    72  	copy(cksum[:], finalHash[:])
    73  	return
    74  }
    75  
    76  // PubKeyToAddress 传入压缩或非压缩形式的公钥,生成base58编码的地址
    77  //(压缩和非压缩形式的公钥生成的地址是不同的,但都是合法的)
    78  func (t btcBaseTransformer) PubKeyToAddress(pub []byte) (addr string, err error) {
    79  	if len(pub) != 33 && len(pub) != 65 { //压缩格式 与 非压缩格式
    80  		//return "", fmt.Errorf("invalid public key byte:%v", len(pub))
    81  		return address.PubKeyToAddr(pub), nil
    82  	}
    83  
    84  	sha256h := sha256.New()
    85  	_, err = sha256h.Write(pub)
    86  	if err != nil {
    87  		return "", err
    88  	}
    89  	//160hash
    90  	ripemd160h := ripemd160.New()
    91  	_, err = ripemd160h.Write(sha256h.Sum([]byte("")))
    92  	if err != nil {
    93  		return "", err
    94  	}
    95  	//添加版本号
    96  	hash160res := append(t.prefix, ripemd160h.Sum([]byte(""))...)
    97  
    98  	//添加校验码
    99  	cksum := checksum(hash160res)
   100  	address := append(hash160res, cksum[:]...)
   101  
   102  	//地址进行base58编码
   103  	addr = base58.Encode(address)
   104  	return
   105  }