Skip to content

Commit 808c84f

Browse files
author
MarcoFalke
committed
Merge #11483: Fix importmulti bug when importing an already imported key
a44a215 Fix importmulti bug when importing an already imported key (Pedro Branco) Pull request description: This PR fixes a bug in `importmulti` RPC call where it returns an invalid response when importing an already imported key. Before: ```sh ❯ bitcoin-cli -regtest importmulti '[{ "keys": ["cNcMUunXhVK1dXJ5riixtpYSxPXZnUAMGS4vpzwChdKmYY3Rz99v"], "scriptPubKey": { "address": "n4YZAf4WE2XF3t4BfeYS2nHAhb8CVx91BR" }, "timestamp": 1507655239 }]' [{ "success": true }] ❯ bitcoin-cli -regtest importmulti '[{ "keys": ["cNcMUunXhVK1dXJ5riixtpYSxPXZnUAMGS4vpzwChdKmYY3Rz99v"], "scriptPubKey": { "address": "n4YZAf4WE2XF3t4BfeYS2nHAhb8CVx91BR" }, "timestamp": 1507655239 }]' '{ "rescan": false }' [ false ] ❯ bitcoin-cli -regtest importmulti '[{ "keys": ["cNcMUunXhVK1dXJ5riixtpYSxPXZnUAMGS4vpzwChdKmYY3Rz99v"], "scriptPubKey": { "address": "n4YZAf4WE2XF3t4BfeYS2nHAhb8CVx91BR" }, "timestamp": 1507655239 }]' '{ "rescan": true }' error code: -1 error message: JSON value is not a boolean as expected ``` After this fix: ```sh ❯ bitcoin-cli -rpcuser=u -rpcpassword=p -regtest importmulti '[{ "keys": ["cNcMUunXhVK1dXJ5riixtpYSxPXZnUAMGS4vpzwChdKmYY3Rz99v"], "scriptPubKey": { "address": "n4YZAf4WE2XF3t4BfeYS2nHAhb8CVx91BR" }, "timestamp": 1507655139 }]' [{ "success": true }] ❯ bitcoin-cli -rpcuser=u -rpcpassword=p -regtest importmulti '[{ "keys": ["cNcMUunXhVK1dXJ5riixtpYSxPXZnUAMGS4vpzwChdKmYY3Rz99v"], "scriptPubKey": { "address": "n4YZAf4WE2XF3t4BfeYS2nHAhb8CVx91BR" }, "timestamp": 1507655139 }]' [{ "success": false, "error": { "code": -4, "message": "The wallet already contains the private key for this address or script" } }] ``` Tree-SHA512: 4acebdfb7d0ebd7cd48e943b93ed1cec072db1ace5c42b3f5cc225603764b6e804e4b823b0710965826aafc2f0c615c53d5aefcfdb9bc9c379f5221b798a318c
2 parents 14b860b + a44a215 commit 808c84f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/wallet/rpcdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, const int6
961961
pwallet->SetAddressBook(vchAddress, label, "receive");
962962

963963
if (pwallet->HaveKey(vchAddress)) {
964-
return false;
964+
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script");
965965
}
966966

967967
pwallet->mapKeyMetadata[vchAddress].nCreateTime = timestamp;

test/functional/importmulti.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ def run_test (self):
160160
assert_equal(address_assert['ismine'], True)
161161
assert_equal(address_assert['timestamp'], timestamp)
162162

163+
self.log.info("Should not import an address with private key if is already imported")
164+
result = self.nodes[1].importmulti([{
165+
"scriptPubKey": {
166+
"address": address['address']
167+
},
168+
"timestamp": "now",
169+
"keys": [ self.nodes[0].dumpprivkey(address['address']) ]
170+
}])
171+
assert_equal(result[0]['success'], False)
172+
assert_equal(result[0]['error']['code'], -4)
173+
assert_equal(result[0]['error']['message'], 'The wallet already contains the private key for this address or script')
174+
163175
# Address + Private key + watchonly
164176
self.log.info("Should not import an address with private key and with watchonly")
165177
address = self.nodes[0].validateaddress(self.nodes[0].getnewaddress())

0 commit comments

Comments
 (0)