Implement ERC-4337 Account Abstraction with Para Server SDK
Account Abstraction (AA) enables powerful smart contract wallet functionality like batched transactions, gas sponsorship, and advanced access controls. Para Server SDK supports integration with leading AA providers, allowing you to implement these capabilities in server-side environments.When using Account Abstraction with Para, your Para wallet acts as the EOA (Externally Owned Account) signer that controls the smart contract wallet. The AA provider deploys and manages the smart contract wallet on-chain, while Para handles the secure signing required for authorizing transactions.
Server-side AA implementation functions identically to client-side implementation, with the additional requirement of signature byte adjustment for contract verification.
Account Abstraction providers use on-chain signature verification within their smart contracts. Para’s 2/2 MPC wallet produces non-deterministic signatures where the last byte (v recovery byte) may need adjustment for proper on-chain verification.
Critical: Para signatures may require adjustment of the last byte for compatibility with AA providers. Without this adjustment, transactions might fail at the network level despite signature success.
First, initialize the Para Server SDK and authenticate with an imported session:
Copy
Ask AI
import { Para as ParaServer } from "@getpara/server-sdk";// Initialize Para Server SDKconst para = new ParaServer("YOUR_API_KEY");// Import a client sessionawait para.importSession(session);
If you need more details on setup please refer tot he .
Create a helper function to properly adjust the signature byte for on-chain verification:
Copy
Ask AI
import { hashMessage, type Hash, type SignableMessage } from "viem";function hexStringToBase64(hexString) { return Buffer.from(hexString, "hex").toString("base64");}// Custom sign message function with signature byte adjustmentasync function customSignMessage(para, message) { // Get the first wallet from Para client const wallet = para.wallets ? Object.values(para.wallets)[0] : null; if (!wallet) { throw new Error("Para wallet not available for signing."); } // Hash and convert the message const hashedMessage = hashMessage(message); const messagePayload = hashedMessage.startsWith("0x") ? hashedMessage.substring(2) : hashedMessage; const messageBase64 = hexStringToBase64(messagePayload); // Sign with Para const res = await para.signMessage({ walletId: wallet.id, messageBase64: messageBase64, }); if (!("signature" in res)) { throw new Error("Signature failed"); } // Adjust the signature's 'v' value for on-chain verification let signature = res.signature; const vHex = signature.slice(-2); const v = parseInt(vHex, 16); if (!isNaN(v) && v < 27) { const adjustedVHex = (v + 27).toString(16).padStart(2, "0"); signature = signature.slice(0, -2) + adjustedVHex; } return `0x${signature}`;}
As mentioned earlier, this function adjusts the last byte of the signature to ensure compatibility with on-chain verification. This is crucial for successful transaction execution when working with Account Abstraction providers.
First, retrieve the smart wallet address (this is different from your Para wallet address):
Copy
Ask AI
// Get the smart wallet addressconst smartWalletAddress = await alchemyClient.getAddress();console.log("Smart Wallet Address:", smartWalletAddress);
Be aware that funds should be sent to the smart wallet address (returned by getAddress()) rather than the Para wallet address. The smart wallet is the entity that holds funds and executes transactions on-chain.
Interact with smart contracts through your AA wallet:
Copy
Ask AI
// Interact with a smart contractconst tokenContract = "0xTokenContractAddress";const data = "0x..." // Encoded contract function callconst contractTxHash = await alchemyClient.sendTransaction({ to: tokenContract, data: data,});
If transactions fail despite successful signing, check that the signature byte adjustment is working correctly. The most common issue is that the ‘v’ byte isn’t properly adjusted for on-chain verification.
Smart Wallet Deployment Issues
First-time use of an AA wallet requires contract deployment, which can be more expensive and complex. Ensure you have sufficient funds in the EOA (Para wallet) for this initial deployment.
Gas Sponsorship Problems
If using gas sponsorship, verify your policy ID and settings in the AA provider’s dashboard. Also check that the transaction meets the policy requirements (value limits, allowed functions, etc.).