134 lines
5.8 KiB
C++
134 lines
5.8 KiB
C++
// Copyright (c) 2013-2015 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
#include "base58.h"
|
|
#include "key.h"
|
|
#include "uint256.h"
|
|
#include "util.h"
|
|
#include "utilstrencodings.h"
|
|
#include "test/test_dash.h"
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
struct TestDerivation {
|
|
std::string pub;
|
|
std::string prv;
|
|
unsigned int nChild;
|
|
};
|
|
|
|
struct TestVector {
|
|
std::string strHexMaster;
|
|
std::vector<TestDerivation> vDerive;
|
|
|
|
TestVector(std::string strHexMasterIn) : strHexMaster(strHexMasterIn) {}
|
|
|
|
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")
|
|
("dpubZ9169KDAEUnyoBhjjmT2VaEodr6pUTDoqCEAeqgbfr2JfkB88BbK77jbTYbcYXb2FVz7DKBdW4P618yd51MwF8DjKVopSbS7Lkgi6Y7Ckb8",
|
|
"dprvLJcNFzfZn6ywEjid4Ver3tsK3puSZFZr3oxCtC1cPdxVsZzjQwGPHvpLxZ83zP37V35uVQNG7GYHjxtkEUWhUKrFTt1bEj49EvPzRfWNPQH",
|
|
0x80000000)
|
|
("dpubZBGW92WdKjuxwnqSwXKfNcLuxffArXhpWof4cw4ie3Db32UJYR8QEG61EpAKHqVZFiGg5Yj4EhTAyXwm236KK1gTX8c5MuLKrgpGQGTaNAV",
|
|
"dprvLLsnFhy2sN6vPLrLGFXUvvyRNeTnwL3rjRP6rHPjMq9nErHuqAoUR5AkjpUPQ6R5C5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qvsS44sVvHCHrwpNg4t1ZGZhgNWF4YQmiPX2PDEB4qtfbMKM1WTxDEnqnFhWjoHBr6HAnbHCRHT316RUVwrjtMJzrnSzVcRwYawkr",
|
|
"dprvLT2B7JJLR4F2K3wAX24mNhJaHzMteW2Qirnae83QEpKQRztTBRGRWA6FxyauCLkVtW3SxqHTfTkWm4ry8PSTHYGSJuoMXNrtYkhZTfLdWhK",
|
|
2)
|
|
("dpubZJmvx44SPcNJcm7AvMzdo843H56Np2bTAQgRpZ4t9pEoJNQR4JX7VuxWYMHWvR8xDZx2Z7MWAsoamdUnH8gzjjMXVuiSFN9X8Hgo6Zfpw3T",
|
|
"dprvLUPD4jWqwEZG4K84F6CTMSgYh3tztpwVP2QU3uPtscAzWCE2M4CBgj3G3PFuMMqZvouBFavH7ZbCockUrhogLX1A7mJranzcF837YG1usdU",
|
|
0);
|
|
|
|
void RunTest(const TestVector &test) {
|
|
std::vector<unsigned char> seed = ParseHex(test.strHexMaster);
|
|
CExtKey key;
|
|
CExtPubKey pubkey;
|
|
key.SetMaster(&seed[0], seed.size());
|
|
pubkey = key.Neuter();
|
|
BOOST_FOREACH(const TestDerivation &derive, test.vDerive) {
|
|
unsigned char data[74];
|
|
key.Encode(data);
|
|
pubkey.Encode(data);
|
|
|
|
// Test private key
|
|
CBitcoinExtKey b58key; b58key.SetKey(key);
|
|
BOOST_CHECK(b58key.ToString() == derive.prv);
|
|
|
|
CBitcoinExtKey b58keyDecodeCheck(derive.prv);
|
|
CExtKey checkKey = b58keyDecodeCheck.GetKey();
|
|
assert(checkKey == key); //ensure a base58 decoded key also matches
|
|
|
|
// Test public key
|
|
CBitcoinExtPubKey b58pubkey; b58pubkey.SetKey(pubkey);
|
|
BOOST_CHECK(b58pubkey.ToString() == derive.pub);
|
|
|
|
CBitcoinExtPubKey b58PubkeyDecodeCheck(derive.pub);
|
|
CExtPubKey checkPubKey = b58PubkeyDecodeCheck.GetKey();
|
|
assert(checkPubKey == pubkey); //ensure a base58 decoded pubkey also matches
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(bip32_tests, BasicTestingSetup)
|
|
|
|
BOOST_AUTO_TEST_CASE(bip32_test1) {
|
|
RunTest(test1);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_CASE(bip32_test2) {
|
|
RunTest(test2);
|
|
}
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|