|
| 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 | + maxDiff = None |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + super(TestTimelockRecovery, self).setUp() |
| 21 | + self.config = SimpleConfig({'electrum_path': self.electrum_path}) |
| 22 | + |
| 23 | + self.wallet_path = os.path.join(self.electrum_path, "timelock_recovery_wallet") |
| 24 | + |
| 25 | + self._saved_stdout = sys.stdout |
| 26 | + self._stdout_buffer = StringIO() |
| 27 | + sys.stdout = self._stdout_buffer |
| 28 | + |
| 29 | + def tearDown(self): |
| 30 | + super(TestTimelockRecovery, self).tearDown() |
| 31 | + # Restore the "real" stdout |
| 32 | + sys.stdout = self._saved_stdout |
| 33 | + |
| 34 | + |
| 35 | + async def test_get_alert_address(self): |
| 36 | + with open(os.path.join(os.path.dirname(__file__), "test_timelock_recovery", "default_wallet"), "r") as f: |
| 37 | + wallet_str = f.read() |
| 38 | + storage = WalletStorage(self.wallet_path) |
| 39 | + db = WalletDB(wallet_str, storage=storage, upgrade=True) |
| 40 | + wallet = Wallet(db, config=self.config) |
| 41 | + |
| 42 | + context = TimelockRecoveryContext(wallet) |
| 43 | + alert_address = context.get_alert_address() |
| 44 | + self.assertEqual(alert_address, 'tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u') |
| 45 | + |
| 46 | + async def test_get_alert_address(self): |
| 47 | + with open(os.path.join(os.path.dirname(__file__), "test_timelock_recovery", "default_wallet"), "r") as f: |
| 48 | + wallet_str = f.read() |
| 49 | + storage = WalletStorage(self.wallet_path) |
| 50 | + db = WalletDB(wallet_str, storage=storage, upgrade=True) |
| 51 | + wallet = Wallet(db, config=self.config) |
| 52 | + |
| 53 | + context = TimelockRecoveryContext(wallet) |
| 54 | + alert_address = context.get_cancellation_address() |
| 55 | + self.assertEqual(alert_address, 'tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u') |
| 56 | + |
| 57 | + async def test_make_unsigned_alert_tx(self): |
| 58 | + with open(os.path.join(os.path.dirname(__file__), "test_timelock_recovery", "default_wallet"), "r") as f: |
| 59 | + wallet_str = f.read() |
| 60 | + storage = WalletStorage(self.wallet_path) |
| 61 | + db = WalletDB(wallet_str, storage=storage, upgrade=True) |
| 62 | + wallet = Wallet(db, config=self.config) |
| 63 | + |
| 64 | + context = TimelockRecoveryContext(wallet) |
| 65 | + context.outputs = [ |
| 66 | + PartialTxOutput(scriptpubkey=address_to_script('tb1qt339ksrha0n5a6lwpql778erkm272hxgamdc0u'), value='!'), |
| 67 | + ] |
| 68 | + |
| 69 | + alert_tx = context.make_unsigned_alert_tx(fee_est=5000) |
| 70 | + alert_tx_json = json.dumps(alert_tx.to_json(), sort_keys=True, indent=2) |
| 71 | + print(alert_tx_json) |
| 72 | + self.assertEqual(json.dumps(alert_tx.to_json(), sort_keys=True, indent=None), "{}") |
0 commit comments