diff options
| author | Bitflash <btf01@proton.me> | 2026-08-01 17:53:17 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-01 17:53:17 -0300 |
| commit | ce59d8c72c667ce638bd12f37db26d8b3408a5d2 (patch) | |
| tree | 95c62a0d0cc0a21bc3349264ce0a3c949deb32c3 /src/main.h | |
| parent | 593cf346dbce0a8ce46a13a73a4f78ddd044ab52 (diff) | |
wallet: derive the key pool from a seed, so a phrase could bring it back (#93)
Adds BIP32 derivation behind the wallet key pool, with no user-facing way to create a seed yet. wallet.dat gains hdmaster/hdchaincode/hdnext; the mnemonic is never stored. TopUpKeyPool derives m/n' when a seed exists and falls back to a random key, with a log line, if derivation fails. New -selftest=wallet-hd, wired into make tests on both platforms.
Diffstat (limited to 'src/main.h')
| -rw-r--r-- | src/main.h | 28 |
1 files changed, 28 insertions, 0 deletions
| @@ -141,6 +141,33 @@ static const int KEYPOOL_SIZE = 100; | |||
| 141 | extern CCriticalSection cs_keyPool; | 141 | extern CCriticalSection cs_keyPool; |
| 142 | extern map<int64, vector<unsigned char> > mapKeyPool; | 142 | extern map<int64, vector<unsigned char> > mapKeyPool; |
| 143 | 143 | ||
| 144 | // --- Deterministic wallet (BIP32) ----------------------------------------- | ||
| 145 | // | ||
| 146 | // When a seed is present the key pool is derived from it -- m/0'/n, hardened, | ||
| 147 | // n increasing -- instead of being made of random keys. That is what lets a | ||
| 148 | // recovery phrase bring a wallet back: the same twelve words reproduce the same | ||
| 149 | // keys in the same order. | ||
| 150 | // | ||
| 151 | // A wallet without a seed keeps working exactly as before. Nothing here | ||
| 152 | // migrates an existing wallet on its own: the seed is created only when the | ||
| 153 | // user asks for a phrase and confirms they have written it down, because a | ||
| 154 | // phrase generated silently is a backup nobody has. | ||
| 155 | // | ||
| 156 | // Empty until a seed exists. | ||
| 157 | extern vector<unsigned char> vchHDMaster; // 32-byte master private key (IL) | ||
| 158 | extern vector<unsigned char> vchHDChainCode; // 32 bytes (IR) | ||
| 159 | extern unsigned int nHDNext; // next child index to derive | ||
| 160 | inline bool HaveHDSeed() { return vchHDMaster.size() == 32 && vchHDChainCode.size() == 32; } | ||
| 161 | |||
| 162 | // Install a seed derived from a mnemonic, replacing any existing one, and reset | ||
| 163 | // the derivation counter. Writes to wallet.dat. Returns false and leaves the | ||
| 164 | // wallet untouched if the phrase is not valid. | ||
| 165 | bool SetHDSeedFromMnemonic(const string& strMnemonic, string& strErrorRet); | ||
| 166 | |||
| 167 | // Derive the child at nIndex and return it as a key. Used by the key pool and | ||
| 168 | // by restore, which needs to run ahead of the pool. | ||
| 169 | bool DeriveHDKey(unsigned int nIndex, CKey& keyRet, string& strErrorRet); | ||
| 170 | |||
| 144 | // Fill the pool back up to KEYPOOL_SIZE. Every key it creates is written to | 171 | // Fill the pool back up to KEYPOOL_SIZE. Every key it creates is written to |
| 145 | // wallet.dat before it is offered to anybody. | 172 | // wallet.dat before it is offered to anybody. |
| 146 | void TopUpKeyPool(); | 173 | void TopUpKeyPool(); |
| @@ -156,6 +183,7 @@ void ReacceptWalletTransactions(); | |||
| 156 | // a key this wallet holds. Returns how many were added or updated. Needed by | 183 | // a key this wallet holds. Returns how many were added or updated. Needed by |
| 157 | // anything that puts a key into the wallet after the fact -- an import, a | 184 | // anything that puts a key into the wallet after the fact -- an import, a |
| 158 | // restored backup, and later a recovery phrase. | 185 | // restored backup, and later a recovery phrase. |
| 186 | bool CanScanWalletTransactions(string& strErrorRet); | ||
| 159 | int ScanForWalletTransactions(CBlockIndex* pindexStart); | 187 | int ScanForWalletTransactions(CBlockIndex* pindexStart); |
| 160 | // Returns how many wallet transactions the chain corrected. | 188 | // Returns how many wallet transactions the chain corrected. |
| 161 | int RescanSpentFlags(); | 189 | int RescanSpentFlags(); |