wallet_implicitsegwit.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2019-2022 The Limenka 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 LimenkaTestFramework
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 def check_explicit_transactions(explicit_keys, explicit_node):
42 # The explicit segwit node doesn't allow conversion from legacy to segwit
43 txs = explicit_node.listtransactions(None, 99999)
44 for a in address_types:
45 pubkey = explicit_keys[a]
46 for b in address_types:
47 b_address = key_to_address(pubkey, b)
48 if a == 'legacy' and a != b:
49 assert(('receive', b_address) not in tuple((tx['category'], tx['address']) for tx in txs))
50 else:
51 assert(('receive', b_address) in tuple((tx['category'], tx['address']) for tx in txs))
52
53 class ImplicitSegwitTest(LimenkaTestFramework):
54 def add_options(self, parser):
55 self.add_wallet_options(parser, descriptors=False)
56
57 def set_test_params(self):
58 self.num_nodes = 2
59 self.supports_cli = False
60 self.extra_args = [
61 [
62 "-walletimplicitsegwit=1",
63 ],
64 [
65 "-walletimplicitsegwit=0",
66 ],
67 ]
68
69 def skip_test_if_missing_module(self):
70 self.skip_if_no_wallet()
71
72 def run_test(self):
73 self.log.info("Manipulating addresses and sending transactions to all variations")
74 implicit_keys = send_a_to_b(self.nodes[0], self.nodes[1])
75 explicit_keys = send_a_to_b(self.nodes[1], self.nodes[0])
76
77 self.sync_all()
78
79 self.log.info("Checking that transactions show up correctly without a restart")
80 check_implicit_transactions(implicit_keys, self.nodes[0])
81 check_explicit_transactions(explicit_keys, self.nodes[1])
82
83 self.log.info("Checking that transactions still show up correctly after a restart")
84 self.restart_node(0)
85 self.restart_node(1)
86
87 check_implicit_transactions(implicit_keys, self.nodes[0])
88 check_explicit_transactions(explicit_keys, self.nodes[1])
89
90 if __name__ == '__main__':
91 ImplicitSegwitTest(__file__).main()
92