From 36c2855c748f0708d2d9c7d7a1453e6dccd045fe Mon Sep 17 00:00:00 2001 From: Sahil Vasava Date: Mon, 8 Apr 2024 17:51:45 +0530 Subject: [PATCH 1/2] feat: update to latest kernel version --- .../abi/kernel_v_3_0_0/KernelAccountAbi.ts | 153 +++++++++++------- .../accounts/kernel/createKernelAccount.ts | 2 +- .../utils/account/ep0_7/getKernelV3Nonce.ts | 2 +- .../plugins/ep0_7/getEncodedPluginsData.ts | 5 +- .../ep0_7/getPluginsEnableTypedData.ts | 4 +- .../actions/paymaster/sponsorUserOperation.ts | 4 +- packages/core/constants.ts | 2 +- packages/test/abis/TokenActionsAbi.ts | 37 +++++ packages/test/config.ts | 2 + packages/test/ecdsaKernelAccount.test.ts | 76 ++++++--- packages/test/v0.7/ecdsaKernelAccount.test.ts | 72 ++++++--- .../test/v0.7/permissionValidator.test.ts | 12 +- packages/test/v0.7/utils.ts | 89 ++++++---- plugins/permission/toPermissionValidator.ts | 6 +- plugins/permission/types.ts | 4 +- 15 files changed, 327 insertions(+), 143 deletions(-) create mode 100644 packages/test/abis/TokenActionsAbi.ts diff --git a/packages/core/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.ts b/packages/core/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.ts index 270f65b5..bfaf3560 100644 --- a/packages/core/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.ts +++ b/packages/core/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.ts @@ -23,11 +23,7 @@ export const KernelV3ExecuteAbi = [ name: "execute", inputs: [ { name: "execMode", type: "bytes32", internalType: "ExecMode" }, - { - name: "executionCalldata", - type: "bytes", - internalType: "bytes" - } + { name: "executionCalldata", type: "bytes", internalType: "bytes" } ], outputs: [], stateMutability: "payable" @@ -37,11 +33,7 @@ export const KernelV3ExecuteAbi = [ name: "executeFromExecutor", inputs: [ { name: "execMode", type: "bytes32", internalType: "ExecMode" }, - { - name: "executionCalldata", - type: "bytes", - internalType: "bytes" - } + { name: "executionCalldata", type: "bytes", internalType: "bytes" } ], outputs: [ { name: "returnData", type: "bytes[]", internalType: "bytes[]" } @@ -144,11 +136,7 @@ export const KernelV3AccountAbi = [ internalType: "address" }, { name: "salt", type: "bytes32", internalType: "bytes32" }, - { - name: "extensions", - type: "uint256[]", - internalType: "uint256[]" - } + { name: "extensions", type: "uint256[]", internalType: "uint256[]" } ], stateMutability: "view" }, @@ -157,11 +145,7 @@ export const KernelV3AccountAbi = [ name: "entrypoint", inputs: [], outputs: [ - { - name: "", - type: "address", - internalType: "contract IEntryPoint" - } + { name: "", type: "address", internalType: "contract IEntryPoint" } ], stateMutability: "view" }, @@ -170,11 +154,7 @@ export const KernelV3AccountAbi = [ name: "execute", inputs: [ { name: "execMode", type: "bytes32", internalType: "ExecMode" }, - { - name: "executionCalldata", - type: "bytes", - internalType: "bytes" - } + { name: "executionCalldata", type: "bytes", internalType: "bytes" } ], outputs: [], stateMutability: "payable" @@ -184,11 +164,7 @@ export const KernelV3AccountAbi = [ name: "executeFromExecutor", inputs: [ { name: "execMode", type: "bytes32", internalType: "ExecMode" }, - { - name: "executionCalldata", - type: "bytes", - internalType: "bytes" - } + { name: "executionCalldata", type: "bytes", internalType: "bytes" } ], outputs: [ { name: "returnData", type: "bytes[]", internalType: "bytes[]" } @@ -266,16 +242,6 @@ export const KernelV3AccountAbi = [ ], stateMutability: "view" }, - { - type: "function", - name: "fallbackConfig", - inputs: [], - outputs: [ - { name: "", type: "address", internalType: "contract IFallback" }, - { name: "", type: "address", internalType: "contract IHook" } - ], - stateMutability: "view" - }, { type: "function", name: "initialize", @@ -303,6 +269,34 @@ export const KernelV3AccountAbi = [ outputs: [], stateMutability: "payable" }, + { + type: "function", + name: "installValidations", + inputs: [ + { name: "vIds", type: "bytes21[]", internalType: "ValidationId[]" }, + { + name: "configs", + type: "tuple[]", + internalType: "struct ValidationManager.ValidationConfig[]", + components: [ + { name: "nonce", type: "uint32", internalType: "uint32" }, + { + name: "hook", + type: "address", + internalType: "contract IHook" + } + ] + }, + { + name: "validationData", + type: "bytes[]", + internalType: "bytes[]" + }, + { name: "hookData", type: "bytes[]", internalType: "bytes[]" } + ], + outputs: [], + stateMutability: "nonpayable" + }, { type: "function", name: "invalidateNonce", @@ -326,11 +320,7 @@ export const KernelV3AccountAbi = [ inputs: [ { name: "moduleType", type: "uint256", internalType: "uint256" }, { name: "module", type: "address", internalType: "address" }, - { - name: "additionalContext", - type: "bytes", - internalType: "bytes" - } + { name: "additionalContext", type: "bytes", internalType: "bytes" } ], outputs: [{ name: "", type: "bool", internalType: "bool" }], stateMutability: "view" @@ -399,7 +389,16 @@ export const KernelV3AccountAbi = [ type: "address", internalType: "contract IHook" }, - { name: "target", type: "address", internalType: "address" } + { + name: "target", + type: "address", + internalType: "address" + }, + { + name: "callType", + type: "bytes1", + internalType: "CallType" + } ] } ], @@ -425,7 +424,7 @@ export const KernelV3AccountAbi = [ type: "function", name: "uninstallModule", inputs: [ - { name: "", type: "uint256", internalType: "uint256" }, + { name: "moduleType", type: "uint256", internalType: "uint256" }, { name: "module", type: "address", internalType: "address" }, { name: "deInitData", type: "bytes", internalType: "bytes" } ], @@ -437,7 +436,21 @@ export const KernelV3AccountAbi = [ name: "uninstallValidation", inputs: [ { name: "vId", type: "bytes21", internalType: "ValidationId" }, - { name: "deinitData", type: "bytes", internalType: "bytes" } + { name: "deinitData", type: "bytes", internalType: "bytes" }, + { name: "hookDeinitData", type: "bytes", internalType: "bytes" } + ], + outputs: [], + stateMutability: "payable" + }, + { + type: "function", + name: "upgradeTo", + inputs: [ + { + name: "_newImplementation", + type: "address", + internalType: "address" + } ], outputs: [], stateMutability: "payable" @@ -507,7 +520,7 @@ export const KernelV3AccountAbi = [ }, { type: "function", - name: "validatorConfig", + name: "validationConfig", inputs: [ { name: "vId", type: "bytes21", internalType: "ValidationId" } ], @@ -547,6 +560,25 @@ export const KernelV3AccountAbi = [ ], anonymous: false }, + { + type: "event", + name: "ModuleUninstallResult", + inputs: [ + { + name: "module", + type: "address", + indexed: false, + internalType: "address" + }, + { + name: "result", + type: "bool", + indexed: false, + internalType: "bool" + } + ], + anonymous: false + }, { type: "event", name: "ModuleUninstalled", @@ -674,6 +706,19 @@ export const KernelV3AccountAbi = [ ], anonymous: false }, + { + type: "event", + name: "Upgraded", + inputs: [ + { + name: "implementation", + type: "address", + indexed: true, + internalType: "address" + } + ], + anonymous: false + }, { type: "event", name: "ValidatorInstalled", @@ -715,15 +760,15 @@ export const KernelV3AccountAbi = [ { type: "error", name: "InvalidMode", inputs: [] }, { type: "error", name: "InvalidModuleType", inputs: [] }, { type: "error", name: "InvalidNonce", inputs: [] }, + { type: "error", name: "InvalidSelector", inputs: [] }, { type: "error", name: "InvalidSignature", inputs: [] }, { type: "error", name: "InvalidValidationType", inputs: [] }, { type: "error", name: "InvalidValidator", inputs: [] }, + { type: "error", name: "NonceInvalidationError", inputs: [] }, + { type: "error", name: "NotSupportedCallType", inputs: [] }, { type: "error", name: "OnlyExecuteUserOp", inputs: [] }, - { - type: "error", - name: "PermissionNotAlllowedForSignature", - inputs: [] - }, + { type: "error", name: "PermissionDataLengthMismatch", inputs: [] }, + { type: "error", name: "PermissionNotAlllowedForSignature", inputs: [] }, { type: "error", name: "PermissionNotAlllowedForUserOp", inputs: [] }, { type: "error", name: "PolicyDataTooLarge", inputs: [] }, { diff --git a/packages/core/accounts/kernel/createKernelAccount.ts b/packages/core/accounts/kernel/createKernelAccount.ts index 4adc7ec8..10754a73 100644 --- a/packages/core/accounts/kernel/createKernelAccount.ts +++ b/packages/core/accounts/kernel/createKernelAccount.ts @@ -116,7 +116,7 @@ export const KERNEL_ADDRESSES: { ENTRYPOINT_V0_6: Address } = { ACCOUNT_LOGIC_V0_6: "0xd3082872F8B06073A021b4602e022d5A070d7cfC", - ACCOUNT_LOGIC_V0_7: "0x079D5D8A3275A230AF743cB59c341Ff74B82488D", + ACCOUNT_LOGIC_V0_7: "0x541E811D24A54745619a5B9f0c1d4B08D5740144", FACTORY_ADDRESS: "0x5de4839a76cf55d0c90e2061ef4386d962E15ae3", ENTRYPOINT_V0_6: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789" } diff --git a/packages/core/accounts/kernel/utils/account/ep0_7/getKernelV3Nonce.ts b/packages/core/accounts/kernel/utils/account/ep0_7/getKernelV3Nonce.ts index 0159ef60..9690cde6 100644 --- a/packages/core/accounts/kernel/utils/account/ep0_7/getKernelV3Nonce.ts +++ b/packages/core/accounts/kernel/utils/account/ep0_7/getKernelV3Nonce.ts @@ -19,6 +19,6 @@ export const getKernelV3Nonce = async ( }) return nonce } catch (error) { - return 2 + return 1 } } diff --git a/packages/core/accounts/kernel/utils/plugins/ep0_7/getEncodedPluginsData.ts b/packages/core/accounts/kernel/utils/plugins/ep0_7/getEncodedPluginsData.ts index af1fbffd..c13128d7 100644 --- a/packages/core/accounts/kernel/utils/plugins/ep0_7/getEncodedPluginsData.ts +++ b/packages/core/accounts/kernel/utils/plugins/ep0_7/getEncodedPluginsData.ts @@ -35,7 +35,10 @@ export const getEncodedPluginsData = async < [ enableData, "0x", - concat([action.selector, action.address, zeroAddress, "0x"]), + // [TODO]: Integrate custom target contract and hook + concat([ + action.selector /*, action.address, zeroAddress, "0x"*/ + ]), enableSignature, userOpSignature ] diff --git a/packages/core/accounts/kernel/utils/plugins/ep0_7/getPluginsEnableTypedData.ts b/packages/core/accounts/kernel/utils/plugins/ep0_7/getPluginsEnableTypedData.ts index 25e04a1d..d79a6122 100644 --- a/packages/core/accounts/kernel/utils/plugins/ep0_7/getPluginsEnableTypedData.ts +++ b/packages/core/accounts/kernel/utils/plugins/ep0_7/getPluginsEnableTypedData.ts @@ -50,10 +50,12 @@ export const getPluginsEnableTypedData = async < validatorData: await validator.getEnableData(accountAddress), hookData: "0x", selectorData: concat([ - action.selector, + action.selector + /* action.address, zeroAddress, "0x" + */ ]) }, primaryType: "Enable" diff --git a/packages/core/actions/paymaster/sponsorUserOperation.ts b/packages/core/actions/paymaster/sponsorUserOperation.ts index 08fe1b8b..5b8b5de7 100644 --- a/packages/core/actions/paymaster/sponsorUserOperation.ts +++ b/packages/core/actions/paymaster/sponsorUserOperation.ts @@ -10,7 +10,6 @@ import type { import { ENTRYPOINT_ADDRESS_V06, deepHexlify } from "permissionless/utils" import type { Address, Hex } from "viem" import type { PartialBy } from "viem/types/utils" -import { KERNEL_ADDRESSES } from "../../accounts/index.js" import type { ZeroDevPaymasterClient } from "../../clients/paymasterClient.js" export type SponsorUserOperationParameters = { @@ -70,8 +69,7 @@ export const sponsorUserOperation = async ( ) as UserOperationWithBigIntAsHex< GetEntryPointVersion >, - entryPointAddress: - args.entryPoint ?? KERNEL_ADDRESSES.ENTRYPOINT_V0_6, + entryPointAddress: args.entryPoint, gasTokenData: args.gasToken && { tokenAddress: args.gasToken }, diff --git a/packages/core/constants.ts b/packages/core/constants.ts index 93c9d4a3..898b9265 100644 --- a/packages/core/constants.ts +++ b/packages/core/constants.ts @@ -10,7 +10,7 @@ export const KernelImplToVersionMap: { [key: Address]: string } = { "0xD3F582F6B4814E989Ee8E96bc3175320B5A540ab": "0.2.3", "0x5FC0236D6c88a65beD32EECDC5D60a5CAb377717": "0.2.3", "0xd3082872F8B06073A021b4602e022d5A070d7cfC": "0.2.4", - "0x079D5D8A3275A230AF743cB59c341Ff74B82488D": "0.3.0-beta" + "0x541E811D24A54745619a5B9f0c1d4B08D5740144": "0.3.0-beta" } export const TOKEN_ACTION = "0x2087C7FfD0d0DAE80a00EE74325aBF3449e0eaf1" export const KERNEL_NAME = "Kernel" diff --git a/packages/test/abis/TokenActionsAbi.ts b/packages/test/abis/TokenActionsAbi.ts new file mode 100644 index 00000000..5f5e38a4 --- /dev/null +++ b/packages/test/abis/TokenActionsAbi.ts @@ -0,0 +1,37 @@ +export const TokenActionsAbi = [ + { + type: "function", + name: "transferERC1155Action", + inputs: [ + { name: "_token", type: "address", internalType: "address" }, + { name: "_id", type: "uint256", internalType: "uint256" }, + { name: "_to", type: "address", internalType: "address" }, + { name: "amount", type: "uint256", internalType: "uint256" }, + { name: "data", type: "bytes", internalType: "bytes" } + ], + outputs: [], + stateMutability: "nonpayable" + }, + { + type: "function", + name: "transferERC20Action", + inputs: [ + { name: "_token", type: "address", internalType: "address" }, + { name: "_amount", type: "uint256", internalType: "uint256" }, + { name: "_to", type: "address", internalType: "address" } + ], + outputs: [], + stateMutability: "nonpayable" + }, + { + type: "function", + name: "transferERC721Action", + inputs: [ + { name: "_token", type: "address", internalType: "address" }, + { name: "_id", type: "uint256", internalType: "uint256" }, + { name: "_to", type: "address", internalType: "address" } + ], + outputs: [], + stateMutability: "nonpayable" + } +] as const diff --git a/packages/test/config.ts b/packages/test/config.ts index b625a4e6..b3c422b5 100644 --- a/packages/test/config.ts +++ b/packages/test/config.ts @@ -29,3 +29,5 @@ export const config: { } } } + +export const TOKEN_ACTION_ADDRESS = "0xA015bd458DbAc61c254104099f544dB6ef570c7C" diff --git a/packages/test/ecdsaKernelAccount.test.ts b/packages/test/ecdsaKernelAccount.test.ts index 6d1518bc..b21c1a0d 100644 --- a/packages/test/ecdsaKernelAccount.test.ts +++ b/packages/test/ecdsaKernelAccount.test.ts @@ -4,7 +4,6 @@ import { verifyMessage } from "@ambire/signature-validator" import { signerToEcdsaValidator } from "@zerodev/ecdsa-validator" import { EIP1271Abi, - KERNEL_ADDRESSES, KernelAccountClient, KernelSmartAccount, createKernelAccount, @@ -14,20 +13,9 @@ import { import { gasTokenAddresses } from "@zerodev/sdk" import dotenv from "dotenv" import { ethers } from "ethers" -import { - BundlerClient, - ENTRYPOINT_ADDRESS_V06, - bundlerActions -} from "permissionless" -import { - SignTransactionNotSupportedBySmartAccount, - SmartAccount -} from "permissionless/accounts" -import { - ENTRYPOINT_ADDRESS_V06_TYPE, - EntryPoint -} from "permissionless/types/entrypoint.js" -import type { UserOperation } from "permissionless/types/userOperation.js" +import { BundlerClient } from "permissionless" +import { SignTransactionNotSupportedBySmartAccount } from "permissionless/accounts" +import { EntryPoint } from "permissionless/types/entrypoint.js" import { Address, Chain, @@ -43,13 +31,15 @@ import { hashTypedData, zeroAddress } from "viem" -import { privateKeyToAccount, sign } from "viem/accounts" +import { privateKeyToAccount } from "viem/accounts" import { goerli } from "viem/chains" import { EntryPointAbi } from "./abis/EntryPoint.js" import { GreeterAbi, GreeterBytecode } from "./abis/Greeter.js" import { TEST_ERC20Abi } from "./abis/Test_ERC20Abi.js" -import { config } from "./config.js" +import { TokenActionsAbi } from "./abis/TokenActionsAbi.js" +import { TOKEN_ACTION_ADDRESS, config } from "./config.js" import { + Test_ERC20Address, findUserOperationEvent, getEcdsaKernelAccountWithRandomSigner, getEntryPoint, @@ -62,6 +52,7 @@ import { index, waitForNonceUpdate } from "./utils.js" +import { mintToAccount } from "./v0.7/utils.js" dotenv.config() @@ -449,18 +440,65 @@ describe("ECDSA kernel Account", () => { test( "Client send UserOp with delegatecall", async () => { + const accountAddress = kernelClient.account.address + const amountToMint = 10000000n + const amountToTransfer = 4337n + await mintToAccount( + publicClient, + kernelClient, + accountAddress, + amountToMint + ) const userOpHash = await kernelClient.sendUserOperation({ userOperation: { callData: await kernelClient.account.encodeCallData({ - to: zeroAddress, + to: TOKEN_ACTION_ADDRESS, value: 0n, - data: "0x", + data: encodeFunctionData({ + abi: TokenActionsAbi, + functionName: "transferERC20Action", + args: [ + Test_ERC20Address, + amountToTransfer, + "0xA02CDdFa44B8C01b4257F54ac1c43F75801E8175" + ] + }), callType: "delegatecall" }) } }) + const transaction = await bundlerClient.waitForUserOperationReceipt( + { + hash: userOpHash + } + ) + console.log( + "transferTransactionHash", + `https://sepolia.etherscan.io/tx/${transaction.receipt.transactionHash}` + ) + const transactionReceipt = + await publicClient.waitForTransactionReceipt({ + hash: transaction.receipt.transactionHash + }) + let transferEventFound = false + for (const log of transactionReceipt.logs) { + try { + const event = decodeEventLog({ + abi: erc20Abi, + ...log + }) + if ( + event.eventName === "Transfer" && + event.args.from === account.address && + event.args.value === amountToTransfer + ) { + transferEventFound = true + } + } catch (error) {} + } expect(userOpHash).toHaveLength(66) + expect(transferEventFound).toBeTrue() await waitForNonceUpdate() }, diff --git a/packages/test/v0.7/ecdsaKernelAccount.test.ts b/packages/test/v0.7/ecdsaKernelAccount.test.ts index d56c3f27..03a7b8e1 100644 --- a/packages/test/v0.7/ecdsaKernelAccount.test.ts +++ b/packages/test/v0.7/ecdsaKernelAccount.test.ts @@ -7,11 +7,8 @@ import { KernelAccountClient, KernelSmartAccount, createKernelAccount, - getERC20PaymasterApproveCall, verifyEIP6492Signature } from "@zerodev/sdk" -import { gasTokenAddresses } from "@zerodev/sdk" -import { universalValidatorByteCode } from "@zerodev/sdk/accounts" import dotenv from "dotenv" import { ethers } from "ethers" import { BundlerClient } from "permissionless" @@ -21,28 +18,24 @@ import { EntryPoint } from "permissionless/types/entrypoint.js" import { Address, Chain, - Client, GetContractReturnType, Hex, type PublicClient, Transport, - concat, decodeEventLog, - encodeAbiParameters, encodeFunctionData, erc20Abi, getContract, hashMessage, hashTypedData, - parseAbiParameters, zeroAddress } from "viem" import { privateKeyToAccount } from "viem/accounts" -import { goerli } from "viem/chains" import { EntryPointAbi } from "../abis/EntryPoint.js" import { GreeterAbi, GreeterBytecode } from "../abis/Greeter.js" -import { TEST_ERC20Abi } from "../abis/Test_ERC20Abi.js" -import { config } from "../config.js" +import { TokenActionsAbi } from "../abis/TokenActionsAbi.js" +import { TOKEN_ACTION_ADDRESS, config } from "../config.js" +import { Test_ERC20Address } from "../utils.js" import { findUserOperationEvent, getEcdsaKernelAccountWithRandomSigner, @@ -50,14 +43,12 @@ import { getKernelAccountClient, getKernelBundlerClient, getPimlicoBundlerClient, - getPimlicoPaymasterClient, getPublicClient, getSignerToEcdsaKernelAccount, - getZeroDevERC20PaymasterClient, getZeroDevPaymasterClient, index, - waitForNonceUpdate, - waitForUserOperationTransaction + mintToAccount, + waitForNonceUpdate } from "./utils.js" dotenv.config() @@ -110,9 +101,11 @@ describe("ECDSA kernel Account", () => { typeof kernelClient, Address > + let owner: Address beforeAll(async () => { account = await getSignerToEcdsaKernelAccount() + owner = privateKeyToAccount(process.env.TEST_PRIVATE_KEY as Hex).address publicClient = await getPublicClient() bundlerClient = getKernelBundlerClient() pimlicoBundlerClient = getPimlicoBundlerClient() @@ -484,22 +477,61 @@ describe("ECDSA kernel Account", () => { test( "Client send UserOp with delegatecall", async () => { + const accountAddress = kernelClient.account.address + const amountToMint = 10000000n + const amountToTransfer = 4337n + await mintToAccount( + publicClient, + kernelClient, + accountAddress, + amountToMint + ) const userOpHash = await kernelClient.sendUserOperation({ userOperation: { callData: await kernelClient.account.encodeCallData({ - to: zeroAddress, + to: TOKEN_ACTION_ADDRESS, value: 0n, - data: "0x", + data: encodeFunctionData({ + abi: TokenActionsAbi, + functionName: "transferERC20Action", + args: [Test_ERC20Address, amountToTransfer, owner] + }), callType: "delegatecall" }) } }) - const txHash = await bundlerClient.waitForUserOperationReceipt({ - hash: userOpHash - }) - console.log("txHash", txHash) + const transaction = await bundlerClient.waitForUserOperationReceipt( + { + hash: userOpHash + } + ) + console.log( + "transferTransactionHash", + `https://sepolia.etherscan.io/tx/${transaction.receipt.transactionHash}` + ) + const transactionReceipt = + await publicClient.waitForTransactionReceipt({ + hash: transaction.receipt.transactionHash + }) + let transferEventFound = false + for (const log of transactionReceipt.logs) { + try { + const event = decodeEventLog({ + abi: erc20Abi, + ...log + }) + if ( + event.eventName === "Transfer" && + event.args.from === account.address && + event.args.value === amountToTransfer + ) { + transferEventFound = true + } + } catch (error) {} + } expect(userOpHash).toHaveLength(66) + expect(transferEventFound).toBeTrue() await waitForNonceUpdate() }, diff --git a/packages/test/v0.7/permissionValidator.test.ts b/packages/test/v0.7/permissionValidator.test.ts index c8a406de..97c0587c 100644 --- a/packages/test/v0.7/permissionValidator.test.ts +++ b/packages/test/v0.7/permissionValidator.test.ts @@ -5,7 +5,6 @@ import { EIP1271Abi, KernelAccountClient, KernelSmartAccount, - KernelV3AccountAbi, verifyEIP6492Signature } from "@zerodev/sdk" import { ethers } from "ethers" @@ -19,12 +18,10 @@ import { PrivateKeyAccount, PublicClient, Transport, - decodeAbiParameters, - decodeErrorResult, - decodeFunctionData, encodeFunctionData, hashMessage, hashTypedData, + parseEther, zeroAddress } from "viem" import { privateKeyToAccount } from "viem/accounts" @@ -147,11 +144,12 @@ describe("Permission kernel Account", () => { } }) gasPolicy = await toGasPolicy({ - allowed: 1000000000000000000n + allowed: parseEther("10") }) + const sudoPolicy = await toSudoPolicy({}) permissionSmartAccountClient = await getKernelAccountClient({ - account: await getSignerToRootPermissionKernelAccount([gasPolicy]), + account: await getSignerToRootPermissionKernelAccount([sudoPolicy]), middleware: { gasPrice: async () => (await pimlicoBundlerClient.getUserOperationGasPrice()) @@ -563,7 +561,7 @@ describe("Permission kernel Account", () => { } }) - await sleep(5000) + await sleep(2 * 5000) const response = await permissionSmartAccountClient.sendTransaction( { diff --git a/packages/test/v0.7/utils.ts b/packages/test/v0.7/utils.ts index 20fcdd40..0c0ca38c 100644 --- a/packages/test/v0.7/utils.ts +++ b/packages/test/v0.7/utils.ts @@ -1,7 +1,4 @@ -import { - KernelValidator, - signerToEcdsaValidator -} from "@zerodev/ecdsa-validator" +import { signerToEcdsaValidator } from "@zerodev/ecdsa-validator" import { KernelAccountClient, KernelSmartAccount, @@ -24,44 +21,37 @@ import { import type { EntryPoint } from "permissionless/types/entrypoint" import { http, + Address, Chain, Hash, Hex, Log, - PrivateKeyAccount, PublicClient, Transport, createPublicClient, decodeEventLog, + encodeFunctionData, getAbiItem, toFunctionSelector, zeroAddress } from "viem" import { generatePrivateKey, privateKeyToAccount } from "viem/accounts" -import { goerli, polygonMumbai } from "viem/chains" +import { polygonMumbai } from "viem/chains" import * as allChains from "viem/chains" -import { - toSignatureCallerPolicy, - toSudoPolicy -} from "../../../plugins/permission/policies" -import { toCallPolicy } from "../../../plugins/permission/policies/toCallPolicy" -import { toGasPolicy } from "../../../plugins/permission/policies/toGasPolicy" -import { toRateLimitPolicy } from "../../../plugins/permission/policies/toRateLimitPolicy" -import { ParamCondition } from "../../../plugins/permission/policies/types" import { toECDSASigner } from "../../../plugins/permission/signers/toECDSASigner" import { toPermissionValidator } from "../../../plugins/permission/toPermissionValidator" import { Policy } from "../../../plugins/permission/types" import { EntryPointAbi } from "../abis/EntryPoint" -import { TEST_ERC20Abi } from "../abis/Test_ERC20Abi" -import { Test_ERC20Address } from "../utils" import { deserializePermissionAccount } from "../../../plugins/permission/deserializePermissionAccount.js" import { serializePermissionAccount } from "../../../plugins/permission/serializePermissionAccount.js" +import { TEST_ERC20Abi } from "../abis/Test_ERC20Abi.js" import { config } from "../config.js" +import { Test_ERC20Address } from "../utils.js" // export const index = 43244782332432423423n -export const index = 4323343756544387823332432423423n +export const index = 43233437532434365532464445487823332432423423n const DEFAULT_PROVIDER = "PIMLICO" const projectId = config["v0.7"].sepolia.projectId @@ -282,7 +272,7 @@ export const getSignerToEcdsaKernelAccount = async < const getBundlerRpc = (provider?: string): string => { const zeroDevProjectId = projectId - const zeroDevBundlerRpcHost = process.env.ZERODEV_BUNDLER_RPC_HOST + const zeroDevBundlerRpcHost = config["v0.7"].sepolia.bundlerUrl if (!zeroDevProjectId || !zeroDevBundlerRpcHost) { throw new Error( "ZERODEV_PROJECT_ID and ZERODEV_BUNDLER_RPC_HOST environment variables must be set" @@ -414,12 +404,6 @@ export const getSignerToPermissionKernelAccount = async ( export const getSignerToRootPermissionKernelAccount = async ( policies: Policy[] ): Promise> => { - const privateKey1 = process.env.TEST_PRIVATE_KEY as Hex - if (!privateKey1) { - throw new Error( - "TEST_PRIVATE_KEY and TEST_PRIVATE_KEY2 environment variables must be set" - ) - } const publicClient = await getPublicClient() const signer1 = privateKeyToAccount(generatePrivateKey()) const ecdsaModularSigner = toECDSASigner({ signer: signer1 }) @@ -447,12 +431,6 @@ export const getSignerToRootPermissionKernelAccount = async ( export const getSignerToRootPermissionWithSecondaryValidatorKernelAccount = async (policies: Policy[]): Promise> => { - const privateKey1 = process.env.TEST_PRIVATE_KEY as Hex - if (!privateKey1) { - throw new Error( - "TEST_PRIVATE_KEY and TEST_PRIVATE_KEY2 environment variables must be set" - ) - } const publicClient = await getPublicClient() const signer1 = privateKeyToAccount(generatePrivateKey()) const ecdsaModularSigner = toECDSASigner({ signer: signer1 }) @@ -500,3 +478,54 @@ export const getSignerToRootPermissionWithSecondaryValidatorKernelAccount = ) return account } + +export async function mintToAccount( + publicClient: PublicClient, + ecdsaSmartAccountClient: KernelAccountClient< + EntryPoint, + Transport, + Chain, + KernelSmartAccount + >, + target: Address, + amount: bigint +) { + const balanceBefore = await publicClient.readContract({ + abi: TEST_ERC20Abi, + address: Test_ERC20Address, + functionName: "balanceOf", + args: [target] + }) + + console.log("balanceBefore of account", balanceBefore) + + const amountToMint = balanceBefore > amount ? 0n : amount + + const mintData = encodeFunctionData({ + abi: TEST_ERC20Abi, + functionName: "mint", + args: [target, amountToMint] + }) + + if (amountToMint > 0n) { + const mintTransactionHash = + await ecdsaSmartAccountClient.sendTransaction({ + to: Test_ERC20Address, + data: mintData + }) + + const balanceAfter = await publicClient.readContract({ + abi: TEST_ERC20Abi, + address: Test_ERC20Address, + functionName: "balanceOf", + args: [target] + }) + + console.log("balanceAfter of account", balanceAfter) + + console.log( + "mintTransactionHash", + `https://sepolia.etherscan.io/tx/${mintTransactionHash}` + ) + } +} diff --git a/plugins/permission/toPermissionValidator.ts b/plugins/permission/toPermissionValidator.ts index cb6a5de3..b7dc1ce3 100644 --- a/plugins/permission/toPermissionValidator.ts +++ b/plugins/permission/toPermissionValidator.ts @@ -58,13 +58,13 @@ export async function toPermissionValidator< ...policies.map((policy) => concat([ policy.getPolicyInfoInBytes(), - policy.getPolicyData(getPermissionId()) + policy.getPolicyData() ]) ), concat([ flag, signer.signerContractAddress, - signer.getSignerData(getPermissionId()) + signer.getSignerData() ]) ] ] @@ -105,7 +105,7 @@ export async function toPermissionValidator< const userOpHash = getUserOperationHash({ userOperation: { ...userOperation, signature: "0x" }, entryPoint: entryPointAddress, - chainId: chainId + chainId }) const signature = await signer.account.signMessage({ diff --git a/plugins/permission/types.ts b/plugins/permission/types.ts index c8571579..da80b4c6 100644 --- a/plugins/permission/types.ts +++ b/plugins/permission/types.ts @@ -29,12 +29,12 @@ export type PolicyParams = { export type ModularSigner = { account: LocalAccount signerContractAddress: Address - getSignerData: (permissionId?: Hex) => Hex + getSignerData: () => Hex getDummySignature: () => Hex } export type Policy = { - getPolicyData: (permissionId?: Hex) => Hex + getPolicyData: () => Hex getPolicyInfoInBytes: () => Hex // return params directly to serialize/deserialize Policy policyParams: From 64098e28638f448241bcd5a6351301c82be53cfd Mon Sep 17 00:00:00 2001 From: Sahil Vasava Date: Mon, 8 Apr 2024 17:58:25 +0530 Subject: [PATCH 2/2] chore: bump version and release core and permissions --- packages/core/package.json | 2 +- plugins/permission/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 58f3ddc5..91073b73 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@zerodev/sdk", - "version": "5.2.1-alpha-2", + "version": "5.2.1-alpha-3", "author": "ZeroDev", "main": "./_cjs/index.js", "module": "./_esm/index.js", diff --git a/plugins/permission/package.json b/plugins/permission/package.json index 805ae24c..3921b239 100644 --- a/plugins/permission/package.json +++ b/plugins/permission/package.json @@ -1,6 +1,6 @@ { "name": "@zerodev/permissions", - "version": "5.2.1-alpha.7", + "version": "5.2.1-alpha.8", "author": "ZeroDev", "main": "./_cjs/index.js", "module": "./_esm/index.js",