| 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 message signing/verification RPC calls |
| 34 |
|
| 35 |
Test the following RPCs: |
| 36 |
- sign |
| 37 |
- verify |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
from framework.wallet import Wallet |
| 42 |
|
| 43 |
class MessageSigningTest(): |
| 44 |
def run_test(self): |
| 45 |
self.create() |
| 46 |
self.check_signing(False, False) |
| 47 |
self.check_signing(False, True) |
| 48 |
self.check_signing(True, False) |
| 49 |
self.check_signing(True, True) |
| 50 |
|
| 51 |
def create(self): |
| 52 |
print('Creating wallets') |
| 53 |
seeds = [ |
| 54 |
'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', |
| 55 |
'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', |
| 56 |
] |
| 57 |
self.address = [ |
| 58 |
'42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', |
| 59 |
'44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW', |
| 60 |
] |
| 61 |
self.wallet = [None, None] |
| 62 |
for i in range(2): |
| 63 |
self.wallet[i] = Wallet(idx = i) |
| 64 |
|
| 65 |
try: self.wallet[i].close_wallet() |
| 66 |
except: pass |
| 67 |
res = self.wallet[i].restore_deterministic_wallet(seed = seeds[i]) |
| 68 |
assert res.address == self.address[i] |
| 69 |
assert res.seed == seeds[i] |
| 70 |
|
| 71 |
def check_signing(self, subaddress, spend_key): |
| 72 |
print('Signing/verifing messages with ' + ('subaddress' if subaddress else 'standard address') + ' ' + ('spend key' if spend_key else 'view key')) |
| 73 |
messages = ['foo', ''] |
| 74 |
if subaddress: |
| 75 |
address = [] |
| 76 |
for i in range(2): |
| 77 |
res = self.wallet[i].create_account() |
| 78 |
if i == 0: |
| 79 |
account_index = res.account_index |
| 80 |
res = self.wallet[i].create_address(account_index = account_index) |
| 81 |
if i == 0: |
| 82 |
address_index = res.address_index |
| 83 |
address.append(res.address) |
| 84 |
else: |
| 85 |
address = [self.address[0], self.address[1]] |
| 86 |
account_index = 0 |
| 87 |
address_index = 0 |
| 88 |
for message in messages: |
| 89 |
res = self.wallet[0].sign(message, account_index = account_index, address_index = address_index, signature_type = 'spend' if spend_key else 'view') |
| 90 |
signature = res.signature |
| 91 |
for i in range(2): |
| 92 |
res = self.wallet[i].verify(message, address[0], signature) |
| 93 |
assert res.good |
| 94 |
assert not res.old |
| 95 |
assert res.version == 2 |
| 96 |
assert res.signature_type == 'spend' if spend_key else 'view' |
| 97 |
res = self.wallet[i].verify('different', address[0], signature) |
| 98 |
assert not res.good |
| 99 |
res = self.wallet[i].verify(message, address[1], signature) |
| 100 |
assert not res.good |
| 101 |
res = self.wallet[i].verify(message, address[0], signature + 'x') |
| 102 |
assert not res.good |
| 103 |
res = self.wallet[i].verify(message, address[0], signature.replace('SigV2','SigV1')) |
| 104 |
assert not res.good |
| 105 |
|
| 106 |
if __name__ == '__main__': |
| 107 |
MessageSigningTest().run_test() |