2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2013-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2013-07-15 01:05:39 +02:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <key.h>
|
Merge #11372: Address encoding cleanup
92f1f8b31 Split off key_io_tests from base58_tests (Pieter Wuille)
119b0f85e Split key_io (address/key encodings) off from base58 (Pieter Wuille)
ebfe217b1 Stop using CBase58Data for ext keys (Pieter Wuille)
32e69fa0d Replace CBitcoinSecret with {Encode,Decode}Secret (Pieter Wuille)
Pull request description:
This PR contains some of the changes left as TODO in #11167 (and built on top of that PR). They are not intended for backporting.
This removes the `CBase58`, `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey` classes, in favor of simple `Encode`/`Decode` functions. Furthermore, all Bitcoin-specific logic (addresses, WIF, BIP32) is moved to `key_io.{h,cpp}`, leaving `base58.{h,cpp}` as a pure utility that implements the base58 encoding/decoding logic.
Tree-SHA512: a5962c0ed27ad53cbe00f22af432cf11aa530e3efc9798e25c004bc9ed1b5673db5df3956e398ee2c085e3a136ac8da69fe7a7d97a05fb2eb3be0b60d0479655
Make linter happy
Dashify
2018-03-07 00:04:56 +01:00
|
|
|
#include <key_io.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <uint256.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/strencodings.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <test/test_dash.h>
|
2013-07-15 01:05:39 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-07-15 01:05:39 +02:00
|
|
|
struct TestDerivation {
|
|
|
|
std::string pub;
|
|
|
|
std::string prv;
|
|
|
|
unsigned int nChild;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestVector {
|
|
|
|
std::string strHexMaster;
|
|
|
|
std::vector<TestDerivation> vDerive;
|
|
|
|
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit TestVector(std::string strHexMasterIn) : strHexMaster(strHexMasterIn) {}
|
2013-07-15 01:05:39 +02:00
|
|
|
|
|
|
|
TestVector& operator()(std::string pub, std::string prv, unsigned int nChild) {
|
|
|
|
vDerive.push_back(TestDerivation());
|
|
|
|
TestDerivation &der = vDerive.back();
|
|
|
|
der.pub = pub;
|
|
|
|
der.prv = prv;
|
|
|
|
der.nChild = nChild;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TestVector test1 =
|
|
|
|
TestVector("000102030405060708090a0b0c0d0e0f")
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8",
|
|
|
|
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
|
2013-07-15 01:05:39 +02:00
|
|
|
0x80000000)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw",
|
|
|
|
"xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7",
|
2013-07-15 01:05:39 +02:00
|
|
|
1)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ",
|
|
|
|
"xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs",
|
2013-07-15 01:05:39 +02:00
|
|
|
0x80000002)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5",
|
|
|
|
"xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM",
|
2013-07-15 01:05:39 +02:00
|
|
|
2)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV",
|
|
|
|
"xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334",
|
2013-07-15 01:05:39 +02:00
|
|
|
1000000000)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy",
|
|
|
|
"xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76",
|
2013-07-15 01:05:39 +02:00
|
|
|
0);
|
|
|
|
|
|
|
|
TestVector test2 =
|
|
|
|
TestVector("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB",
|
|
|
|
"xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
|
2013-07-15 01:05:39 +02:00
|
|
|
0)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH",
|
|
|
|
"xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt",
|
2013-07-15 01:05:39 +02:00
|
|
|
0xFFFFFFFF)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a",
|
|
|
|
"xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9",
|
2013-07-15 01:05:39 +02:00
|
|
|
1)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon",
|
|
|
|
"xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef",
|
2013-07-15 01:05:39 +02:00
|
|
|
0xFFFFFFFE)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL",
|
|
|
|
"xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc",
|
2013-07-15 01:05:39 +02:00
|
|
|
2)
|
2016-08-02 00:00:24 +02:00
|
|
|
("xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt",
|
|
|
|
"xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j",
|
2013-07-15 01:05:39 +02:00
|
|
|
0);
|
|
|
|
|
2019-01-07 10:55:35 +01:00
|
|
|
TestVector test3 =
|
|
|
|
TestVector("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be")
|
|
|
|
("xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13",
|
|
|
|
"xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6",
|
|
|
|
0x80000000)
|
|
|
|
("xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y",
|
|
|
|
"xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L",
|
|
|
|
0);
|
|
|
|
|
2018-05-04 22:42:39 +02:00
|
|
|
static void RunTest(const TestVector &test) {
|
2013-07-15 01:05:39 +02:00
|
|
|
std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
|
|
|
|
CExtKey key;
|
|
|
|
CExtPubKey pubkey;
|
2018-05-21 09:41:44 +02:00
|
|
|
key.SetSeed(seed.data(), seed.size());
|
2013-07-15 01:05:39 +02:00
|
|
|
pubkey = key.Neuter();
|
2019-07-05 09:06:28 +02:00
|
|
|
for (const TestDerivation &derive : test.vDerive) {
|
2013-07-15 01:05:39 +02:00
|
|
|
unsigned char data[74];
|
|
|
|
key.Encode(data);
|
|
|
|
pubkey.Encode(data);
|
2015-07-23 16:22:37 +02:00
|
|
|
|
2013-07-15 01:05:39 +02:00
|
|
|
// Test private key
|
Merge #11372: Address encoding cleanup
92f1f8b31 Split off key_io_tests from base58_tests (Pieter Wuille)
119b0f85e Split key_io (address/key encodings) off from base58 (Pieter Wuille)
ebfe217b1 Stop using CBase58Data for ext keys (Pieter Wuille)
32e69fa0d Replace CBitcoinSecret with {Encode,Decode}Secret (Pieter Wuille)
Pull request description:
This PR contains some of the changes left as TODO in #11167 (and built on top of that PR). They are not intended for backporting.
This removes the `CBase58`, `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey` classes, in favor of simple `Encode`/`Decode` functions. Furthermore, all Bitcoin-specific logic (addresses, WIF, BIP32) is moved to `key_io.{h,cpp}`, leaving `base58.{h,cpp}` as a pure utility that implements the base58 encoding/decoding logic.
Tree-SHA512: a5962c0ed27ad53cbe00f22af432cf11aa530e3efc9798e25c004bc9ed1b5673db5df3956e398ee2c085e3a136ac8da69fe7a7d97a05fb2eb3be0b60d0479655
Make linter happy
Dashify
2018-03-07 00:04:56 +01:00
|
|
|
BOOST_CHECK(EncodeExtKey(key) == derive.prv);
|
|
|
|
BOOST_CHECK(DecodeExtKey(derive.prv) == key); //ensure a base58 decoded key also matches
|
2015-07-23 16:22:37 +02:00
|
|
|
|
2013-07-15 01:05:39 +02:00
|
|
|
// Test public key
|
Merge #11372: Address encoding cleanup
92f1f8b31 Split off key_io_tests from base58_tests (Pieter Wuille)
119b0f85e Split key_io (address/key encodings) off from base58 (Pieter Wuille)
ebfe217b1 Stop using CBase58Data for ext keys (Pieter Wuille)
32e69fa0d Replace CBitcoinSecret with {Encode,Decode}Secret (Pieter Wuille)
Pull request description:
This PR contains some of the changes left as TODO in #11167 (and built on top of that PR). They are not intended for backporting.
This removes the `CBase58`, `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey` classes, in favor of simple `Encode`/`Decode` functions. Furthermore, all Bitcoin-specific logic (addresses, WIF, BIP32) is moved to `key_io.{h,cpp}`, leaving `base58.{h,cpp}` as a pure utility that implements the base58 encoding/decoding logic.
Tree-SHA512: a5962c0ed27ad53cbe00f22af432cf11aa530e3efc9798e25c004bc9ed1b5673db5df3956e398ee2c085e3a136ac8da69fe7a7d97a05fb2eb3be0b60d0479655
Make linter happy
Dashify
2018-03-07 00:04:56 +01:00
|
|
|
BOOST_CHECK(EncodeExtPubKey(pubkey) == derive.pub);
|
|
|
|
BOOST_CHECK(DecodeExtPubKey(derive.pub) == pubkey); //ensure a base58 decoded pubkey also matches
|
2015-07-23 16:22:37 +02:00
|
|
|
|
2013-07-15 01:05:39 +02:00
|
|
|
// Derive new keys
|
|
|
|
CExtKey keyNew;
|
|
|
|
BOOST_CHECK(key.Derive(keyNew, derive.nChild));
|
|
|
|
CExtPubKey pubkeyNew = keyNew.Neuter();
|
|
|
|
if (!(derive.nChild & 0x80000000)) {
|
|
|
|
// Compare with public derivation
|
|
|
|
CExtPubKey pubkeyNew2;
|
|
|
|
BOOST_CHECK(pubkey.Derive(pubkeyNew2, derive.nChild));
|
|
|
|
BOOST_CHECK(pubkeyNew == pubkeyNew2);
|
|
|
|
}
|
|
|
|
key = keyNew;
|
|
|
|
pubkey = pubkeyNew;
|
2017-05-29 13:51:40 +02:00
|
|
|
|
|
|
|
CDataStream ssPub(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssPub << pubkeyNew;
|
2017-09-15 13:35:55 +02:00
|
|
|
BOOST_CHECK(ssPub.size() == 75);
|
2017-05-29 13:51:40 +02:00
|
|
|
|
|
|
|
CDataStream ssPriv(SER_DISK, CLIENT_VERSION);
|
|
|
|
ssPriv << keyNew;
|
2017-09-15 13:35:55 +02:00
|
|
|
BOOST_CHECK(ssPriv.size() == 75);
|
2017-05-29 13:51:40 +02:00
|
|
|
|
|
|
|
CExtPubKey pubCheck;
|
|
|
|
CExtKey privCheck;
|
|
|
|
ssPub >> pubCheck;
|
|
|
|
ssPriv >> privCheck;
|
|
|
|
|
|
|
|
BOOST_CHECK(pubCheck == pubkeyNew);
|
|
|
|
BOOST_CHECK(privCheck == keyNew);
|
2013-07-15 01:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(bip32_tests, BasicTestingSetup)
|
2013-07-15 01:05:39 +02:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(bip32_test1) {
|
|
|
|
RunTest(test1);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(bip32_test2) {
|
|
|
|
RunTest(test2);
|
|
|
|
}
|
|
|
|
|
2019-01-07 10:55:35 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(bip32_test3) {
|
|
|
|
RunTest(test3);
|
|
|
|
}
|
|
|
|
|
2013-07-15 01:05:39 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|