Skip to content

Support HD derivation of master blinding key #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: elements-0.14.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ CPubKey CWallet::GenerateNewKey()
return pubkey;
}

void CWallet::DeriveBlindingKey(CKeyMetadata& metadata, CKey& secret)
{
// Key is derived at m/0'/2' to avoid collision with hd split wallets
CKey key; //master key seed (256bit)
CExtKey masterKey; //hd master key
CExtKey accountKey; //key at m/0'
CExtKey externalChainChildKey; //key at m/0'/2'

// try to get the master key
if (!GetKey(hdChain.masterKeyID, key))
throw std::runtime_error(std::string(__func__) + ": Master key not found");

masterKey.SetMaster(key.begin(), key.size());

// derive m/0'
// use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT);

// derive m/0'/2'
accountKey.Derive(externalChainChildKey, 2 | BIP32_HARDENED_KEY_LIMIT);

metadata.hdKeypath = "m/0'/2'";
metadata.hdMasterKeyID = hdChain.masterKeyID;
secret = externalChainChildKey.key;
return;
}

void CWallet::DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret)
{
// for now we use a fixed keypath scheme of m/0'/0'/k
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
*/
CPubKey GenerateNewKey();
void DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret);
//! Derives static blinding key at m/0'/2'
void DeriveBlindingKey(CKeyMetadata& metadata, CKey& secret);
//! Adds a key to the store, and saves it to disk.
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
//! Adds a key to the store, without saving it to disk (used by LoadWallet)
Expand Down
8 changes: 7 additions & 1 deletion src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,13 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)

if (result == DB_LOAD_OK && pwallet->blinding_derivation_key.IsNull()) {
CKey key;
key.MakeNewKey(true);
if (pwallet->IsHDEnabled()) {
int64_t nCreationTime = GetTime();
CKeyMetadata metadata(nCreationTime);
pwallet->DeriveBlindingKey(metadata, key);
} else {
key.MakeNewKey(true);
}
uint256 keybin;
memcpy(keybin.begin(), key.begin(), key.size());
pwallet->blinding_derivation_key = keybin;
Expand Down