github.com/0xsequence/ethkit@v1.25.0/ethwallet/hdnode_test.go (about)

     1  package ethwallet_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/0xsequence/ethkit/ethwallet"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestHDNodeMnemonicAndEntropy(t *testing.T) {
    11  	testMnemonic := "outdoor sentence roast truly flower surface power begin ocean silent debate funny"
    12  
    13  	entropy, err := ethwallet.MnemonicToEntropy(testMnemonic)
    14  	assert.NoError(t, err)
    15  	assert.NotEmpty(t, entropy)
    16  
    17  	mnemonic, err := ethwallet.EntropyToMnemonic(entropy)
    18  	assert.NoError(t, err)
    19  	assert.Equal(t, testMnemonic, mnemonic)
    20  }
    21  
    22  func TestHDNode(t *testing.T) {
    23  	hdnode, err := ethwallet.NewHDNodeFromRandomEntropy(ethwallet.EntropyBitSize12WordMnemonic, nil)
    24  	assert.NoError(t, err)
    25  	assert.NotNil(t, hdnode)
    26  	assert.NotEmpty(t, hdnode.Mnemonic())
    27  	assert.NotEqual(t, hdnode.Address().Hex(), "0x0000000000000000000000000000000000000000")
    28  
    29  	hdnode2, err := ethwallet.NewHDNodeFromMnemonic(hdnode.Mnemonic(), nil)
    30  	assert.NoError(t, err)
    31  	assert.NotNil(t, hdnode2)
    32  	assert.Equal(t, hdnode.Address().Hex(), hdnode2.Address().Hex())
    33  }
    34  
    35  func TestHDNodeDerivationPath(t *testing.T) {
    36  	testMnemonic := "outdoor sentence roast truly flower surface power begin ocean silent debate funny"
    37  
    38  	hdnode, err := ethwallet.NewHDNodeFromMnemonic(testMnemonic, nil)
    39  	assert.NoError(t, err)
    40  	assert.NotNil(t, hdnode)
    41  	assert.Equal(t, "0xe0C9828dee3411A28CcB4bb82a18d0aAd24489E0", hdnode.Address().Hex())
    42  	assert.Equal(t, testMnemonic, hdnode.Mnemonic())
    43  
    44  	err = hdnode.DeriveAccountIndex(1)
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, "0x9e02d584c27Ec74f832154985046C0f3c5E0f724", hdnode.Address().Hex())
    47  
    48  	err = hdnode.DeriveAccountIndex(0)
    49  	assert.NoError(t, err)
    50  	assert.Equal(t, "0xe0C9828dee3411A28CcB4bb82a18d0aAd24489E0", hdnode.Address().Hex())
    51  }
    52  
    53  func TestHDNodeDerivationFailsWithInvalidMnemonic(t *testing.T) {
    54  	testInvalidMnemonics := []string{
    55  		// one word too short
    56  		"sentence roast truly flower surface power begin ocean silent debate funny",
    57  
    58  		// first word is jamaican patois, not valid bip39 word
    59  		"wahgwaan sentence roast truly flower surface power begin ocean silent debate funny",
    60  
    61  		// one word too long
    62  		"outdoor outdoor sentence roast truly flower surface power begin ocean silent debate funny",
    63  
    64  		// invalid checksum word
    65  		"outdoor sentence roast truly flower surface power begin ocean silent debate outdoor",
    66  	}
    67  
    68  	for _, mnemonic := range testInvalidMnemonics {
    69  		hdnode, err := ethwallet.NewHDNodeFromMnemonic(mnemonic, nil)
    70  		assert.Errorf(t, err, "Expected error for invalid mnemonic '%v'", mnemonic)
    71  		assert.Nilf(t, hdnode, "Expected nil hdnode for invalid mnemonic '%v'", mnemonic)
    72  	}
    73  }