66from sdk .celo_account .datastructures import SignedTransaction
77from hexbytes import HexBytes
88from web3 import Web3
9+ import eth_keys
10+ from sdk .utils import hash_utils
911
1012
1113class Wallet :
@@ -27,7 +29,9 @@ def __init__(self, web3: Web3, priv_key: bytes, gas_price_contract: "GasPriceCon
2729 self .__accounts = {}
2830 acc = Account ()
2931 self .active_account = acc .from_key (priv_key )
30- self .__accounts .update ({self .active_account .address : self .active_account })
32+ self .sign_with_provider = False
33+ self .__accounts .update (
34+ {self .active_account .address : self .active_account })
3135 self ._gas_price_contract = gas_price_contract
3236 self ._fee_currency = None
3337 self ._gateway_fee_recipient = None
@@ -56,15 +60,15 @@ def gas_price(self) -> int:
5660 @property
5761 def gas (self ) -> int :
5862 return self ._gas
59-
63+
6064 @property
6165 def threshold_gas_value (self ) -> int :
6266 return self ._threshold_gas_value
6367
6468 @property
6569 def accounts (self ) -> dict :
6670 return self .__accounts
67-
71+
6872 @property
6973 def gas_price_contract (self ) -> 'GasPriceMinimum' :
7074 return self ._gas_price_contract
@@ -98,7 +102,7 @@ def gas(self, new_gas: int):
98102 if type (new_gas ) != int :
99103 raise TypeError ("Incorrect new gas type data" )
100104 self ._gas = new_gas
101-
105+
102106 @threshold_gas_value .setter
103107 def threshold_gas_value (self , new_threshold_gas_value : int ):
104108 if type (new_threshold_gas_value ) != int :
@@ -111,22 +115,24 @@ def accounts(self, new_acc: Account):
111115 raise TypeError ("Incorrect new account type" )
112116 self .__accounts .update ({new_acc .address : new_acc })
113117 self .active_account = new_acc
114-
118+
115119 @gas_price_contract .setter
116120 def gas_price_contract (self , gas_price_wrapper : 'GasPriceMinimum' ):
117121 self ._gas_price_contract = gas_price_wrapper
118122
119123 def add_new_key (self , priv_key : bytes ):
120124 acc = Account ()
121125 self .active_account = acc .from_key (priv_key )
122- self .__accounts .update ({self .active_account .address : self .active_account })
126+ self .__accounts .update (
127+ {self .active_account .address : self .active_account })
123128
124129 def remove_account (self , account_address : str ):
125130 del self .__accounts [account_address ]
126131
127132 def change_account (self , account_address : str ):
128133 if account_address not in self .__accounts :
129- raise KeyError ("There is no account with such an address in wallet" )
134+ raise KeyError (
135+ "There is no account with such an address in wallet" )
130136 self .active_account = self .__accounts [account_address ]
131137
132138 def construct_transaction (self , contract_method : web3 ._utils .datatypes , parameters : dict = None ) -> dict :
@@ -144,17 +150,18 @@ def construct_transaction(self, contract_method: web3._utils.datatypes, paramete
144150 raise ValueError (
145151 "Can't construct transaction without fee currency, set fee currency please" )
146152
147- nonce = self .web3 .eth .getTransactionCount (self .active_account .address )
153+ nonce = self .web3 .eth .getTransactionCount (
154+ self .active_account .address )
148155 gas_price = self ._gas_price if self ._gas_price else self .get_network_gas_price ()
149156 base_rows = {'nonce' : nonce , 'gasPrice' : gas_price , 'gas' : self ._gas ,
150- 'feeCurrency' : self ._fee_currency , 'from' : self .active_account .address }
157+ 'feeCurrency' : self ._fee_currency , 'from' : self .active_account .address }
151158
152159 if self ._gateway_fee_recipient :
153160 base_rows ['gatewayFeeRecipient' ] = self ._gateway_fee_recipient
154161
155162 if self ._gateway_fee :
156163 base_rows ['gatewayFee' ] = self ._gateway_fee
157-
164+
158165 if parameters :
159166 for k , item in parameters .items ():
160167 base_rows [k ] = item
@@ -166,36 +173,62 @@ def construct_transaction(self, contract_method: web3._utils.datatypes, paramete
166173 raise Exception (
167174 f"Error while construct transaction: { sys .exc_info ()[1 ]} " )
168175
169- def sign_transaction (self , tx : dict ) -> SignedTransaction :
176+ def sign_transaction (self , tx : dict ) -> 'RawSignedTransaction' :
170177 """
171- Takes transaction dict, signs it and sends to the blockchain
178+ Takes transaction dict, signs it and returns raw signed transaciton
172179
173180 Parameters:
174181 tx: dict
175182 transaction data in dict
176183 Returns:
177- signed transaction object
184+ signed raw transaction
178185 """
179186 try :
180187 signed_tx = self .active_account .sign_transaction (tx )
181- return signed_tx
188+ print (f"Signed tx localy: { signed_tx } " )
189+ return signed_tx .rawTransaction
190+ except :
191+ raise Exception (
192+ f"Error while sign transaction: { sys .exc_info ()[1 ]} " )
193+
194+ def sign_message (self , message : 'bytes str' ) -> 'Signature' :
195+ signer_priv_key = eth_keys .keys .PrivateKey (self .active_account ._private_key )
196+ signature = signer_priv_key .sign_msg (message )
197+ return signature
198+
199+ def sign_transaction_with_provider (self , tx : dict ) -> 'RawSignedTransaction' :
200+ """
201+ Takes transaction dict, signs it and returns raw signed transaciton
202+
203+ Parameters:
204+ tx: dict
205+ transaction data in dict
206+ Returns:
207+ signed raw transaction
208+ """
209+ try :
210+ signed_tx = self .web3 .eth .signTransaction (tx )
211+ return signed_tx .raw
182212 except :
183213 raise Exception (
184214 f"Error while sign transaction: { sys .exc_info ()[1 ]} " )
185215
186- def construct_and_sign_transaction (self , contract_method : web3 ._utils .datatypes ) -> SignedTransaction :
216+ def construct_and_sign_transaction (self , contract_method : web3 ._utils .datatypes ) -> 'RawSignedTransaction' :
187217 """
188218 Takes contract method call object, call method to build transaction and return signed transaction
189219
190220 Parameters:
191221 contract_method: web3._utils.datatypes
192222 object of contract method call
193223 Returns:
194- signed transaction object
224+ signed raw transaction
195225 """
196226 try :
197227 tx = self .construct_transaction (contract_method )
198- signed_tx = self .sign_transaction (tx )
228+ if self .sign_with_provider :
229+ signed_tx = self .sign_transaction_with_provider (tx )
230+ else :
231+ signed_tx = self .sign_transaction (tx )
199232 return signed_tx
200233 except :
201234 raise Exception (
@@ -214,14 +247,19 @@ def send_transaction(self, contract_method: web3._utils.datatypes, parameters: d
214247 """
215248 try :
216249 tx = self .construct_transaction (contract_method , parameters )
217- signed_tx = self .sign_transaction (tx )
218- return self .push_tx_to_blockchain (signed_tx .rawTransaction )
250+ if self .sign_with_provider :
251+ signed_tx = self .sign_transaction_with_provider (tx )
252+ else :
253+ signed_tx = self .sign_transaction (tx )
254+
255+ return self .push_tx_to_blockchain (signed_tx )
219256 except ValueError as e :
220257 error_message = ast .literal_eval (str (e ))['message' ]
221258 if error_message == 'intrinsic gas too low' :
222259 gas = gas + self .gas_increase_step if gas else self ._gas + self .gas_increase_step
223260 if gas > self .threshold_gas_value :
224- raise Exception (f"Transaction requires a lot of gas use({ gas } ), if you want to send transaction set higher gas value and increase threshold gas value" )
261+ raise Exception (
262+ f"Transaction requires a lot of gas use({ gas } ), if you want to send transaction set higher gas value and increase threshold gas value" )
225263 self .send_transaction (contract_method , parameters = {'gas' : gas })
226264 else :
227265 raise ValueError (error_message )
0 commit comments