Skip to content

Commit 99cccb9

Browse files
committed
Add a method to add a pubkey to the keypool
Introduces AddKeypoolPubkey in order to add a pubkey to the keypool
1 parent 8d0ec74 commit 99cccb9

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,20 +3443,8 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
34433443
internal = true;
34443444
}
34453445

3446-
assert(m_max_keypool_index < std::numeric_limits<int64_t>::max()); // How in the hell did you use so many keys?
3447-
int64_t index = ++m_max_keypool_index;
3448-
34493446
CPubKey pubkey(GenerateNewKey(batch, internal));
3450-
if (!batch.WritePool(index, CKeyPool(pubkey, internal))) {
3451-
throw std::runtime_error(std::string(__func__) + ": writing generated key failed");
3452-
}
3453-
3454-
if (internal) {
3455-
setInternalKeyPool.insert(index);
3456-
} else {
3457-
setExternalKeyPool.insert(index);
3458-
}
3459-
m_pool_key_to_index[pubkey.GetID()] = index;
3447+
AddKeypoolPubkeyWithDB(pubkey, internal, batch);
34603448
}
34613449
if (missingInternal + missingExternal > 0) {
34623450
WalletLogPrintf("keypool added %d keys (%d internal), size=%u (%u internal)\n", missingInternal + missingExternal, missingInternal, setInternalKeyPool.size() + setExternalKeyPool.size() + set_pre_split_keypool.size(), setInternalKeyPool.size());
@@ -3466,6 +3454,29 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
34663454
return true;
34673455
}
34683456

3457+
void CWallet::AddKeypoolPubkey(const CPubKey& pubkey, const bool internal)
3458+
{
3459+
WalletBatch batch(*database);
3460+
AddKeypoolPubkeyWithDB(pubkey, internal, batch);
3461+
NotifyCanGetAddressesChanged();
3462+
}
3463+
3464+
void CWallet::AddKeypoolPubkeyWithDB(const CPubKey& pubkey, const bool internal, WalletBatch& batch)
3465+
{
3466+
LOCK(cs_wallet);
3467+
assert(m_max_keypool_index < std::numeric_limits<int64_t>::max()); // How in the hell did you use so many keys?
3468+
int64_t index = ++m_max_keypool_index;
3469+
if (!batch.WritePool(index, CKeyPool(pubkey, internal))) {
3470+
throw std::runtime_error(std::string(__func__) + ": writing imported pubkey failed");
3471+
}
3472+
if (internal) {
3473+
setInternalKeyPool.insert(index);
3474+
} else {
3475+
setExternalKeyPool.insert(index);
3476+
}
3477+
m_pool_key_to_index[pubkey.GetID()] = index;
3478+
}
3479+
34693480
bool CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal)
34703481
{
34713482
nIndex = -1;

src/wallet/wallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
10011001
bool NewKeyPool();
10021002
size_t KeypoolCountExternalKeys() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10031003
bool TopUpKeyPool(unsigned int kpSize = 0);
1004+
void AddKeypoolPubkey(const CPubKey& pubkey, const bool internal);
1005+
void AddKeypoolPubkeyWithDB(const CPubKey& pubkey, const bool internal, WalletBatch& batch);
10041006

10051007
/**
10061008
* Reserves a key from the keypool and sets nIndex to its index

0 commit comments

Comments
 (0)