From 31d6390fd17803ab9870e239a0c0c4d0a6be5c68 Mon Sep 17 00:00:00 2001 From: Eric Shaw Date: Thu, 25 Sep 2014 18:39:28 -0400 Subject: [PATCH] Fixed setaccount accepting foreign address Fixed issue #4209 where using setaccount with a foreign address causes the address to be added to your receiving addresses. --- src/rpcwallet.cpp | 19 ++++++++++++------- src/test/rpc_wallet_tests.cpp | 8 ++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 35637362a..b361e6791 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -230,15 +230,20 @@ Value setaccount(const Array& params, bool fHelp) if (params.size() > 1) strAccount = AccountFromValue(params[1]); - // Detect when changing the account of an address that is the 'unused current key' of another account: - if (pwalletMain->mapAddressBook.count(address.Get())) + // Only add the account if the address is yours. + if (IsMine(*pwalletMain, address.Get())) { - string strOldAccount = pwalletMain->mapAddressBook[address.Get()].name; - if (address == GetAccountAddress(strOldAccount)) - GetAccountAddress(strOldAccount, true); + // Detect when changing the account of an address that is the 'unused current key' of another account: + if (pwalletMain->mapAddressBook.count(address.Get())) + { + string strOldAccount = pwalletMain->mapAddressBook[address.Get()].name; + if (address == GetAccountAddress(strOldAccount)) + 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; } diff --git a/src/test/rpc_wallet_tests.cpp b/src/test/rpc_wallet_tests.cpp index 1dc2a3d82..8c1d2900b 100644 --- a/src/test/rpc_wallet_tests.cpp +++ b/src/test/rpc_wallet_tests.cpp @@ -80,11 +80,15 @@ BOOST_AUTO_TEST_CASE(rpc_wallet) walletdb.WriteAccount(strAccount, account); }); - + CPubKey setaccountDemoPubkey = pwalletMain->GenerateNewKey(); + CBitcoinAddress setaccountDemoAddress = CBitcoinAddress(CTxDestination(setaccountDemoPubkey.GetID())); + /********************************* * 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); /* 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X (33 chars) is an illegal address (should be 34 chars) */ BOOST_CHECK_THROW(CallRPC("setaccount 1D1ZrZNe3JUo7ZycKEYQQiQAWd9y54F4X nullaccount"), runtime_error);