2016-10-20 09:04:18 +02:00
#!/usr/bin/env python3
2020-12-31 18:50:11 +01:00
# Copyright (c) 2014-2020 The Bitcoin Core developers
2016-10-20 09:04:18 +02:00
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
2021-10-28 07:28:23 +02:00
""" Test the importmulti RPC.
Test importmulti by generating keys on node0 , importing the scriptPubKeys and
addresses on node1 and then testing the address info for the different address
variants .
- ` get_key ( ) ` and ` get_multisig ( ) ` are called to generate keys on node0 and
return the privkeys , pubkeys and all variants of scriptPubKey and address .
- ` test_importmulti ( ) ` is called to send an importmulti call to node1 , test
success , and ( if unsuccessful ) test the error code and error message returned .
- ` test_address ( ) ` is called to call getaddressinfo for an address on node1
and test the values returned . """
2021-05-31 11:26:16 +02:00
from test_framework . blocktools import COINBASE_MATURITY
2021-10-28 07:28:23 +02:00
from test_framework . script import (
CScript ,
OP_NOP ,
)
2016-10-20 09:04:18 +02:00
from test_framework . test_framework import BitcoinTestFramework
2021-10-28 09:57:55 +02:00
from test_framework . descriptors import descsum_create
2018-09-10 20:04:31 +02:00
from test_framework . util import (
assert_equal ,
assert_greater_than ,
assert_raises_rpc_error ,
)
2019-01-09 22:58:40 +01:00
from test_framework . wallet_util import (
get_key ,
get_multisig ,
test_address ,
)
2020-04-26 16:47:31 +02:00
2018-09-10 20:04:31 +02:00
class ImportMultiTest ( BitcoinTestFramework ) :
2017-09-01 18:47:13 +02:00
def set_test_params ( self ) :
2016-10-20 09:04:18 +02:00
self . num_nodes = 2
self . setup_clean_chain = True
2019-02-15 00:10:52 +01:00
self . extra_args = [ [ ' -usehd=1 ' ] ] * self . num_nodes
2016-10-20 09:04:18 +02:00
2018-09-13 12:33:15 +02:00
def skip_test_if_missing_module ( self ) :
self . skip_if_no_wallet ( )
2017-05-02 20:02:55 +02:00
def setup_network ( self ) :
self . setup_nodes ( )
2016-10-20 09:04:18 +02:00
Merge #16726: tests: Avoid common Python default parameter gotcha when mutable dict/list:s are used as default parameter values
e4f4ea47ebf7774fb6f445adde7bf7ea71fa05a1 lint: Catch use of [] or {} as default parameter values in Python functions (practicalswift)
25dd86715039586d92176eee16e9c6644d2547f0 Avoid using mutable default parameter values (practicalswift)
Pull request description:
Avoid common Python default parameter gotcha when mutable `dict`/`list`:s are used as default parameter values.
Examples of this gotcha caught during review:
* https://github.com/bitcoin/bitcoin/pull/16673#discussion_r317415261
* https://github.com/bitcoin/bitcoin/pull/14565#discussion_r241942304
Perhaps surprisingly this is how mutable list and dictionary default parameter values behave in Python:
```
>>> def f(i, j=[], k={}):
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1, 1], {1: True})
>>> f(2)
([1, 1, 2], {1: True, 2: True})
```
In contrast to:
```
>>> def f(i, j=None, k=None):
... if j is None:
... j = []
... if k is None:
... k = {}
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1], {1: True})
>>> f(2)
([2], {2: True})
```
The latter is typically the intended behaviour.
This PR fixes two instances of this and adds a check guarding against this gotcha going forward :-)
ACKs for top commit:
Sjors:
Oh Python... ACK e4f4ea47ebf7774fb6f445adde7bf7ea71fa05a1. Testing tip: swap the two commits.
Tree-SHA512: 56e14d24fc866211a20185c9fdb274ed046c3aed2dc0e07699e58b6f9fa3b79f6d0c880fb02d72b7fe5cc5eb7c0ff6da0ead33123344e1a872209370c2e49e3f
2019-08-28 19:34:22 +02:00
def test_importmulti ( self , req , success , error_code = None , error_message = None , warnings = None ) :
2021-10-28 07:28:23 +02:00
""" Run importmulti and assert success """
Merge #16726: tests: Avoid common Python default parameter gotcha when mutable dict/list:s are used as default parameter values
e4f4ea47ebf7774fb6f445adde7bf7ea71fa05a1 lint: Catch use of [] or {} as default parameter values in Python functions (practicalswift)
25dd86715039586d92176eee16e9c6644d2547f0 Avoid using mutable default parameter values (practicalswift)
Pull request description:
Avoid common Python default parameter gotcha when mutable `dict`/`list`:s are used as default parameter values.
Examples of this gotcha caught during review:
* https://github.com/bitcoin/bitcoin/pull/16673#discussion_r317415261
* https://github.com/bitcoin/bitcoin/pull/14565#discussion_r241942304
Perhaps surprisingly this is how mutable list and dictionary default parameter values behave in Python:
```
>>> def f(i, j=[], k={}):
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1, 1], {1: True})
>>> f(2)
([1, 1, 2], {1: True, 2: True})
```
In contrast to:
```
>>> def f(i, j=None, k=None):
... if j is None:
... j = []
... if k is None:
... k = {}
... j.append(i)
... k[i] = True
... return j, k
...
>>> f(1)
([1], {1: True})
>>> f(1)
([1], {1: True})
>>> f(2)
([2], {2: True})
```
The latter is typically the intended behaviour.
This PR fixes two instances of this and adds a check guarding against this gotcha going forward :-)
ACKs for top commit:
Sjors:
Oh Python... ACK e4f4ea47ebf7774fb6f445adde7bf7ea71fa05a1. Testing tip: swap the two commits.
Tree-SHA512: 56e14d24fc866211a20185c9fdb274ed046c3aed2dc0e07699e58b6f9fa3b79f6d0c880fb02d72b7fe5cc5eb7c0ff6da0ead33123344e1a872209370c2e49e3f
2019-08-28 19:34:22 +02:00
if warnings is None :
warnings = [ ]
2021-10-28 07:28:23 +02:00
result = self . nodes [ 1 ] . importmulti ( [ req ] )
2021-10-28 07:30:15 +02:00
observed_warnings = [ ]
if ' warnings ' in result [ 0 ] :
2020-04-26 16:47:31 +02:00
observed_warnings = result [ 0 ] [ ' warnings ' ]
2021-10-28 07:30:15 +02:00
assert_equal ( " \n " . join ( sorted ( warnings ) ) , " \n " . join ( sorted ( observed_warnings ) ) )
2021-10-28 07:28:23 +02:00
assert_equal ( result [ 0 ] [ ' success ' ] , success )
if error_code is not None :
assert_equal ( result [ 0 ] [ ' error ' ] [ ' code ' ] , error_code )
assert_equal ( result [ 0 ] [ ' error ' ] [ ' message ' ] , error_message )
def run_test ( self ) :
2017-03-09 21:16:20 +01:00
self . log . info ( " Mining blocks... " )
2016-10-20 09:04:18 +02:00
self . nodes [ 0 ] . generate ( 1 )
self . nodes [ 1 ] . generate ( 1 )
2017-02-15 11:12:00 +01:00
timestamp = self . nodes [ 1 ] . getblock ( self . nodes [ 1 ] . getbestblockhash ( ) ) [ ' mediantime ' ]
2020-12-06 19:12:49 +01:00
self . nodes [ 1 ] . syncwithvalidationinterfacequeue ( ) # Sync the timestamp to the wallet, so that importmulti works
2016-10-20 09:04:18 +02:00
2020-12-17 13:46:20 +01:00
node0_address1 = self . nodes [ 0 ] . getaddressinfo ( self . nodes [ 0 ] . getnewaddress ( ) )
2016-10-20 09:04:18 +02:00
2021-10-28 07:28:23 +02:00
# Check only one address
2016-10-20 09:04:18 +02:00
assert_equal ( node0_address1 [ ' ismine ' ] , True )
2021-10-28 07:28:23 +02:00
# Node 1 sync test
assert_equal ( self . nodes [ 1 ] . getblockcount ( ) , 1 )
2016-10-20 09:04:18 +02:00
2021-10-28 07:28:23 +02:00
# Address Test - before import
2020-12-17 13:46:20 +01:00
address_info = self . nodes [ 1 ] . getaddressinfo ( node0_address1 [ ' address ' ] )
2016-10-20 09:04:18 +02:00
assert_equal ( address_info [ ' iswatchonly ' ] , False )
assert_equal ( address_info [ ' ismine ' ] , False )
# RPC importmulti -----------------------------------------------
2021-09-11 21:56:58 +02:00
# Bitcoin Address (implicit non-internal)
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import an address " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
self . test_importmulti ( { " scriptPubKey " : { " address " : key . p2pkh_addr } ,
2021-10-28 07:28:23 +02:00
" timestamp " : " now " } ,
2019-01-09 22:58:40 +01:00
success = True )
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
timestamp = timestamp ,
ischange = False )
watchonly_address = key . p2pkh_addr
2017-02-15 11:12:00 +01:00
watchonly_timestamp = timestamp
2016-10-20 09:04:18 +02:00
2017-03-09 21:16:20 +01:00
self . log . info ( " Should not import an invalid address " )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : { " address " : " not valid address " } ,
" timestamp " : " now " } ,
2019-01-09 22:58:40 +01:00
success = False ,
2021-10-28 07:28:23 +02:00
error_code = - 5 ,
2021-10-28 07:30:15 +02:00
error_message = ' Invalid address \" not valid address \" ' )
2016-10-20 09:04:18 +02:00
# ScriptPubKey + internal
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import a scriptPubKey with internal flag " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" internal " : True } ,
2019-01-09 22:58:40 +01:00
success = True )
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
timestamp = timestamp ,
ischange = True )
2016-10-20 09:04:18 +02:00
2021-09-28 21:48:33 +02:00
# ScriptPubKey + internal + label
self . log . info ( " Should not allow a label to be specified when internal is true " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" internal " : True ,
2019-11-26 16:57:23 +01:00
" label " : " Unsuccessful labelling for internal addresses " } ,
2019-01-09 22:58:40 +01:00
success = False ,
2021-10-28 07:28:23 +02:00
error_code = - 8 ,
error_message = ' Internal addresses should not have a label ' )
2021-09-28 21:48:33 +02:00
2018-09-10 19:01:13 +02:00
# Nonstandard scriptPubKey + !internal
self . log . info ( " Should not import a nonstandard scriptPubKey without internal flag " )
2021-10-28 07:28:23 +02:00
nonstandardScriptPubKey = key . p2pkh_script + CScript ( [ OP_NOP ] ) . hex ( )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : nonstandardScriptPubKey ,
" timestamp " : " now " } ,
2019-01-09 22:58:40 +01:00
success = False ,
2021-10-28 07:28:23 +02:00
error_code = - 8 ,
error_message = ' Internal must be set to true for nonstandard scriptPubKey imports. ' )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = False ,
ismine = False ,
timestamp = None )
2016-10-20 09:04:18 +02:00
2021-09-11 21:56:58 +02:00
# Address + Public key + !Internal(explicit)
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import an address with public key " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
self . test_importmulti ( { " scriptPubKey " : { " address " : key . p2pkh_addr } ,
2021-10-28 07:28:23 +02:00
" timestamp " : " now " ,
" pubkeys " : [ key . pubkey ] ,
" internal " : False } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
# ScriptPubKey + Public key + internal
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import a scriptPubKey with internal and with public key " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" pubkeys " : [ key . pubkey ] ,
" internal " : True } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
2018-09-10 19:01:13 +02:00
# Nonstandard scriptPubKey + Public key + !internal
self . log . info ( " Should not import a nonstandard scriptPubKey without internal and with public key " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : nonstandardScriptPubKey ,
" timestamp " : " now " ,
" pubkeys " : [ key . pubkey ] } ,
2019-01-09 22:58:40 +01:00
success = False ,
2021-10-28 07:28:23 +02:00
error_code = - 8 ,
error_message = ' Internal must be set to true for nonstandard scriptPubKey imports. ' )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = False ,
ismine = False ,
timestamp = None )
2016-10-20 09:04:18 +02:00
# Address + Private key + !watchonly
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import an address with private key " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
self . test_importmulti ( { " scriptPubKey " : { " address " : key . p2pkh_addr } ,
2021-10-28 07:28:23 +02:00
" timestamp " : " now " ,
" keys " : [ key . privkey ] } ,
2019-01-09 22:58:40 +01:00
success = True )
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = False ,
ismine = True ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
Merge #11483: Fix importmulti bug when importing an already imported key
a44a21517 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
2017-10-17 21:40:48 +02:00
self . log . info ( " Should not import an address with private key if is already imported " )
2019-01-09 22:58:40 +01:00
self . test_importmulti ( { " scriptPubKey " : { " address " : key . p2pkh_addr } ,
2021-10-28 07:28:23 +02:00
" timestamp " : " now " ,
" keys " : [ key . privkey ] } ,
2019-01-09 22:58:40 +01:00
success = False ,
2021-10-28 07:28:23 +02:00
error_code = - 4 ,
2021-10-28 09:52:04 +02:00
error_message = ' The wallet already contains the private key for this address or script ( " ' + key . p2pkh_script + ' " ) ' )
Merge #11483: Fix importmulti bug when importing an already imported key
a44a21517 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
2017-10-17 21:40:48 +02:00
2016-10-20 09:04:18 +02:00
# Address + Private key + watchonly
2021-10-28 07:30:15 +02:00
self . log . info ( " Should import an address with private key and with watchonly " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
self . test_importmulti ( { " scriptPubKey " : { " address " : key . p2pkh_addr } ,
2021-10-28 07:28:23 +02:00
" timestamp " : " now " ,
" keys " : [ key . privkey ] ,
" watchonly " : True } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = False ,
ismine = True ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
# ScriptPubKey + Private key + internal
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import a scriptPubKey with internal and with private key " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" keys " : [ key . privkey ] ,
" internal " : True } ,
2019-01-09 22:58:40 +01:00
success = True )
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = False ,
ismine = True ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
2018-09-10 19:01:13 +02:00
# Nonstandard scriptPubKey + Private key + !internal
self . log . info ( " Should not import a nonstandard scriptPubKey without internal and with private key " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : nonstandardScriptPubKey ,
" timestamp " : " now " ,
" keys " : [ key . privkey ] } ,
2019-01-09 22:58:40 +01:00
success = False ,
2021-10-28 07:28:23 +02:00
error_code = - 8 ,
error_message = ' Internal must be set to true for nonstandard scriptPubKey imports. ' )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = False ,
ismine = False ,
timestamp = None )
2016-10-20 09:04:18 +02:00
# P2SH address
2019-01-09 22:58:40 +01:00
multisig = get_multisig ( self . nodes [ 0 ] )
2021-05-31 11:26:16 +02:00
self . nodes [ 1 ] . generate ( COINBASE_MATURITY )
2021-10-28 07:28:23 +02:00
self . nodes [ 1 ] . sendtoaddress ( multisig . p2sh_addr , 10.00 )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 1 )
2017-02-15 11:12:00 +01:00
timestamp = self . nodes [ 1 ] . getblock ( self . nodes [ 1 ] . getbestblockhash ( ) ) [ ' mediantime ' ]
2020-04-26 16:47:31 +02:00
self . nodes [ 1 ] . syncwithvalidationinterfacequeue ( )
2016-10-20 09:04:18 +02:00
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import a p2sh " )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : { " address " : multisig . p2sh_addr } ,
" timestamp " : " now " } ,
2019-01-09 22:58:40 +01:00
success = True )
test_address ( self . nodes [ 1 ] ,
multisig . p2sh_addr ,
isscript = True ,
iswatchonly = True ,
timestamp = timestamp )
2021-10-28 07:28:23 +02:00
p2shunspent = self . nodes [ 1 ] . listunspent ( 0 , 999999 , [ multisig . p2sh_addr ] ) [ 0 ]
2016-10-20 09:04:18 +02:00
assert_equal ( p2shunspent [ ' spendable ' ] , False )
assert_equal ( p2shunspent [ ' solvable ' ] , False )
# P2SH + Redeem script
2019-01-09 22:58:40 +01:00
multisig = get_multisig ( self . nodes [ 0 ] )
2021-05-31 11:26:16 +02:00
self . nodes [ 1 ] . generate ( COINBASE_MATURITY )
2021-10-28 07:28:23 +02:00
self . nodes [ 1 ] . sendtoaddress ( multisig . p2sh_addr , 10.00 )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 1 )
2017-02-15 11:12:00 +01:00
timestamp = self . nodes [ 1 ] . getblock ( self . nodes [ 1 ] . getbestblockhash ( ) ) [ ' mediantime ' ]
2020-04-26 16:47:31 +02:00
self . nodes [ 1 ] . syncwithvalidationinterfacequeue ( )
2016-10-20 09:04:18 +02:00
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import a p2sh with respective redeem script " )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : { " address " : multisig . p2sh_addr } ,
" timestamp " : " now " ,
" redeemscript " : multisig . redeem_script } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
multisig . p2sh_addr , timestamp = timestamp , iswatchonly = True , ismine = False , solvable = True )
2021-10-28 07:28:23 +02:00
p2shunspent = self . nodes [ 1 ] . listunspent ( 0 , 999999 , [ multisig . p2sh_addr ] ) [ 0 ]
2016-10-20 09:04:18 +02:00
assert_equal ( p2shunspent [ ' spendable ' ] , False )
assert_equal ( p2shunspent [ ' solvable ' ] , True )
# P2SH + Redeem script + Private Keys + !Watchonly
2019-01-09 22:58:40 +01:00
multisig = get_multisig ( self . nodes [ 0 ] )
2021-05-31 11:26:16 +02:00
self . nodes [ 1 ] . generate ( COINBASE_MATURITY )
2021-10-28 07:28:23 +02:00
self . nodes [ 1 ] . sendtoaddress ( multisig . p2sh_addr , 10.00 )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 1 )
2017-02-15 11:12:00 +01:00
timestamp = self . nodes [ 1 ] . getblock ( self . nodes [ 1 ] . getbestblockhash ( ) ) [ ' mediantime ' ]
2020-04-26 16:47:31 +02:00
self . nodes [ 1 ] . syncwithvalidationinterfacequeue ( )
2016-10-20 09:04:18 +02:00
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import a p2sh with respective redeem script and private keys " )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : { " address " : multisig . p2sh_addr } ,
" timestamp " : " now " ,
" redeemscript " : multisig . redeem_script ,
" keys " : multisig . privkeys [ 0 : 2 ] } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
multisig . p2sh_addr ,
timestamp = timestamp ,
ismine = False ,
iswatchonly = True ,
solvable = True )
2021-10-28 07:28:23 +02:00
p2shunspent = self . nodes [ 1 ] . listunspent ( 0 , 999999 , [ multisig . p2sh_addr ] ) [ 0 ]
2016-10-20 09:04:18 +02:00
assert_equal ( p2shunspent [ ' spendable ' ] , False )
assert_equal ( p2shunspent [ ' solvable ' ] , True )
# P2SH + Redeem script + Private Keys + Watchonly
2019-01-09 22:58:40 +01:00
multisig = get_multisig ( self . nodes [ 0 ] )
2021-05-31 11:26:16 +02:00
self . nodes [ 1 ] . generate ( COINBASE_MATURITY )
2021-10-28 07:28:23 +02:00
self . nodes [ 1 ] . sendtoaddress ( multisig . p2sh_addr , 10.00 )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 1 )
2017-03-17 20:34:55 +01:00
timestamp = self . nodes [ 1 ] . getblock ( self . nodes [ 1 ] . getbestblockhash ( ) ) [ ' mediantime ' ]
2020-04-26 16:47:31 +02:00
self . nodes [ 1 ] . syncwithvalidationinterfacequeue ( )
2016-10-20 09:04:18 +02:00
2017-03-09 21:16:20 +01:00
self . log . info ( " Should import a p2sh with respective redeem script and private keys " )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : { " address " : multisig . p2sh_addr } ,
" timestamp " : " now " ,
" redeemscript " : multisig . redeem_script ,
" keys " : multisig . privkeys [ 0 : 2 ] ,
" watchonly " : True } ,
2019-01-09 22:58:40 +01:00
success = True )
test_address ( self . nodes [ 1 ] ,
multisig . p2sh_addr ,
iswatchonly = True ,
ismine = False ,
solvable = True ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
# Address + Public key + !Internal + Wrong pubkey
2021-10-28 07:30:15 +02:00
self . log . info ( " Should not import an address with the wrong public key as non-solvable " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
wrong_key = get_key ( self . nodes [ 0 ] ) . pubkey
self . test_importmulti ( { " scriptPubKey " : { " address " : key . p2pkh_addr } ,
2021-10-28 07:28:23 +02:00
" timestamp " : " now " ,
" pubkeys " : [ wrong_key ] } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Importing as non-solvable: some required keys are missing. If this is intentional, don ' t provide any keys, pubkeys, or redeemscript. " , " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
solvable = False ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
# ScriptPubKey + Public key + internal + Wrong pubkey
2021-10-28 07:30:15 +02:00
self . log . info ( " Should import a scriptPubKey with internal and with a wrong public key as non-solvable " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
wrong_key = get_key ( self . nodes [ 0 ] ) . pubkey
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" pubkeys " : [ wrong_key ] ,
" internal " : True } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Importing as non-solvable: some required keys are missing. If this is intentional, don ' t provide any keys, pubkeys, or redeemscript. " , " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
solvable = False ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
# Address + Private key + !watchonly + Wrong private key
2021-10-28 07:30:15 +02:00
self . log . info ( " Should import an address with a wrong private key as non-solvable " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
wrong_privkey = get_key ( self . nodes [ 0 ] ) . privkey
self . test_importmulti ( { " scriptPubKey " : { " address " : key . p2pkh_addr } ,
2021-10-28 07:28:23 +02:00
" timestamp " : " now " ,
" keys " : [ wrong_privkey ] } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Importing as non-solvable: some required keys are missing. If this is intentional, don ' t provide any keys, pubkeys, or redeemscript. " , " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
solvable = False ,
timestamp = timestamp )
2016-10-20 09:04:18 +02:00
# ScriptPubKey + Private key + internal + Wrong private key
2021-10-28 07:30:15 +02:00
self . log . info ( " Should import a scriptPubKey with internal and with a wrong private key as non-solvable " )
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
wrong_privkey = get_key ( self . nodes [ 0 ] ) . privkey
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" keys " : [ wrong_privkey ] ,
" internal " : True } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 07:30:15 +02:00
warnings = [ " Importing as non-solvable: some required keys are missing. If this is intentional, don ' t provide any keys, pubkeys, or redeemscript. " , " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
iswatchonly = True ,
ismine = False ,
solvable = False ,
timestamp = timestamp )
2017-03-17 20:34:55 +01:00
# Importing existing watch only address with new timestamp should replace saved timestamp.
assert_greater_than ( timestamp , watchonly_timestamp )
2017-03-19 10:13:45 +01:00
self . log . info ( " Should replace previously saved watch only timestamp. " )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : { " address " : watchonly_address } ,
" timestamp " : " now " } ,
2019-01-09 22:58:40 +01:00
success = True )
test_address ( self . nodes [ 1 ] ,
watchonly_address ,
iswatchonly = True ,
ismine = False ,
timestamp = timestamp )
2017-03-17 20:34:55 +01:00
watchonly_timestamp = timestamp
2017-02-15 11:12:00 +01:00
# restart nodes to check for proper serialization/deserialization of watch only address
2017-06-02 12:08:48 +02:00
self . stop_nodes ( )
2017-09-01 18:47:13 +02:00
self . start_nodes ( )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
watchonly_address ,
iswatchonly = True ,
ismine = False ,
timestamp = watchonly_timestamp )
2016-10-20 09:04:18 +02:00
2017-02-03 22:23:13 +01:00
# Bad or missing timestamps
2017-03-09 21:16:20 +01:00
self . log . info ( " Should throw on invalid or missing timestamp values " )
2019-09-25 11:34:51 +02:00
assert_raises_rpc_error ( - 3 , ' Missing required timestamp field for key ' ,
2021-10-28 07:28:23 +02:00
self . nodes [ 1 ] . importmulti , [ { " scriptPubKey " : key . p2pkh_script } ] )
2019-09-25 11:34:51 +02:00
assert_raises_rpc_error ( - 3 , ' Expected number or " now " timestamp value for key. got type string ' ,
2021-10-28 07:28:23 +02:00
self . nodes [ 1 ] . importmulti , [ {
" scriptPubKey " : key . p2pkh_script ,
" timestamp " : " "
} ] )
2017-02-03 22:23:13 +01:00
2021-10-28 09:52:04 +02:00
# Test ranged descriptor fails if range is not specified
xpriv = " tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg "
desc = " sh(pkh( " + xpriv + " /0 ' /0 ' /* ' " + " )) "
self . log . info ( " Ranged descriptor import should fail without a specified range " )
2021-10-28 09:57:55 +02:00
self . test_importmulti ( { " desc " : descsum_create ( desc ) ,
2021-10-28 09:52:04 +02:00
" timestamp " : " now " } ,
success = False ,
error_code = - 8 ,
error_message = ' Descriptor is ranged, please specify the range ' )
2022-09-18 22:55:36 +02:00
# Test importing of a ranged descriptor with xpriv
2021-10-28 09:52:04 +02:00
self . log . info ( " Should import the ranged descriptor with specified range as solvable " )
2021-10-28 09:57:55 +02:00
self . test_importmulti ( { " desc " : descsum_create ( desc ) ,
2021-10-28 09:52:04 +02:00
" timestamp " : " now " ,
Merge #15497: rpc: Consistent range arguments in scantxoutset/importmulti/deriveaddresses
ca253f6ebf Make deriveaddresses use stop/[start,stop] notation for ranges (Pieter Wuille)
1675b7ce55 Use stop/[start,stop] notation in importmulti desc range (Pieter Wuille)
4566011631 Add support for stop/[start,stop] ranges to scantxoutset (Pieter Wuille)
6b9f45e81b Support ranges arguments in RPC help (Pieter Wuille)
7aa6a8aefb Add ParseRange function to parse args of the form int/[int,int] (Pieter Wuille)
Pull request description:
This introduces a consistent notation for RPC arguments in `scantxoutset`, `importmulti`, and `deriveaddresses`, either:
* `"range" : int` to just specify the end of the range
* `"range" : [int,int]` to specify both the begin and the end of the range.
For `scantxoutset`, this is a backward compatible new feature. For the two other RPCs, it's an incompatible change, but neither of them has been in a release so far. Because of that non-released reason, this only makes sense in 0.18, in my opinion.
I suggest this as an alternative to #15496, which only makes `deriveaddresses` compatible with `importmulti`, but not with the existing `scantxoutset` RPC. I also think `[int,int]` is more convenient than `{"start":int,"stop":int}`.
I realize this is technically a feature added to `scantxoutset` after the feature freeze. If desired, I'll drop the `scantxoutset` changes.
Tree-SHA512: 1cbebb90cf34f106786dbcec7afbf3f43fb8b7e46cc7e6763faf1bc1babf12375a1b3c3cf86ee83c21ed2171d99b5a2f60331850bc613db25538c38b6a056676
2019-03-01 15:13:05 +01:00
" range " : 1 } ,
2022-09-18 22:55:36 +02:00
success = True )
2021-10-28 09:52:04 +02:00
2019-05-10 14:09:34 +02:00
self . test_importmulti ( { " desc " : descsum_create ( desc ) , " timestamp " : " now " , " range " : - 1 } ,
success = False , error_code = - 8 , error_message = ' End of range is too high ' )
self . test_importmulti ( { " desc " : descsum_create ( desc ) , " timestamp " : " now " , " range " : [ - 1 , 10 ] } ,
success = False , error_code = - 8 , error_message = ' Range should be greater or equal than 0 ' )
self . test_importmulti ( { " desc " : descsum_create ( desc ) , " timestamp " : " now " , " range " : [ ( 2 << 31 + 1 ) - 1000000 , ( 2 << 31 + 1 ) ] } ,
success = False , error_code = - 8 , error_message = ' End of range is too high ' )
self . test_importmulti ( { " desc " : descsum_create ( desc ) , " timestamp " : " now " , " range " : [ 2 , 1 ] } ,
success = False , error_code = - 8 , error_message = ' Range specified as [begin,end] must not have begin after end ' )
self . test_importmulti ( { " desc " : descsum_create ( desc ) , " timestamp " : " now " , " range " : [ 0 , 1000001 ] } ,
success = False , error_code = - 8 , error_message = ' Range is too large ' )
2022-09-18 22:55:36 +02:00
# Test importing a descriptor containing a WIF private key
wif_priv = " cTT3BvHnd51YJf8fkdr2XvZTQRRUZruWhRvRyQY1raVFg5Lvam2A "
address = " ySWABbcNKyHUgBb1ffhpuETuis9jsdR3aq "
desc = " sh(pkh( " + wif_priv + " )) "
self . log . info ( " Should import a descriptor with a WIF private key as spendable " )
self . test_importmulti ( { " desc " : descsum_create ( desc ) ,
" timestamp " : " now " } ,
success = True )
test_address ( self . nodes [ 1 ] ,
address ,
solvable = True ,
ismine = True )
# dump the private key to ensure it matches what was imported
privkey = self . nodes [ 1 ] . dumpprivkey ( address )
assert_equal ( privkey , wif_priv )
2021-10-28 09:52:04 +02:00
# Test importing of a P2PKH address via descriptor
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2019-11-26 16:57:23 +01:00
p2pkh_label = " P2PKH descriptor import "
2021-10-28 09:52:04 +02:00
self . log . info ( " Should import a p2pkh address from descriptor " )
2021-10-28 09:57:55 +02:00
self . test_importmulti ( { " desc " : descsum_create ( " pkh( " + key . pubkey + " ) " ) ,
2021-10-28 09:52:04 +02:00
" timestamp " : " now " ,
2019-11-26 16:57:23 +01:00
" label " : p2pkh_label } ,
2019-01-09 22:58:40 +01:00
success = True ,
2021-10-28 09:52:04 +02:00
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key . p2pkh_addr ,
2021-10-28 09:52:04 +02:00
solvable = True ,
ismine = False ,
2020-01-07 23:23:58 +01:00
labels = [ p2pkh_label ] )
2021-10-28 09:52:04 +02:00
# Test import fails if both desc and scriptPubKey are provided
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 09:52:04 +02:00
self . log . info ( " Import should fail if both scriptPubKey and desc are provided " )
2021-10-28 09:57:55 +02:00
self . test_importmulti ( { " desc " : descsum_create ( " pkh( " + key . pubkey + " ) " ) ,
2021-10-28 09:52:04 +02:00
" scriptPubKey " : { " address " : key . p2pkh_addr } ,
" timestamp " : " now " } ,
success = False ,
error_code = - 8 ,
error_message = ' Both a descriptor and a scriptPubKey should not be provided. ' )
# Test import fails if neither desc nor scriptPubKey are present
2019-01-09 22:58:40 +01:00
key = get_key ( self . nodes [ 0 ] )
2021-10-28 09:52:04 +02:00
self . log . info ( " Import should fail if neither a descriptor nor a scriptPubKey are provided " )
self . test_importmulti ( { " timestamp " : " now " } ,
success = False ,
error_code = - 8 ,
error_message = ' Either a descriptor or scriptPubKey must be provided. ' )
# Test importing of a multisig via descriptor
2019-01-09 22:58:40 +01:00
key1 = get_key ( self . nodes [ 0 ] )
key2 = get_key ( self . nodes [ 0 ] )
2021-10-28 09:52:04 +02:00
self . log . info ( " Should import a 1-of-2 bare multisig from descriptor " )
2021-10-28 09:57:55 +02:00
self . test_importmulti ( { " desc " : descsum_create ( " multi(1, " + key1 . pubkey + " , " + key2 . pubkey + " ) " ) ,
2021-10-28 09:52:04 +02:00
" timestamp " : " now " } ,
2019-02-15 00:10:52 +01:00
True ,
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2021-10-28 09:52:04 +02:00
self . log . info ( " Should not treat individual keys from the imported bare multisig as watchonly " )
2019-01-09 22:58:40 +01:00
test_address ( self . nodes [ 1 ] ,
key1 . p2pkh_addr ,
2021-10-28 09:52:04 +02:00
ismine = False ,
iswatchonly = False )
2019-02-15 00:10:52 +01:00
# Import pubkeys with key origin info
self . log . info ( " Addresses should have hd keypath and master key id after import with key origin " )
pub_addr = self . nodes [ 1 ] . getnewaddress ( )
pub_addr = self . nodes [ 1 ] . getnewaddress ( )
info = self . nodes [ 1 ] . getaddressinfo ( pub_addr )
pub = info [ ' pubkey ' ]
pub_keypath = info [ ' hdkeypath ' ]
pub_fpr = info [ ' hdmasterfingerprint ' ]
result = self . nodes [ 0 ] . importmulti (
[ {
' desc ' : descsum_create ( " pkh([ " + pub_fpr + pub_keypath [ 1 : ] + " ] " + pub + " ) " ) ,
" timestamp " : " now " ,
} ]
)
assert result [ 0 ] [ ' success ' ]
pub_import_info = self . nodes [ 0 ] . getaddressinfo ( pub_addr )
assert_equal ( pub_import_info [ ' hdmasterfingerprint ' ] , pub_fpr )
assert_equal ( pub_import_info [ ' pubkey ' ] , pub )
assert_equal ( pub_import_info [ ' hdkeypath ' ] , pub_keypath )
# Import privkeys with key origin info
priv_addr = self . nodes [ 1 ] . getnewaddress ( )
info = self . nodes [ 1 ] . getaddressinfo ( priv_addr )
priv = self . nodes [ 1 ] . dumpprivkey ( priv_addr )
priv_keypath = info [ ' hdkeypath ' ]
priv_fpr = info [ ' hdmasterfingerprint ' ]
result = self . nodes [ 0 ] . importmulti (
[ {
' desc ' : descsum_create ( " pkh([ " + priv_fpr + priv_keypath [ 1 : ] + " ] " + priv + " ) " ) ,
" timestamp " : " now " ,
} ]
)
assert result [ 0 ] [ ' success ' ]
priv_import_info = self . nodes [ 0 ] . getaddressinfo ( priv_addr )
assert_equal ( priv_import_info [ ' hdmasterfingerprint ' ] , priv_fpr )
assert_equal ( priv_import_info [ ' hdkeypath ' ] , priv_keypath )
# Make sure the key origin info are still there after a restart
self . stop_nodes ( )
self . start_nodes ( )
import_info = self . nodes [ 0 ] . getaddressinfo ( pub_addr )
assert_equal ( import_info [ ' hdmasterfingerprint ' ] , pub_fpr )
assert_equal ( import_info [ ' hdkeypath ' ] , pub_keypath )
import_info = self . nodes [ 0 ] . getaddressinfo ( priv_addr )
assert_equal ( import_info [ ' hdmasterfingerprint ' ] , priv_fpr )
assert_equal ( import_info [ ' hdkeypath ' ] , priv_keypath )
# Check legacy import does not import key origin info
self . log . info ( " Legacy imports don ' t have key origin info " )
pub_addr = self . nodes [ 1 ] . getnewaddress ( )
info = self . nodes [ 1 ] . getaddressinfo ( pub_addr )
pub = info [ ' pubkey ' ]
result = self . nodes [ 0 ] . importmulti (
[ {
' scriptPubKey ' : { ' address ' : pub_addr } ,
' pubkeys ' : [ pub ] ,
" timestamp " : " now " ,
} ]
)
assert result [ 0 ] [ ' success ' ]
pub_import_info = self . nodes [ 0 ] . getaddressinfo ( pub_addr )
assert_equal ( pub_import_info [ ' pubkey ' ] , pub )
assert ' hdmasterfingerprint ' not in pub_import_info
assert ' hdkeypath ' not in pub_import_info
2020-04-26 16:47:31 +02:00
2019-02-15 00:51:38 +01:00
# Import some public keys to the keypool of a no privkey wallet
self . log . info ( " Adding pubkey to keypool of disableprivkey wallet " )
self . nodes [ 1 ] . createwallet ( wallet_name = " noprivkeys " , disable_private_keys = True )
wrpc = self . nodes [ 1 ] . get_wallet_rpc ( " noprivkeys " )
addr1 = self . nodes [ 0 ] . getnewaddress ( )
addr2 = self . nodes [ 0 ] . getnewaddress ( )
pub1 = self . nodes [ 0 ] . getaddressinfo ( addr1 ) [ ' pubkey ' ]
pub2 = self . nodes [ 0 ] . getaddressinfo ( addr2 ) [ ' pubkey ' ]
result = wrpc . importmulti (
[ {
' desc ' : descsum_create ( ' pkh( ' + pub1 + ' ) ' ) ,
' keypool ' : True ,
" timestamp " : " now " ,
} ,
{
' desc ' : descsum_create ( ' pkh( ' + pub2 + ' ) ' ) ,
' keypool ' : True ,
" timestamp " : " now " ,
} ]
)
assert result [ 0 ] [ ' success ' ]
assert result [ 1 ] [ ' success ' ]
assert_equal ( wrpc . getwalletinfo ( ) [ " keypoolsize " ] , 2 )
newaddr1 = wrpc . getnewaddress ( )
assert_equal ( addr1 , newaddr1 )
newaddr2 = wrpc . getnewaddress ( )
assert_equal ( addr2 , newaddr2 )
# Import some public keys to the internal keypool of a no privkey wallet
self . log . info ( " Adding pubkey to internal keypool of disableprivkey wallet " )
addr1 = self . nodes [ 0 ] . getnewaddress ( )
addr2 = self . nodes [ 0 ] . getnewaddress ( )
pub1 = self . nodes [ 0 ] . getaddressinfo ( addr1 ) [ ' pubkey ' ]
pub2 = self . nodes [ 0 ] . getaddressinfo ( addr2 ) [ ' pubkey ' ]
result = wrpc . importmulti (
[ {
' desc ' : descsum_create ( ' pkh( ' + pub1 + ' ) ' ) ,
' keypool ' : True ,
' internal ' : True ,
" timestamp " : " now " ,
} ,
{
' desc ' : descsum_create ( ' pkh( ' + pub2 + ' ) ' ) ,
' keypool ' : True ,
' internal ' : True ,
" timestamp " : " now " ,
} ]
)
assert result [ 0 ] [ ' success ' ]
assert result [ 1 ] [ ' success ' ]
assert_equal ( wrpc . getwalletinfo ( ) [ " keypoolsize_hd_internal " ] , 2 )
newaddr1 = wrpc . getrawchangeaddress ( )
assert_equal ( addr1 , newaddr1 )
newaddr2 = wrpc . getrawchangeaddress ( )
assert_equal ( addr2 , newaddr2 )
# Import a multisig and make sure the keys don't go into the keypool
2022-04-25 21:13:24 +02:00
self . log . info ( ' Imported scripts with pubkeys should not have their pubkeys go into the keypool ' )
2019-02-15 00:51:38 +01:00
addr1 = self . nodes [ 0 ] . getnewaddress ( )
addr2 = self . nodes [ 0 ] . getnewaddress ( )
pub1 = self . nodes [ 0 ] . getaddressinfo ( addr1 ) [ ' pubkey ' ]
pub2 = self . nodes [ 0 ] . getaddressinfo ( addr2 ) [ ' pubkey ' ]
result = wrpc . importmulti (
[ {
' desc ' : descsum_create ( ' sh(multi(2, ' + pub1 + ' , ' + pub2 + ' )) ' ) ,
' keypool ' : True ,
" timestamp " : " now " ,
} ]
)
assert result [ 0 ] [ ' success ' ]
assert_equal ( wrpc . getwalletinfo ( ) [ " keypoolsize " ] , 0 )
# Cannot import those pubkeys to keypool of wallet with privkeys
self . log . info ( " Pubkeys cannot be added to the keypool of a wallet with private keys " )
2022-11-30 20:23:48 +01:00
wrpc = self . nodes [ 1 ] . get_wallet_rpc ( self . default_wallet_name )
2019-02-15 00:51:38 +01:00
assert wrpc . getwalletinfo ( ) [ ' private_keys_enabled ' ]
result = wrpc . importmulti (
[ {
' desc ' : descsum_create ( ' pkh( ' + pub1 + ' ) ' ) ,
' keypool ' : True ,
" timestamp " : " now " ,
} ]
)
assert_equal ( result [ 0 ] [ ' error ' ] [ ' code ' ] , - 8 )
assert_equal ( result [ 0 ] [ ' error ' ] [ ' message ' ] , " Keys can only be imported to the keypool when private keys are disabled " )
# Make sure ranged imports import keys in order
self . log . info ( ' Key ranges should be imported in order ' )
wrpc = self . nodes [ 1 ] . get_wallet_rpc ( " noprivkeys " )
assert_equal ( wrpc . getwalletinfo ( ) [ " keypoolsize " ] , 0 )
assert_equal ( wrpc . getwalletinfo ( ) [ " private_keys_enabled " ] , False )
xpub = " tpubDAXcJ7s7ZwicqjprRaEWdPoHKrCS215qxGYxpusRLLmJuT69ZSicuGdSfyvyKpvUNYBW1s2U3NSrT6vrCYB9e6nZUEvrqnwXPF8ArTCRXMY "
addresses = [
' yUxX4qnzWntXhEGrYB92v7ez4EZBnUjB1y ' , # m/0'/0'/0
' yRhTPsPd2qYgYbFFCqY2nuPHJQBjTnMQxg ' , # m/0'/0'/1
' yUyn3UV9rBdWfw6yJJ6eAoKuzDJ8RVLP1o ' , # m/0'/0'/2
' yi8GEkfLBgK85wGmBFsMFdSbEvPPNCSnVx ' , # m/0'/0'/3
' yYB4whdY8APWoCez6ryNdMBrrDjwzFbqMi ' , # m/0'/0'/4
]
result = wrpc . importmulti (
[ {
' desc ' : descsum_create ( ' pkh([80002067/0h/0h] ' + xpub + ' /*) ' ) ,
' keypool ' : True ,
' timestamp ' : ' now ' ,
' range ' : [ 0 , 4 ] ,
} ]
)
for i in range ( 0 , 5 ) :
addr = wrpc . getnewaddress ( ' ' )
assert_equal ( addr , addresses [ i ] )
2016-10-20 09:04:18 +02:00
if __name__ == ' __main__ ' :
2021-10-28 07:28:23 +02:00
ImportMultiTest ( ) . main ( )