bip32_test.mx raw

   1  package bip32
   2  
   3  import (
   4  	"bytes"
   5  	"testing"
   6  )
   7  
   8  // hexStr formats a byte slice as a lowercase hex string. Working around
   9  // %x in t.Errorf rendering raw bytes in the moxie test harness.
  10  func hexStr(b []byte) string {
  11  	const digits = "0123456789abcdef"
  12  	out := []byte{:len(b) * 2}
  13  	for i, v := range b {
  14  		out[i*2] = digits[v>>4]
  15  		out[i*2+1] = digits[v&0x0f]
  16  	}
  17  	return string(out)
  18  }
  19  
  20  // TestBIP32MasterAndChildren runs BIP-32 master + child derivation against
  21  // vector 1 from the BIP-32 spec.
  22  //
  23  // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1
  24  //
  25  // Compares the 32-byte secret key bytes (the deterministic core of each
  26  // xprv); the base58 xprv/xpub serialization with metadata is not tested
  27  // since that layer is not implemented in this package.
  28  func TestBIP32MasterAndChildren(t *testing.T) {
  29  	seed := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  30  		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
  31  
  32  	rootKey, rootChain := DerivePath(seed, []uint32{})
  33  	if rootKey == nil {
  34  		t.Fatal("DerivePath(m) failed")
  35  	}
  36  	wantRootKey := []byte{
  37  		0xe8, 0xf3, 0x2e, 0x72, 0x3d, 0xec, 0xf4, 0x05, 0x1a, 0xef, 0xac, 0x8e, 0x2c, 0x93, 0xc9, 0xc5,
  38  		0xb2, 0x14, 0x31, 0x38, 0x17, 0xcd, 0xb0, 0x1a, 0x14, 0x94, 0xb9, 0x17, 0xc8, 0x43, 0x6b, 0x35,
  39  	}
  40  	wantRootChain := []byte{
  41  		0x87, 0x3d, 0xff, 0x81, 0xc0, 0x2f, 0x52, 0x56, 0x23, 0xfd, 0x1f, 0xe5, 0x16, 0x7e, 0xac, 0x3a,
  42  		0x55, 0xa0, 0x49, 0xde, 0x3d, 0x31, 0x4b, 0xb4, 0x2e, 0xe2, 0x27, 0xff, 0xed, 0x37, 0xd5, 0x08,
  43  	}
  44  	if !bytes.Equal(rootKey, wantRootKey) {
  45  		t.Errorf("root key mismatch\n want=%s got=%s", hexStr(wantRootKey), hexStr(rootKey))
  46  	}
  47  	if !bytes.Equal(rootChain, wantRootChain) {
  48  		t.Errorf("root chain mismatch\n want=%s got=%s", hexStr(wantRootChain), hexStr(rootChain))
  49  	}
  50  
  51  	// m/0' (hardened)
  52  	k1, _ := DerivePath(seed, []uint32{0 | HardenedBit})
  53  	wantK1 := []byte{
  54  		0xed, 0xb2, 0xe1, 0x4f, 0x9e, 0xe7, 0x7d, 0x26, 0xdd, 0x93, 0xb4, 0xec, 0xed, 0xe8, 0xd1, 0x6e,
  55  		0xd4, 0x08, 0xce, 0x14, 0x9b, 0x6c, 0xd8, 0x0b, 0x07, 0x15, 0xa2, 0xd9, 0x11, 0xa0, 0xaf, 0xea,
  56  	}
  57  	if !bytes.Equal(k1, wantK1) {
  58  		t.Errorf("m/0' key mismatch\n want=%s got=%s", hexStr(wantK1), hexStr(k1))
  59  	}
  60  
  61  	// m/0'/1 (normal child of hardened)
  62  	k2, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1})
  63  	wantK2 := []byte{
  64  		0x3c, 0x6c, 0xb8, 0xd0, 0xf6, 0xa2, 0x64, 0xc9, 0x1e, 0xa8, 0xb5, 0x03, 0x0f, 0xad, 0xaa, 0x8e,
  65  		0x53, 0x8b, 0x02, 0x0f, 0x0a, 0x38, 0x74, 0x21, 0xa1, 0x2d, 0xe9, 0x31, 0x9d, 0xc9, 0x33, 0x68,
  66  	}
  67  	if !bytes.Equal(k2, wantK2) {
  68  		t.Errorf("m/0'/1 key mismatch\n want=%s got=%s", hexStr(wantK2), hexStr(k2))
  69  	}
  70  
  71  	// m/0'/1/2'
  72  	k3, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1, 2 | HardenedBit})
  73  	wantK3 := []byte{
  74  		0xcb, 0xce, 0x0d, 0x71, 0x9e, 0xcf, 0x74, 0x31, 0xd8, 0x8e, 0x6a, 0x89, 0xfa, 0x14, 0x83, 0xe0,
  75  		0x2e, 0x35, 0x09, 0x2a, 0xf6, 0x0c, 0x04, 0x2b, 0x1d, 0xf2, 0xff, 0x59, 0xfa, 0x42, 0x4d, 0xca,
  76  	}
  77  	if !bytes.Equal(k3, wantK3) {
  78  		t.Errorf("m/0'/1/2' key mismatch\n want=%s got=%s", hexStr(wantK3), hexStr(k3))
  79  	}
  80  
  81  	// m/0'/1/2'/2 (depth 4 - normal child of hardened parent)
  82  	k4, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1, 2 | HardenedBit, 2})
  83  	wantK4 := []byte{
  84  		0x0f, 0x47, 0x92, 0x45, 0xfb, 0x19, 0xa3, 0x8a, 0x19, 0x54, 0xc5, 0xc7, 0xc0, 0xeb, 0xab, 0x2f,
  85  		0x9b, 0xdf, 0xd9, 0x6a, 0x17, 0x56, 0x3e, 0xf2, 0x8a, 0x6a, 0x4b, 0x1a, 0x2a, 0x76, 0x4e, 0xf4,
  86  	}
  87  	if !bytes.Equal(k4, wantK4) {
  88  		t.Errorf("m/0'/1/2'/2 key mismatch\n want=%s got=%s", hexStr(wantK4), hexStr(k4))
  89  	}
  90  
  91  	// m/0'/1/2'/2/1000000000 (full depth-5 path, exercises high-index normal derivation).
  92  	// Expected value cross-checked against a pure-Python BIP-32 reference impl.
  93  	k5, _ := DerivePath(seed, []uint32{0 | HardenedBit, 1, 2 | HardenedBit, 2, 1000000000})
  94  	wantK5 := []byte{
  95  		0x47, 0x1b, 0x76, 0xe3, 0x89, 0xe5, 0x28, 0xd6, 0xde, 0x6d, 0x81, 0x68, 0x57, 0xe0, 0x12, 0xc5,
  96  		0x45, 0x50, 0x51, 0xca, 0xd6, 0x66, 0x08, 0x50, 0xe5, 0x83, 0x72, 0xa6, 0xc3, 0xe6, 0xe7, 0xc8,
  97  	}
  98  	if !bytes.Equal(k5, wantK5) {
  99  		t.Errorf("m/0'/1/2'/2/1000000000 key mismatch\n want=%s got=%s", hexStr(wantK5), hexStr(k5))
 100  	}
 101  }
 102