github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/go-bip32/bip32_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bip32_test
     6  
     7  import (
     8  	"encoding/hex"
     9  	"testing"
    10  
    11  	bip32 "github.com/turingchain2020/turingchain/wallet/bipwallet/go-bip32"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  type testMasterKey struct {
    16  	seed     string
    17  	children []testChildKey
    18  	privKey  string
    19  	pubKey   string
    20  }
    21  
    22  type testChildKey struct {
    23  	pathFragment uint32
    24  	privKey      string
    25  	pubKey       string
    26  }
    27  
    28  func TestBip32TestVectors(t *testing.T) {
    29  	hStart := bip32.FirstHardenedChild
    30  
    31  	vector1 := testMasterKey{
    32  		seed:    "000102030405060708090a0b0c0d0e0f",
    33  		privKey: "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
    34  		pubKey:  "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8",
    35  		children: []testChildKey{
    36  			{
    37  				pathFragment: hStart,
    38  				privKey:      "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7",
    39  				pubKey:       "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw",
    40  			},
    41  			{
    42  				pathFragment: 1,
    43  				privKey:      "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs",
    44  				pubKey:       "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ",
    45  			},
    46  			{
    47  				pathFragment: 2 + hStart,
    48  				privKey:      "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM",
    49  				pubKey:       "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5",
    50  			},
    51  			{
    52  				pathFragment: 2,
    53  				privKey:      "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334",
    54  				pubKey:       "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV",
    55  			},
    56  			{
    57  				pathFragment: 1000000000,
    58  				privKey:      "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76",
    59  				pubKey:       "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy",
    60  			},
    61  		},
    62  	}
    63  
    64  	vector2 := testMasterKey{
    65  		seed:    "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542",
    66  		privKey: "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
    67  		pubKey:  "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB",
    68  		children: []testChildKey{
    69  			{
    70  				pathFragment: 0,
    71  				privKey:      "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt",
    72  				pubKey:       "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH",
    73  			},
    74  			{
    75  				pathFragment: 2147483647 + hStart,
    76  				privKey:      "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9",
    77  				pubKey:       "xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a",
    78  			},
    79  			{
    80  				pathFragment: 1,
    81  				privKey:      "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef",
    82  				pubKey:       "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon",
    83  			},
    84  			{
    85  				pathFragment: 2147483646 + hStart,
    86  				privKey:      "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc",
    87  				pubKey:       "xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL",
    88  			},
    89  			{
    90  				pathFragment: 2,
    91  				privKey:      "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j",
    92  				pubKey:       "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt",
    93  			},
    94  		},
    95  	}
    96  
    97  	testVectorKeyPairs(t, vector1)
    98  	testVectorKeyPairs(t, vector2)
    99  }
   100  
   101  func testVectorKeyPairs(t *testing.T, vector testMasterKey) {
   102  	// Decode master seed into hex
   103  	seed, _ := hex.DecodeString(vector.seed)
   104  
   105  	// Generate a master private and public key
   106  	privKey, err := bip32.NewMasterKey(seed)
   107  	assert.NoError(t, err)
   108  
   109  	pubKey := privKey.PublicKey()
   110  
   111  	assert.Equal(t, vector.privKey, privKey.String())
   112  	assert.Equal(t, vector.pubKey, pubKey.String())
   113  
   114  	// Iterate over the entire child chain and test the given keys
   115  	for _, testChildKey := range vector.children {
   116  		// Get the private key at the given key tree path
   117  		privKey, err = privKey.NewChildKey(testChildKey.pathFragment)
   118  		assert.NoError(t, err)
   119  
   120  		// Get this private key's public key
   121  		pubKey = privKey.PublicKey()
   122  
   123  		// Assert correctness
   124  		assert.Equal(t, testChildKey.privKey, privKey.String())
   125  		assert.Equal(t, testChildKey.pubKey, pubKey.String())
   126  	}
   127  
   128  	childPubkey, err := pubKey.NewChildKey(0)
   129  	assert.NoError(t, err)
   130  	assert.NotNil(t, childPubkey)
   131  }