|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2019 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 the wallet implicit segwit feature.""" |
| 6 | + |
| 7 | +import test_framework.address as address |
| 8 | +from test_framework.test_framework import BitcoinTestFramework |
| 9 | + |
| 10 | +# TODO: Might be nice to test p2pk here too |
| 11 | +address_types = ('legacy', 'bech32', 'p2sh-segwit') |
| 12 | + |
| 13 | +def key_to_address(key, address_type): |
| 14 | + if address_type == 'legacy': |
| 15 | + return address.key_to_p2pkh(key) |
| 16 | + elif address_type == 'p2sh-segwit': |
| 17 | + return address.key_to_p2sh_p2wpkh(key) |
| 18 | + elif address_type == 'bech32': |
| 19 | + return address.key_to_p2wpkh(key) |
| 20 | + |
| 21 | +def send_a_to_b(receive_node, send_node): |
| 22 | + keys = {} |
| 23 | + for a in address_types: |
| 24 | + a_address = receive_node.getnewaddress(address_type=a) |
| 25 | + pubkey = receive_node.getaddressinfo(a_address)['pubkey'] |
| 26 | + keys[a] = pubkey |
| 27 | + for b in address_types: |
| 28 | + b_address = key_to_address(pubkey, b) |
| 29 | + send_node.sendtoaddress(address=b_address, amount=1) |
| 30 | + return keys |
| 31 | + |
| 32 | +def check_implicit_transactions(implicit_keys, implicit_node): |
| 33 | + # The implicit segwit node allows conversion all possible ways |
| 34 | + txs = implicit_node.listtransactions(None, 99999) |
| 35 | + for a in address_types: |
| 36 | + pubkey = implicit_keys[a] |
| 37 | + for b in address_types: |
| 38 | + b_address = key_to_address(pubkey, b) |
| 39 | + assert(('receive', b_address) in tuple((tx['category'], tx['address']) for tx in txs)) |
| 40 | + |
| 41 | +class ImplicitSegwitTest(BitcoinTestFramework): |
| 42 | + def set_test_params(self): |
| 43 | + self.num_nodes = 2 |
| 44 | + |
| 45 | + def skip_test_if_missing_module(self): |
| 46 | + self.skip_if_no_wallet() |
| 47 | + |
| 48 | + def run_test(self): |
| 49 | + self.log.info("Manipulating addresses and sending transactions to all variations") |
| 50 | + implicit_keys = send_a_to_b(self.nodes[0], self.nodes[1]) |
| 51 | + |
| 52 | + self.sync_all() |
| 53 | + |
| 54 | + self.log.info("Checking that transactions show up correctly without a restart") |
| 55 | + check_implicit_transactions(implicit_keys, self.nodes[0]) |
| 56 | + |
| 57 | + self.log.info("Checking that transactions still show up correctly after a restart") |
| 58 | + self.restart_node(0) |
| 59 | + self.restart_node(1) |
| 60 | + |
| 61 | + check_implicit_transactions(implicit_keys, self.nodes[0]) |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + ImplicitSegwitTest().main() |
0 commit comments