You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bip32_tests.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2013 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include <boost/test/unit_test.hpp>
  5. #include "base58.h"
  6. #include "key.h"
  7. #include "uint256.h"
  8. #include "util.h"
  9. #include "utilstrencodings.h"
  10. #include "test/test_bitcoin.h"
  11. #include <string>
  12. #include <vector>
  13. struct TestDerivation {
  14. std::string pub;
  15. std::string prv;
  16. unsigned int nChild;
  17. };
  18. struct TestVector {
  19. std::string strHexMaster;
  20. std::vector<TestDerivation> vDerive;
  21. TestVector(std::string strHexMasterIn) : strHexMaster(strHexMasterIn) {}
  22. TestVector& operator()(std::string pub, std::string prv, unsigned int nChild) {
  23. vDerive.push_back(TestDerivation());
  24. TestDerivation &der = vDerive.back();
  25. der.pub = pub;
  26. der.prv = prv;
  27. der.nChild = nChild;
  28. return *this;
  29. }
  30. };
  31. TestVector test1 =
  32. TestVector("000102030405060708090a0b0c0d0e0f")
  33. ("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8",
  34. "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
  35. 0x80000000)
  36. ("xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw",
  37. "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7",
  38. 1)
  39. ("xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ",
  40. "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs",
  41. 0x80000002)
  42. ("xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5",
  43. "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM",
  44. 2)
  45. ("xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV",
  46. "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334",
  47. 1000000000)
  48. ("xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy",
  49. "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76",
  50. 0);
  51. TestVector test2 =
  52. TestVector("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")
  53. ("xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB",
  54. "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
  55. 0)
  56. ("xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH",
  57. "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt",
  58. 0xFFFFFFFF)
  59. ("xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a",
  60. "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9",
  61. 1)
  62. ("xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon",
  63. "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef",
  64. 0xFFFFFFFE)
  65. ("xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL",
  66. "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc",
  67. 2)
  68. ("xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt",
  69. "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j",
  70. 0);
  71. void RunTest(const TestVector &test) {
  72. std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
  73. CExtKey key;
  74. CExtPubKey pubkey;
  75. key.SetMaster(&seed[0], seed.size());
  76. pubkey = key.Neuter();
  77. BOOST_FOREACH(const TestDerivation &derive, test.vDerive) {
  78. unsigned char data[74];
  79. key.Encode(data);
  80. pubkey.Encode(data);
  81. // Test private key
  82. CBitcoinExtKey b58key; b58key.SetKey(key);
  83. BOOST_CHECK(b58key.ToString() == derive.prv);
  84. // Test public key
  85. CBitcoinExtPubKey b58pubkey; b58pubkey.SetKey(pubkey);
  86. BOOST_CHECK(b58pubkey.ToString() == derive.pub);
  87. // Derive new keys
  88. CExtKey keyNew;
  89. BOOST_CHECK(key.Derive(keyNew, derive.nChild));
  90. CExtPubKey pubkeyNew = keyNew.Neuter();
  91. if (!(derive.nChild & 0x80000000)) {
  92. // Compare with public derivation
  93. CExtPubKey pubkeyNew2;
  94. BOOST_CHECK(pubkey.Derive(pubkeyNew2, derive.nChild));
  95. BOOST_CHECK(pubkeyNew == pubkeyNew2);
  96. }
  97. key = keyNew;
  98. pubkey = pubkeyNew;
  99. }
  100. }
  101. BOOST_FIXTURE_TEST_SUITE(bip32_tests, BasicTestingSetup)
  102. BOOST_AUTO_TEST_CASE(bip32_test1) {
  103. RunTest(test1);
  104. }
  105. BOOST_AUTO_TEST_CASE(bip32_test2) {
  106. RunTest(test2);
  107. }
  108. BOOST_AUTO_TEST_SUITE_END()