-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart_account.py
567 lines (484 loc) · 21.1 KB
/
smart_account.py
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
from web3 import Web3
from bundler import Bundler
from paymaster import Paymaster
from userop import UserOperation, UserOperationLib
from eth_abi import encode
from eth_utils import keccak, to_checksum_address
from eth_account import Account
from utils.modules import ValidationModule
from typing import Union
import json
import utils.constants as constants
class BiconomyV2SmartAccount:
"""
Represents a Biconomy V2 Smart Account.
Attributes:
provider (Web3): The Web3 provider instance.
bundler (Bundler): The bundler instance for managing user operations.
validation_module (ValidationModule): The validation module to be used.
private_key (str): The private key for signing transactions.
eoa_address (str): The externally owned account (EOA) address.
smart_account_address (str): The smart account address.
paymaster_url (str): The paymaster ID.
index (int): The index of the account.
"""
def __init__(
self,
rpc_url: str,
bundler_url: str,
private_key: str,
index: int = 0,
paymaster_url: Union[str, None] = None,
validation_module: ValidationModule = ValidationModule.ECDSA,
):
"""
Initializes the BiconomyV2SmartAccount instance.
Args:
rpc_url (str): The URL of the RPC provider.
bundler_url (str): The URL of the bundler.
paymaster_url (str): The paymaster ID.
private_key (str): The private key for signing transactions.
index (int, optional): The index of the account. Defaults to 0.
validation_module (ValidationModule, optional): The validation module to be used. Defaults to ValidationModule.ECDSA.
"""
self.provider = Web3(Web3.HTTPProvider(rpc_url))
self.bundler = Bundler(bundler_url)
self._check_chain_ids()
self.validation_module = validation_module
self._set_contract_instances()
self.index = index
self._check_index()
self.private_key = private_key
self.eoa_address = self.get_eoa_address()
self.smart_account_address = self.get_smart_account_address()
if paymaster_url:
self.paymaster = Paymaster(paymaster_url)
else:
self.paymaster = None
def get_eoa_address(self) -> str:
"""
Returns the externally owned account (EOA) address derived from the private key.
Returns:
str: The EOA address.
"""
account = Account.from_key(self.private_key)
return account.address
def get_smart_account_address(self) -> str:
"""
Computes the smart account address based on the provided configuration.
Returns:
str: The smart account address.
"""
# Generate setup data for setting eoa as owner of sa in ecdsa module
ownership_module_setup_data = self._get_module_setup_data()
# Generate call data for initializing smart account
sa_init_calldata = self.smart_account_implementation_v2.encode_abi(
fn_name="init",
args=[
constants.DEFAULT_FALLBACK_HANDLER_ADDRESS,
self.validation_module.get_module_address(),
bytes.fromhex(ownership_module_setup_data[2:]),
],
)
salt = Web3.solidity_keccak(
["bytes32", "uint256"],
[keccak(bytes.fromhex(sa_init_calldata[2:])), self.index],
)
create2_hash = Web3.solidity_keccak(
["bytes1", "address", "bytes32", "bytes32"],
[
b"\xff",
constants.SMART_ACCOUNT_FACTORY_V2,
salt,
constants.PROXY_CREATION_CODE_HASH,
],
)
smart_account_address = to_checksum_address(create2_hash[-20:])
return smart_account_address
def get_smart_account_native_balance(self) -> int:
"""
Returns the native balance of the smart account.
Returns:
int: The native balance of the smart account in wei.
"""
balance = self.provider.eth.get_balance(self.smart_account_address)
return balance
def send_eth(self, recipient: str, amount_wei: int, nonce_key: int = 0) -> str:
"""
Sends a specified amount of Ether to a recipient address using a user operation.
Args:
recipient (str): The Ethereum address of the recipient.
amount_wei (int): The amount of Ether to send, in wei.
nonce_key (int, optional): The key for the nonce value. Default is 0.
Returns:
str: The transaction hash of the user operation.
Raises:
ValueError: If the amount is not a positive integer, or if the recipient address is invalid,
or if the amount is greater than the account balance.
Exception: If the smart account is not deployed.
"""
if amount_wei <= 0 or not isinstance(amount_wei, int):
raise ValueError("Amount must be a positive integer")
if not Web3.is_address(recipient):
raise ValueError("Recipient must be a valid ethereum address")
if not self.is_deployed():
raise Exception(
f"Account at address {self.smart_account_address} isn't deployed"
)
sa_balance = self.provider.eth.get_balance(self.smart_account_address)
if amount_wei > sa_balance:
raise ValueError("Amount is greater than account balance")
call_data = self.smart_account_implementation_v2.encode_abi(
fn_name="execute",
args=[
recipient,
amount_wei,
b"",
],
)
userop = self.build_user_op(
nonce=self.get_nonce(nonce_key), call_data=bytes.fromhex(call_data[2:])
)
userop = self.sign_userop(userop)
return self.send_userop(userop)
def get_nonce(self, key: int = 0) -> int:
"""
Retrieves the nonce for the smart account.
Args:
key (int): The nonce key.
Returns:
int: The nonce value.
Raises:
ValueError: If the key is not a positive integer.
"""
if key < 0 or not isinstance(key, int):
raise ValueError("Key must be a positive integer")
# Get nonce from EP
nonce = self.entry_point.functions.getNonce(
self.smart_account_address, key
).call()
return nonce
def fund_account(
self,
amount_wei: int,
gas: int = 50000,
max_fee_per_gas: int = 0,
max_priority_fee_per_gas: int = 0,
) -> str:
"""
Funds the smart account with the specified amount of wei.
Args:
amount_wei (int): The amount of wei to fund the account with.
gas (int): The amount of gas to send with the transaction.
max_fee_per_gas (int): The max fee per unit of gas.
max_priority_fee_per_gas (int): The max additional fee over the baseFeePerGas per gas.
Returns:
str: The userop hash of the funding transaction.
Raises:
ValueError: If the amount is greater than the account balance or is not a positive integer.
"""
if amount_wei <= 0 or not isinstance(amount_wei, int):
raise ValueError("Amount must be a positive integer")
eoa_balance = self.provider.eth.get_balance(self.eoa_address)
if amount_wei >= eoa_balance:
raise ValueError("Amount is greater than account balance")
if max_fee_per_gas == 0 or max_priority_fee_per_gas == 0:
latest_block = self.provider.eth.get_block("latest")
base_fee_per_gas = latest_block["baseFeePerGas"]
max_priority_fee_per_gas = Web3.to_wei(2, "gwei")
max_fee_per_gas = base_fee_per_gas + max_priority_fee_per_gas
transaction = {
"from": self.eoa_address,
"to": self.smart_account_address,
"value": amount_wei,
"nonce": self.provider.eth.get_transaction_count(self.eoa_address),
"gas": gas,
"maxFeePerGas": max_fee_per_gas,
"maxPriorityFeePerGas": max_priority_fee_per_gas,
"chainId": self.provider.eth.chain_id,
}
signed_txn = Account.sign_transaction(transaction, self.private_key)
tx_hash = self.provider.eth.send_raw_transaction(signed_txn.raw_transaction)
return tx_hash.hex()
def deploy_smart_account(self, nonce_key: int = 0) -> str:
"""
Deploys the smart account if it is not already deployed.
Args:
nonce_key (int, optional): The nonce key to use. Defaults to 0.
Returns:
str: The userop hash of the deployment transaction.
Raises:
Exception: If the account is already deployed.
"""
# Check if account is already deployed
if self.is_deployed():
raise Exception(f"Account at address {self.smart_account_address} exists")
# Generate setup data for setting eoa as owner of sa in ecdsa module
ownership_module_setup_data = self._get_module_setup_data()
factory_address = constants.SMART_ACCOUNT_FACTORY_V2
factory_data = self.smart_account_factory_v2.encode_abi(
fn_name="deployCounterFactualAccount",
args=[
self.validation_module.get_module_address(),
bytes.fromhex(ownership_module_setup_data[2:]),
self.index,
],
)
init_code = bytes.fromhex(factory_address[2:] + factory_data[2:])
nonce = self.get_nonce(nonce_key)
# Build userop
userop: UserOperation = self.build_user_op(nonce=nonce, init_code=init_code)
userop = self.sign_userop(userop)
return self.send_userop(userop)
def build_user_op(
self,
# If nonce is null, they will be derived from this smart account instance
nonce: Union[int, None] = None,
init_code: bytes = b"",
call_data: bytes = b"",
paymaster_context: dict = constants.DEFAULT_PAYMASTER_CONTEXT,
) -> UserOperation:
"""
Builds a user operation with the specified parameters.
Args:
sender (str): The sender address.
nonce (int): The nonce value.
init_code (bytes, optional): The initialization code. Defaults to b"".
call_data (bytes, optional): The call data. Defaults to b"".
paymaster_and_data (bytes, optional): The paymaster and data. Defaults to b"".
max_fee_per_gas (int, optional): The maximum fee per gas. Defaults to 0.
max_priority_fee_per_gas (int, optional): The maximum priority fee per gas. Defaults to 0.
pre_verification_gas (int, optional): The pre-verification gas. Defaults to 0.
verification_gas_limit (int, optional): The verification gas limit. Defaults to 0.
call_gas_limit (int, optional): The call gas limit. Defaults to 0.
Returns:
UserOperation: The constructed user operation.
"""
# Build the initial user operation object
userop = UserOperation(
self.smart_account_address,
nonce if nonce is not None else self.get_nonce(),
init_code,
call_data,
call_gas_limit=0,
verification_gas_limit=0,
pre_verification_gas=0,
max_fee_per_gas=0,
max_priority_fee_per_gas=0,
paymaster_and_data=b"",
signature=b"",
)
# Get dummy signature for gas estimation
estimation_sig = encode(
["bytes", "address"],
[
self._sign_hash(constants.DUMMY_DATA_HASH),
self.validation_module.get_module_address(),
],
)
userop.signature = estimation_sig
# Get gas estimation from bundler
gas_estimations = self.bundler.estimate_userop_gas(
userop, self.entry_point.address
)
userop.max_fee_per_gas = gas_estimations["maxFeePerGas"]
userop.max_priority_fee_per_gas = gas_estimations["maxPriorityFeePerGas"]
if self.paymaster:
# Get gas estimation from paymaster
paymaster_and_data = self.paymaster.sponsor_user_operation(
userop, paymaster_context
)
userop.paymaster_and_data = bytes.fromhex(
paymaster_and_data["paymasterAndData"][2:]
)
userop.pre_verification_gas = int(paymaster_and_data["preVerificationGas"])
userop.verification_gas_limit = int(
paymaster_and_data["verificationGasLimit"]
)
userop.call_gas_limit = int(paymaster_and_data["callGasLimit"])
else:
# If no paymaster set, use bundler gas estimations
userop.pre_verification_gas = gas_estimations["preVerificationGas"]
userop.verification_gas_limit = gas_estimations["verificationGasLimit"]
userop.call_gas_limit = gas_estimations["callGasLimit"]
userop.signature = b""
return userop
def sign_userop(self, userop: UserOperation) -> UserOperation:
"""
Signs the user operation.
Args:
userop (UserOperation): The user operation to sign.
Returns:
UserOperation: The signed user operation.
"""
if self.validation_module == ValidationModule.ECDSA:
userop_hash = UserOperationLib.hash(
userop, self.entry_point.address, self.provider.eth.chain_id
)
signed_userop_hash = self._sign_hash(userop_hash)
complete_userop_signature = encode(
["bytes", "address"],
[
signed_userop_hash,
self.validation_module.get_module_address(),
],
)
userop.signature = complete_userop_signature
return userop
elif self.validation_module == ValidationModule.MULTICHAIN_VALIDATION_MODULE:
raise ValueError(f"Module not yet supported: {self.validation_module}")
elif self.validation_module == ValidationModule.BATCHED_SESSION_ROUTER_MODULE:
raise ValueError(f"Module not yet supported: {self.validation_module}")
elif self.validation_module == ValidationModule.ABI_SESSION_VALIDATION_MODULE:
raise ValueError(f"Module not yet supported: {self.validation_module}")
elif self.validation_module == ValidationModule.SESSION_KEY_MANAGER_V1:
raise ValueError(f"Module not yet supported: {self.validation_module}")
else:
raise ValueError(f"Unknown validation module: {self.validation_module}")
def send_userop(self, userop: UserOperation) -> str:
"""
Sends the user operation to the bundler.
Args:
userop (UserOperation): The user operation to send.
Returns:
str: The userop hash of the sent user operation.
"""
return self.bundler.send_userop(userop, self.entry_point.address)
def get_userop_status(self, userop_hash: str) -> str:
"""
Retrieves the status of a user operation by its hash.
Args:
userop_hash (str): The hash of the user operation.
Returns:
str: The status of the user operation.
"""
return self.bundler.get_user_operation_status(userop_hash)
def get_userop_by_hash(self, userop_hash: str) -> Union[UserOperation, None]:
"""
Retrieves a user operation by its hash.
Args:
userop_hash (str): The hash of the user operation.
Returns:
Union[UserOperation, None]: The user operation, or None if not found.
"""
return self.bundler.get_user_operation_by_hash(userop_hash)
def get_userop_receipt(self, userop_hash: str) -> Union[dict, None]:
"""
Retrieves the receipt of a user operation by its hash.
Args:
userop_hash (str): The hash of the user operation.
Returns:
Union[dict, None]: The receipt of the user operation, or None if not found.
"""
return self.bundler.get_user_operation_receipt(userop_hash)
def is_deployed(self) -> bool:
"""
Checks if the smart account is already deployed.
Returns:
bool: True if the smart account is deployed, False otherwise.
"""
return True if self.provider.eth.get_code(self.smart_account_address) else False
def _check_chain_ids(self) -> None:
"""
Checks if the chain IDs of the RPC provider and bundler match.
Raises:
ValueError: If the chain IDs do not match.
"""
# Fetch the chain ID from the RPC provider
rpc_chain_id = self.provider.eth.chain_id
bundler_chain_id = self.bundler.get_chain_id()
# Compare the chain IDs
if rpc_chain_id != bundler_chain_id:
raise ValueError(
f"Chain ID mismatch: RPC chain ID is {rpc_chain_id}, Bundler chain ID is {bundler_chain_id}"
)
def _set_contract_instances(self):
"""
Sets the contract instances for the smart account, factory, and validation modules.
"""
entry_point_address = constants.ENTRY_POINT_OTHER_CHAINS
# if using chiliz mainnet or testnet, adjust entrypoint
if self.provider.eth.chain_id == 88888 or self.provider.eth.chain_id == 88880:
entry_point_address = constants.ENTRY_POINT_CHILIZ_MAINNET_TESTNET
# Instantiate global entrypoint
entry_point_abi = self.read_abi("./contract_abis/entry_point.json")
self.entry_point = self.provider.eth.contract(
address=entry_point_address,
abi=entry_point_abi,
)
# Instantiate global SA factory
smart_account_factory_v2_abi = self.read_abi(
"./contract_abis/smart_account_factory_v2.json"
)
self.smart_account_factory_v2 = self.provider.eth.contract(
address=constants.SMART_ACCOUNT_FACTORY_V2,
abi=smart_account_factory_v2_abi,
)
# Instantiate global SA implementation
smart_account_implementation_v2_abi = self.read_abi(
"./contract_abis/smart_account_implementation_v2.json"
)
self.smart_account_implementation_v2 = self.provider.eth.contract(
address=constants.SMART_ACCOUNT_IMPLEMENTATION_V2,
abi=smart_account_implementation_v2_abi,
)
# Instantiate global ECDSA implementation
ecdsa_ownership_module_abi = self.read_abi(
"./contract_abis/ecdsa_ownership_module.json"
)
self.ecdsa_ownership_module = self.provider.eth.contract(
address=self.validation_module.get_module_address(),
abi=ecdsa_ownership_module_abi,
)
def _check_index(self):
"""
Checks if the index is a positive integer.
Raises:
ValueError: If the index is not a positive integer.
"""
if self.index < 0 or not isinstance(self.index, int):
raise ValueError("Index must be a positive integer")
def _sign_hash(self, hash) -> bytes:
"""
Signs the given hash with the private key.
Args:
hash: The hash to sign.
Returns:
bytes: The signature of the hash.
"""
signed_hash = Account.signHash(hash, self.private_key)
return signed_hash.signature
def _get_module_setup_data(self) -> str:
"""
Returns the setup data for the validation module.
Returns:
str: The setup data.
Raises:
ValueError: If the validation module is not supported or unknown.
"""
if self.validation_module == ValidationModule.ECDSA:
return self.ecdsa_ownership_module.encode_abi(
fn_name="initForSmartAccount", args=[self.eoa_address]
)
elif self.validation_module == ValidationModule.MULTICHAIN_VALIDATION_MODULE:
raise ValueError(f"Module not yet supported: {self.validation_module}")
elif self.validation_module == ValidationModule.BATCHED_SESSION_ROUTER_MODULE:
raise ValueError(f"Module not yet supported: {self.validation_module}")
elif self.validation_module == ValidationModule.ABI_SESSION_VALIDATION_MODULE:
raise ValueError(f"Module not yet supported: {self.validation_module}")
elif self.validation_module == ValidationModule.SESSION_KEY_MANAGER_V1:
raise ValueError(f"Module not yet supported: {self.validation_module}")
else:
raise ValueError(f"Unknown validation module: {self.validation_module}")
@staticmethod
def read_abi(file_path: str) -> list:
"""
Reads and returns the ABI from the specified file.
Args:
file_path (str): The path to the ABI file.
Returns:
list: The ABI read from the file.
"""
with open(file_path, "r") as file:
abi = json.load(file)
return abi