|
| 1 | +from io import StringIO |
| 2 | +import json |
| 3 | +import os, sys |
| 4 | +from electrum.bitcoin import address_to_script |
| 5 | +from electrum.plugins.timelock_recovery.timelock_recovery import TimelockRecoveryContext |
| 6 | +from electrum.simple_config import SimpleConfig |
| 7 | +from electrum.storage import WalletStorage |
| 8 | +from electrum.transaction import PartialTxOutput |
| 9 | +from electrum.wallet import Wallet |
| 10 | +from electrum.wallet_db import WalletDB |
| 11 | + |
| 12 | +from . import ElectrumTestCase |
| 13 | + |
| 14 | + |
| 15 | +class TestTimelockRecovery(ElectrumTestCase): |
| 16 | + TESTNET = True |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + super(TestTimelockRecovery, self).setUp() |
| 20 | + self.config = SimpleConfig({'electrum_path': self.electrum_path}) |
| 21 | + |
| 22 | + self.wallet_path = os.path.join(self.electrum_path, "timelock_recovery_wallet") |
| 23 | + |
| 24 | + self._saved_stdout = sys.stdout |
| 25 | + self._stdout_buffer = StringIO() |
| 26 | + sys.stdout = self._stdout_buffer |
| 27 | + |
| 28 | + def tearDown(self): |
| 29 | + super(TestTimelockRecovery, self).tearDown() |
| 30 | + # Restore the "real" stdout |
| 31 | + sys.stdout = self._saved_stdout |
| 32 | + |
| 33 | + def _create_default_wallet(self): |
| 34 | + with open(os.path.join(os.path.dirname(__file__), "test_timelock_recovery", "default_wallet"), "r") as f: |
| 35 | + wallet_str = f.read() |
| 36 | + storage = WalletStorage(self.wallet_path) |
| 37 | + db = WalletDB(wallet_str, storage=storage, upgrade=True) |
| 38 | + wallet = Wallet(db, config=self.config) |
| 39 | + return wallet |
| 40 | + |
| 41 | + async def test_get_alert_address(self): |
| 42 | + wallet = self._create_default_wallet() |
| 43 | + |
| 44 | + context = TimelockRecoveryContext(wallet) |
| 45 | + alert_address = context.get_alert_address() |
| 46 | + self.assertEqual(alert_address, 'tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u') |
| 47 | + |
| 48 | + async def test_get_cancellation_address(self): |
| 49 | + wallet = self._create_default_wallet() |
| 50 | + |
| 51 | + context = TimelockRecoveryContext(wallet) |
| 52 | + alert_address = context.get_cancellation_address() |
| 53 | + self.assertEqual(alert_address, 'tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u') |
| 54 | + |
| 55 | + async def test_make_unsigned_alert_tx(self): |
| 56 | + wallet = self._create_default_wallet() |
| 57 | + |
| 58 | + context = TimelockRecoveryContext(wallet) |
| 59 | + context.outputs = [ |
| 60 | + PartialTxOutput(scriptpubkey=address_to_script('tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u'), value='!'), |
| 61 | + ] |
| 62 | + |
| 63 | + alert_tx = context.make_unsigned_alert_tx(fee_est=5000) |
| 64 | + self.assertEqual(alert_tx.version, 2) |
| 65 | + alert_tx_inputs = [tx_input.prevout.to_str() for tx_input in alert_tx.inputs()] |
| 66 | + self.assertEqual(alert_tx_inputs, [ |
| 67 | + '59a9ff5fa62586f102b92504584f52e47f4ca0d5af061e99a0a3023fa70a70e2:1', |
| 68 | + '778b01899d5ed48df03e406bc5babd1fdc8f1be4b7e5b9d20dd8caf24dd66ff4:1', |
| 69 | + ]) |
| 70 | + alert_tx_outputs = [(tx_output.address, tx_output.value) for tx_output in alert_tx.outputs()] |
| 71 | + self.assertEqual(alert_tx_outputs, [ |
| 72 | + ('tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u', 600), |
| 73 | + ('tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u', 743065), |
| 74 | + ]) |
| 75 | + self.assertEqual(alert_tx.txid(), 'eff2f0b7bb21673afff875af9c9313a8eccbefc8f80b87e0a560cb424e1b5b1b') |
| 76 | + |
| 77 | + async def test_make_unsigned_recovery_tx(self): |
| 78 | + wallet = self._create_default_wallet() |
| 79 | + |
| 80 | + context = TimelockRecoveryContext(wallet) |
| 81 | + context.outputs = [ |
| 82 | + PartialTxOutput(scriptpubkey=address_to_script('tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u'), value='!'), |
| 83 | + ] |
| 84 | + context.alert_tx = context.make_unsigned_alert_tx(fee_est=5000) |
| 85 | + context.timelock_days = 90 |
| 86 | + |
| 87 | + recovery_tx = context.make_unsigned_recovery_tx(fee_est=5000) |
| 88 | + self.assertEqual(recovery_tx.version, 2) |
| 89 | + recovery_tx_inputs = [tx_input.prevout.to_str() for tx_input in recovery_tx.inputs()] |
| 90 | + self.assertEqual(recovery_tx_inputs, [ |
| 91 | + 'eff2f0b7bb21673afff875af9c9313a8eccbefc8f80b87e0a560cb424e1b5b1b:1', |
| 92 | + ]) |
| 93 | + self.assertEqual(recovery_tx.inputs()[0].nsequence, 0x00403b54) |
| 94 | + |
| 95 | + recovery_tx_outputs = [(tx_output.address, tx_output.value) for tx_output in recovery_tx.outputs()] |
| 96 | + self.assertEqual(recovery_tx_outputs, [ |
| 97 | + ('tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u', 738065), |
| 98 | + ]) |
| 99 | + |
| 100 | + async def test_make_unsigned_cancellation_tx(self): |
| 101 | + wallet = self._create_default_wallet() |
| 102 | + |
| 103 | + context = TimelockRecoveryContext(wallet) |
| 104 | + context.outputs = [ |
| 105 | + PartialTxOutput(scriptpubkey=address_to_script('tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u'), value='!'), |
| 106 | + ] |
| 107 | + context.alert_tx = context.make_unsigned_alert_tx(fee_est=5000) |
| 108 | + |
| 109 | + cancellation_tx = context.make_unsigned_cancellation_tx(fee_est=6000) |
| 110 | + self.assertEqual(cancellation_tx.version, 2) |
| 111 | + cancellation_tx_inputs = [tx_input.prevout.to_str() for tx_input in cancellation_tx.inputs()] |
| 112 | + self.assertEqual(cancellation_tx_inputs, [ |
| 113 | + 'eff2f0b7bb21673afff875af9c9313a8eccbefc8f80b87e0a560cb424e1b5b1b:1', |
| 114 | + ]) |
| 115 | + self.assertEqual(cancellation_tx.inputs()[0].nsequence, 0xfffffffd) |
| 116 | + cancellation_tx_outputs = [(tx_output.address, tx_output.value) for tx_output in cancellation_tx.outputs()] |
| 117 | + self.assertEqual(cancellation_tx_outputs, [ |
| 118 | + ('tb1qtf9mwfv8ux0j90cwtx9nvz9l46jav40sak7ncg', 737065), |
| 119 | + ]) |
0 commit comments