github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/crypto/keys/hd/fundraiser_test.go (about) 1 package hd 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 bip39 "github.com/cosmos/go-bip39" 13 14 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto" 15 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/secp256k1" 16 ) 17 18 type addrData struct { 19 Mnemonic string 20 Master string 21 Seed string 22 Priv string 23 Pub string 24 Addr string 25 } 26 27 func TestFullFundraiserPath(t *testing.T) { 28 require.Equal(t, "m/44'/118'/0'/0/0", NewFundraiserParams(0, 118, 0).String()) 29 } 30 31 func initFundraiserTestVectors(t *testing.T) []addrData { 32 // NOTE: atom fundraiser address 33 // var hdPath string = "m/44'/118'/0'/0/0" 34 var hdToAddrTable []addrData 35 36 b, err := ioutil.ReadFile("test.json") 37 if err != nil { 38 t.Fatalf("could not read fundraiser test vector file (test.json): %s", err) 39 } 40 41 err = json.Unmarshal(b, &hdToAddrTable) 42 if err != nil { 43 t.Fatalf("could not decode test vectors (test.json): %s", err) 44 } 45 return hdToAddrTable 46 } 47 48 func TestFundraiserCompatibility(t *testing.T) { 49 hdToAddrTable := initFundraiserTestVectors(t) 50 51 for i, d := range hdToAddrTable { 52 privB, _ := hex.DecodeString(d.Priv) 53 pubB, _ := hex.DecodeString(d.Pub) 54 addrB, _ := hex.DecodeString(d.Addr) 55 seedB, _ := hex.DecodeString(d.Seed) 56 masterB, _ := hex.DecodeString(d.Master) 57 58 seed := bip39.NewSeed(d.Mnemonic, "") 59 60 t.Log("================================") 61 t.Logf("ROUND: %d MNEMONIC: %s", i, d.Mnemonic) 62 63 master, ch := ComputeMastersFromSeed(seed) 64 priv, err := DerivePrivateKeyForPath(master, ch, "m/44'/118'/0'/0/0") 65 require.NoError(t, err) 66 pub := secp256k1.PrivKeySecp256k1(priv).PubKey() 67 68 t.Log("\tNODEJS GOLANG\n") 69 t.Logf("SEED \t%X %X\n", seedB, seed) 70 t.Logf("MSTR \t%X %X\n", masterB, master) 71 t.Logf("PRIV \t%X %X\n", privB, priv) 72 t.Logf("PUB \t%X %X\n", pubB, pub) 73 74 require.Equal(t, seedB, seed) 75 require.Equal(t, master[:], masterB, fmt.Sprintf("Expected masters to match for %d", i)) 76 require.Equal(t, priv[:], privB, "Expected priv keys to match") 77 var pubBFixed [33]byte 78 copy(pubBFixed[:], pubB) 79 require.Equal(t, pub, secp256k1.PubKeySecp256k1(pubBFixed), fmt.Sprintf("Expected pub keys to match for %d", i)) 80 81 addr := pub.Address() 82 t.Logf("ADDR \t%X %X\n", addrB, addr) 83 require.Equal(t, addr, crypto.Address(addrB), fmt.Sprintf("Expected addresses to match %d", i)) 84 85 } 86 }