github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/transformer/btcbase/btcbase_test.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  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/turingchain2020/turingchain/types"
    13  
    14  	"github.com/turingchain2020/turingchain/wallet/bipwallet/transformer"
    15  	"github.com/mr-tron/base58/base58"
    16  )
    17  
    18  //TODO: 更新USDT的测试数据
    19  var testPrivkey = map[string]string{
    20  	"BTC":  "L3U5kJqHAPXzRGeHvtV5HXTwyjmiAvYG8bk42zswDPz7XwUJ6XfP",
    21  	"BCH":  "L3U5kJqHAPXzRGeHvtV5HXTwyjmiAvYG8bk42zswDPz7XwUJ6XfP",
    22  	"LTC":  "T4qcryHFU8c1kKNWYCFymCafmaVasjm6doJwga5CSDtENenrNwPj",
    23  	"ZEC":  "5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj",
    24  	"USDT": "L3U5kJqHAPXzRGeHvtV5HXTwyjmiAvYG8bk42zswDPz7XwUJ6XfP",
    25  	"TRC":  "L3U5kJqHAPXzRGeHvtV5HXTwyjmiAvYG8bk42zswDPz7XwUJ6XfP",
    26  }
    27  
    28  var testPrivByte = make(map[string][]byte)
    29  
    30  var ansPubkey = map[string]string{
    31  	"BTC":  "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    32  	"BCH":  "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    33  	"LTC":  "0325b737cdf14ec8885b578fd901dbb8e2c1020863865656dc74377df4fee67891",
    34  	"ZEC":  "030b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a744",
    35  	"USDT": "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    36  	"TRC":  "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    37  }
    38  
    39  var testPubkey = map[string]string{
    40  	"BTC":  "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    41  	"BCH":  "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    42  	"LTC":  "0325b737cdf14ec8885b578fd901dbb8e2c1020863865656dc74377df4fee67891",
    43  	"ZEC":  "040b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a7447b5f0bba9e01e6fe4735c8383e6e7a3347a0fd72381b8f797a19f694054e5a69",
    44  	"USDT": "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    45  	"TRC":  "0367476225d991b4850b64f751bdb58a65904b70dd09f6cb30f31855f45302ac6a",
    46  }
    47  
    48  var ansAddress = map[string]string{
    49  	"BTC":  "1MeionVMkdVuPV82BAXSZsHyxtXNxLFVN8",
    50  	"BCH":  "1MeionVMkdVuPV82BAXSZsHyxtXNxLFVN8",
    51  	"LTC":  "LTFdikYPHPKBbkgfxcvDN9g8x6jgRCmDrE",
    52  	"ZEC":  "t1h8SqgtM3QM5e2M8EzhhT1yL2PXXtA6oqe",
    53  	"USDT": "1MeionVMkdVuPV82BAXSZsHyxtXNxLFVN8",
    54  	"TRC":  "1MeionVMkdVuPV82BAXSZsHyxtXNxLFVN8",
    55  }
    56  
    57  //测试基于BTC规则的币种
    58  func TestBtcBaseTransformer(t *testing.T) {
    59  	initialBTCPrivByte()
    60  	for name := range testPrivkey {
    61  		testPrivToPub(t, name)
    62  		testPubToAddr(t, name)
    63  	}
    64  }
    65  
    66  //将base58编码的私钥初始化成32字节的形式
    67  func initialBTCPrivByte() (err error) {
    68  	for name, privKey := range testPrivkey {
    69  		privByte, err := base58.Decode(privKey)
    70  		if err != nil {
    71  			return fmt.Errorf("initial priv byte error: %s", err)
    72  		}
    73  		testPrivByte[name] = privByte[1:33]
    74  	}
    75  	return nil
    76  }
    77  
    78  //测试私钥生成公钥
    79  func testPrivToPub(t *testing.T, name string) {
    80  	coinTrans, err := transformer.New(name)
    81  	if err != nil {
    82  		t.Errorf("new %s transformer error: %s", name, err)
    83  	}
    84  	pubByte, err := coinTrans.PrivKeyToPub(types.SECP256K1, testPrivByte[name])
    85  	if err != nil {
    86  		t.Errorf("%s PrivKeyToPub error: %s", name, err)
    87  	}
    88  	if hex.EncodeToString(pubByte) == ansPubkey[name] {
    89  		t.Logf("%s public key match", name)
    90  	} else {
    91  		t.Errorf("%s public key mismatch: want: %s have: %x", name, ansPubkey[name], pubByte)
    92  	}
    93  }
    94  
    95  //测试公钥生成地址
    96  func testPubToAddr(t *testing.T, name string) {
    97  	coinTrans, err := transformer.New(name)
    98  	if err != nil {
    99  		t.Errorf("new %s transformer error: %s", name, err)
   100  	}
   101  	pubByte, err := hex.DecodeString(testPubkey[name])
   102  	if err != nil {
   103  		t.Errorf("generate %s public key byte error: %s", name, err)
   104  	}
   105  	genAddr, err := coinTrans.PubKeyToAddress(pubByte)
   106  	if err != nil {
   107  		t.Errorf("%s PubKeyToAddress error: %s", name, err)
   108  	}
   109  	if genAddr == ansAddress[name] {
   110  		t.Logf("%s address match", name)
   111  	} else {
   112  		t.Errorf("%s address mismatch: want: %s have %s", name, ansAddress[name], genAddr)
   113  	}
   114  }