Python API

Smart Account Utilities

Utilities for managing and signing payloads with smart accounts

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

ParameterTypeDescription
payloadbytesThe payload that needs to be signed.
secretHexStrThe 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

ParameterTypeDescription
payloadbytesThe 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 from using the address derived from the ECDSA private key.
  • Populates nonce via provider.zksync.get_transaction_count(Web3.to_checksum_address(tx.from_), EthBlockParams.PENDING.value).
  • Populates gas_limit via provider.zksync.zks_estimate_fee(tx.to_zk_transaction()). If tx.from is not EOA,
  • the estimation is done with address derived from the ECDSA private key.
  • Populates chain_id via provider.eth.chain_id.
  • Populates type with EIP_712_TX_TYPE.
  • Populates value if set, otherwise to 0.
  • Populates data with 0x.
  • Populates meta with {factoryDeps=[], gasPerPubdata=50_000}.

Inputs

ParameterTypeDescription
txTransaction712The transaction that needs to be populated.
secretstring or ethers.SigningKeyThe ECDSA private key.
providerWeb3The 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

ParameterTypeDescription
txTransaction712The transaction that needs to be populated.
secret[HexStr]List of ECDSA private keys.
providerWeb3The 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)
For development and testing, it is recommended to use burner wallets. Avoid using real private keys to prevent security risks.

Made with ❤️ by the ZKsync Community