Skip to content

Commit 52c3f34

Browse files
committed
Merge #8142: Improve CWallet API with new GetAccountPubkey function.
152ab23 Improve CWallet API with new GetAccountPubkey function. (Patrick Strateman)
2 parents 6b781df + 152ab23 commit 52c3f34

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -146,38 +146,12 @@ UniValue getnewaddress(const UniValue& params, bool fHelp)
146146

147147
CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
148148
{
149-
CWalletDB walletdb(pwalletMain->strWalletFile);
150-
151-
CAccount account;
152-
walletdb.ReadAccount(strAccount, account);
153-
154-
if (!bForceNew) {
155-
if (!account.vchPubKey.IsValid())
156-
bForceNew = true;
157-
else {
158-
// Check if the current key has been used
159-
CScript scriptPubKey = GetScriptForDestination(account.vchPubKey.GetID());
160-
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin();
161-
it != pwalletMain->mapWallet.end() && account.vchPubKey.IsValid();
162-
++it)
163-
BOOST_FOREACH(const CTxOut& txout, (*it).second.vout)
164-
if (txout.scriptPubKey == scriptPubKey) {
165-
bForceNew = true;
166-
break;
167-
}
168-
}
169-
}
170-
171-
// Generate a new key
172-
if (bForceNew) {
173-
if (!pwalletMain->GetKeyFromPool(account.vchPubKey))
174-
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
175-
176-
pwalletMain->SetAddressBook(account.vchPubKey.GetID(), strAccount, "receive");
177-
walletdb.WriteAccount(strAccount, account);
149+
CPubKey pubKey;
150+
if (!pwalletMain->GetAccountPubkey(pubKey, strAccount, bForceNew)) {
151+
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
178152
}
179153

180-
return CBitcoinAddress(account.vchPubKey.GetID());
154+
return CBitcoinAddress(pubKey.GetID());
181155
}
182156

183157
UniValue getaccountaddress(const UniValue& params, bool fHelp)

src/wallet/wallet.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,44 @@ bool CWallet::AccountMove(std::string strFrom, std::string strTo, CAmount nAmoun
640640
return true;
641641
}
642642

643+
bool CWallet::GetAccountPubkey(CPubKey &pubKey, std::string strAccount, bool bForceNew)
644+
{
645+
CWalletDB walletdb(strWalletFile);
646+
647+
CAccount account;
648+
walletdb.ReadAccount(strAccount, account);
649+
650+
if (!bForceNew) {
651+
if (!account.vchPubKey.IsValid())
652+
bForceNew = true;
653+
else {
654+
// Check if the current key has been used
655+
CScript scriptPubKey = GetScriptForDestination(account.vchPubKey.GetID());
656+
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin();
657+
it != mapWallet.end() && account.vchPubKey.IsValid();
658+
++it)
659+
BOOST_FOREACH(const CTxOut& txout, (*it).second.vout)
660+
if (txout.scriptPubKey == scriptPubKey) {
661+
bForceNew = true;
662+
break;
663+
}
664+
}
665+
}
666+
667+
// Generate a new key
668+
if (bForceNew) {
669+
if (!GetKeyFromPool(account.vchPubKey))
670+
return false;
671+
672+
SetAddressBook(account.vchPubKey.GetID(), strAccount, "receive");
673+
walletdb.WriteAccount(strAccount, account);
674+
}
675+
676+
pubKey = account.vchPubKey;
677+
678+
return true;
679+
}
680+
643681
void CWallet::MarkDirty()
644682
{
645683
{

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
719719
*/
720720
int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
721721
bool AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment = "");
722+
bool GetAccountPubkey(CPubKey &pubKey, std::string strAccount, bool bForceNew = false);
722723

723724
void MarkDirty();
724725
bool AddToWallet(const CWalletTx& wtxIn, bool fFromLoadWallet, CWalletDB* pwalletdb);

0 commit comments

Comments
 (0)