Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 196 Lines • ▼ Show 20 Lines | CPubKey CWallet::GenerateNewKey(WalletBatch &batch, bool internal) { | ||||
} | } | ||||
return pubkey; | return pubkey; | ||||
} | } | ||||
void CWallet::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata &metadata, | void CWallet::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata &metadata, | ||||
CKey &secret, bool internal) { | 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 | ||||
// master key seed (256bit) | // seed (256bit) | ||||
CKey key; | CKey seed; | ||||
// hd master key | // hd master key | ||||
CExtKey masterKey; | CExtKey masterKey; | ||||
// key at m/0' | // key at m/0' | ||||
CExtKey accountKey; | CExtKey accountKey; | ||||
// key at m/0'/0' (external) or m/0'/1' (internal) | // key at m/0'/0' (external) or m/0'/1' (internal) | ||||
CExtKey chainChildKey; | CExtKey chainChildKey; | ||||
// key at m/0'/0'/<n>' | // key at m/0'/0'/<n>' | ||||
CExtKey childKey; | CExtKey childKey; | ||||
// 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__) + | throw std::runtime_error(std::string(__func__) + ": seed not found"); | ||||
": Master key 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 | // use hardened derivation (child keys >= 0x80000000 are hardened after | ||||
// bip32) | // bip32) | ||||
masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT); | masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT); | ||||
// derive m/0'/0' (external chain) OR m/0'/1' (internal chain) | // derive m/0'/0' (external chain) OR m/0'/1' (internal chain) | ||||
assert(internal ? CanSupportFeature(FEATURE_HD_SPLIT) : true); | assert(internal ? CanSupportFeature(FEATURE_HD_SPLIT) : true); | ||||
▲ Show 20 Lines • Show All 551 Lines • ▼ Show 20 Lines | if (!crypter.Encrypt(_vMasterKey, kMasterKey.vchCryptedKey)) { | ||||
} | } | ||||
delete encrypted_batch; | delete encrypted_batch; | ||||
encrypted_batch = nullptr; | encrypted_batch = nullptr; | ||||
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()) { | ||||
SetHDSeed(GenerateNewSeed()); | SetHDSeed(GenerateNewSeed()); | ||||
} | } | ||||
NewKeyPool(); | NewKeyPool(); | ||||
Lock(); | Lock(); | ||||
// Need to completely rewrite the wallet file; if we don't, bdb might | // Need to completely rewrite the wallet file; if we don't, bdb might | ||||
▲ Show 20 Lines • Show All 754 Lines • ▼ Show 20 Lines | CPubKey CWallet::GenerateNewSeed() { | ||||
key.MakeNewKey(true); | key.MakeNewKey(true); | ||||
return DeriveNewSeed(key); | return DeriveNewSeed(key); | ||||
} | } | ||||
CPubKey CWallet::DeriveNewSeed(const CKey &key) { | 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__) + | throw std::runtime_error(std::string(__func__) + | ||||
": AddKeyPubKey failed"); | ": AddKeyPubKey failed"); | ||||
} | } | ||||
return pubkey; | return seed; | ||||
} | } | ||||
void CWallet::SetHDSeed(const CPubKey &seed) { | void CWallet::SetHDSeed(const CPubKey &seed) { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
// Store the keyid (hash160) together with the child index counter in the | // Store the keyid (hash160) together with the child index counter in the | ||||
// database as a hdchain object. | // database as a hdchain object. | ||||
CHDChain newHdChain; | CHDChain newHdChain; | ||||
▲ Show 20 Lines • Show All 2,894 Lines • ▼ Show 20 Lines | if (fFirstRun) { | ||||
if (!gArgs.GetBoolArg("-usehd", true)) { | if (!gArgs.GetBoolArg("-usehd", true)) { | ||||
InitError(strprintf(_("Error creating %s: You can't create non-HD " | InitError(strprintf(_("Error creating %s: You can't create non-HD " | ||||
"wallets with this version."), | "wallets with this version."), | ||||
walletFile)); | walletFile)); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
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(); | ||||
walletInstance->SetHDSeed(masterPubKey); | walletInstance->SetHDSeed(seed); | ||||
// Top up the keypool | // Top up the keypool | ||||
if (!walletInstance->TopUpKeyPool()) { | if (!walletInstance->TopUpKeyPool()) { | ||||
InitError(_("Unable to generate initial keys") += "\n"); | InitError(_("Unable to generate initial keys") += "\n"); | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
walletInstance->ChainStateFlushed(chainActive.GetLocator()); | walletInstance->ChainStateFlushed(chainActive.GetLocator()); | ||||
▲ Show 20 Lines • Show All 365 Lines • Show Last 20 Lines |