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