neobytes/src/test/bip39_tests.cpp
UdjinM6 27f3218de9 HD wallet (#1405)
* HD wallet

Minimal set of changes (no refactoring) backported from Bitcoin upstream to make HD wallets work in Dash 0.12.1.x+

* minimal bip44 (hardcoded account and change)

* minimal bip39

Additional cmd-line options for new wallet:
-mnemonic
-mnemonicpassphrase

* Do not recreate HD wallet on encryption

Adjusted keypool.py test

* Do not store any private keys for hd wallet besides the master one

Derive all keys on the fly.
Original idea/implementation - btc PR9298, backported and improved

* actually use bip39

* pbkdf2 test

* backport wallet-hd.py test

* Allow specifying hd seed, add dumphdseed rpc, fix bugs

- -hdseed cmd-line param to specify HD seed on wallet creation
- dumphdseed rpc to dump HD seed
- allow seed of any size
- fix dumpwallet rpc bug (wasn't decrypting HD seed)
- print HD seed and extended public masterkey on dumpwallet

* top up keypool on HD wallet encryption

* split HD chain: external/internal

* add missing cs_wallet lock in init.cpp

* fix `const char *` issues (use strings)

* default mnemonic passphrase is an empty string in all cases

* store mnemonic/mnemonicpassphrase

replace dumphdseed with dumphdinfo

* Add fCrypted flag to CHDChain

* prepare internal structures for multiple HD accounts

(plus some code cleanup)

* use secure allocator for storing sensitive HD data

* use secure strings for mnemonic(passphrase)

* small fix in GenerateNewHDChain

* use 24 words for mnemonic by default

* make sure mnemonic passphrase provided by user does not exceed 256 symbols

* more usage of secure allocators and memory_cleanse

* code cleanup

* rename: CSecureVector -> SecureVector

* add missing include

* fix warning in rpcdump.cpp

* refactor mnemonic_check (also fix a bug)

* move bip39 functions to CMnemonic

* Few fixes for CMnemonic:
- use `SecureVector` for data, bits, seed
- `Check` should return bool

* init vectors with desired size where possible
2017-05-29 14:51:40 +03:00

68 lines
2.2 KiB
C++

// Copyright (c) 2014-2017 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "base58.h"
#include "data/bip39_vectors.json.h"
#include "key.h"
#include "util.h"
#include "utilstrencodings.h"
#include "test/test_dash.h"
#include "bip39.h"
#include <boost/test/unit_test.hpp>
#include <univalue.h>
// In script_tests.cpp
extern UniValue read_json(const std::string& jsondata);
BOOST_FIXTURE_TEST_SUITE(bip39_tests, BasicTestingSetup)
// https://github.com/trezor/python-mnemonic/blob/b502451a33a440783926e04428115e0bed87d01f/vectors.json
BOOST_AUTO_TEST_CASE(bip39_vectors)
{
UniValue tests = read_json(std::string(json_tests::bip39_vectors, json_tests::bip39_vectors + sizeof(json_tests::bip39_vectors)));
for (unsigned int i = 0; i < tests.size(); i++) {
// printf("%d\n", i);
UniValue test = tests[i];
std::string strTest = test.write();
if (test.size() < 4) // Allow for extra stuff (useful for comments)
{
BOOST_ERROR("Bad test: " << strTest);
continue;
}
std::vector<uint8_t> vData = ParseHex(test[0].get_str());
SecureVector data(vData.begin(), vData.end());
SecureString m = CMnemonic::FromData(data, data.size());
std::string strMnemonic = test[1].get_str();
SecureString mnemonic(strMnemonic.begin(), strMnemonic.end());
// printf("%s\n%s\n", m.c_str(), mnemonic.c_str());
BOOST_CHECK(m == mnemonic);
BOOST_CHECK(CMnemonic::Check(mnemonic));
SecureVector seed;
SecureString passphrase("TREZOR");
CMnemonic::ToSeed(mnemonic, passphrase, seed);
// printf("seed: %s\n", HexStr(std::string(seed.begin(), seed.end())).c_str());
BOOST_CHECK(HexStr(std::string(seed.begin(), seed.end())) == test[2].get_str());
CExtKey key;
CExtPubKey pubkey;
key.SetMaster(&seed[0], 64);
pubkey = key.Neuter();
CBitcoinExtKey b58key;
b58key.SetKey(key);
// printf("CBitcoinExtKey: %s\n", b58key.ToString().c_str());
BOOST_CHECK(b58key.ToString() == test[3].get_str());
}
}
BOOST_AUTO_TEST_SUITE_END()