| 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 |
from __future__ import print_function |
| 32 |
|
| 33 |
"""Test misc proofs (tx key, send, receive, reserve) |
| 34 |
""" |
| 35 |
|
| 36 |
from framework.daemon import Daemon |
| 37 |
from framework.wallet import Wallet |
| 38 |
|
| 39 |
class ProofsTest(): |
| 40 |
def run_test(self): |
| 41 |
self.reset() |
| 42 |
self.mine('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 80) |
| 43 |
self.create_wallets() |
| 44 |
txid, tx_key, amount = self.transfer() |
| 45 |
self.check_tx_key(txid, tx_key, amount) |
| 46 |
self.check_tx_proof(txid, amount) |
| 47 |
self.check_spend_proof(txid) |
| 48 |
self.check_reserve_proof() |
| 49 |
|
| 50 |
def reset(self): |
| 51 |
print('Resetting blockchain') |
| 52 |
daemon = Daemon() |
| 53 |
res = daemon.get_height() |
| 54 |
daemon.pop_blocks(res.height - 1) |
| 55 |
daemon.flush_txpool() |
| 56 |
|
| 57 |
def mine(self, address, blocks): |
| 58 |
print("Mining some blocks") |
| 59 |
daemon = Daemon() |
| 60 |
daemon.generateblocks(address, blocks) |
| 61 |
|
| 62 |
def transfer(self): |
| 63 |
print('Creating transaction') |
| 64 |
self.wallet[0].refresh() |
| 65 |
dst = {'address': '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW', 'amount':123456789000} |
| 66 |
res = self.wallet[0].transfer([dst], get_tx_key = True) |
| 67 |
assert len(res.tx_hash) == 64 |
| 68 |
assert len(res.tx_key) == 64 |
| 69 |
daemon = Daemon() |
| 70 |
daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1) |
| 71 |
return (res.tx_hash, res.tx_key, 123456789000) |
| 72 |
|
| 73 |
def create_wallets(self): |
| 74 |
print('Creating wallets') |
| 75 |
seeds = [ |
| 76 |
'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', |
| 77 |
'peeled mixture ionic radar utopia puddle buying illness nuns gadget river spout cavernous bounced paradise drunk looking cottage jump tequila melting went winter adjust spout', |
| 78 |
] |
| 79 |
self.wallet = [None, None] |
| 80 |
for i in range(2): |
| 81 |
self.wallet[i] = Wallet(idx = i) |
| 82 |
try: self.wallet[i].close_wallet() |
| 83 |
except: pass |
| 84 |
res = self.wallet[i].restore_deterministic_wallet(seed = seeds[i]) |
| 85 |
|
| 86 |
def check_tx_key(self, txid, tx_key, amount): |
| 87 |
daemon = Daemon() |
| 88 |
|
| 89 |
print('Checking tx key') |
| 90 |
|
| 91 |
self.wallet[0].refresh() |
| 92 |
self.wallet[1].refresh() |
| 93 |
|
| 94 |
sending_address = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' |
| 95 |
receiving_address = '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW' |
| 96 |
res = self.wallet[0].get_tx_key(txid) |
| 97 |
assert res.tx_key == tx_key |
| 98 |
res = self.wallet[0].check_tx_key(txid = txid, tx_key = tx_key, address = receiving_address) |
| 99 |
assert res.received == amount |
| 100 |
assert not res.in_pool |
| 101 |
assert res.confirmations == 1 |
| 102 |
res = self.wallet[1].check_tx_key(txid = txid, tx_key = tx_key, address = receiving_address) |
| 103 |
assert res.received == amount |
| 104 |
assert not res.in_pool |
| 105 |
assert res.confirmations == 1 |
| 106 |
|
| 107 |
self.wallet[1].check_tx_key(txid = txid, tx_key = tx_key, address = sending_address) |
| 108 |
assert res.received >= 0 |
| 109 |
assert not res.in_pool |
| 110 |
assert res.confirmations == 1 |
| 111 |
|
| 112 |
ok = False |
| 113 |
try: self.wallet[1].check_tx_key(txid = '0' * 64, tx_key = tx_key, address = receiving_address) |
| 114 |
except: ok = True |
| 115 |
assert ok |
| 116 |
|
| 117 |
res = self.wallet[1].check_tx_key(txid = txid, tx_key = '0' * 64, address = receiving_address) |
| 118 |
assert res.received == 0 |
| 119 |
assert not res.in_pool |
| 120 |
assert res.confirmations == 1 |
| 121 |
|
| 122 |
def check_tx_proof(self, txid, amount): |
| 123 |
daemon = Daemon() |
| 124 |
|
| 125 |
print('Checking tx proof') |
| 126 |
|
| 127 |
self.wallet[0].refresh() |
| 128 |
self.wallet[1].refresh() |
| 129 |
|
| 130 |
sending_address = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' |
| 131 |
receiving_address = '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW' |
| 132 |
res = self.wallet[0].get_tx_proof(txid, sending_address, 'foo'); |
| 133 |
assert res.signature.startswith('InProofV2'); |
| 134 |
signature0i = res.signature |
| 135 |
res = self.wallet[0].get_tx_proof(txid, receiving_address, 'bar'); |
| 136 |
assert res.signature.startswith('OutProofV2'); |
| 137 |
signature0o = res.signature |
| 138 |
res = self.wallet[1].get_tx_proof(txid, receiving_address, 'baz'); |
| 139 |
assert res.signature.startswith('InProofV2'); |
| 140 |
signature1 = res.signature |
| 141 |
|
| 142 |
res = self.wallet[0].check_tx_proof(txid, sending_address, 'foo', signature0i); |
| 143 |
assert res.good |
| 144 |
assert res.received > 0 |
| 145 |
assert not res.in_pool |
| 146 |
assert res.confirmations == 1 |
| 147 |
|
| 148 |
ok = False |
| 149 |
try: res = self.wallet[0].check_tx_proof('0' * 64, sending_address, 'foo', signature0i); |
| 150 |
except: ok = True |
| 151 |
assert ok or not res.good |
| 152 |
|
| 153 |
ok = False |
| 154 |
try: res = self.wallet[0].check_tx_proof(txid, receiving_address, 'foo', signature0i); |
| 155 |
except: ok = True |
| 156 |
assert ok or not res.good |
| 157 |
|
| 158 |
ok = False |
| 159 |
try: res = self.wallet[0].check_tx_proof(txid, sending_address, '', signature0i); |
| 160 |
except: ok = True |
| 161 |
assert ok or not res.good |
| 162 |
|
| 163 |
ok = False |
| 164 |
try: res = self.wallet[0].check_tx_proof(txid, sending_address, 'foo', signature1); |
| 165 |
except: ok = True |
| 166 |
assert ok or not res.good |
| 167 |
|
| 168 |
|
| 169 |
res = self.wallet[0].check_tx_proof(txid, receiving_address, 'bar', signature0o); |
| 170 |
assert res.good |
| 171 |
assert res.received == amount |
| 172 |
assert not res.in_pool |
| 173 |
assert res.confirmations == 1 |
| 174 |
|
| 175 |
ok = False |
| 176 |
try: res = self.wallet[0].check_tx_proof('0' * 64, receiving_address, 'bar', signature0o); |
| 177 |
except: ok = True |
| 178 |
assert ok or not res.good |
| 179 |
|
| 180 |
ok = False |
| 181 |
try: res = self.wallet[0].check_tx_proof(txid, sending_address, 'bar', signature0o); |
| 182 |
except: ok = True |
| 183 |
assert ok or not res.good |
| 184 |
|
| 185 |
ok = False |
| 186 |
try: res = self.wallet[0].check_tx_proof(txid, receiving_address, '', signature0o); |
| 187 |
except: ok = True |
| 188 |
assert ok or not res.good |
| 189 |
|
| 190 |
ok = False |
| 191 |
try: res = self.wallet[0].check_tx_proof(txid, receiving_address, 'bar', signature0i); |
| 192 |
except: ok = True |
| 193 |
assert ok or not res.good |
| 194 |
|
| 195 |
|
| 196 |
res = self.wallet[1].check_tx_proof(txid, receiving_address, 'baz', signature1); |
| 197 |
assert res.good |
| 198 |
assert res.received == amount |
| 199 |
assert not res.in_pool |
| 200 |
assert res.confirmations == 1 |
| 201 |
|
| 202 |
ok = False |
| 203 |
try: res = self.wallet[1].check_tx_proof('0' * 64, receiving_address, 'baz', signature1); |
| 204 |
except: ok = True |
| 205 |
assert ok or not res.good |
| 206 |
|
| 207 |
ok = False |
| 208 |
try: res = self.wallet[1].check_tx_proof(txid, sending_address, 'baz', signature1); |
| 209 |
except: ok = True |
| 210 |
assert ok or not res.good |
| 211 |
|
| 212 |
ok = False |
| 213 |
try: res = self.wallet[1].check_tx_proof(txid, receiving_address, '', signature1); |
| 214 |
except: ok = True |
| 215 |
assert ok or not res.good |
| 216 |
|
| 217 |
ok = False |
| 218 |
try: res = self.wallet[1].check_tx_proof(txid, receiving_address, 'baz', signature0o); |
| 219 |
except: ok = True |
| 220 |
assert ok or not res.good |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
ok = False |
| 225 |
try: res = self.wallet[0].check_tx_proof(txid, sending_address, 'foo', signature0i.replace('ProofV2','ProofV1')); |
| 226 |
except: ok = True |
| 227 |
assert ok or not res.good |
| 228 |
|
| 229 |
ok = False |
| 230 |
try: res = self.wallet[0].check_tx_proof(txid, receiving_address, 'bar', signature0o.replace('ProofV2','ProofV1')); |
| 231 |
except: ok = True |
| 232 |
assert ok or not res.good |
| 233 |
|
| 234 |
ok = False |
| 235 |
try: res = self.wallet[1].check_tx_proof(txid, receiving_address, 'baz', signature1.replace('ProofV2','ProofV1')); |
| 236 |
except: ok = True |
| 237 |
assert ok or not res.good |
| 238 |
|
| 239 |
def check_spend_proof(self, txid): |
| 240 |
daemon = Daemon() |
| 241 |
|
| 242 |
print('Checking spend proof') |
| 243 |
|
| 244 |
self.wallet[0].refresh() |
| 245 |
self.wallet[1].refresh() |
| 246 |
|
| 247 |
res = self.wallet[0].get_spend_proof(txid, message = 'foo') |
| 248 |
assert len(res.signature) > 0 |
| 249 |
signature = res.signature |
| 250 |
res = self.wallet[1].check_spend_proof(txid, message = 'foo', signature = signature) |
| 251 |
assert res.good |
| 252 |
|
| 253 |
res = self.wallet[0].get_spend_proof(txid, message = 'foobar') |
| 254 |
assert len(res.signature) > 0 |
| 255 |
signature2 = res.signature |
| 256 |
res = self.wallet[1].check_spend_proof(txid, message = 'foobar', signature = signature2) |
| 257 |
assert res.good |
| 258 |
|
| 259 |
ok = False |
| 260 |
try: res = self.wallet[1].check_spend_proof('0' * 64, message = 'foo', signature = signature) |
| 261 |
except: ok = True |
| 262 |
assert ok or not res.good |
| 263 |
|
| 264 |
ok = False |
| 265 |
try: res = self.wallet[1].check_spend_proof(txid, message = 'bar', signature = signature) |
| 266 |
except: ok = True |
| 267 |
assert ok or not res.good |
| 268 |
|
| 269 |
ok = False |
| 270 |
try: res = self.wallet[1].check_spend_proof(txid, message = 'foo', signature = signature2) |
| 271 |
except: ok = True |
| 272 |
assert ok or not res.good |
| 273 |
|
| 274 |
def check_reserve_proof(self): |
| 275 |
daemon = Daemon() |
| 276 |
|
| 277 |
print('Checking reserve proof') |
| 278 |
|
| 279 |
address0 = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm' |
| 280 |
address1 = '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW' |
| 281 |
|
| 282 |
self.wallet[0].refresh() |
| 283 |
res = self.wallet[0].get_balance() |
| 284 |
balance0 = res.balance |
| 285 |
self.wallet[1].refresh() |
| 286 |
res = self.wallet[1].get_balance() |
| 287 |
balance1 = res.balance |
| 288 |
|
| 289 |
res = self.wallet[0].get_reserve_proof(all_ = True, message = 'foo') |
| 290 |
assert res.signature.startswith('ReserveProofV2') |
| 291 |
signature = res.signature |
| 292 |
for i in range(2): |
| 293 |
res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature) |
| 294 |
assert res.good |
| 295 |
assert res.total == balance0 |
| 296 |
|
| 297 |
ok = False |
| 298 |
try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'bar', signature = signature) |
| 299 |
except: ok = True |
| 300 |
assert ok or not res.good |
| 301 |
|
| 302 |
ok = False |
| 303 |
try: res = self.wallet[i].check_reserve_proof(address = address1, message = 'foo', signature = signature) |
| 304 |
except: ok = True |
| 305 |
assert ok or not res.good |
| 306 |
|
| 307 |
|
| 308 |
ok = False |
| 309 |
try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature.replace('ProofV2','ProofV1')) |
| 310 |
except: ok = True |
| 311 |
assert ok or not res.good |
| 312 |
|
| 313 |
amount = int(balance0 / 10) |
| 314 |
res = self.wallet[0].get_reserve_proof(all_ = False, amount = amount, message = 'foo') |
| 315 |
assert res.signature.startswith('ReserveProofV2') |
| 316 |
signature = res.signature |
| 317 |
for i in range(2): |
| 318 |
res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature) |
| 319 |
assert res.good |
| 320 |
assert res.total >= amount and res.total <= balance0 |
| 321 |
|
| 322 |
ok = False |
| 323 |
try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'bar', signature = signature) |
| 324 |
except: ok = True |
| 325 |
assert ok or not res.good |
| 326 |
|
| 327 |
ok = False |
| 328 |
try: res = self.wallet[i].check_reserve_proof(address = address1, message = 'foo', signature = signature) |
| 329 |
except: ok = True |
| 330 |
assert ok or not res.good |
| 331 |
|
| 332 |
|
| 333 |
ok = False |
| 334 |
try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature.replace('ProofV2','ProofV1')) |
| 335 |
except: ok = True |
| 336 |
assert ok or not res.good |
| 337 |
|
| 338 |
ok = False |
| 339 |
try: self.wallet[0].get_reserve_proof(all_ = False, amount = balance0 + 1, message = 'foo') |
| 340 |
except: ok = True |
| 341 |
assert ok |
| 342 |
|
| 343 |
|
| 344 |
class Guard: |
| 345 |
def __enter__(self): |
| 346 |
for i in range(4): |
| 347 |
Wallet(idx = i).auto_refresh(False) |
| 348 |
def __exit__(self, exc_type, exc_value, traceback): |
| 349 |
for i in range(4): |
| 350 |
Wallet(idx = i).auto_refresh(True) |
| 351 |
|
| 352 |
if __name__ == '__main__': |
| 353 |
with Guard() as guard: |
| 354 |
ProofsTest().run_test() |