From d54a07170808b921c79e25c2a0a8f6b9c447d4ea Mon Sep 17 00:00:00 2001 From: Bitflash Date: Sat, 1 Aug 2026 21:48:03 -0300 Subject: wallet: reserve index 0 before deriving it, and name the address the phrase does not cover (#104) Follow-up to #102. Reserves derivation index 0 before deriving it, so a failure between installing the seed and writing the default key cannot leave the pool free to hand that index out as an ordinary key. Renames the address that stops being the default to 'Your Address (created before the recovery phrase)', which was otherwise left under the same name as the new one. Also documents why the key-pool clear precedes the seed write, correcting a review comment of mine that asked for the reverse. --- src/selftest.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/selftest.cpp') diff --git a/src/selftest.cpp b/src/selftest.cpp index bc9fa5a..88e9863 100644 --- a/src/selftest.cpp +++ b/src/selftest.cpp @@ -334,6 +334,10 @@ static int RunWalletHDSelfTest() nFail += Check(nRandomPoolSize == KEYPOOL_SIZE, "a wallet can start with a random key pool") ? 0 : 1; + // The address the wallet was showing before any of this. It has to + // survive under a name that says it is not covered by the phrase. + std::string strPreSeedAddr = PubKeyToAddress(keyUser.GetPubKey()); + nFail += Check(SetHDSeedFromMnemonic(strPhraseA, strError), "a valid phrase installs a seed") ? 0 : 1; nFail += Check(HaveHDSeed(), "the wallet reports having a seed") ? 0 : 1; @@ -356,6 +360,14 @@ static int RunWalletHDSelfTest() nFail += Check(WalletHasPrivateKey(vchDefaultKey), "the derived default key is stored in wallet.dat") ? 0 : 1; + std::string strNewAddr = PubKeyToAddress(vchDefaultKey); + nFail += Check(strPreSeedAddr != strNewAddr, + "the visible address stops being the pre-seed one") ? 0 : 1; + nFail += Check(mapAddressBook.count(strPreSeedAddr) > 0 && + mapAddressBook[strPreSeedAddr] != + mapAddressBook[strNewAddr], + "the pre-seed address is kept, named apart from the new one") ? 0 : 1; + std::vector vchBefore = vchHDMaster; nFail += Check(!SetHDSeedFromMnemonic("not a mnemonic at all", strError), "an invalid phrase is refused") ? 0 : 1; -- cgit v1.2.3