aboutsummaryrefslogtreecommitdiff
path: root/tests/functional_tests/proofs.py
blob: fe734d6973af9d610d08adc86fa8397d2ec3829f (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3

# Copyright (c) 2019-2022, The Monero Project
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice, this list of
#    conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
#    of conditions and the following disclaimer in the documentation and/or other
#    materials provided with the distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its contributors may be
#    used to endorse or promote products derived from this software without specific
#    prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

"""Test misc proofs (tx key, send, receive, reserve)
"""

from framework.daemon import Daemon
from framework.wallet import Wallet

class ProofsTest():
    def run_test(self):
        self.reset()
        self.mine('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 80)
        self.create_wallets()
        txid, tx_key, amount = self.transfer()
        self.check_tx_key(txid, tx_key, amount)
        self.check_tx_proof(txid, amount)
        self.check_spend_proof(txid)
        self.check_reserve_proof()

    def reset(self):
        print('Resetting blockchain')
        daemon = Daemon()
        res = daemon.get_height()
        daemon.pop_blocks(res.height - 1)
        daemon.flush_txpool()

    def mine(self, address, blocks):
        print("Mining some blocks")
        daemon = Daemon()
        daemon.generateblocks(address, blocks)

    def transfer(self):
        print('Creating transaction')
        self.wallet[0].refresh()
        dst = {'address': '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW', 'amount':123456789000}
        res = self.wallet[0].transfer([dst], get_tx_key = True)
        assert len(res.tx_hash) == 64
        assert len(res.tx_key) == 64
        daemon = Daemon()
        daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1)
        return (res.tx_hash, res.tx_key, 123456789000)

    def create_wallets(self):
      print('Creating wallets')
      seeds = [
        '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',
        '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',
      ]
      self.wallet = [None, None]
      for i in range(2):
        self.wallet[i] = Wallet(idx = i)
        try: self.wallet[i].close_wallet()
        except: pass
        res = self.wallet[i].restore_deterministic_wallet(seed = seeds[i])

    def check_tx_key(self, txid, tx_key, amount):
        daemon = Daemon()

        print('Checking tx key')

        self.wallet[0].refresh()
        self.wallet[1].refresh()

        sending_address = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
        receiving_address = '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW'
        res = self.wallet[0].get_tx_key(txid)
        assert res.tx_key == tx_key
        res = self.wallet[0].check_tx_key(txid = txid, tx_key = tx_key, address = receiving_address)
        assert res.received == amount
        assert not res.in_pool
        assert res.confirmations == 1
        res = self.wallet[1].check_tx_key(txid = txid, tx_key = tx_key, address = receiving_address)
        assert res.received == amount
        assert not res.in_pool
        assert res.confirmations == 1

        self.wallet[1].check_tx_key(txid = txid, tx_key = tx_key, address = sending_address)
        assert res.received >= 0 # might be change
        assert not res.in_pool
        assert res.confirmations == 1

        ok = False
        try: self.wallet[1].check_tx_key(txid = '0' * 64, tx_key = tx_key, address = receiving_address)
        except: ok = True
        assert ok

        res = self.wallet[1].check_tx_key(txid = txid, tx_key = '0' * 64, address = receiving_address)
        assert res.received == 0
        assert not res.in_pool
        assert res.confirmations == 1

    def check_tx_proof(self, txid, amount):
        daemon = Daemon()

        print('Checking tx proof')

        self.wallet[0].refresh()
        self.wallet[1].refresh()

        sending_address = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
        receiving_address = '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW'
        res = self.wallet[0].get_tx_proof(txid, sending_address, 'foo');
        assert res.signature.startswith('InProofV2');
        signature0i = res.signature
        res = self.wallet[0].get_tx_proof(txid, receiving_address, 'bar');
        assert res.signature.startswith('OutProofV2');
        signature0o = res.signature
        res = self.wallet[1].get_tx_proof(txid, receiving_address, 'baz');
        assert res.signature.startswith('InProofV2');
        signature1 = res.signature

        res = self.wallet[0].check_tx_proof(txid, sending_address, 'foo', signature0i);
        assert res.good
        assert res.received > 0 # likely change
        assert not res.in_pool
        assert res.confirmations == 1

        ok = False
        try: res = self.wallet[0].check_tx_proof('0' * 64, sending_address, 'foo', signature0i);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, receiving_address, 'foo', signature0i);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, sending_address, '', signature0i);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, sending_address, 'foo', signature1);
        except: ok = True
        assert ok or not res.good


        res = self.wallet[0].check_tx_proof(txid, receiving_address, 'bar', signature0o);
        assert res.good
        assert res.received == amount
        assert not res.in_pool
        assert res.confirmations == 1

        ok = False
        try: res = self.wallet[0].check_tx_proof('0' * 64, receiving_address, 'bar', signature0o);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, sending_address, 'bar', signature0o);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, receiving_address, '', signature0o);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, receiving_address, 'bar', signature0i);
        except: ok = True
        assert ok or not res.good


        res = self.wallet[1].check_tx_proof(txid, receiving_address, 'baz', signature1);
        assert res.good
        assert res.received == amount
        assert not res.in_pool
        assert res.confirmations == 1

        ok = False
        try: res = self.wallet[1].check_tx_proof('0' * 64, receiving_address, 'baz', signature1);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[1].check_tx_proof(txid, sending_address, 'baz', signature1);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[1].check_tx_proof(txid, receiving_address, '', signature1);
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[1].check_tx_proof(txid, receiving_address, 'baz', signature0o);
        except: ok = True
        assert ok or not res.good

        
        # Test bad cross-version verification
        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, sending_address, 'foo', signature0i.replace('ProofV2','ProofV1'));
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[0].check_tx_proof(txid, receiving_address, 'bar', signature0o.replace('ProofV2','ProofV1'));
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[1].check_tx_proof(txid, receiving_address, 'baz', signature1.replace('ProofV2','ProofV1'));
        except: ok = True
        assert ok or not res.good

    def check_spend_proof(self, txid):
        daemon = Daemon()

        print('Checking spend proof')

        self.wallet[0].refresh()
        self.wallet[1].refresh()

        res = self.wallet[0].get_spend_proof(txid, message = 'foo')
        assert len(res.signature) > 0
        signature = res.signature
        res = self.wallet[1].check_spend_proof(txid, message = 'foo', signature = signature)
        assert res.good

        res = self.wallet[0].get_spend_proof(txid, message = 'foobar')
        assert len(res.signature) > 0
        signature2 = res.signature
        res = self.wallet[1].check_spend_proof(txid, message = 'foobar', signature = signature2)
        assert res.good

        ok = False
        try: res = self.wallet[1].check_spend_proof('0' * 64, message = 'foo', signature = signature)
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[1].check_spend_proof(txid, message = 'bar', signature = signature)
        except: ok = True
        assert ok or not res.good

        ok = False
        try: res = self.wallet[1].check_spend_proof(txid, message = 'foo', signature = signature2)
        except: ok = True
        assert ok or not res.good

    def check_reserve_proof(self):
        daemon = Daemon()

        print('Checking reserve proof')

        address0 = '42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm'
        address1 = '44Kbx4sJ7JDRDV5aAhLJzQCjDz2ViLRduE3ijDZu3osWKBjMGkV1XPk4pfDUMqt1Aiezvephdqm6YD19GKFD9ZcXVUTp6BW'

        self.wallet[0].refresh()
        res = self.wallet[0].get_balance()
        balance0 = res.balance
        self.wallet[1].refresh()
        res = self.wallet[1].get_balance()
        balance1 = res.balance

        res = self.wallet[0].get_reserve_proof(all_ = True, message = 'foo')
        assert res.signature.startswith('ReserveProofV2')
        signature = res.signature
        for i in range(2):
          res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature)
          assert res.good
          assert res.total == balance0

          ok = False
          try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'bar', signature = signature)
          except: ok = True
          assert ok or not res.good

          ok = False
          try: res = self.wallet[i].check_reserve_proof(address = address1, message = 'foo', signature = signature)
          except: ok = True
          assert ok or not res.good

          # Test bad cross-version verification
          ok = False
          try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature.replace('ProofV2','ProofV1'))
          except: ok = True
          assert ok or not res.good

        amount = int(balance0 / 10)
        res = self.wallet[0].get_reserve_proof(all_ = False, amount = amount, message = 'foo')
        assert res.signature.startswith('ReserveProofV2')
        signature = res.signature
        for i in range(2):
          res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature)
          assert res.good
          assert res.total >= amount and res.total <= balance0

          ok = False
          try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'bar', signature = signature)
          except: ok = True
          assert ok or not res.good

          ok = False
          try: res = self.wallet[i].check_reserve_proof(address = address1, message = 'foo', signature = signature)
          except: ok = True
          assert ok or not res.good

          # Test bad cross-version verification
          ok = False
          try: res = self.wallet[i].check_reserve_proof(address = address0, message = 'foo', signature = signature.replace('ProofV2','ProofV1'))
          except: ok = True
          assert ok or not res.good

        ok = False
        try: self.wallet[0].get_reserve_proof(all_ = False, amount = balance0 + 1, message = 'foo')
        except: ok = True
        assert ok


class Guard:
    def __enter__(self):
        for i in range(4):
            Wallet(idx = i).auto_refresh(False)
    def __exit__(self, exc_type, exc_value, traceback):
        for i in range(4):
            Wallet(idx = i).auto_refresh(True)

if __name__ == '__main__':
    with Guard() as guard:
        ProofsTest().run_test()