2016-10-20 09:04:18 +02:00
#!/usr/bin/env python3
2021-09-28 21:48:33 +02:00
# Copyright (c) 2014-2018 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 . """
from collections import namedtuple
from test_framework . address import (
key_to_p2pkh ,
script_to_p2sh ,
)
from test_framework . script import (
CScript ,
OP_2 ,
OP_3 ,
OP_CHECKMULTISIG ,
OP_CHECKSIG ,
OP_DUP ,
OP_EQUAL ,
OP_EQUALVERIFY ,
OP_HASH160 ,
OP_NOP ,
hash160 ,
)
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 ,
)
2016-10-20 09:04:18 +02:00
2021-10-28 07:28:23 +02:00
Key = namedtuple ( ' Key ' , [ ' privkey ' ,
' pubkey ' ,
' p2pkh_script ' ,
' p2pkh_addr ' ] )
Multisig = namedtuple ( ' Multisig ' , [ ' privkeys ' ,
' pubkeys ' ,
' p2sh_script ' ,
' p2sh_addr ' ,
' redeem_script ' ] )
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
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
2021-10-28 07:28:23 +02:00
def get_key ( self ) :
""" Generate a fresh key on node0
Returns a named tuple of privkey , pubkey and all address and scripts . """
addr = self . nodes [ 0 ] . getnewaddress ( )
pubkey = self . nodes [ 0 ] . getaddressinfo ( addr ) [ ' pubkey ' ]
pkh = hash160 ( bytes . fromhex ( pubkey ) )
return Key ( self . nodes [ 0 ] . dumpprivkey ( addr ) ,
pubkey ,
CScript ( [ OP_DUP , OP_HASH160 , pkh , OP_EQUALVERIFY , OP_CHECKSIG ] ) . hex ( ) , # p2pkh
key_to_p2pkh ( pubkey ) ) # p2pkh addr
def get_multisig ( self ) :
""" Generate a fresh multisig on node0
Returns a named tuple of privkeys , pubkeys and all address and scripts . """
addrs = [ ]
pubkeys = [ ]
for _ in range ( 3 ) :
addr = self . nodes [ 0 ] . getaddressinfo ( self . nodes [ 0 ] . getnewaddress ( ) )
addrs . append ( addr [ ' address ' ] )
pubkeys . append ( addr [ ' pubkey ' ] )
script_code = CScript ( [ OP_2 ] + [ bytes . fromhex ( pubkey ) for pubkey in pubkeys ] + [ OP_3 , OP_CHECKMULTISIG ] )
return Multisig ( [ self . nodes [ 0 ] . dumpprivkey ( addr ) for addr in addrs ] ,
pubkeys ,
CScript ( [ OP_HASH160 , hash160 ( script_code ) , OP_EQUAL ] ) . hex ( ) , # p2sh
script_to_p2sh ( script_code ) , # p2sh addr
script_code . hex ( ) ) # redeem script
2021-10-28 07:30:15 +02:00
def test_importmulti ( self , req , success , error_code = None , error_message = None , warnings = [ ] ) :
2021-10-28 07:28:23 +02:00
""" Run importmulti and assert success """
result = self . nodes [ 1 ] . importmulti ( [ req ] )
2021-10-28 07:30:15 +02:00
observed_warnings = [ ]
if ' warnings ' in result [ 0 ] :
observed_warnings = result [ 0 ] [ ' warnings ' ]
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 test_address ( self , address , * * kwargs ) :
""" Get address info for `address` and test whether the returned values are as expected. """
addr_info = self . nodes [ 1 ] . getaddressinfo ( address )
for key , value in kwargs . items ( ) :
if value is None :
if key in addr_info . keys ( ) :
raise AssertionError ( " key {} unexpectedly returned in getaddressinfo. " . format ( key ) )
elif addr_info [ key ] != value :
raise AssertionError ( " key {} value {} did not match expected value {} " . format ( key , addr_info [ key ] , value ) )
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 ' ]
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : { " address " : address } ,
" timestamp " : " now " } ,
True )
self . test_address ( address ,
iswatchonly = True ,
ismine = False ,
timestamp = timestamp ,
ischange = False )
watchonly_address = address
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 " } ,
False ,
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" internal " : True } ,
True )
self . test_address ( 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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" internal " : True ,
" label " : " Example label " } ,
False ,
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 ( )
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : nonstandardScriptPubKey ,
" timestamp " : " now " } ,
False ,
error_code = - 8 ,
error_message = ' Internal must be set to true for nonstandard scriptPubKey imports. ' )
self . test_address ( address ,
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : { " address " : address } ,
" timestamp " : " now " ,
" pubkeys " : [ key . pubkey ] ,
" internal " : False } ,
2021-10-28 07:30:15 +02:00
True ,
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( address ,
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" pubkeys " : [ key . pubkey ] ,
" internal " : True } ,
2021-10-28 07:30:15 +02:00
True ,
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( address ,
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : nonstandardScriptPubKey ,
" timestamp " : " now " ,
" pubkeys " : [ key . pubkey ] } ,
False ,
error_code = - 8 ,
error_message = ' Internal must be set to true for nonstandard scriptPubKey imports. ' )
self . test_address ( address ,
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : { " address " : address } ,
" timestamp " : " now " ,
" keys " : [ key . privkey ] } ,
True )
self . test_address ( address ,
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 " )
2021-10-28 07:28:23 +02:00
self . test_importmulti ( { " scriptPubKey " : { " address " : address } ,
" timestamp " : " now " ,
" keys " : [ key . privkey ] } ,
False ,
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : { " address " : address } ,
" timestamp " : " now " ,
" keys " : [ key . privkey ] ,
" watchonly " : True } ,
2021-10-28 07:30:15 +02:00
True ,
warnings = [ " All private keys are provided, outputs will be considered spendable. If this is intentional, do not specify the watchonly flag. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( address ,
iswatchonly = False ,
2021-10-28 07:30:15 +02:00
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" keys " : [ key . privkey ] ,
" internal " : True } ,
True )
self . test_address ( address ,
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
self . test_importmulti ( { " scriptPubKey " : nonstandardScriptPubKey ,
" timestamp " : " now " ,
" keys " : [ key . privkey ] } ,
False ,
error_code = - 8 ,
error_message = ' Internal must be set to true for nonstandard scriptPubKey imports. ' )
self . test_address ( address ,
iswatchonly = False ,
ismine = False ,
timestamp = None )
2016-10-20 09:04:18 +02:00
# P2SH address
2021-10-28 07:28:23 +02:00
multisig = self . get_multisig ( )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 100 )
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 ' ]
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 " } ,
True )
self . test_address ( multisig . p2sh_addr ,
isscript = True ,
iswatchonly = True ,
timestamp = timestamp )
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
2021-10-28 07:28:23 +02:00
multisig = self . get_multisig ( )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 100 )
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 ' ]
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 } ,
2021-10-28 07:30:15 +02:00
True ,
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
self . test_address ( 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
2021-10-28 07:28:23 +02:00
multisig = self . get_multisig ( )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 100 )
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 ' ]
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 ] } ,
2021-10-28 07:30:15 +02:00
True ,
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( multisig . p2sh_addr ,
2021-10-28 07:30:15 +02:00
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
2021-10-28 07:28:23 +02:00
multisig = self . get_multisig ( )
2016-10-20 09:04:18 +02:00
self . nodes [ 1 ] . generate ( 100 )
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 ' ]
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 } ,
2021-10-28 07:30:15 +02:00
True )
self . test_address ( 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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
wrong_key = self . get_key ( ) . pubkey
self . test_importmulti ( { " scriptPubKey " : { " address " : address } ,
" timestamp " : " now " ,
" pubkeys " : [ wrong_key ] } ,
2021-10-28 07:30:15 +02:00
True ,
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. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( address ,
2021-10-28 07:30:15 +02:00
iswatchonly = True ,
2021-10-28 07:28:23 +02:00
ismine = False ,
2021-10-28 07:30:15 +02:00
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
wrong_key = self . get_key ( ) . pubkey
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" pubkeys " : [ wrong_key ] ,
" internal " : True } ,
2021-10-28 07:30:15 +02:00
True ,
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. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( address ,
2021-10-28 07:30:15 +02:00
iswatchonly = True ,
2021-10-28 07:28:23 +02:00
ismine = False ,
2021-10-28 07:30:15 +02:00
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
wrong_privkey = self . get_key ( ) . privkey
self . test_importmulti ( { " scriptPubKey " : { " address " : address } ,
" timestamp " : " now " ,
" keys " : [ wrong_privkey ] } ,
2021-10-28 07:30:15 +02:00
True ,
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. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( address ,
2021-10-28 07:30:15 +02:00
iswatchonly = True ,
2021-10-28 07:28:23 +02:00
ismine = False ,
2021-10-28 07:30:15 +02:00
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 " )
2021-10-28 07:28:23 +02:00
key = self . get_key ( )
address = key . p2pkh_addr
wrong_privkey = self . get_key ( ) . privkey
self . test_importmulti ( { " scriptPubKey " : key . p2pkh_script ,
" timestamp " : " now " ,
" keys " : [ wrong_privkey ] ,
" internal " : True } ,
2021-10-28 07:30:15 +02:00
True ,
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. " ] )
2021-10-28 07:28:23 +02:00
self . test_address ( address ,
2021-10-28 07:30:15 +02:00
iswatchonly = True ,
2021-10-28 07:28:23 +02:00
ismine = False ,
2021-10-28 07:30:15 +02:00
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 " } ,
True )
self . test_address ( 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 ( )
2021-10-28 07:28:23 +02:00
self . test_address ( 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 ' )
# Test importing of a ranged descriptor without keys
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 " ,
" range " : { " end " : 1 } } ,
success = True ,
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
# Test importing of a P2PKH address via descriptor
key = self . get_key ( )
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 " ,
" label " : " Descriptor import test " } ,
True ,
warnings = [ " Some private keys are missing, outputs will be considered watchonly. If this is intentional, specify the watchonly flag. " ] )
self . test_address ( key . p2pkh_addr ,
solvable = True ,
ismine = False ,
label = " Descriptor import test " )
# Test import fails if both desc and scriptPubKey are provided
key = self . get_key ( )
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
key = self . get_key ( )
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
key1 = self . get_key ( )
key2 = self . get_key ( )
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 " } ,
success = True )
self . log . info ( " Should not treat individual keys from the imported bare multisig as watchonly " )
self . test_address ( key1 . p2pkh_addr ,
ismine = False ,
iswatchonly = False )
2017-02-03 22:23:13 +01:00
2016-10-20 09:04:18 +02:00
if __name__ == ' __main__ ' :
2021-10-28 07:28:23 +02:00
ImportMultiTest ( ) . main ( )