Merge pull request #4985

bc470c4 Changed mixed indentation to four spaces (Eric Shaw)
31d6390 Fixed setaccount accepting foreign address (Eric Shaw)
This commit is contained in:
Wladimir J. van der Laan 2014-10-02 19:31:48 +02:00
commit 20a4b69360
No known key found for this signature in database
GPG key ID: 74810B012346C9A6
2 changed files with 82 additions and 74 deletions

View file

@ -230,6 +230,9 @@ Value setaccount(const Array& params, bool fHelp)
if (params.size() > 1) if (params.size() > 1)
strAccount = AccountFromValue(params[1]); strAccount = AccountFromValue(params[1]);
// Only add the account if the address is yours.
if (IsMine(*pwalletMain, address.Get()))
{
// Detect when changing the account of an address that is the 'unused current key' of another account: // Detect when changing the account of an address that is the 'unused current key' of another account:
if (pwalletMain->mapAddressBook.count(address.Get())) if (pwalletMain->mapAddressBook.count(address.Get()))
{ {
@ -237,8 +240,10 @@ Value setaccount(const Array& params, bool fHelp)
if (address == GetAccountAddress(strOldAccount)) if (address == GetAccountAddress(strOldAccount))
GetAccountAddress(strOldAccount, true); GetAccountAddress(strOldAccount, true);
} }
pwalletMain->SetAddressBook(address.Get(), strAccount, "receive"); pwalletMain->SetAddressBook(address.Get(), strAccount, "receive");
}
else
throw JSONRPCError(RPC_MISC_ERROR, "setaccount can only be used with own address");
return Value::null; return Value::null;
} }

View file

@ -80,11 +80,15 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
walletdb.WriteAccount(strAccount, account); walletdb.WriteAccount(strAccount, account);
}); });
CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey();
CBitcoinAddress setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID()));
/********************************* /*********************************
* setaccount * setaccount
*********************************/ *********************************/
BOOST_CHECK_NO_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ nullaccount")); BOOST_CHECK_NO_THROW(CallRPC("setaccount " + setaccountDemoAddress.ToString() + " nullaccount"));
/* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ is not owned by the test wallet. */
BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4XZ nullaccount"), runtime_error);
BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error); BOOST_CHECK_THROW(CallRPC("setaccount"), runtime_error);
/* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X (33 chars) is an illegal address (should be 34 chars) */ /* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X (33 chars) is an illegal address (should be 34 chars) */
BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X nullaccount"), runtime_error); BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X nullaccount"), runtime_error);
@ -173,7 +177,6 @@ BOOST_AUTO_TEST_CASE(rpc_wallet)
Array arr = retValue.get_array(); Array arr = retValue.get_array();
BOOST_CHECK(arr.size() > 0); BOOST_CHECK(arr.size() > 0);
BOOST_CHECK(CBitcoinAddress(arr[0].get_str()).Get() == demoAddress.Get()); BOOST_CHECK(CBitcoinAddress(arr[0].get_str()).Get() == demoAddress.Get());
} }