Skip to content

Commit

Permalink
Merge #8142: Improve CWallet API with new GetAccountPubkey function.
Browse files Browse the repository at this point in the history
152ab23 Improve CWallet API  with new GetAccountPubkey function. (Patrick Strateman)
  • Loading branch information
laanwj committed Jun 6, 2016
2 parents 6b781df + 152ab23 commit 52c3f34
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
34 changes: 4 additions & 30 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -146,38 +146,12 @@ UniValue getnewaddress(const UniValue& params, bool fHelp)


CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false) CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
{ {
CWalletDB walletdb(pwalletMain->strWalletFile); CPubKey pubKey;

if (!pwalletMain->GetAccountPubkey(pubKey, strAccount, bForceNew)) {
CAccount account; throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
walletdb.ReadAccount(strAccount, account);

if (!bForceNew) {
if (!account.vchPubKey.IsValid())
bForceNew = true;
else {
// Check if the current key has been used
CScript scriptPubKey = GetScriptForDestination(account.vchPubKey.GetID());
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin();
it != pwalletMain->mapWallet.end() && account.vchPubKey.IsValid();
++it)
BOOST_FOREACH(const CTxOut& txout, (*it).second.vout)
if (txout.scriptPubKey == scriptPubKey) {
bForceNew = true;
break;
}
}
}

// Generate a new key
if (bForceNew) {
if (!pwalletMain->GetKeyFromPool(account.vchPubKey))
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");

pwalletMain->SetAddressBook(account.vchPubKey.GetID(), strAccount, "receive");
walletdb.WriteAccount(strAccount, account);
} }


return CBitcoinAddress(account.vchPubKey.GetID()); return CBitcoinAddress(pubKey.GetID());
} }


UniValue getaccountaddress(const UniValue& params, bool fHelp) UniValue getaccountaddress(const UniValue& params, bool fHelp)
Expand Down
38 changes: 38 additions & 0 deletions src/wallet/wallet.cpp
Expand Up @@ -640,6 +640,44 @@ bool CWallet::AccountMove(std::string strFrom, std::string strTo, CAmount nAmoun
return true; return true;
} }


bool CWallet::GetAccountPubkey(CPubKey &pubKey, std::string strAccount, bool bForceNew)
{
CWalletDB walletdb(strWalletFile);

CAccount account;
walletdb.ReadAccount(strAccount, account);

if (!bForceNew) {
if (!account.vchPubKey.IsValid())
bForceNew = true;
else {
// Check if the current key has been used
CScript scriptPubKey = GetScriptForDestination(account.vchPubKey.GetID());
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin();
it != mapWallet.end() && account.vchPubKey.IsValid();
++it)
BOOST_FOREACH(const CTxOut& txout, (*it).second.vout)
if (txout.scriptPubKey == scriptPubKey) {
bForceNew = true;
break;
}
}
}

// Generate a new key
if (bForceNew) {
if (!GetKeyFromPool(account.vchPubKey))
return false;

SetAddressBook(account.vchPubKey.GetID(), strAccount, "receive");
walletdb.WriteAccount(strAccount, account);
}

pubKey = account.vchPubKey;

return true;
}

void CWallet::MarkDirty() void CWallet::MarkDirty()
{ {
{ {
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Expand Up @@ -719,6 +719,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
*/ */
int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL); int64_t IncOrderPosNext(CWalletDB *pwalletdb = NULL);
bool AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment = ""); bool AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment = "");
bool GetAccountPubkey(CPubKey &pubKey, std::string strAccount, bool bForceNew = false);


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

0 comments on commit 52c3f34

Please sign in to comment.