wallet_watchonly.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2018-2022 The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Test createwallet watchonly arguments. 6 """ 7 8 from test_framework.blocktools import COINBASE_MATURITY 9 from test_framework.test_framework import BitcoinTestFramework 10 from test_framework.util import ( 11 assert_equal, 12 assert_raises_rpc_error 13 ) 14 15 16 class CreateWalletWatchonlyTest(BitcoinTestFramework): 17 def add_options(self, parser): 18 self.add_wallet_options(parser) 19 20 def set_test_params(self): 21 self.num_nodes = 1 22 23 def skip_test_if_missing_module(self): 24 self.skip_if_no_wallet() 25 26 def run_test(self): 27 node = self.nodes[0] 28 29 self.nodes[0].createwallet(wallet_name='default') 30 def_wallet = node.get_wallet_rpc('default') 31 32 a1 = def_wallet.getnewaddress() 33 wo_change = def_wallet.getnewaddress() 34 wo_addr = def_wallet.getnewaddress() 35 36 self.nodes[0].createwallet(wallet_name='wo', disable_private_keys=True) 37 wo_wallet = node.get_wallet_rpc('wo') 38 39 wo_wallet.importpubkey(pubkey=def_wallet.getaddressinfo(wo_addr)['pubkey']) 40 wo_wallet.importpubkey(pubkey=def_wallet.getaddressinfo(wo_change)['pubkey']) 41 42 # generate some btc for testing 43 self.generatetoaddress(node, COINBASE_MATURITY + 1, a1) 44 45 # send 1 btc to our watch-only address 46 txid = def_wallet.sendtoaddress(wo_addr, 1) 47 self.generate(self.nodes[0], 1) 48 49 # getbalance 50 self.log.info('include_watchonly should default to true for watch-only wallets') 51 self.log.info('Testing getbalance watch-only defaults') 52 assert_equal(wo_wallet.getbalance(), 1) 53 assert_equal(len(wo_wallet.listtransactions()), 1) 54 assert_equal(wo_wallet.getbalance(include_watchonly=False), 0) 55 56 self.log.info('Test sending from a watch-only wallet raises RPC error') 57 msg = "Error: Private keys are disabled for this wallet" 58 assert_raises_rpc_error(-4, msg, wo_wallet.sendtoaddress, a1, 0.1) 59 assert_raises_rpc_error(-4, msg, wo_wallet.sendmany, amounts={a1: 0.1}) 60 61 self.log.info('Testing listreceivedbyaddress watch-only defaults') 62 result = wo_wallet.listreceivedbyaddress() 63 assert_equal(len(result), 1) 64 assert_equal(result[0]["involvesWatchonly"], True) 65 result = wo_wallet.listreceivedbyaddress(include_watchonly=False) 66 assert_equal(len(result), 0) 67 68 self.log.info('Testing listreceivedbylabel watch-only defaults') 69 result = wo_wallet.listreceivedbylabel() 70 assert_equal(len(result), 1) 71 assert_equal(result[0]["involvesWatchonly"], True) 72 result = wo_wallet.listreceivedbylabel(include_watchonly=False) 73 assert_equal(len(result), 0) 74 75 self.log.info('Testing listtransactions watch-only defaults') 76 result = wo_wallet.listtransactions() 77 assert_equal(len(result), 1) 78 assert_equal(result[0]["involvesWatchonly"], True) 79 result = wo_wallet.listtransactions(include_watchonly=False) 80 assert_equal(len(result), 0) 81 82 self.log.info('Testing listsinceblock watch-only defaults') 83 result = wo_wallet.listsinceblock() 84 assert_equal(len(result["transactions"]), 1) 85 assert_equal(result["transactions"][0]["involvesWatchonly"], True) 86 result = wo_wallet.listsinceblock(include_watchonly=False) 87 assert_equal(len(result["transactions"]), 0) 88 89 self.log.info('Testing gettransaction watch-only defaults') 90 result = wo_wallet.gettransaction(txid) 91 assert_equal(result["details"][0]["involvesWatchonly"], True) 92 result = wo_wallet.gettransaction(txid=txid, include_watchonly=False) 93 assert_equal(len(result["details"]), 0) 94 95 self.log.info('Testing walletcreatefundedpsbt watch-only defaults') 96 inputs = [] 97 outputs = [{a1: 0.5}] 98 options = {'changeAddress': wo_change} 99 no_wo_options = {'changeAddress': wo_change, 'includeWatching': False} 100 101 result = wo_wallet.walletcreatefundedpsbt(inputs=inputs, outputs=outputs, **options) 102 assert_equal("psbt" in result, True) 103 assert_raises_rpc_error(-4, "Insufficient funds", wo_wallet.walletcreatefundedpsbt, inputs, outputs, 0, no_wo_options) 104 105 self.log.info('Testing fundrawtransaction watch-only defaults') 106 rawtx = wo_wallet.createrawtransaction(inputs=inputs, outputs=outputs) 107 result = wo_wallet.fundrawtransaction(hexstring=rawtx, **options) 108 assert_equal("hex" in result, True) 109 assert_raises_rpc_error(-4, "Insufficient funds", wo_wallet.fundrawtransaction, rawtx, no_wo_options) 110 111 112 113 if __name__ == '__main__': 114 CreateWalletWatchonlyTest().main()