From 1a9b512f91e7aea4d9656846bc0733fd301ad18d Mon Sep 17 00:00:00 2001 From: Sulpiride Date: Sun, 2 Feb 2025 08:41:24 +0500 Subject: [PATCH] feat: passkey paymaster handler & general paymaster handler & fix types --- packages/sdk/src/client/passkey/client.ts | 2 ++ .../src/client/passkey/decorators/wallet.ts | 31 ++++++++++++++++--- .../sdk/src/client/session/actions/session.ts | 22 +++++++++++-- .../src/client/session/decorators/wallet.ts | 29 +++++++++++++---- .../sdk/src/paymaster/handlers/general.ts | 14 +++++++++ packages/sdk/src/paymaster/handlers/index.ts | 1 + 6 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 packages/sdk/src/paymaster/handlers/general.ts diff --git a/packages/sdk/src/client/passkey/client.ts b/packages/sdk/src/client/passkey/client.ts index 05c38fb..ef06d2d 100644 --- a/packages/sdk/src/client/passkey/client.ts +++ b/packages/sdk/src/client/passkey/client.ts @@ -1,6 +1,7 @@ import { type Account, type Address, type Chain, type Client, createClient, getAddress, type Prettify, type PublicActions, publicActions, type PublicRpcSchema, type RpcSchema, type Transport, type WalletActions, walletActions, type WalletClientConfig, type WalletRpcSchema } from "viem"; import { eip712WalletActions } from "viem/zksync"; +import type { CustomPaymasterHandler } from "../../paymaster/index.js"; import { passkeyHashSignatureResponseFormat } from "../../utils/passkey.js"; import { toPasskeyAccount } from "./account.js"; import { requestPasskeyAuthentication } from "./actions/passkey.js"; @@ -63,6 +64,7 @@ type ZksyncSsoPasskeyData = { userName: string; // Basically unique user id (which is called `userName` in webauthn) userDisplayName: string; // Also option required for webauthn contracts: PasskeyRequiredContracts; + paymasterHandler?: CustomPaymasterHandler; }; export type ClientWithZksyncSsoPasskeyData< diff --git a/packages/sdk/src/client/passkey/decorators/wallet.ts b/packages/sdk/src/client/passkey/decorators/wallet.ts index 066d824..e12636e 100644 --- a/packages/sdk/src/client/passkey/decorators/wallet.ts +++ b/packages/sdk/src/client/passkey/decorators/wallet.ts @@ -1,6 +1,7 @@ -import { type Account, bytesToHex, type Chain, formatTransaction, type Transport, type WalletActions } from "viem"; +import { getTransactionWithPaymasterData } from "src/paymaster/index.js"; +import { type Account, bytesToHex, type Chain, type ExactPartial, formatTransaction, type RpcTransaction, type Transport, type WalletActions } from "viem"; import { deployContract, getAddresses, getChainId, sendRawTransaction, signMessage, signTypedData, writeContract } from "viem/actions"; -import { signTransaction, type ZksyncEip712Meta } from "viem/zksync"; +import { signTransaction, type TransactionRequestEIP712, type ZksyncEip712Meta } from "viem/zksync"; import { sendEip712Transaction } from "../../session/actions/sendEip712Transaction.js"; import type { ClientWithZksyncSsoPasskeyData } from "../client.js"; @@ -33,19 +34,39 @@ export function zksyncSsoPasskeyWalletActions< delete unformattedTx.eip712Meta; } + /* eslint-disable @typescript-eslint/no-unused-vars */ + const { chainId: _, ...unformattedTxWithPaymaster } = await getTransactionWithPaymasterData( + client.chain.id, + client.account.address, + unformattedTx, + client.paymasterHandler, + ); + const formatters = client.chain?.formatters; const format = formatters?.transaction?.format || formatTransaction; const tx = { - ...format(unformattedTx), + ...format(unformattedTxWithPaymaster as ExactPartial), type: "eip712", }; return await sendEip712Transaction(client, tx); }, signMessage: (args) => signMessage(client, args), - // eslint-disable-next-line @typescript-eslint/no-explicit-any - signTransaction: (args) => signTransaction(client, args as any), + + signTransaction: async (args) => { + /* eslint-disable @typescript-eslint/no-unused-vars */ + const { chainId: _, ...unformattedTxWithPaymaster } = await getTransactionWithPaymasterData( + client.chain.id, + client.account.address, + args as TransactionRequestEIP712, + client.paymasterHandler, + ); + return signTransaction(client, { + ...args, + unformattedTxWithPaymaster, + } as any); + }, signTypedData: (args) => signTypedData(client, args), writeContract: (args) => writeContract(client, args), }; diff --git a/packages/sdk/src/client/session/actions/session.ts b/packages/sdk/src/client/session/actions/session.ts index e792915..5f63e50 100644 --- a/packages/sdk/src/client/session/actions/session.ts +++ b/packages/sdk/src/client/session/actions/session.ts @@ -3,6 +3,7 @@ import { waitForTransactionReceipt } from "viem/actions"; import { getGeneralPaymasterInput, sendTransaction } from "viem/zksync"; import { SessionKeyModuleAbi } from "../../../abi/SessionKeyModule.js"; +import { type CustomPaymasterHandler, getTransactionWithPaymasterData } from "../../../paymaster/index.js"; import { noThrow } from "../../../utils/helpers.js"; import type { SessionConfig } from "../../../utils/session.js"; @@ -16,6 +17,7 @@ export type CreateSessionArgs = { paymasterInput?: Hex; }; onTransactionSent?: (hash: Hash) => void; + paymasterHandler?: CustomPaymasterHandler; }; export type CreateSessionReturnType = { transactionReceipt: TransactionReceipt; @@ -41,7 +43,14 @@ export const createSession = async < // eslint-disable-next-line @typescript-eslint/no-explicit-any } as any; - const transactionHash = await sendTransaction(client, sendTransactionArgs); + const transactionWithPaymasterData: any = await getTransactionWithPaymasterData( + client.chain.id, + client.account.address, + sendTransactionArgs, + args.paymasterHandler, + ); + + const transactionHash = await sendTransaction(client, transactionWithPaymasterData); if (args.onTransactionSent) { noThrow(() => args.onTransactionSent?.(transactionHash)); } @@ -64,6 +73,7 @@ export type RevokeSessionArgs = { paymasterInput?: Hex; }; onTransactionSent?: (hash: Hash) => void; + paymasterHandler?: CustomPaymasterHandler; }; export type RevokeSessionReturnType = { transactionReceipt: TransactionReceipt; @@ -89,7 +99,15 @@ export const revokeSession = async < // eslint-disable-next-line @typescript-eslint/no-explicit-any } as any; - const transactionHash = await sendTransaction(client, sendTransactionArgs); + const transactionWithPaymasterData: any = await getTransactionWithPaymasterData( + client.chain.id, + client.account.address, + sendTransactionArgs, + args.paymasterHandler, + ); + + const transactionHash = await sendTransaction(client, transactionWithPaymasterData); + if (args.onTransactionSent) { noThrow(() => args.onTransactionSent?.(transactionHash)); } diff --git a/packages/sdk/src/client/session/decorators/wallet.ts b/packages/sdk/src/client/session/decorators/wallet.ts index b4d1f7c..ccea7d9 100644 --- a/packages/sdk/src/client/session/decorators/wallet.ts +++ b/packages/sdk/src/client/session/decorators/wallet.ts @@ -1,6 +1,12 @@ -import { type Account, bytesToHex, type Chain, formatTransaction, type Transport, type WalletActions } from "viem"; -import { deployContract, getAddresses, getChainId, sendRawTransaction, signMessage, signTypedData, writeContract } from "viem/actions"; -import { signTransaction, type ZksyncEip712Meta } from "viem/zksync"; +import { + type Account, bytesToHex, + type Chain, type ExactPartial, formatTransaction, type RpcTransaction, + type Transport, type WalletActions } from "viem"; +import { + deployContract, getAddresses, getChainId, sendRawTransaction, + signMessage, signTypedData, writeContract, +} from "viem/actions"; +import { signTransaction, type TransactionRequestEIP712, type ZksyncEip712Meta } from "viem/zksync"; import { getTransactionWithPaymasterData } from "../../../paymaster/index.js"; import { sendEip712Transaction } from "../actions/sendEip712Transaction.js"; @@ -46,15 +52,26 @@ export function zksyncSsoWalletActions< const format = formatters?.transaction?.format || formatTransaction; const tx = { - ...format(unformattedTxWithPaymaster as any), + ...format(unformattedTxWithPaymaster as ExactPartial), type: "eip712", }; return await sendEip712Transaction(client, tx); }, signMessage: (args) => signMessage(client, args), - // eslint-disable-next-line @typescript-eslint/no-explicit-any - signTransaction: (args) => signTransaction(client, args as any), + + signTransaction: async (args) => { + const { chainId: _, ...unformattedTxWithPaymaster } = await getTransactionWithPaymasterData( + client.chain.id, + client.account.address, + args as TransactionRequestEIP712, + client.paymasterHandler, + ); + return signTransaction(client, { + ...args, + unformattedTxWithPaymaster, + } as any); + }, signTypedData: (args) => signTypedData(client, args), writeContract: (args) => writeContract(client, args), }; diff --git a/packages/sdk/src/paymaster/handlers/general.ts b/packages/sdk/src/paymaster/handlers/general.ts new file mode 100644 index 0000000..96163f2 --- /dev/null +++ b/packages/sdk/src/paymaster/handlers/general.ts @@ -0,0 +1,14 @@ +import type { Address } from "viem"; +import { getGeneralPaymasterInput } from "viem/zksync"; + +import type { CustomPaymasterHandler, CustomPaymasterHandlerResponse, CustomPaymasterParameters } from "../index.js"; + +export function createGeneralPaymaster(paymaster: Address): CustomPaymasterHandler { + /* eslint-disable @typescript-eslint/no-unused-vars */ + return async (_: CustomPaymasterParameters): CustomPaymasterHandlerResponse => { + return { + paymaster, + paymasterInput: getGeneralPaymasterInput({ innerInput: "0x" }), + }; + }; +} diff --git a/packages/sdk/src/paymaster/handlers/index.ts b/packages/sdk/src/paymaster/handlers/index.ts index a9b5966..cc7e4dd 100644 --- a/packages/sdk/src/paymaster/handlers/index.ts +++ b/packages/sdk/src/paymaster/handlers/index.ts @@ -1 +1,2 @@ +export * from "./general.js"; export * from "./zyfi.js";