Skip to content

Commit

Permalink
feat: add CABv2.1 support to session-account
Browse files Browse the repository at this point in the history
  • Loading branch information
SahilVasava committed Oct 17, 2024
1 parent 05480fc commit bf10d47
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 30 deletions.
Binary file modified bun.lockb
Binary file not shown.
33 changes: 19 additions & 14 deletions packages/test/v0.7/multiTenantSessionAccount.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-expect-error
import { beforeAll, describe, test } from "bun:test"
import { createKernelCABClient } from "@zerodev/cab"
import { CAB_V0_2_1, createKernelCABClient } from "@zerodev/cab"
import {
type KernelAccountClient,
type KernelSmartAccount,
Expand All @@ -14,6 +14,7 @@ import {
getInstallDMAsExecutorCallData
} from "@zerodev/session-account"
import {
type ENFORCER_VERSION,
ParamCondition,
toAllowedParamsEnforcer
} from "@zerodev/session-account/enforcers"
Expand Down Expand Up @@ -82,6 +83,7 @@ describe("Yi SubAccount", () => {
KernelSmartAccount<ENTRYPOINT_ADDRESS_V07_TYPE, Transport, Chain>
>
let delegations: Delegation[]
const enforcerVersion: ENFORCER_VERSION = "v0_2"

beforeAll(async () => {
const ownerPrivateKey = process.env.TEST_PRIVATE_KEY
Expand All @@ -103,12 +105,12 @@ describe("Yi SubAccount", () => {
}
})

// const installTx = await kernelClient.sendTransaction({
// to: kernelClient.account.address,
// value: 0n,
// data: getInstallDMAsExecutorCallData(),
// })
// console.log({ installTx })
const installTx = await kernelClient.sendTransaction({
to: kernelClient.account.address,
value: 0n,
data: getInstallDMAsExecutorCallData()
})
console.log({ installTx })

bundlerClient = kernelClient.extend(bundlerActions(getEntryPoint()))
})
Expand Down Expand Up @@ -140,7 +142,7 @@ describe("Yi SubAccount", () => {
Transport,
Chain
>
>()
>({ enforcerVersion })
)

const mainDeleGatorSignature = await kernelClientDM.signDelegation({
Expand Down Expand Up @@ -234,7 +236,7 @@ describe("Yi SubAccount", () => {
Transport,
Chain
>
>()
>({ enforcerVersion })
)

const installDMAndDelegateHash =
Expand Down Expand Up @@ -310,7 +312,8 @@ describe("Yi SubAccount", () => {
const kernelCabClient = createKernelCABClient(kernelClient, {
transport: http(CAB_PAYMASTER_SERVER_URL),
entryPoint: getEntryPoint(),
chain: sepolia
chain: sepolia,
cabVersion: CAB_V0_2_1
})
console.log("kernelCabClient addr", kernelCabClient.account.address)
await kernelCabClient.enableCAB({
Expand Down Expand Up @@ -374,7 +377,8 @@ describe("Yi SubAccount", () => {
{
transport: http(CAB_PAYMASTER_SERVER_URL),
entryPoint: getEntryPoint(),
chain: optimismSepolia
chain: optimismSepolia,
cabVersion: CAB_V0_2_1
}
)
console.log(
Expand Down Expand Up @@ -425,7 +429,8 @@ describe("Yi SubAccount", () => {
]
})
const cabCaveat = await toCABPaymasterEnforcer({
accountAddress: mainDelegatorAccount.address
accountAddress: mainDelegatorAccount.address,
enforcerVersion
})
const caveats = [cabCaveat]

Expand All @@ -450,7 +455,7 @@ describe("Yi SubAccount", () => {
Transport,
Chain
>
>()
>({ enforcerVersion })
)

const mainDeleGatorSignature = await kernelClientDM.signDelegation({
Expand Down Expand Up @@ -501,7 +506,7 @@ describe("Yi SubAccount", () => {
Transport,
Chain
>
>()
>({ enforcerVersion })
)
const repayTokens = [
{
Expand Down
5 changes: 3 additions & 2 deletions packages/test/v0.7/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ export const getEcdsaKernelAccountWithRandomSigner = async (
chain?: number
) => {
return getEcdsaKernelAccountWithPrivateKey(
// "0x3688628d97b817ee5e25dfce254ba4d87b5fd894449fce6c2acc60fdf98906de" ??
generatePrivateKey(),
"0xdfbb0d855aafff58aa0ae92aa9d03e88562bad9befe209f5693db89b65cc4a9a" ??
"0x3688628d97b817ee5e25dfce254ba4d87b5fd894449fce6c2acc60fdf98906de" ??
generatePrivateKey(),
initConfig,
chain
)
Expand Down
7 changes: 7 additions & 0 deletions plugins/multi-tenant-session-account/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @zerodev/session-account

## 5.3.1

### Patch Changes

- Added support for CABv2.1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ import {
encodeCABEnforcerArgs,
encodePaymasterTokens
} from "../enforcers/cab-paymaster/index.js"
import { CABPaymasterEnforcerAddress } from "../enforcers/cab-paymaster/toCABPaymasterEnforcer.js"
import {
type ENFORCER_VERSION,
getEnforcerAddress
} from "../enforcers/cab-paymaster/toCABPaymasterEnforcer.js"
import { toDelegationHash } from "../utils/index.js"

export type EncodeCallDataWithCABParameters<
Expand All @@ -66,6 +69,7 @@ export type EncodeCallDataWithCABParameters<
GetChainParameter<TChain, TChainOverride> & {
calls: { to: Address; value: bigint; data: Hex }[]
repayTokens: RepayTokens
enforcerVersion?: ENFORCER_VERSION
}
>

Expand All @@ -89,6 +93,9 @@ export async function encodeCallDataWithCAB<
TChainOverride
>
): Promise<Hash> {
const cabEnforcerAddress = getEnforcerAddress(
args.enforcerVersion ?? "v0_2"
)
const { account: account_ = client.account, calls, repayTokens } = args

if (!account_) {
Expand Down Expand Up @@ -183,15 +190,13 @@ export async function encodeCallDataWithCAB<
// @ts-expect-error
repayTokens,
// @ts-expect-error
CABPaymasterEnforcerAddress
cabEnforcerAddress
]
})
const cabEnforcerIndex = account.delegations[
account.delegations.length - 1
].caveats.findIndex(
(c) =>
c.enforcer.toLowerCase() ===
CABPaymasterEnforcerAddress.toLowerCase()
(c) => c.enforcer.toLowerCase() === cabEnforcerAddress.toLowerCase()
)
const cabEnforcer =
account.delegations[account.delegations.length - 1].caveats[
Expand Down Expand Up @@ -334,7 +339,7 @@ export async function encodeCallDataWithCAB<
// @ts-expect-error
repayTokenDataEncoded,
// @ts-expect-error
CABPaymasterEnforcerAddress
cabEnforcerAddress
]
})
if (cabEnforcer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
installDMAsExecutor,
signDelegation
} from "../../actions/index.js"
import type { ENFORCER_VERSION } from "../../enforcers/cab-paymaster/toCABPaymasterEnforcer.js"

export type DMActionsEip7710<
TEntryPoint extends EntryPoint,
Expand Down Expand Up @@ -77,7 +78,11 @@ const dmActionsEip7710 =
| undefined =
| SmartAccount<TEntryPoint, string, TTransport, TChain>
| undefined
>() =>
>({
enforcerVersion
}: {
enforcerVersion: ENFORCER_VERSION
}) =>
(
client: Client<TTransport, TChain, TAccount>
): DMActionsEip7710<TEntryPoint, TTransport, TChain, TAccount> => ({
Expand All @@ -97,7 +102,8 @@ const dmActionsEip7710 =
encodeCallDataWithCAB(
client as Client<TTransport, TChain, TAccount>,
{
...args
...args,
enforcerVersion: enforcerVersion
} as EncodeCallDataWithCABParameters<
TEntryPoint,
TTransport,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export {
CABPaymasterEnforcerAddress,
CABPaymasterEnforcerAddressV0_1,
CABPaymasterEnforcerAddressV0_2,
type ENFORCER_VERSION,
getEnforcerAddress,
type CABPaymasterEnforcerArgs,
type CABPaymasterEnforcerParams,
toCABPaymasterEnforcer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export type CABPaymasterEnforcerArgs = {
paymasterSignature: Hex
}

export type ENFORCER_VERSION = "v0_1" | "v0_2"

export type CABPaymasterEnforcerParams = {
accountAddress: Address
enforcerAddress?: Address
enforcerVersion: ENFORCER_VERSION
}

export type Allowance = {
Expand All @@ -30,13 +32,25 @@ export type Allowance = {
vaults: { chainId: bigint; vault: Address; multiplier: bigint }[]
}

export const CABPaymasterEnforcerAddress =
export const CABPaymasterEnforcerAddressV0_1 =
"0x78b09791499931CC36919Ef6A38BEC8B569E7f57"

export const CABPaymasterEnforcerAddressV0_2 =
"0x9A3b8B3eAEDf076956b12A5d1a0248FDD2CA9E78"

export const getEnforcerAddress = (version: ENFORCER_VERSION) => {
if (version === "v0_1") {
return CABPaymasterEnforcerAddressV0_1
} else {
return CABPaymasterEnforcerAddressV0_2
}
}

export async function toCABPaymasterEnforcer({
accountAddress,
enforcerAddress = CABPaymasterEnforcerAddress
enforcerVersion = "v0_2"
}: CABPaymasterEnforcerParams): Promise<Caveat> {
const enforcerAddress = getEnforcerAddress(enforcerVersion)
const cabClient = createPublicClient({
transport: http(CAB_PAYMASTER_SERVER_URL)
})
Expand Down
4 changes: 2 additions & 2 deletions plugins/multi-tenant-session-account/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zerodev/session-account",
"version": "5.3.0",
"version": "5.3.1",
"author": "ZeroDev",
"main": "./_cjs/index.js",
"module": "./_esm/index.js",
Expand Down Expand Up @@ -67,7 +67,7 @@
"viem": ">=2.16.3 <2.18.0",
"permissionless": ">=0.1.44 <=0.1.45",
"@zerodev/sdk": "^5.3.14",
"@zerodev/cab": "0.0.18"
"@zerodev/cab": "^0.0.25"
},
"dependencies": {
"semver": "^7.6.0"
Expand Down

0 comments on commit bf10d47

Please sign in to comment.