15 "github.com/stretchr/testify/assert"
16 "github.com/tyler-smith/go-bip39"
18 //"github.com/btcsuite/btcd/chaincfg"
19 //"github.com/btcsuite/btcutil/hdkeychain"
20 //"github.com/mndrix/btcutil"
21 //"github.com/tyler-smith/go-bip32"
23 "github.com/tendermint/go-crypto"
26 type addrData struct {
35 // NOTE: atom fundraiser address
36 // var hdPath string = "m/44'/118'/0'/0/0"
37 var hdToAddrTable []addrData
41 b, err := ioutil.ReadFile("test.json")
47 err = json.Unmarshal(b, &hdToAddrTable)
54 func TestHDToAddr(t *testing.T) {
56 for i, d := range hdToAddrTable {
57 privB, _ := hex.DecodeString(d.Priv)
58 pubB, _ := hex.DecodeString(d.Pub)
59 addrB, _ := hex.DecodeString(d.Addr)
60 seedB, _ := hex.DecodeString(d.Seed)
61 masterB, _ := hex.DecodeString(d.Master)
63 seed := bip39.NewSeed(d.Mnemonic, "")
65 fmt.Println("================================")
66 fmt.Println("ROUND:", i, "MNEMONIC:", d.Mnemonic)
68 // master, priv, pub := tylerSmith(seed)
69 // master, priv, pub := btcsuite(seed)
70 master, priv, pub := gocrypto(seed)
72 fmt.Printf("\tNODEJS GOLANG\n")
73 fmt.Printf("SEED \t%X %X\n", seedB, seed)
74 fmt.Printf("MSTR \t%X %X\n", masterB, master)
75 fmt.Printf("PRIV \t%X %X\n", privB, priv)
76 fmt.Printf("PUB \t%X %X\n", pubB, pub)
79 assert.Equal(t, master, masterB, fmt.Sprintf("Expected masters to match for %d", i))
80 assert.Equal(t, priv, privB, "Expected priv keys to match")
81 assert.Equal(t, pub, pubB, fmt.Sprintf("Expected pub keys to match for %d", i))
83 var pubT crypto.PubKeySecp256k1
85 addr := pubT.Address()
86 fmt.Printf("ADDR \t%X %X\n", addrB, addr)
87 assert.Equal(t, addr, addrB, fmt.Sprintf("Expected addresses to match %d", i))
92 func TestReverseBytes(t *testing.T) {
93 tests := [...]struct {
97 {[]byte(""), []byte("")},
99 {[]byte("Tendermint"), []byte("tnimredneT")},
100 {[]byte("T"), []byte("T")},
101 {[]byte("Te"), []byte("eT")},
104 for i, tt := range tests {
105 got := ReverseBytes(tt.v)
106 if !bytes.Equal(got, tt.want) {
107 t.Errorf("#%d:\ngot= (%x)\nwant=(%x)", i, got, tt.want)
113 func ifExit(err error, n int) {
121 func gocrypto(seed []byte) ([]byte, []byte, []byte) {
123 _, priv, ch, _ := ComputeMastersFromSeed(string(seed))
125 privBytes := DerivePrivateKeyForPath(
131 pubBytes := PubKeyBytesFromPrivKeyBytes(privBytes, true)
133 return HexDecode(priv), privBytes, pubBytes
137 func btcsuite(seed []byte) ([]byte, []byte, []byte) {
139 masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
141 hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
142 hmac.Write([]byte(seed))
143 intermediary := hmac.Sum(nil)
145 curve := btcutil.Secp256k1()
146 curveParams := curve.Params()
148 // Split it into our key and chain code
149 keyBytes := intermediary[:32]
150 fmt.Printf("\t%X\n", keyBytes)
151 fmt.Printf("\t%X\n", curveParams.N.Bytes())
152 keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes))
153 fmt.Printf("\t%d\n", keyInt)
155 fh := hdkeychain.HardenedKeyStart
156 k, err := masterKey.Child(uint32(fh + 44))
158 k, err = k.Child(uint32(fh + 118))
160 k, err = k.Child(uint32(fh + 0))
162 k, err = k.Child(uint32(0))
164 k, err = k.Child(uint32(0))
166 ecpriv, err := k.ECPrivKey()
168 ecpub, err := k.ECPubKey()
171 priv := ecpriv.Serialize()
172 pub := ecpub.SerializeCompressed()
173 mkey, _ := masterKey.ECPrivKey()
174 return mkey.Serialize(), priv, pub
177 // return priv and pub
178 func tylerSmith(seed []byte) ([]byte, []byte, []byte) {
179 masterKey, err := bip32.NewMasterKey(seed)
181 hmac := hmac.New(sha512.New, []byte("Bitcoin seed"))
182 hmac.Write([]byte(seed))
183 intermediary := hmac.Sum(nil)
185 curve := btcutil.Secp256k1()
186 curveParams := curve.Params()
188 // Split it into our key and chain code
189 keyBytes := intermediary[:32]
190 fmt.Printf("\t%X\n", keyBytes)
191 fmt.Printf("\t%X\n", curveParams.N.Bytes())
192 keyInt, _ := binary.ReadVarint(bytes.NewBuffer(keyBytes))
193 fmt.Printf("\t%d\n", keyInt)
197 fh := bip32.FirstHardenedChild
198 k, err := masterKey.NewChildKey(fh + 44)
200 k, err = k.NewChildKey(fh + 118)
202 k, err = k.NewChildKey(fh + 0)
204 k, err = k.NewChildKey(0)
206 k, err = k.NewChildKey(0)
210 pub := k.PublicKey().Key
211 return masterKey.Key, priv, pub
216 var revBytesCases = [][]byte{
223 []byte("abcdefghijklmnop"),
226 []byte("abcdefghijklmnopqrstuvwxyz123456"),
229 []byte("abcdefghijklmnopqrstuvwxyz123456abcdefghijklmnopqrstuvwxyz123456"),
232 func BenchmarkReverseBytes(b *testing.B) {
234 for i := 0; i < b.N; i++ {
235 for _, tt := range revBytesCases {
236 sink = ReverseBytes(tt)
241 // sink is necessary to ensure if the compiler tries
242 // to smart, that it won't optimize away the benchmarks.