| 1 |
from io import StringIO |
| 2 |
import os |
| 3 |
import sys |
| 4 |
|
| 5 |
from electrum.bitcoin import address_to_script |
| 6 |
from electrum.fee_policy import FixedFeePolicy |
| 7 |
from electrum.simple_config import SimpleConfig |
| 8 |
from electrum.storage import WalletStorage |
| 9 |
from electrum.transaction import PartialTxOutput |
| 10 |
from electrum.wallet import Wallet |
| 11 |
from electrum.wallet_db import WalletDB |
| 12 |
|
| 13 |
from electrum.plugins.timelock_recovery.timelock_recovery import TimelockRecoveryContext, TimelockRecoveryPlugin |
| 14 |
|
| 15 |
from .. import ElectrumTestCase |
| 16 |
|
| 17 |
|
| 18 |
class TestTimelockRecovery(ElectrumTestCase): |
| 19 |
TESTNET = True |
| 20 |
|
| 21 |
def setUp(self): |
| 22 |
super(TestTimelockRecovery, self).setUp() |
| 23 |
self.config = SimpleConfig({'electrum_path': self.electrum_path}) |
| 24 |
|
| 25 |
self.wallet_path = os.path.join(self.electrum_path, "timelock_recovery_wallet") |
| 26 |
|
| 27 |
self._saved_stdout = sys.stdout |
| 28 |
self._stdout_buffer = StringIO() |
| 29 |
sys.stdout = self._stdout_buffer |
| 30 |
|
| 31 |
def tearDown(self): |
| 32 |
super(TestTimelockRecovery, self).tearDown() |
| 33 |
|
| 34 |
sys.stdout = self._saved_stdout |
| 35 |
|
| 36 |
def _create_default_wallet(self): |
| 37 |
with open(os.path.join(os.path.dirname(__file__), "test_timelock_recovery", "default_wallet"), "r") as f: |
| 38 |
wallet_str = f.read() |
| 39 |
storage = WalletStorage(self.wallet_path) |
| 40 |
db = WalletDB(wallet_str, storage=storage, upgrade=True) |
| 41 |
wallet = Wallet(db, config=self.config) |
| 42 |
return wallet |
| 43 |
|
| 44 |
async def test_get_alert_address(self): |
| 45 |
wallet = self._create_default_wallet() |
| 46 |
|
| 47 |
context = TimelockRecoveryContext(wallet) |
| 48 |
alert_address = context.get_alert_address() |
| 49 |
self.assertEqual(alert_address, 'tb1qchyc02y9mv4xths4je9puc4yzuxt8rfm26ef07') |
| 50 |
|
| 51 |
async def test_get_cancellation_address(self): |
| 52 |
wallet = self._create_default_wallet() |
| 53 |
|
| 54 |
context = TimelockRecoveryContext(wallet) |
| 55 |
context.get_alert_address() |
| 56 |
cancellation_address = context.get_cancellation_address() |
| 57 |
self.assertEqual(cancellation_address, 'tb1q6k5h4cz6ra8nzhg90xm9wldvadgh0fpttfthcg') |
| 58 |
|
| 59 |
async def test_make_unsigned_alert_tx(self): |
| 60 |
wallet = self._create_default_wallet() |
| 61 |
|
| 62 |
context = TimelockRecoveryContext(wallet) |
| 63 |
context.outputs = [ |
| 64 |
PartialTxOutput(scriptpubkey=address_to_script('tb1q4s8z6g5jqzllkgt8a4har94wl8tg0k9m8kv5zd'), value='!'), |
| 65 |
] |
| 66 |
|
| 67 |
alert_tx = context.make_unsigned_alert_tx(fee_policy=FixedFeePolicy(5000)) |
| 68 |
self.assertEqual(alert_tx.version, 2) |
| 69 |
alert_tx_inputs = [tx_input.prevout.to_str() for tx_input in alert_tx.inputs()] |
| 70 |
self.assertEqual(alert_tx_inputs, [ |
| 71 |
'59a9ff5fa62586f102b92504584f52e47f4ca0d5af061e99a0a3023fa70a70e2:1', |
| 72 |
'778b01899d5ed48df03e406bc5babd1fdc8f1be4b7e5b9d20dd8caf24dd66ff4:1', |
| 73 |
]) |
| 74 |
alert_tx_outputs = [(tx_output.address, tx_output.value) for tx_output in alert_tx.outputs()] |
| 75 |
self.assertEqual(alert_tx_outputs, [ |
| 76 |
('tb1q4s8z6g5jqzllkgt8a4har94wl8tg0k9m8kv5zd', 600), |
| 77 |
('tb1qchyc02y9mv4xths4je9puc4yzuxt8rfm26ef07', 743065), |
| 78 |
]) |
| 79 |
self.assertEqual(alert_tx.txid(), '01c227f136c4490ec7cb0fe2ba5e44c436f58906b7fc29a83cb865d7e3bfaa60') |
| 80 |
|
| 81 |
async def test_make_unsigned_recovery_tx(self): |
| 82 |
wallet = self._create_default_wallet() |
| 83 |
|
| 84 |
context = TimelockRecoveryContext(wallet) |
| 85 |
context.outputs = [ |
| 86 |
PartialTxOutput(scriptpubkey=address_to_script('tb1q4s8z6g5jqzllkgt8a4har94wl8tg0k9m8kv5zd'), value='!'), |
| 87 |
] |
| 88 |
context.alert_tx = context.make_unsigned_alert_tx(fee_policy=FixedFeePolicy(5000)) |
| 89 |
context.timelock_days = 90 |
| 90 |
|
| 91 |
recovery_tx = context.make_unsigned_recovery_tx(fee_policy=FixedFeePolicy(5000)) |
| 92 |
self.assertEqual(recovery_tx.version, 2) |
| 93 |
recovery_tx_inputs = [tx_input.prevout.to_str() for tx_input in recovery_tx.inputs()] |
| 94 |
self.assertEqual(recovery_tx_inputs, [ |
| 95 |
'01c227f136c4490ec7cb0fe2ba5e44c436f58906b7fc29a83cb865d7e3bfaa60:1', |
| 96 |
]) |
| 97 |
self.assertEqual(recovery_tx.inputs()[0].nsequence, 0x00403b54) |
| 98 |
|
| 99 |
recovery_tx_outputs = [(tx_output.address, tx_output.value) for tx_output in recovery_tx.outputs()] |
| 100 |
self.assertEqual(recovery_tx_outputs, [ |
| 101 |
('tb1q4s8z6g5jqzllkgt8a4har94wl8tg0k9m8kv5zd', 738065), |
| 102 |
]) |
| 103 |
|
| 104 |
async def test_make_unsigned_cancellation_tx(self): |
| 105 |
wallet = self._create_default_wallet() |
| 106 |
|
| 107 |
context = TimelockRecoveryContext(wallet) |
| 108 |
context.outputs = [ |
| 109 |
PartialTxOutput(scriptpubkey=address_to_script('tb1q4s8z6g5jqzllkgt8a4har94wl8tg0k9m8kv5zd'), value='!'), |
| 110 |
] |
| 111 |
context.alert_tx = context.make_unsigned_alert_tx(fee_policy=FixedFeePolicy(5000)) |
| 112 |
|
| 113 |
cancellation_tx = context.make_unsigned_cancellation_tx(fee_policy=FixedFeePolicy(6000)) |
| 114 |
self.assertEqual(cancellation_tx.version, 2) |
| 115 |
cancellation_tx_inputs = [tx_input.prevout.to_str() for tx_input in cancellation_tx.inputs()] |
| 116 |
self.assertEqual(cancellation_tx_inputs, [ |
| 117 |
'01c227f136c4490ec7cb0fe2ba5e44c436f58906b7fc29a83cb865d7e3bfaa60:1', |
| 118 |
]) |
| 119 |
self.assertEqual(cancellation_tx.inputs()[0].nsequence, 0xfffffffd) |
| 120 |
cancellation_tx_outputs = [(tx_output.address, tx_output.value) for tx_output in cancellation_tx.outputs()] |
| 121 |
self.assertEqual(cancellation_tx_outputs, [ |
| 122 |
('tb1q6k5h4cz6ra8nzhg90xm9wldvadgh0fpttfthcg', 737065), |
| 123 |
]) |
| 124 |
|
| 125 |
def test_checksum_non_ascii(self): |
| 126 |
|
| 127 |
|
| 128 |
json_data = {"wallet_name": "Ωmega Wörld Ñoño 日本語 中文 עברית العربية", "id": "abc-123"} |
| 129 |
result = TimelockRecoveryPlugin.json_checksum(json_data) |
| 130 |
self.assertEqual(result, "74674eca") |
| 131 |
|
| 132 |
def test_checksum_bip_example(self): |
| 133 |
|
| 134 |
json_data = { |
| 135 |
"kind": "timelock-recovery-plan", |
| 136 |
"id": "exported-692452189b301b561ed57cbe", |
| 137 |
"name": "Recovery Plan ac300e72-7612-497e-96b0-df2fdeda59ea", |
| 138 |
"description": "RITREK APP 1.1.0: Trezor Account #1", |
| 139 |
"created_at": "2025-11-24T12:39:53.532Z", |
| 140 |
"plugin_version": "1.0.1", |
| 141 |
"wallet_version": "1.0.1", |
| 142 |
"wallet_name": "RITREK Service", |
| 143 |
"wallet_kind": "RITREK BACKEND", |
| 144 |
"timelock_days": 2, |
| 145 |
"anchor_amount_sats": 600, |
| 146 |
"anchor_addresses": [ |
| 147 |
"bc1qnda6x2gxdh3yujd2zjpsd7qzx3awxmlaf9wwlk" |
| 148 |
], |
| 149 |
"alert_address": "bc1qj0f9sjenwyjs0u7mlgvptjp05z3syzq7mru3ep", |
| 150 |
"alert_inputs": [ |
| 151 |
"a265a485df4c6417019b91379257eb387bceeda96f7bb6311794b8ed358cf104:0", |
| 152 |
"2f621c2151f33173983133cbc1000e3b603b8a18423b0379feffe8513171d5d3:0" |
| 153 |
], |
| 154 |
"alert_tx": "0200000000010204F18C35EDB8941731B67B6FA9EDCE7B38EB579237919B0117644CDF85A465A20000000000FDFFFFFFD3D5713151E8FFFE79033B42188A3B603B0E00C1CB3331987331F351211C622F0000000000FDFFFFFF0258020000000000001600149B7BA329066DE24E49AA148306F802347AE36FFD205600000000000016001493D2584B33712507F3DBFA1815C82FA0A302081E02483045022100DCDBAE77C35EB4A0B3ED0DE5484206AB6B07041BE99B2BBAF0243C125916523C0220396959C3C52B2B1F9E472AEEE7C5D9540531B131C3221DE942754C6D0941397D012103C08FF3ADBA14B742646572BCA6F07AEB910666FB28E4DDDC40E33755E7C869D30248304502210089084472FDA3CF82D6ABC11BF1A5E77C9B423617C8B840F58C02746035B3BA6302203942AA1FA13F952F49FB114D48130A9AAF70151E7D09036D15734DB1F41A8B6001210397064EDED7DAD7D662290DC2847E87C5C27DA8865B89DDB58FDE9A006BA7DB3900000000", |
| 155 |
"alert_txid": "f1413fedadaf30697820bcd8f6a393fcc73ea00a15bea3253f89d5658690d2f7", |
| 156 |
"alert_fee": 231, |
| 157 |
"alert_weight": 834, |
| 158 |
"recovery_tx": "02000000000101F7D2908665D5893F25A3BE150AA03EC7FC93A3F6D8BC20786930AFADED3F41F101000000005201400001A6550000000000001600149B7BA329066DE24E49AA148306F802347AE36FFD0247304402204AFF87C2127F5697F300C6522067A8D5E5290CA8D140D2E5BCEF4A36606C5FE5022056673BEC5BB459DFFBD4D266EE95AEF0D701383ED80BD433A02C3C486A826D76012102774DBCD59F2D08EFF718BC09972ADC609FBC31C26B551B3E4EA30A1D43EEDB9700000000", |
| 159 |
"recovery_txid": "bc304610e8f282036345e87163d4cba5b16488a3bf2e4d738379d7bda3a0bca3", |
| 160 |
"recovery_fee": 122, |
| 161 |
"recovery_weight": 437, |
| 162 |
"recovery_outputs": [ |
| 163 |
[ |
| 164 |
"bc1qnda6x2gxdh3yujd2zjpsd7qzx3awxmlaf9wwlk", |
| 165 |
21926, |
| 166 |
"My Backup Wallet" |
| 167 |
] |
| 168 |
], |
| 169 |
"metadata": "sig:825d6b3858c175c7fc16da3134030e095c4f9089c3c89722247eeedc08a7ef4f", |
| 170 |
} |
| 171 |
result = TimelockRecoveryPlugin.json_checksum(json_data) |
| 172 |
self.assertEqual(result, "92f8b3da") |