@@ -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+
550595BOOST_AUTO_TEST_SUITE_END ()
0 commit comments