package bip32 import ( "bytes" "testing" ) // hexStr formats a byte slice as a lowercase hex string. Working around // %x in t.Errorf rendering raw bytes in the moxie test harness. func hexStr(b []byte) string { const digits = "0123456789abcdef" out := []byte{:len(b) * 2} for i, v := range b { out[i*2] = digits[v>>4] out[i*2+1] = digits[v&0x0f] } return string(out) } // TestBIP32MasterAndChildren runs BIP-32 master + child derivation against // vector 1 from the BIP-32 spec. // // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1 // // Compares the 32-byte secret key bytes (the deterministic core of each // xprv); the base58 xprv/xpub serialization with metadata is not tested // since that layer is not implemented in this package. func TestBIP32MasterAndChildren(t *testing.T) { seed := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} rootKey, rootChain := DerivePath(seed, []uint32{}) if rootKey == nil { t.Fatal("DerivePath(m) failed") } wantRootKey := []byte{ 0xe8, 0xf3, 0x2e, 0x72, 0x3d, 0xec, 0xf4, 0x05, 0x1a, 0xef, 0xac, 0x8e, 0x2c, 0x93, 0xc9, 0xc5, 0xb2, 0x14, 0x31, 0x38, 0x17, 0xcd, 0xb0, 0x1a, 0x14, 0x94, 0xb9, 0x17, 0xc8, 0x43, 0x6b, 0x35, } wantRootChain := []byte{ 0x87, 0x3d, 0xff, 0x81, 0xc0, 0x2f, 0x52, 0x56, 0x23, 0xfd, 0x1f, 0xe5, 0x16, 0x7e, 0xac, 0x3a, 0x55, 0xa0, 0x49, 0xde, 0x3d, 0x31, 0x4b, 0xb4, 0x2e, 0xe2, 0x27, 0xff, 0xed, 0x37, 0xd5, 0x08, } if !bytes.Equal(rootKey, wantRootKey) { t.Errorf("root key mismatch\n want=%s got=%s", hexStr(wantRootKey), hexStr(rootKey)) } if !bytes.Equal(rootChain, wantRootChain) { t.Errorf("root chain mismatch\n want=%s got=%s", hexStr(wantRootChain), hexStr(rootChain)) } // m/0' (hardened) k1, _ := DerivePath(seed, []uint32{0 | HardenedBit}) wantK1 := []byte{ 0xed, 0xb2, 0xe1, 0x4f, 0x9e, 0xe7, 0x7d, 0x26, 0xdd, 0x93, 0xb4, 0xec, 0xed, 0xe8, 0xd1, 0x6e, 0xd4, 0x08, 0xce, 0x14, 0x9b, 0x6c, 0xd8, 0x0b, 0x07, 0x15, 0xa2, 0xd9, 0x11, 0xa0, 0xaf, 0xea, } if !bytes.Equal(k1, wantK1) { t.Errorf("m/0' key mismatch\n want=%s got=%s", hexStr(wantK1), hexStr(k1)) } // m/0'/1 (normal child of hardened) k2, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1}) wantK2 := []byte{ 0x3c, 0x6c, 0xb8, 0xd0, 0xf6, 0xa2, 0x64, 0xc9, 0x1e, 0xa8, 0xb5, 0x03, 0x0f, 0xad, 0xaa, 0x8e, 0x53, 0x8b, 0x02, 0x0f, 0x0a, 0x38, 0x74, 0x21, 0xa1, 0x2d, 0xe9, 0x31, 0x9d, 0xc9, 0x33, 0x68, } if !bytes.Equal(k2, wantK2) { t.Errorf("m/0'/1 key mismatch\n want=%s got=%s", hexStr(wantK2), hexStr(k2)) } // m/0'/1/2' k3, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1, 2 | HardenedBit}) wantK3 := []byte{ 0xcb, 0xce, 0x0d, 0x71, 0x9e, 0xcf, 0x74, 0x31, 0xd8, 0x8e, 0x6a, 0x89, 0xfa, 0x14, 0x83, 0xe0, 0x2e, 0x35, 0x09, 0x2a, 0xf6, 0x0c, 0x04, 0x2b, 0x1d, 0xf2, 0xff, 0x59, 0xfa, 0x42, 0x4d, 0xca, } if !bytes.Equal(k3, wantK3) { t.Errorf("m/0'/1/2' key mismatch\n want=%s got=%s", hexStr(wantK3), hexStr(k3)) } // m/0'/1/2'/2 (depth 4 - normal child of hardened parent) k4, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1, 2 | HardenedBit, 2}) wantK4 := []byte{ 0x0f, 0x47, 0x92, 0x45, 0xfb, 0x19, 0xa3, 0x8a, 0x19, 0x54, 0xc5, 0xc7, 0xc0, 0xeb, 0xab, 0x2f, 0x9b, 0xdf, 0xd9, 0x6a, 0x17, 0x56, 0x3e, 0xf2, 0x8a, 0x6a, 0x4b, 0x1a, 0x2a, 0x76, 0x4e, 0xf4, } if !bytes.Equal(k4, wantK4) { t.Errorf("m/0'/1/2'/2 key mismatch\n want=%s got=%s", hexStr(wantK4), hexStr(k4)) } // m/0'/1/2'/2/1000000000 (full depth-5 path, exercises high-index normal derivation). // Expected value cross-checked against a pure-Python BIP-32 reference impl. k5, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1, 2 | HardenedBit, 2, 1000000000}) wantK5 := []byte{ 0x47, 0x1b, 0x76, 0xe3, 0x89, 0xe5, 0x28, 0xd6, 0xde, 0x6d, 0x81, 0x68, 0x57, 0xe0, 0x12, 0xc5, 0x45, 0x50, 0x51, 0xca, 0xd6, 0x66, 0x08, 0x50, 0xe5, 0x83, 0x72, 0xa6, 0xc3, 0xe6, 0xe7, 0xc8, } if !bytes.Equal(k5, wantK5) { t.Errorf("m/0'/1/2'/2/1000000000 key mismatch\n want=%s got=%s", hexStr(wantK5), hexStr(k5)) } }