2 # Copyright (c) 2017 The Zcash developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or https://www.opensource.org/licenses/mit-license.php .
6 import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
8 from decimal import Decimal
9 from test_framework.test_framework import BitcoinTestFramework
10 from test_framework.util import assert_equal, assert_greater_than, start_nodes, initialize_chain_clean, connect_nodes_bi
14 logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
17 class KeyImportExportTest (BitcoinTestFramework):
19 def setup_chain(self):
20 print("Initializing test directory "+self.options.tmpdir)
21 initialize_chain_clean(self.options.tmpdir, 4)
23 def setup_network(self, split=False):
24 self.nodes = start_nodes(4, self.options.tmpdir )
25 connect_nodes_bi(self.nodes,0,1)
26 connect_nodes_bi(self.nodes,1,2)
27 connect_nodes_bi(self.nodes,0,2)
28 connect_nodes_bi(self.nodes,0,3)
29 self.is_network_split=False
33 [alice, bob, charlie, miner] = self.nodes
35 def alice_to_bob(amount):
36 alice.sendtoaddress(addr, Decimal(amount))
41 def verify_utxos(node, amounts):
42 utxos = node.listunspent(1, 10**9, [addr])
44 def cmp_confirmations_high_to_low(a, b):
45 return cmp(b["confirmations"], a["confirmations"])
47 utxos.sort(cmp_confirmations_high_to_low)
50 assert_equal(amounts, [utxo["amount"] for utxo in utxos])
51 except AssertionError:
53 'Expected amounts: %r; utxos: %r',
57 # Seed Alice with some funds
63 # Now get a pristine address for receiving transfers:
64 addr = bob.getnewaddress()
66 verify_utxos(charlie, [])
68 # the amounts of each txn embodied which generates a single UTXO:
69 amounts = map(Decimal, ['2.3', '3.7', '0.1', '0.5', '1.0', '0.19'])
71 # Internal test consistency assertion:
74 reduce(Decimal.__add__, amounts))
76 logging.info("Sending pre-export txns...")
77 for amount in amounts[0:2]:
80 logging.info("Exporting privkey from bob...")
81 privkey = bob.dumpprivkey(addr)
83 logging.info("Sending post-export txns...")
84 for amount in amounts[2:4]:
87 verify_utxos(bob, amounts[:4])
88 verify_utxos(charlie, [])
90 logging.info("Importing privkey into charlie...")
91 ipkaddr = charlie.importprivkey(privkey, '', True)
92 assert_equal(addr, ipkaddr)
94 # importprivkey should have rescanned, so this should pass:
95 verify_utxos(charlie, amounts[:4])
97 # Verify idempotent behavior:
98 ipkaddr2 = charlie.importprivkey(privkey, '', True)
99 assert_equal(addr, ipkaddr2)
101 # amounts should be unchanged
102 verify_utxos(charlie, amounts[:4])
104 logging.info("Sending post-import txns...")
105 for amount in amounts[4:]:
108 verify_utxos(bob, amounts)
109 verify_utxos(charlie, amounts)
112 if __name__ == '__main__':
113 KeyImportExportTest().main()