Smart Accounts
The Web3.js plugin for ZKsync allows users to create ZKsync smart accounts with custom logic for building and signing transactions.
Create a smart account
The SmartAccount constructor
requires a parameter that implements the SmartAccountSigner interface
and also accepts an optional L2 provider parameter, which can be omitted for smart accounts that are intended for
offline usage. The SmartAccountSigner interface specifies two required parameters and two optional parameters:
address: (string) The address to which theSmartAccountis bound.secret: (any) The secret that will be used to sign payloads.payloadSigner: (PayloadSignerfunction) Optional. A function that accepts a payload, an optional secret, and an optional L2 provider and returns a serialized signature in hexadecimal format. The default value is the providedsignPayloadWithECDSAfunction.transactionBuilder: (TransactionBuilderfunction) Optional. A function that accepts partial transaction data, an optional secret, and an optional L2 provider and returns aPromisethat resolves to a populated transaction. The default value is the providedpopulateTransactionECDSAfunction.
The following code sample demonstrates creating a basic SmartAccount and using it to interact with the ZKsync Era network:
import { Web3 } from "web3";
import {
SmartAccount,
types,
Web3ZKsyncL2,
ZKsyncPlugin,
} from "web3-plugin-zksync";
async function main() {
const web3: Web3 = new Web3(/* optional L1 provider */);
web3.registerPlugin(
new ZKsyncPlugin(
Web3ZKsyncL2.initWithDefaultProvider(types.Network.Sepolia),
),
);
const zksync: ZKsyncPlugin = web3.ZKsync;
// create a smart account with the default signer and transaction builder
const smartAccount: SmartAccount = new SmartAccount(
{ address: "<ACCOUNT_ADDRESS>", secret: "<PRIVATE_KEY>" },
zksync.L2,
);
console.log("Smart account balance:", await smartAccount.getBalance());
}
main()
.then(() => console.log("✅ Script executed successfully"))
.catch((error) => console.error(`❌ Error executing script: ${error}`));
ECDSA Smart Accounts
The Web3.js plugin for ZKsync includes a helper function for creating an ECDSA smart account, which is demonstrated in the following code snippet:
const ecdsaSmartAccount: SmartAccount = ECDSASmartAccount.create(
"<ACCOUNT_ADDRESS>",
"<PRIVATE_KEY>",
zksync.L2,
);
Multi-Signature smart accounts
The Web3.js plugin for ZKsync includes a helper function for creating a multi-signature ECDSA smart account, which is demonstrated in the following code snippet:
const multiSigEcdsaSmartAccount: SmartAccount = MultisigECDSASmartAccount.create(
"<ACCOUNT_ADDRESS>",
["<PRIVATE_KEY1>", "<PRIVATE_KEY2>"],
zksync.L2,
);