Debian Codegraph @

tests/functional_tests/sign_message.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Copyright (c) 2019-2022, The Monero Project
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without modification, are
8 # permitted provided that the following conditions are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright notice, this list of
11 # conditions and the following disclaimer.
12 #
13 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
14 # of conditions and the following disclaimer in the documentation and/or other
15 # materials provided with the distribution.
16 #
17 # 3. Neither the name of the copyright holder nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without specific
19 # prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
22 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 # close the wallet if any, will throw if none is loaded
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()