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

     1  package bipwallet
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/turingchain2020/turingchain/types"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var (
    12  	mnem          = "叛 促 映 的 庆 站 袖 火 赋 仇 徙 酯 完 砖 乐 据 划 明 犯 谓 杂 模 卷 现"
    13  	ed25519Pub    = "9039742b7dc5553ede2cb3bb61b73bdf5df3b21062b1df75109ba045766cb966"
    14  	ed25519Addr   = "1Ptp353sEjjFa37UToFkeFRXSkNTXnxNM3"
    15  	secp256k1Pub  = "03b2cb62dd207277abcda55523c467edba786db21106446e040fe2d3515053c8e5"
    16  	secp256k1Addr = "17L828pQH9QGaZe1SfoRXqVau8BcyGJVgP"
    17  	sm2Pub        = "03c4b4e32badcd3cb67bd43cf7d597e993ba2e4b5d121afbf22b4c5f7f064dea97"
    18  	sm2Addr       = "1Nu3bt6GsoZb9szwP6oLm2yx5Dm9fK6mAp"
    19  )
    20  
    21  func TestNewMnemonicString(t *testing.T) {
    22  	mne, err := NewMnemonicString(1, 128)
    23  	assert.Nil(t, err)
    24  	t.Log(mne)
    25  	mne, err = NewMnemonicString(0, 128)
    26  	assert.Nil(t, err)
    27  	t.Log(mne)
    28  
    29  	_, err = NewMnemonicString(0, 129)
    30  	assert.NotNil(t, err)
    31  }
    32  
    33  func TestNewWalletFromMnemonic(t *testing.T) {
    34  	_, err := NewWalletFromMnemonic(TypeYcc, types.ED25519, mnem+"test")
    35  	assert.NotNil(t, err)
    36  }
    37  
    38  func TestEd25519PrivPub(t *testing.T) {
    39  	wallet, err := NewWalletFromMnemonic(TypeYcc, types.ED25519, mnem)
    40  	assert.Nil(t, err)
    41  	priv, pub, err := wallet.NewKeyPair(0)
    42  	assert.Nil(t, err)
    43  	assert.Equal(t, len(priv), 64)
    44  	assert.Equal(t, hex.EncodeToString(pub), ed25519Pub)
    45  
    46  	//test address
    47  	addr, err := PubToAddress(pub)
    48  	assert.Nil(t, err)
    49  	assert.Equal(t, addr, ed25519Addr)
    50  	naddr, err := wallet.NewAddress(0)
    51  	assert.Nil(t, err)
    52  	assert.Equal(t, addr, naddr)
    53  	tpub, err := PrivkeyToPub(TypeYcc, types.ED25519, priv)
    54  	assert.Nil(t, err)
    55  	assert.Equal(t, tpub, pub)
    56  	_, err = PrivkeyToPub(TypeYcc+1, types.ED25519, priv)
    57  	assert.NotNil(t, err)
    58  
    59  }
    60  
    61  func TestSecp256k1PrivPub(t *testing.T) {
    62  	wallet, err := NewWalletFromMnemonic(TypeYcc, types.SECP256K1, mnem)
    63  	assert.Nil(t, err)
    64  	priv, pub, err := wallet.NewKeyPair(0)
    65  	assert.Nil(t, err)
    66  	assert.Equal(t, 32, len(priv))
    67  	assert.Equal(t, hex.EncodeToString(pub), secp256k1Pub)
    68  
    69  	//test address
    70  	addr, err := PubToAddress(pub)
    71  	assert.Nil(t, err)
    72  	assert.Equal(t, addr, secp256k1Addr)
    73  	naddr, err := wallet.NewAddress(0)
    74  	assert.Nil(t, err)
    75  	assert.Equal(t, addr, naddr)
    76  
    77  	tpub, err := PrivkeyToPub(TypeYcc, types.SECP256K1, priv)
    78  	assert.Nil(t, err)
    79  	assert.Equal(t, tpub, pub)
    80  }
    81  
    82  func TestSm2PrivPub(t *testing.T) {
    83  	wallet, err := NewWalletFromMnemonic(TypeYcc, types.AuthSM2, mnem)
    84  	assert.Nil(t, err)
    85  	priv, pub, err := wallet.NewKeyPair(0)
    86  	assert.Nil(t, err)
    87  	assert.Equal(t, 32, len(priv))
    88  	assert.Equal(t, sm2Pub, hex.EncodeToString(pub))
    89  
    90  	//test address
    91  	addr, err := PubToAddress(pub)
    92  	assert.Nil(t, err)
    93  	assert.Equal(t, sm2Addr, addr)
    94  	naddr, err := wallet.NewAddress(0)
    95  	assert.Nil(t, err)
    96  	assert.Equal(t, addr, naddr)
    97  
    98  	tpub, err := PrivkeyToPub(TypeYcc, types.AuthSM2, priv)
    99  	assert.Nil(t, err)
   100  	assert.Equal(t, tpub, pub)
   101  }