Skip to content

Commit c375fe8

Browse files
Merge pull request dogecoin#3780 from edtubbs/1.15.0-dev-update-keypath
wallet: update derivation path to m/0'/3'/n'
2 parents 71929ba + cceba59 commit c375fe8

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,49 @@ BOOST_AUTO_TEST_CASE(GetMinimumFee_dust_test)
547547
CWallet::discardThreshold = COIN / 100;
548548
}
549549

550+
BOOST_FIXTURE_TEST_CASE(derive_new_child_key_test, WalletTestingSetup)
551+
{
552+
CWallet* wallet = pwalletMain;
553+
LOCK(wallet->cs_wallet);
554+
555+
// Use a fixed seed and known child public key
556+
std::string testSeedHex = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
557+
std::string expectedPubKeyHex = "0305a077194300e27d320a9504f808a16f05b38dabead31f10104c075d88f81a38";
558+
std::vector<unsigned char> seed = ParseHex(testSeedHex);
559+
560+
// Initialize the master key with the fixed seed and get the public key
561+
CKey masterKey;
562+
masterKey.Set(seed.begin(), seed.end(), true);
563+
CPubKey masterPubKey = masterKey.GetPubKey();
564+
BOOST_CHECK(masterKey.VerifyPubKey(masterPubKey));
565+
566+
// Set up master metadata: path "m" and master key ID equal to the public key hash
567+
int64_t nCreationTime = GetTime();
568+
CKeyMetadata masterMetadata(nCreationTime);
569+
masterMetadata.hdKeypath = "m";
570+
masterMetadata.hdMasterKeyID = masterPubKey.GetID();
571+
572+
// Store the master key and metadata in the wallet
573+
wallet->mapKeyMetadata[masterPubKey.GetID()] = masterMetadata;
574+
BOOST_CHECK(wallet->AddKeyPubKey(masterKey, masterPubKey));
575+
BOOST_CHECK(wallet->SetHDMasterKey(masterPubKey));
576+
577+
// Derive a new child key using at derivation path (m/0'/3'/0')
578+
CKeyMetadata childMetadata(GetTime());
579+
CKey childKey;
580+
wallet->DeriveNewChildKey(childMetadata, childKey);
581+
582+
// Verify the child key is valid
583+
CPubKey childPubKey = childKey.GetPubKey();
584+
BOOST_CHECK(childKey.IsValid());
585+
BOOST_CHECK(childPubKey.IsFullyValid());
586+
587+
// Verify that the metadata reflects the path "m/0'/3'/0'"
588+
BOOST_CHECK_EQUAL(childMetadata.hdKeypath, "m/0'/3'/0'");
589+
590+
// Verify the child public key matches the expected value
591+
std::string derivedPubKeyHex = HexStr(childPubKey.begin(), childPubKey.end());
592+
BOOST_CHECK_EQUAL(derivedPubKeyHex, expectedPubKeyHex);
593+
}
594+
550595
BOOST_AUTO_TEST_SUITE_END()

src/wallet/wallet.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ bool fWalletRbf = DEFAULT_WALLET_RBF;
4848

4949
const char * DEFAULT_WALLET_DAT = "wallet.dat";
5050
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
51+
const uint32_t BIP44_COIN_TYPE = 3;
5152

5253
/**
5354
* Fees smaller than this (in satoshi) are considered zero fee (for transaction creation)
@@ -130,12 +131,12 @@ CPubKey CWallet::GenerateNewKey()
130131

131132
void CWallet::DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret)
132133
{
133-
// for now we use a fixed keypath scheme of m/0'/0'/k
134+
// for now we use a fixed keypath scheme of m/0'/3'/k
134135
CKey key; //master key seed (256bit)
135136
CExtKey masterKey; //hd master key
136137
CExtKey accountKey; //key at m/0'
137-
CExtKey externalChainChildKey; //key at m/0'/0'
138-
CExtKey childKey; //key at m/0'/0'/<n>'
138+
CExtKey externalChainChildKey; //key at m/0'/3'
139+
CExtKey childKey; //key at m/0'/3'/<n>'
139140

140141
// try to get the master key
141142
if (!GetKey(hdChain.masterKeyID, key))
@@ -147,8 +148,8 @@ void CWallet::DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret)
147148
// use hardened derivation (child keys >= 0x80000000 are hardened after bip32)
148149
masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT);
149150

150-
// derive m/0'/0'
151-
accountKey.Derive(externalChainChildKey, BIP32_HARDENED_KEY_LIMIT);
151+
// derive m/0'/3'
152+
accountKey.Derive(externalChainChildKey, BIP44_COIN_TYPE | BIP32_HARDENED_KEY_LIMIT);
152153

153154
// derive child key at next index, skip keys already known to the wallet
154155
do {

0 commit comments

Comments
 (0)