Smart Account Utilities
The Smart Account Utilities in the ZKsync Python SDK provide functions to manage and sign payloads using ECDSA private keys.
Functions
sign_payload_with_ecdsa
Signs the payload using an ECDSA private key.
Inputs
| Parameter | Type | Description |
|---|---|---|
payload | bytes | The payload that needs to be signed. |
secret | HexStr | The ECDSA private key. |
Examples
Sign EIP712 transaction hash.
from zksync2.account.smart_account_utils import sign_payload_with_ecdsa
if __name__ == "__main__":
ZKSYNC_PROVIDER = "https://sepolia.era.zksync.dev"
zksync = ZkSyncBuilder.build(ZKSYNC_PROVIDER)
tx = TxTransfer(
from_=address_1,
to=address_2,
value=7_000_000_000,
token=ADDRESS_DEFAULT
).tx712(0)
account: LocalAccount = Account.from_key(secret)
signer = PrivateKeyEthSigner(account, provider.eth.chain_id)
account = sign_payload_with_ecdsa(tx.signable_bytes(signer.domain), "<PRIVATE_KEY>')
Sign message hash.
from zksync2.account.smart_account_utils import sign_payload_with_ecdsa
if __name__ == "__main__":
ZKSYNC_PROVIDER = "https://sepolia.era.zksync.dev"
zksync = ZkSyncBuilder.build(ZKSYNC_PROVIDER)
account = sign_payload_with_ecdsa("Hello World!"encode('utf-8'), "<PRIVATE_KEY>')
sign_payload_with_multiple_ecdsa
Signs the payload using multiple ECDSA private keys.
The signature is generated by concatenating signatures created by signing with each key individually.
The length of the resulting signature should be secrets.length * 65 + 2.
Inputs
| Parameter | Type | Description |
|---|---|---|
payload | bytes | The payload that needs to be signed. |
secret | [HexStr] | The list of the ECDSA private keys. |
Examples
Sign EIP712 transaction hash.
from zksync2.account.smart_account_utils import sign_payload_with_multiple_ecdsa
if __name__ == "__main__":
ZKSYNC_PROVIDER = "https://sepolia.era.zksync.dev"
zksync = ZkSyncBuilder.build(ZKSYNC_PROVIDER)
tx = TxTransfer(
from_=address_1,
to=address_2,
value=7_000_000_000,
token=ADDRESS_DEFAULT
).tx712(0)
account: LocalAccount = Account.from_key(secret)
signer = PrivateKeyEthSigner(account, provider.eth.chain_id)
account = sign_payload_with_multiple_ecdsa(tx.signable_bytes(signer.domain), ["<PRIVATE_KEY_1>, <PRIVATE_KEY_2>]")
Sign message hash.
from zksync2.account.smart_account_utils import sign_payload_with_multiple_ecdsa
if __name__ == "__main__":
account = sign_payload_with_multiple_ecdsa("Hello World!"encode('utf-8'), ["<PRIVATE_KEY_1>, <PRIVATE_KEY_2>]")
populate_transaction_ecdsa
Populates missing properties meant for signing using an ECDSA private key:
- Populates
fromusing the address derived from the ECDSA private key. - Populates
nonceviaprovider.zksync.get_transaction_count(Web3.to_checksum_address(tx.from_), EthBlockParams.PENDING.value). - Populates
gas_limitviaprovider.zksync.zks_estimate_fee(tx.to_zk_transaction()). Iftx.fromis not EOA, - the estimation is done with address derived from the ECDSA private key.
- Populates
chain_idviaprovider.eth.chain_id. - Populates
typewithEIP_712_TX_TYPE. - Populates
valueif set, otherwise to0. - Populates
datawith0x. - Populates
metawith{factoryDeps=[], gasPerPubdata=50_000}.
Inputs
| Parameter | Type | Description |
|---|---|---|
tx | Transaction712 | The transaction that needs to be populated. |
secret | string or ethers.SigningKey | The ECDSA private key. |
provider | Web3 | The provider which fetches data from the network. |
Examples
from eth_account import Account
from eth_account.signers.local import LocalAccount
from web3 import Web3
from zksync2.account.smart_account_utils import populate_transaction
from zksync2.module.module_builder import ZkSyncBuilder
if __name__ == "__main__":
ZKSYNC_PROVIDER = "https://sepolia.era.zksync.dev"
zksync = ZkSyncBuilder.build(ZKSYNC_PROVIDER)
populated = populate_transaction(TxTransfer(
from_=address_1,
to=address_2,
value=7_000_000_000,
token=ADDRESS_DEFAULT
).tx712(0),
"<PRIVATE_KEY_1>",
zksync)
populate_transaction_multiple_ecdsa
Populates missing properties meant for signing using multiple ECDSA private keys.
It uses populate_transaction_ecdsa, where the address
of the first ECDSA key is set as the secret argument.
Inputs
| Parameter | Type | Description |
|---|---|---|
tx | Transaction712 | The transaction that needs to be populated. |
secret | [HexStr] | List of ECDSA private keys. |
provider | Web3 | The provider which fetches data from the network. |
Examples
from eth_account import Account
from eth_account.signers.local import LocalAccount
from web3 import Web3
from zksync2.account.smart_account_utils import populate_transaction_multiple_ecdsa
from zksync2.module.module_builder import ZkSyncBuilder
if __name__ == "__main__":
ZKSYNC_PROVIDER = "https://sepolia.era.zksync.dev"
zksync = ZkSyncBuilder.build(ZKSYNC_PROVIDER)
populated = populate_transaction_multiple_ecdsa(TxTransfer(
from_=address_1,
to=address_2,
value=7_000_000_000,
token=ADDRESS_DEFAULT
).tx712(0),
["<PRIVATE_KEY_1>", "<PRIVATE_KEY_2>"],
zksync)