| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
"""Test cold tx signing |
| 32 |
""" |
| 33 |
|
| 34 |
from __future__ import print_function |
| 35 |
from framework.daemon import Daemon |
| 36 |
from framework.wallet import Wallet |
| 37 |
import random |
| 38 |
|
| 39 |
SEED = 'velvet lymph giddy number token physics poetry unquoted nibs useful sabotage limits benches lifestyle eden nitrogen anvil fewest avoid batch vials washing fences goat unquoted' |
| 40 |
STANDARD_ADDRESS = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' |
| 41 |
SUBADDRESS = '84QRUYawRNrU3NN1VpFRndSukeyEb3Xpv8qZjjsoJZnTYpDYceuUTpog13D7qPxpviS7J29bSgSkR11hFFoXWk2yNdsR9WF' |
| 42 |
|
| 43 |
class ColdSigningTest(): |
| 44 |
def run_test(self): |
| 45 |
self.reset() |
| 46 |
self.create(0) |
| 47 |
self.mine() |
| 48 |
for piecemeal_output_export in [False, True]: |
| 49 |
self.transfer(piecemeal_output_export) |
| 50 |
for piecemeal_output_export in [False, True]: |
| 51 |
self.self_transfer_to_subaddress(piecemeal_output_export) |
| 52 |
self.transfer_after_empty_export_import() |
| 53 |
|
| 54 |
def reset(self): |
| 55 |
print('Resetting blockchain') |
| 56 |
daemon = Daemon() |
| 57 |
res = daemon.get_height() |
| 58 |
daemon.pop_blocks(res.height - 1) |
| 59 |
daemon.flush_txpool() |
| 60 |
|
| 61 |
def create(self, idx): |
| 62 |
print('Creating hot and cold wallet') |
| 63 |
|
| 64 |
self.hot_wallet = Wallet(idx = 0) |
| 65 |
|
| 66 |
try: self.hot_wallet.close_wallet() |
| 67 |
except: pass |
| 68 |
|
| 69 |
self.cold_wallet = Wallet(idx = 5) |
| 70 |
|
| 71 |
try: self.cold_wallet.close_wallet() |
| 72 |
except: pass |
| 73 |
|
| 74 |
res = self.cold_wallet.restore_deterministic_wallet(seed = SEED) |
| 75 |
spend_key = self.cold_wallet.query_key("spend_key").key |
| 76 |
view_key = self.cold_wallet.query_key("view_key").key |
| 77 |
res = self.hot_wallet.generate_from_keys(viewkey = view_key, address = STANDARD_ADDRESS) |
| 78 |
|
| 79 |
ok = False |
| 80 |
try: res = self.hot_wallet.query_key("spend_key") |
| 81 |
except: ok = True |
| 82 |
assert ok |
| 83 |
ok = False |
| 84 |
try: self.hot_wallet.query_key("mnemonic") |
| 85 |
except: ok = True |
| 86 |
assert ok |
| 87 |
assert self.cold_wallet.query_key("view_key").key == view_key |
| 88 |
assert self.cold_wallet.get_address().address == self.hot_wallet.get_address().address |
| 89 |
assert self.cold_wallet.get_address().address == STANDARD_ADDRESS |
| 90 |
|
| 91 |
def mine(self): |
| 92 |
print("Mining some blocks") |
| 93 |
daemon = Daemon() |
| 94 |
wallet = Wallet() |
| 95 |
|
| 96 |
daemon.generateblocks(STANDARD_ADDRESS, 80) |
| 97 |
wallet.refresh() |
| 98 |
|
| 99 |
def export_import(self, piecemeal_output_export): |
| 100 |
self.hot_wallet.refresh() |
| 101 |
|
| 102 |
if piecemeal_output_export: |
| 103 |
res = self.hot_wallet.incoming_transfers() |
| 104 |
num_outputs = len(res.transfers) |
| 105 |
done = [False] * num_outputs |
| 106 |
while len([x for x in done if not done[x]]) > 0: |
| 107 |
start = int(random.random() * num_outputs) |
| 108 |
if start == num_outputs: |
| 109 |
num_outputs -= 1 |
| 110 |
count = 1 + int(random.random() * 5) |
| 111 |
res = self.hot_wallet.export_outputs(all = True, start = start, count = count) |
| 112 |
|
| 113 |
|
| 114 |
ok = False |
| 115 |
try: |
| 116 |
self.hot_wallet.import_outputs(res.outputs_data_hex) |
| 117 |
except: |
| 118 |
ok = True |
| 119 |
assert ok |
| 120 |
|
| 121 |
try: |
| 122 |
self.cold_wallet.import_outputs(res.outputs_data_hex) |
| 123 |
except Exception as e: |
| 124 |
|
| 125 |
|
| 126 |
if 'Imported outputs omit more outputs that we know of' not in str(e): |
| 127 |
raise |
| 128 |
for i in range(start, start + count): |
| 129 |
if i < len(done): |
| 130 |
done[i] = True |
| 131 |
else: |
| 132 |
res = self.hot_wallet.export_outputs() |
| 133 |
self.cold_wallet.import_outputs(res.outputs_data_hex) |
| 134 |
|
| 135 |
res = self.cold_wallet.export_key_images(True) |
| 136 |
self.hot_wallet.import_key_images(res.signed_key_images, offset = res.offset) |
| 137 |
|
| 138 |
def create_tx(self, destination_addr, piecemeal_output_export): |
| 139 |
daemon = Daemon() |
| 140 |
|
| 141 |
dst = {'address': destination_addr, 'amount': 1000000000000} |
| 142 |
|
| 143 |
self.export_import(piecemeal_output_export) |
| 144 |
|
| 145 |
res = self.hot_wallet.transfer([dst], ring_size = 16, get_tx_key = False) |
| 146 |
assert len(res.tx_hash) == 32*2 |
| 147 |
txid = res.tx_hash |
| 148 |
assert len(res.tx_key) == 0 |
| 149 |
assert res.amount > 0 |
| 150 |
amount = res.amount |
| 151 |
assert res.fee > 0 |
| 152 |
fee = res.fee |
| 153 |
assert len(res.tx_blob) == 0 |
| 154 |
assert len(res.tx_metadata) == 0 |
| 155 |
assert len(res.multisig_txset) == 0 |
| 156 |
assert len(res.unsigned_txset) > 0 |
| 157 |
unsigned_txset = res.unsigned_txset |
| 158 |
|
| 159 |
print('Signing transaction with cold wallet') |
| 160 |
res = self.cold_wallet.describe_transfer(unsigned_txset = unsigned_txset) |
| 161 |
assert len(res.desc) == 1 |
| 162 |
desc = res.desc[0] |
| 163 |
assert desc.amount_in >= amount + fee |
| 164 |
assert desc.amount_out == desc.amount_in - fee |
| 165 |
assert desc.ring_size == 16 |
| 166 |
assert desc.unlock_time == 0 |
| 167 |
assert desc.payment_id in ['', '0000000000000000'] |
| 168 |
assert desc.change_amount == desc.amount_in - 1000000000000 - fee |
| 169 |
assert desc.change_address == STANDARD_ADDRESS |
| 170 |
assert desc.fee == fee |
| 171 |
assert len(desc.recipients) == 1 |
| 172 |
rec = desc.recipients[0] |
| 173 |
assert rec.address == destination_addr |
| 174 |
assert rec.amount == 1000000000000 |
| 175 |
|
| 176 |
res = self.cold_wallet.sign_transfer(unsigned_txset) |
| 177 |
assert len(res.signed_txset) > 0 |
| 178 |
signed_txset = res.signed_txset |
| 179 |
assert len(res.tx_hash_list) == 1 |
| 180 |
txid = res.tx_hash_list[0] |
| 181 |
assert len(txid) == 64 |
| 182 |
|
| 183 |
print('Submitting transaction with hot wallet') |
| 184 |
res = self.hot_wallet.submit_transfer(signed_txset) |
| 185 |
assert len(res.tx_hash_list) > 0 |
| 186 |
assert res.tx_hash_list[0] == txid |
| 187 |
|
| 188 |
res = self.hot_wallet.get_transfers() |
| 189 |
assert len([x for x in (res['pending'] if 'pending' in res else []) if x.txid == txid]) == 1 |
| 190 |
assert len([x for x in (res['out'] if 'out' in res else []) if x.txid == txid]) == 0 |
| 191 |
|
| 192 |
daemon.generateblocks(STANDARD_ADDRESS, 1) |
| 193 |
self.hot_wallet.refresh() |
| 194 |
|
| 195 |
res = self.hot_wallet.get_transfers() |
| 196 |
assert len([x for x in (res['pending'] if 'pending' in res else []) if x.txid == txid]) == 0 |
| 197 |
assert len([x for x in (res['out'] if 'out' in res else []) if x.txid == txid]) == 1 |
| 198 |
|
| 199 |
res = self.hot_wallet.get_tx_key(txid) |
| 200 |
assert len(res.tx_key) == 0 or res.tx_key == '01' + '0' * 62 |
| 201 |
res = self.cold_wallet.get_tx_key(txid) |
| 202 |
assert len(res.tx_key) == 64 |
| 203 |
|
| 204 |
self.export_import(piecemeal_output_export) |
| 205 |
|
| 206 |
def transfer(self, piecemeal_output_export): |
| 207 |
print("Creating transaction in hot wallet") |
| 208 |
self.create_tx(STANDARD_ADDRESS, piecemeal_output_export) |
| 209 |
|
| 210 |
res = self.cold_wallet.get_address() |
| 211 |
assert len(res['addresses']) == 1 |
| 212 |
assert res['addresses'][0].address == STANDARD_ADDRESS |
| 213 |
assert res['addresses'][0].used |
| 214 |
|
| 215 |
res = self.hot_wallet.get_address() |
| 216 |
assert len(res['addresses']) == 1 |
| 217 |
assert res['addresses'][0].address == STANDARD_ADDRESS |
| 218 |
assert res['addresses'][0].used |
| 219 |
|
| 220 |
def self_transfer_to_subaddress(self, piecemeal_output_export): |
| 221 |
print("Self-spending to subaddress in hot wallet") |
| 222 |
self.create_tx(SUBADDRESS, piecemeal_output_export) |
| 223 |
|
| 224 |
res = self.cold_wallet.get_address() |
| 225 |
assert len(res['addresses']) == 2 |
| 226 |
assert res['addresses'][0].address == STANDARD_ADDRESS |
| 227 |
assert res['addresses'][0].used |
| 228 |
assert res['addresses'][1].address == SUBADDRESS |
| 229 |
assert res['addresses'][1].used |
| 230 |
|
| 231 |
res = self.hot_wallet.get_address() |
| 232 |
assert len(res['addresses']) == 2 |
| 233 |
assert res['addresses'][0].address == STANDARD_ADDRESS |
| 234 |
assert res['addresses'][0].used |
| 235 |
assert res['addresses'][1].address == SUBADDRESS |
| 236 |
assert res['addresses'][1].used |
| 237 |
|
| 238 |
def transfer_after_empty_export_import(self): |
| 239 |
print("Creating transaction in hot wallet after empty export & import") |
| 240 |
start_len = len(self.hot_wallet.get_transfers()['in']) |
| 241 |
self.export_import(False) |
| 242 |
assert start_len == len(self.hot_wallet.get_transfers()['in']) |
| 243 |
self.create_tx(STANDARD_ADDRESS, False) |
| 244 |
assert start_len == len(self.hot_wallet.get_transfers()['in']) - 1 |
| 245 |
|
| 246 |
class Guard: |
| 247 |
def __enter__(self): |
| 248 |
for i in range(2): |
| 249 |
Wallet(idx = i).auto_refresh(False) |
| 250 |
def __exit__(self, exc_type, exc_value, traceback): |
| 251 |
for i in range(2): |
| 252 |
Wallet(idx = i).auto_refresh(True) |
| 253 |
|
| 254 |
if __name__ == '__main__': |
| 255 |
with Guard() as guard: |
| 256 |
cs = ColdSigningTest().run_test() |