[refactor] manually change remaining instances of master key to seed.

This commit is contained in:
John Newbery 2018-04-04 12:47:55 -04:00
parent 131d4450b9
commit c75c351419
6 changed files with 34 additions and 34 deletions

View file

@ -755,10 +755,10 @@ UniValue dumpwallet(const JSONRPCRequest& request)
CKeyID seed_id = pwallet->GetHDChain().seed_id; CKeyID seed_id = pwallet->GetHDChain().seed_id;
if (!seed_id.IsNull()) if (!seed_id.IsNull())
{ {
CKey key; CKey seed;
if (pwallet->GetKey(seed_id, key)) { if (pwallet->GetKey(seed_id, seed)) {
CExtKey masterKey; CExtKey masterKey;
masterKey.SetSeed(key.begin(), key.size()); masterKey.SetSeed(seed.begin(), seed.size());
file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n"; file << "# extended private masterkey: " << EncodeExtKey(masterKey) << "\n\n";
} }
@ -777,8 +777,8 @@ UniValue dumpwallet(const JSONRPCRequest& request)
file << "hdseed=1"; file << "hdseed=1";
} else if (mapKeyPool.count(keyid)) { } else if (mapKeyPool.count(keyid)) {
file << "reserve=1"; file << "reserve=1";
} else if (pwallet->mapKeyMetadata[keyid].hdKeypath == "m") { } else if (pwallet->mapKeyMetadata[keyid].hdKeypath == "s") {
file << "inactivehdmaster=1"; file << "inactivehdseed=1";
} else { } else {
file << "change=1"; file << "change=1";
} }

View file

@ -2925,7 +2925,7 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
" \"keypoolsize_hd_internal\": xxxx, (numeric) how many new keys are pre-generated for internal use (used for change outputs, only appears if the wallet is using this feature, otherwise external keys are used)\n" " \"keypoolsize_hd_internal\": xxxx, (numeric) how many new keys are pre-generated for internal use (used for change outputs, only appears if the wallet is using this feature, otherwise external keys are used)\n"
" \"unlocked_until\": ttt, (numeric) the timestamp in seconds since epoch (midnight Jan 1 1970 GMT) that the wallet is unlocked for transfers, or 0 if the wallet is locked\n" " \"unlocked_until\": ttt, (numeric) the timestamp in seconds since epoch (midnight Jan 1 1970 GMT) that the wallet is unlocked for transfers, or 0 if the wallet is locked\n"
" \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT + "/kB\n" " \"paytxfee\": x.xxxx, (numeric) the transaction fee configuration, set in " + CURRENCY_UNIT + "/kB\n"
" \"hdseedid\": \"<hash160>\" (string, optional) the Hash160 of the HD master pubkey (only present when HD is enabled)\n" " \"hdseedid\": \"<hash160>\" (string, optional) the Hash160 of the HD seed (only present when HD is enabled)\n"
"}\n" "}\n"
"\nExamples:\n" "\nExamples:\n"
+ HelpExampleCli("getwalletinfo", "") + HelpExampleCli("getwalletinfo", "")
@ -3954,7 +3954,7 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
" \"account\" : \"account\" (string) DEPRECATED. This field will be removed in V0.18. To see this deprecated field, start bitcoind with -deprecatedrpc=accounts. The account associated with the address, \"\" is the default account\n" " \"account\" : \"account\" (string) DEPRECATED. This field will be removed in V0.18. To see this deprecated field, start bitcoind with -deprecatedrpc=accounts. The account associated with the address, \"\" is the default account\n"
" \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n" " \"timestamp\" : timestamp, (number, optional) The creation time of the key if available in seconds since epoch (Jan 1 1970 GMT)\n"
" \"hdkeypath\" : \"keypath\" (string, optional) The HD keypath if the key is HD and available\n" " \"hdkeypath\" : \"keypath\" (string, optional) The HD keypath if the key is HD and available\n"
" \"hdseedid\" : \"<hash160>\" (string, optional) The Hash160 of the HD master pubkey\n" " \"hdseedid\" : \"<hash160>\" (string, optional) The Hash160 of the HD seed\n"
" \"labels\" (object) Array of labels associated with the address.\n" " \"labels\" (object) Array of labels associated with the address.\n"
" [\n" " [\n"
" { (json object of label data)\n" " { (json object of label data)\n"

View file

@ -191,17 +191,17 @@ CPubKey CWallet::GenerateNewKey(WalletBatch &batch, bool internal)
void CWallet::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, bool internal) void CWallet::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, bool internal)
{ {
// for now we use a fixed keypath scheme of m/0'/0'/k // for now we use a fixed keypath scheme of m/0'/0'/k
CKey key; //master key seed (256bit) CKey seed; //seed (256bit)
CExtKey masterKey; //hd master key CExtKey masterKey; //hd master key
CExtKey accountKey; //key at m/0' CExtKey accountKey; //key at m/0'
CExtKey chainChildKey; //key at m/0'/0' (external) or m/0'/1' (internal) CExtKey chainChildKey; //key at m/0'/0' (external) or m/0'/1' (internal)
CExtKey childKey; //key at m/0'/0'/<n>' CExtKey childKey; //key at m/0'/0'/<n>'
// try to get the master key // try to get the seed
if (!GetKey(hdChain.seed_id, key)) if (!GetKey(hdChain.seed_id, seed))
throw std::runtime_error(std::string(__func__) + ": Master key not found"); throw std::runtime_error(std::string(__func__) + ": seed not found");
masterKey.SetSeed(key.begin(), key.size()); masterKey.SetSeed(seed.begin(), seed.size());
// derive m/0' // derive m/0'
// use hardened derivation (child keys >= 0x80000000 are hardened after bip32) // use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
@ -689,7 +689,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
Lock(); Lock();
Unlock(strWalletPassphrase); Unlock(strWalletPassphrase);
// if we are using HD, replace the HD master key (seed) with a new one // if we are using HD, replace the HD seed with a new one
if (IsHDEnabled()) { if (IsHDEnabled()) {
if (!SetHDSeed(GenerateNewSeed())) { if (!SetHDSeed(GenerateNewSeed())) {
return false; return false;
@ -1462,29 +1462,29 @@ CPubKey CWallet::DeriveNewSeed(const CKey& key)
int64_t nCreationTime = GetTime(); int64_t nCreationTime = GetTime();
CKeyMetadata metadata(nCreationTime); CKeyMetadata metadata(nCreationTime);
// calculate the pubkey // calculate the seed
CPubKey pubkey = key.GetPubKey(); CPubKey seed = key.GetPubKey();
assert(key.VerifyPubKey(pubkey)); assert(key.VerifyPubKey(seed));
// set the hd keypath to "m" -> Master, refers the masterkeyid to itself // set the hd keypath to "s" -> Seed, refers the seed to itself
metadata.hdKeypath = "m"; metadata.hdKeypath = "s";
metadata.hd_seed_id = pubkey.GetID(); metadata.hd_seed_id = seed.GetID();
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
// mem store the metadata // mem store the metadata
mapKeyMetadata[pubkey.GetID()] = metadata; mapKeyMetadata[seed.GetID()] = metadata;
// write the key&metadata to the database // write the key&metadata to the database
if (!AddKeyPubKey(key, pubkey)) if (!AddKeyPubKey(key, seed))
throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed"); throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed");
} }
return pubkey; return seed;
} }
bool CWallet::SetHDSeed(const CPubKey& pubkey) bool CWallet::SetHDSeed(const CPubKey& seed)
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
// store the keyid (hash160) together with // store the keyid (hash160) together with
@ -1492,7 +1492,7 @@ bool CWallet::SetHDSeed(const CPubKey& pubkey)
// as a hdchain object // as a hdchain object
CHDChain newHdChain; CHDChain newHdChain;
newHdChain.nVersion = CanSupportFeature(FEATURE_HD_SPLIT) ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE; newHdChain.nVersion = CanSupportFeature(FEATURE_HD_SPLIT) ? CHDChain::VERSION_HD_CHAIN_SPLIT : CHDChain::VERSION_HD_BASE;
newHdChain.seed_id = pubkey.GetID(); newHdChain.seed_id = seed.GetID();
SetHDChain(newHdChain, false); SetHDChain(newHdChain, false);
return true; return true;
@ -4164,10 +4164,10 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
} }
walletInstance->SetMinVersion(FEATURE_LATEST); walletInstance->SetMinVersion(FEATURE_LATEST);
// generate a new master key // generate a new seed
CPubKey masterPubKey = walletInstance->GenerateNewSeed(); CPubKey seed = walletInstance->GenerateNewSeed();
if (!walletInstance->SetHDSeed(masterPubKey)) if (!walletInstance->SetHDSeed(seed))
throw std::runtime_error(std::string(__func__) + ": Storing master key failed"); throw std::runtime_error(std::string(__func__) + ": Storing HD seed failed");
// Top up the keypool // Top up the keypool
if (!walletInstance->TopUpKeyPool()) { if (!walletInstance->TopUpKeyPool()) {

View file

@ -1139,14 +1139,14 @@ public:
/* Returns true if HD is enabled */ /* Returns true if HD is enabled */
bool IsHDEnabled() const; bool IsHDEnabled() const;
/* Generates a new HD master key (will not be activated) */ /* Generates a new HD seed (will not be activated) */
CPubKey GenerateNewSeed(); CPubKey GenerateNewSeed();
/* Derives a new HD master key (will not be activated) */ /* Derives a new HD master key (will not be activated) */
CPubKey DeriveNewSeed(const CKey& key); CPubKey DeriveNewSeed(const CKey& key);
/* Set the current HD master key (will reset the chain child index counters) /* Set the current HD seed (will reset the chain child index counters)
Sets the master key's version based on the current wallet version (so the Sets the seed's version based on the current wallet version (so the
caller must ensure the current wallet version is correct before calling caller must ensure the current wallet version is correct before calling
this function). */ this function). */
bool SetHDSeed(const CPubKey& key); bool SetHDSeed(const CPubKey& key);

View file

@ -62,7 +62,7 @@ class CHDChain
public: public:
uint32_t nExternalChainCounter; uint32_t nExternalChainCounter;
uint32_t nInternalChainCounter; uint32_t nInternalChainCounter;
CKeyID seed_id; //!< master key hash160 CKeyID seed_id; //!< seed hash160
static const int VERSION_HD_BASE = 1; static const int VERSION_HD_BASE = 1;
static const int VERSION_HD_CHAIN_SPLIT = 2; static const int VERSION_HD_CHAIN_SPLIT = 2;
@ -99,7 +99,7 @@ public:
int nVersion; int nVersion;
int64_t nCreateTime; // 0 means unknown int64_t nCreateTime; // 0 means unknown
std::string hdKeypath; //optional HD/bip32 keypath std::string hdKeypath; //optional HD/bip32 keypath
CKeyID hd_seed_id; //id of the HD masterkey used to derive this key CKeyID hd_seed_id; //id of the HD seed used to derive this key
CKeyMetadata() CKeyMetadata()
{ {

View file

@ -36,7 +36,7 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
addr_keypath = comment.split(" addr=")[1] addr_keypath = comment.split(" addr=")[1]
addr = addr_keypath.split(" ")[0] addr = addr_keypath.split(" ")[0]
keypath = None keypath = None
if keytype == "inactivehdmaster=1": if keytype == "inactivehdseed=1":
# ensure the old master is still available # ensure the old master is still available
assert(hd_master_addr_old == addr) assert(hd_master_addr_old == addr)
elif keytype == "hdseed=1": elif keytype == "hdseed=1":