44
55from web3 import Web3
66from eth_keys import keys
7+ from eth_keys .datatypes import PublicKey
8+ from hexbytes import HexBytes
9+ import eth_keys
10+ from sdk .celo_account .messages import encode_defunct
711
812from sdk .kit import Kit
913from sdk .tests import test_data
14+ from sdk .utils import utils
15+ from sdk .celo_account ._utils import signing
1016
1117
1218class TestAccountsWrapper (unittest .TestCase ):
@@ -15,11 +21,12 @@ class TestAccountsWrapper(unittest.TestCase):
1521 def setUpClass (self ):
1622 # https://alfajores-forno.celo-testnet.org
1723 # http://localhost:8545
18- self .kit = Kit ('https ://alfajores-forno.celo-testnet.org ' )
24+ self .kit = Kit ('http ://localhost:8544 ' )
1925 self .accounts_wrapper = self .kit .base_wrapper .create_and_get_contract_by_name (
2026 'Accounts' )
21- self .kit .wallet_add_new_key = test_data .pk1
22- self .kit .wallet_add_new_key = test_data .pk2
27+ self .kit .wallet .sign_with_provider = True
28+ for _ , v in test_data .deriv_pks .items ():
29+ self .kit .wallet_add_new_key = v
2330
2431 self .validators_contract = self .kit .base_wrapper .create_and_get_contract_by_name (
2532 'Validators' )
@@ -30,99 +37,198 @@ def setUpClass(self):
3037
3138 self .bls_public_key = '0x4fa3f67fc913878b068d1fa1cdddc54913d3bf988dbe5a36a20fa888f20d4894c408a6773f3d7bde11154f2a3076b700d345a42fd25a0e5e83f4db5586ac7979ac2053cd95d8f2efd3e959571ceccaa743e02cf4be3f5d7aaddb0b06fc9aff00'
3239 self .bls_pop = '0xcdb77255037eb68897cd487fdd85388cbda448f617f874449d4b11588b0b7ad8ddc20d9bb450b513bb35664ea3923900'
40+
41+ def test_create_acc (self ):
42+ accounts = self .kit .w3 .eth .accounts
43+ self .kit .wallet_change_account = accounts [1 ]
44+ self .kit .wallet .sign_with_provider = True
45+ print (self .accounts_wrapper .create_account ())
3346
47+ def test_eth_sign (self ):
48+ accounts = self .kit .w3 .eth .accounts
49+
50+ account = accounts [0 ]
51+ signer = accounts [1 ]
52+
53+ self .kit .w3 .eth .defaultAccount = signer
54+ self .kit .wallet_change_account = signer
55+
56+ signature = self .kit .w3 .eth .sign (account = signer , text = '0x8857ae6cb26d382cac66a53711ede22129df1628ab102efa2e4707c9a92cf123' )
57+ print (f"Signature:\n { signature .hex ()} " )
58+
59+ def test_contract_verify_signature (self ):
60+ accounts = self .kit .w3 .eth .accounts
61+
62+ account = accounts [0 ]
63+ signer = accounts [1 ]
64+
65+ self .kit .w3 .eth .defaultAccount = signer
66+ self .kit .wallet_change_account = signer
67+ print (f"Signer: { signer } " )
68+
69+ message = self .kit .w3 .soliditySha3 (['address' ], [signer ]).hex ()
70+ message = encode_defunct (text = message )
71+ print (f"Message { message .body } " )
72+ signature = self .kit .wallet .sign_message (message .body )
73+ print (f"signatures of message: { signature } " )
74+ print (f"Result: { self .accounts_wrapper ._contract .functions .getSigner (signer , 27 , HexBytes (signature .r ), HexBytes (signature .s )).call ()} " )
75+
76+ def test_hash_address_with_contract (self ):
77+ accounts = self .kit .w3 .eth .accounts
78+
79+ account = accounts [0 ]
80+ signer = accounts [1 ]
81+
82+ self .kit .w3 .eth .defaultAccount = signer
83+ self .kit .wallet_change_account = signer
84+
85+ print (f"Hash: { HexBytes (self .accounts_wrapper ._contract .functions .getHashAddress (signer ).call ()).hex ()} " )
86+
87+ def test_pub_key_recovering (self ):
88+ accounts = self .kit .w3 .eth .accounts
89+
90+ account = accounts [0 ]
91+ signer = accounts [1 ]
92+
93+ self .kit .w3 .eth .defaultAccount = signer
94+ self .kit .wallet_change_account = signer
95+
96+ message = self .kit .w3 .soliditySha3 (['address' ], [signer ]).hex ()
97+ message = encode_defunct (text = message )
98+ signature = self .kit .wallet .active_account .sign_message (message )
99+ print (f"Signature: { signature } " )
100+
101+ pub_key = PublicKey .recover_from_msg_hash (message .body , signature ).to_hex ()
102+ print (f"Signer pub key: { pub_key } " )
103+
104+ def test_recover_with_eth_keys (self ):
105+ accounts = self .kit .w3 .eth .accounts
106+
107+ account = accounts [0 ]
108+ signer = accounts [1 ]
109+
110+ self .kit .w3 .eth .defaultAccount = signer
111+ self .kit .wallet_change_account = signer
112+
113+ signerPrivKey = eth_keys .keys .PrivateKey (HexBytes ('0x5d862464fe9303452126c8bc94274b8c5f9874cbd219789b3eb2128075a76f72' ))
114+ print (f"address: { signerPrivKey .public_key .to_checksum_address ()} " )
115+ message = self .kit .w3 .soliditySha3 (['address' ], [signer ]).hex ()
116+ message = encode_defunct (text = message )
117+ signature = signerPrivKey .sign_msg (message .body )
118+
119+ recoveredPubKey = signature .recover_public_key_from_msg (message .body )
120+ print (f"Recovered pub key: { recoveredPubKey } " )
121+
122+ signerPubKey = signerPrivKey .public_key
123+ print (f"Signare actual pub key: { signerPubKey } " )
124+
34125 def test_authorize_validator_key_not_validator (self ):
35- accounts = list ( self .kit .wallet . accounts . values ())[ 1 :]
126+ accounts = self .kit .w3 . eth . accounts
36127
37128 account = accounts [0 ]
38129 signer = accounts [1 ]
39130
40- self .kit .wallet_change_account = account .address
131+ self .kit .w3 .eth .defaultAccount = account
132+ self .kit .wallet_change_account = account
41133
42134 self .accounts_wrapper .create_account ()
43135
44- sig = self .get_parsed_signature_of_address (account . address , signer . address )
136+ sig = self .get_parsed_signature_of_address (account , signer )
45137
46- print (self .accounts_wrapper .authorize_validator_signer (signer . address , sig ))
138+ print (self .accounts_wrapper .authorize_validator_signer (signer , sig ))
47139
48- # def test_authorize_validator_key_validator(self):
49- # accounts = list( self.kit.wallet.accounts.values())[1:]
140+ def test_authorize_validator_key_validator (self ):
141+ accounts = self .kit .w3 . eth . accounts
50142
51- # account = accounts[0]
52- # signer = accounts[1]
143+ account = accounts [0 ]
144+ signer = accounts [1 ]
53145
54- # self.kit.wallet_change_account = account.address
146+ self .kit .wallet_change_account = account
147+ self .kit .w3 .eth .defaultAccount = account
55148
56- # self.accounts_wrapper.create_account()
149+ self .accounts_wrapper .create_account ()
57150
58- # self.setup_validator(account.address )
151+ self .setup_validator (account )
59152
60- # sig = self.get_parsed_signature_of_address(account.address, signer.address)
153+ self .kit .wallet_change_account = signer
154+ self .kit .w3 .eth .defaultAccount = signer
61155
62- # print( self.accounts_wrapper.authorize_validator_signer(signer.address, sig) )
156+ sig = self .get_parsed_signature_of_address ( account , signer )
63157
64- # def test_authorize_validator_key_change_bls_key(self):
65- # hex_characters = '0123456789abcdef'
66- # hex_sample = [random.choice(hex_characters) for _ in range(96)]
67- # new_bls_public_key = '0x'+''.join(hex_sample)
158+ self .kit .wallet_change_account = account
159+ self .kit .w3 .eth .defaultAccount = account
68160
69- # hex_sample = [random.choice(hex_characters) for _ in range(48)]
70- # new_bls_pop = '0x'+''.join(hex_sample)
161+ print (self .accounts_wrapper .authorize_validator_signer (signer .address , sig ))
71162
72- # accounts = list(self.kit.wallet.accounts.values())[1:]
163+ def test_authorize_validator_key_change_bls_key (self ):
164+ hex_characters = '0123456789abcdef'
165+ hex_sample = [random .choice (hex_characters ) for _ in range (96 )]
166+ new_bls_public_key = '0x' + '' .join (hex_sample )
73167
74- # account = accounts[0 ]
75- # signer = accounts[1]
168+ hex_sample = [ random . choice ( hex_characters ) for _ in range ( 48 ) ]
169+ new_bls_pop = '0x' + '' . join ( hex_sample )
76170
77- # self.accounts_wrapper.create_account()
171+ accounts = list ( self .kit . wallet . accounts . values ())[ 1 :]
78172
79- # self.setup_validator(account.address)
173+ account = accounts [0 ]
174+ signer = accounts [1 ]
80175
81- # sig = self.get_parsed_signature_of_address(account.address, signer.address )
176+ self .accounts_wrapper . create_account ( )
82177
83- # print( self.accounts_wrapper.authorize_validator_signer_and_bls(signer .address, sig, new_bls_public_key, new_bls_pop) )
178+ self .setup_validator ( account .address )
84179
85- # def test_set_wallet_address_to_caller(self):
86- # accounts = list(self.kit.wallet.accounts.values())[1:]
180+ sig = self .get_parsed_signature_of_address (account .address , signer .address )
87181
88- # self.accounts_wrapper.create_account()
89- # print(self.accounts_wrapper.set_wallet_address(accounts[0].address))
182+ print (self .accounts_wrapper .authorize_validator_signer_and_bls (signer .address , sig , new_bls_public_key , new_bls_pop ))
90183
91- # def test_set_wallet_address_to_different_address (self):
92- # accounts = list(self.kit.wallet.accounts.values())[1:]
184+ def test_set_wallet_address_to_caller (self ):
185+ accounts = list (self .kit .wallet .accounts .values ())[1 :]
93186
94- # account = accounts[0]
95- # signer = accounts[1]
187+ self .accounts_wrapper .create_account ()
188+ print (self .accounts_wrapper .set_wallet_address (accounts [0 ].address ))
189+
190+ def test_set_wallet_address_to_different_address (self ):
191+ accounts = list (self .kit .wallet .accounts .values ())[1 :]
192+
193+ account = accounts [0 ]
194+ signer = accounts [1 ]
96195
97- # self.accounts_wrapper.create_account()
196+ self .accounts_wrapper .create_account ()
98197
99- # signature = self.accounts_wrapper.generate_proof_of_key_possession(account.address, signer.address)
198+ signature = self .accounts_wrapper .generate_proof_of_key_possession (account .address , signer .address )
100199
101- # print(self.accounts_wrapper.set_wallet_address(signer.address, signature))
200+ print (self .accounts_wrapper .set_wallet_address (signer .address , signature ))
102201
103- # def test_set_wallet_address_without_signature(self):
104- # """
105- # Should fail
106- # """
107- # accounts = list(self.kit.wallet.accounts.values())[1:]
108- # print(self.accounts_wrapper.set_wallet_address(accounts[1].address))
202+ def test_set_wallet_address_without_signature (self ):
203+ """
204+ Should fail
205+ """
206+ accounts = list (self .kit .wallet .accounts .values ())[1 :]
207+ print (self .accounts_wrapper .set_wallet_address (accounts [1 ].address ))
109208
110- # def register_account_with_locked_gold(self, account: str):
111- # if not self.accounts_wrapper.is_account(account):
112- # _ = self.accounts_wrapper.create_account({'from': account})
113- # _ = self.locked_gold_contract.lock(
114- # {'from': account, 'value': self.min_locked_gold_value})
209+ def register_account_with_locked_gold (self , account : str ):
210+ if not self .accounts_wrapper .is_account (account ):
211+ _ = self .accounts_wrapper .create_account ({'from' : account })
212+ _ = self .locked_gold_contract .lock (
213+ {'from' : account , 'value' : self .min_locked_gold_value })
115214
116215 def get_parsed_signature_of_address (self , address : str , signer : 'Account object' ) -> 'Signature object' :
216+ self .kit .w3 .eth .defaultAccount = signer
217+ self .kit .wallet_change_account = signer
218+
117219 message = self .kit .w3 .soliditySha3 (['address' ], [address ]).hex ()
118- signature = self .kit .wallet .active_account .signHash (message )
220+ message = encode_defunct (text = message )
221+ signature = self .kit .wallet .active_account .sign_message (message )
222+
223+ self .kit .w3 .eth .defaultAccount = address
224+ self .kit .wallet_change_account = address
119225 return signature
120226
121- # def setup_validator(self, validator_account: str):
122- # """
123- # validator_account should be an address of active account in wallet now
124- # """
125- # self.register_account_with_locked_gold(validator_account)
126- # priv_key = keys.PrivateKey(self.kit.wallet.active_account.privateKey)
127- # pub_key = priv_key.public_key
128- # _ = self.validators_contract.register_validator(pub_key, self.bls_public_key, self.bls_pop)
227+ def setup_validator (self , validator_account : str ):
228+ """
229+ validator_account should be an address of active account in wallet now
230+ """
231+ self .register_account_with_locked_gold (validator_account )
232+ priv_key = keys .PrivateKey (self .kit .wallet .active_account .privateKey )
233+ pub_key = priv_key .public_key
234+ _ = self .validators_contract .register_validator (pub_key , self .bls_public_key , self .bls_pop )
0 commit comments