github.com/0xsequence/ethkit@v1.25.0/ethwallet/ethwallet_test.go (about) 1 package ethwallet_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/0xsequence/ethkit/ethwallet" 8 "github.com/0xsequence/ethkit/go-ethereum/common/hexutil" 9 "github.com/0xsequence/ethkit/go-ethereum/crypto" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestWalletRandom(t *testing.T) { 14 wallet, err := ethwallet.NewWalletFromRandomEntropy() 15 assert.NoError(t, err) 16 assert.NotNil(t, wallet) 17 } 18 19 func TestWalletSignMessage(t *testing.T) { 20 wallet, err := ethwallet.NewWalletFromMnemonic("dose weasel clever culture letter volume endorse used harvest ripple circle install") 21 assert.NoError(t, err) 22 23 address := wallet.Address() 24 assert.NoError(t, err) 25 assert.Equal(t, "0xb59ba5A13f0fb106EA6094a1F69786AA69be1424", address.String()) 26 27 sig, err := wallet.SignMessage([]byte("hi")) 28 assert.NoError(t, err) 29 30 sigHash := hexutil.Encode(sig) 31 expected := "0xebe541eda2d15b7abc8ff48a052be03b6ae8f05c1a88ac0483af741a896ab75945ed5dddc8a839ed1e78f0591d8878181c5d00a79d7a4f0778b19c34dee6e8a41c" 32 assert.Equal(t, expected, sigHash) 33 34 // Lets validate the signature for good measure 35 valid, err := ethwallet.ValidateEthereumSignature(address.String(), []byte("hi"), sigHash) 36 assert.NoError(t, err) 37 assert.True(t, valid) 38 } 39 40 func TestWalletSignMessageExistingPrefix(t *testing.T) { 41 wallet, err := ethwallet.NewWalletFromMnemonic("dose weasel clever culture letter volume endorse used harvest ripple circle install") 42 assert.NoError(t, err) 43 44 address := wallet.Address() 45 assert.NoError(t, err) 46 assert.Equal(t, "0xb59ba5A13f0fb106EA6094a1F69786AA69be1424", address.String()) 47 48 // Sign the same message as in test above, but this time we will include 49 // the prefix as part of the message. We are testing that wallet.SignMessage() 50 // will adhere to EIP191 in both cases. 51 message := "hi" 52 message = fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message) 53 54 sig, err := wallet.SignMessage([]byte(message)) 55 assert.NoError(t, err) 56 57 sigHash := hexutil.Encode(sig) 58 expected := "0xebe541eda2d15b7abc8ff48a052be03b6ae8f05c1a88ac0483af741a896ab75945ed5dddc8a839ed1e78f0591d8878181c5d00a79d7a4f0778b19c34dee6e8a41c" 59 assert.Equal(t, expected, sigHash) 60 61 // Lets validate the signature for good measure 62 valid, err := ethwallet.ValidateEthereumSignature(address.String(), []byte("hi"), sigHash) 63 assert.NoError(t, err) 64 assert.True(t, valid) 65 } 66 67 func TestWalletSignMessageFromPrivateKey(t *testing.T) { 68 wallet, err := ethwallet.NewWalletFromPrivateKey("3c121e5b2c2b2426f386bfc0257820846d77610c20e0fd4144417fb8fd79bfb8") 69 assert.NoError(t, err) 70 71 address := wallet.Address() 72 assert.NoError(t, err) 73 assert.Equal(t, "0x95a7D93FEf729ed829C761FF0e035BB6Dd2c7052", address.String()) 74 75 sig, err := wallet.SignMessage([]byte("hi")) 76 assert.NoError(t, err) 77 78 sigHash := hexutil.Encode(sig) 79 expected := "0x14c0b4cbb654b3da1140cdf5c000bfbf5db810f5a7fb339dd4514230d20e1bae4bf9ab78b6431b975260676a020cb4f7c164161776ee6fedbce39eb4103b257f1c" 80 assert.Equal(t, expected, sigHash) 81 82 // Lets validate the signature for good measure 83 valid, err := ethwallet.ValidateEthereumSignature(address.String(), []byte("hi"), sigHash) 84 assert.NoError(t, err) 85 assert.True(t, valid) 86 87 valid, err = ethwallet.IsValid191Signature(address, []byte("hi"), sig) 88 assert.NoError(t, err) 89 assert.True(t, valid) 90 91 } 92 93 func TestWalletSignAndRecover(t *testing.T) { 94 wallet, err := ethwallet.NewWalletFromPrivateKey("3c121e5b2c2b2426f386bfc0257820846d77610c20e0fd4144417fb8fd79bfb8") 95 assert.NoError(t, err) 96 97 address := wallet.Address() 98 assert.NoError(t, err) 99 assert.Equal(t, "0x95a7D93FEf729ed829C761FF0e035BB6Dd2c7052", address.String()) 100 101 sig, err := wallet.SignMessage([]byte("hi")) 102 assert.NoError(t, err) 103 104 recoveredAddress, err := ethwallet.RecoverAddress([]byte("hi"), sig) 105 assert.NoError(t, err) 106 107 assert.Equal(t, address, recoveredAddress) 108 109 valid, err := wallet.IsValidSignature([]byte("hi"), sig) 110 assert.NoError(t, err) 111 assert.True(t, valid) 112 } 113 114 func TestWalletSignDataAndRecover(t *testing.T) { 115 wallet, err := ethwallet.NewWalletFromPrivateKey("3c121e5b2c2b2426f386bfc0257820846d77610c20e0fd4144417fb8fd79bfb8") 116 assert.NoError(t, err) 117 118 address := wallet.Address() 119 assert.NoError(t, err) 120 assert.Equal(t, "0x95a7D93FEf729ed829C761FF0e035BB6Dd2c7052", address.String()) 121 122 data := []byte("hi") 123 sig, err := wallet.SignData(data) 124 assert.NoError(t, err) 125 126 recoveredAddress, err := ethwallet.RecoverAddressFromDigest(crypto.Keccak256(data), sig) 127 assert.NoError(t, err) 128 129 assert.Equal(t, address, recoveredAddress) 130 }