From 8556ddfd040ec227de86d77226f060baa5d6fd55 Mon Sep 17 00:00:00 2001 From: jstinhw Date: Wed, 17 Jul 2024 14:07:40 +0800 Subject: [PATCH 1/2] fix: policy parsing in grantPermission --- wallet/KernelEIP1193Provider.ts | 3 +- wallet/types/index.ts | 1 + wallet/types/policy.ts | 25 +++++++++++++ wallet/types/provider.ts | 16 +++++--- wallet/utils/permissions.ts | 65 +++++++++++++++++++++------------ 5 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 wallet/types/policy.ts diff --git a/wallet/KernelEIP1193Provider.ts b/wallet/KernelEIP1193Provider.ts index d6ef8836..cd5dfb7e 100644 --- a/wallet/KernelEIP1193Provider.ts +++ b/wallet/KernelEIP1193Provider.ts @@ -482,7 +482,8 @@ export class KernelEIP1193Provider< return { grantedPermissions: permissions.map((permission) => ({ type: permission.type, - data: permission.data + data: permission.data, + policies: permission.policies })), expiry: params[0].expiry, permissionsContext: permissionValidator.getIdentifier() diff --git a/wallet/types/index.ts b/wallet/types/index.ts index 566b9c0e..e3052da7 100644 --- a/wallet/types/index.ts +++ b/wallet/types/index.ts @@ -1,4 +1,5 @@ export type { + Permission, PaymasterServiceCapability, SendCallsParams, SendCallsResult, diff --git a/wallet/types/policy.ts b/wallet/types/policy.ts new file mode 100644 index 00000000..0f939271 --- /dev/null +++ b/wallet/types/policy.ts @@ -0,0 +1,25 @@ +import type { OneOf } from "viem" + +/** @internal */ +export type GasLimitPolicy = { + type: "gas-limit" + data: { + /** Gas limit (in wei). */ + limit: uint256 + } +} + +/** @internal */ +export type RateLimitPolicy = { + type: "rate-limit" + data: { + /** Number of times during each interval. */ + count: number + /** Interval (in seconds). */ + interval: number + } +} + +export type Policy = OneOf< + GasLimitPolicy | RateLimitPolicy +> diff --git a/wallet/types/provider.ts b/wallet/types/provider.ts index 1da94a95..88ea1e85 100644 --- a/wallet/types/provider.ts +++ b/wallet/types/provider.ts @@ -1,5 +1,6 @@ import type { EntryPoint } from "permissionless/types" import type { Address, Hex } from "viem" +import type { Policy } from "./policy" export type PaymasterServiceCapability = { url: string @@ -44,13 +45,16 @@ export type GetCallsResult = { export type ShowCallsParams = string // wallet_issuePermissions +export type Permission = { + type: string + // biome-ignore lint/suspicious/noExplicitAny: + data: Record + required: boolean + policies: Policy[] +} + export type GrantPermissionsParams = { - permissions: { - type: string - // biome-ignore lint/suspicious/noExplicitAny: - data: Record - required: boolean - }[] + permissions: Permission[] expiry: number } diff --git a/wallet/utils/permissions.ts b/wallet/utils/permissions.ts index e02b08cc..15a7377f 100644 --- a/wallet/utils/permissions.ts +++ b/wallet/utils/permissions.ts @@ -1,5 +1,4 @@ import { - type RateLimitPolicyParams, type SignatureCallerPolicyParams, toCallPolicy, toGasPolicy, @@ -10,7 +9,7 @@ import { } from "@zerodev/permissions/policies" import type { Policy } from "@zerodev/permissions/types" import { type Address, toHex } from "viem" -import type { GrantPermissionsParams, SessionType } from "../types" +import type { GrantPermissionsParams, Permission, SessionType } from "../types" export const validatePermissions = ( permissionsParams: GrantPermissionsParams, @@ -31,38 +30,56 @@ export const validatePermissions = ( } } +export const getPermissionPoliciy = (permission: Permission): Policy[] => { + const policies: Policy[] = [] + switch (permission.type) { + case "sudo": + policies.push(toSudoPolicy({})) + break + case "contract-call": + policies.push(toCallPolicy(permission.data)) + break + case "signature": + policies.push( + toSignatureCallerPolicy( + permission.data as SignatureCallerPolicyParams + ) + ) + break + default: + break + } + for (const policy of permission.policies) { + switch (policy.type) { + case "gas-limit": + policies.push( + toGasPolicy({ + allowed: policy.data.limit + }) + ) + break + case "rate-limit": + policies.push(toRateLimitPolicy(policy.data)) + break + default: + break + } + } + return policies +} + export const getPolicies = ( permissionsParams: GrantPermissionsParams ): Policy[] => { const policies = permissionsParams.permissions - .map((permission) => { - switch (permission.type) { - case "sudo": - return toSudoPolicy({}) - case "contract-call": - return toCallPolicy(permission.data) - case "rate-limit": - return toRateLimitPolicy( - permission.data as RateLimitPolicyParams - ) - case "gas-limit": - return toGasPolicy(permission.data) - case "signature": - return toSignatureCallerPolicy( - permission.data as SignatureCallerPolicyParams - ) - default: - return undefined - } - }) + .flatMap((permission) => getPermissionPoliciy(permission)) .concat([ toTimestampPolicy({ validAfter: Math.floor(new Date().valueOf() / 1000), validUntil: permissionsParams.expiry }) ]) - - return policies.filter((p) => p !== undefined) as Policy[] + return policies } export const isSessionValid = ( From 7cc29eb9abac5d5af9ca4abcd529be080c1e11c9 Mon Sep 17 00:00:00 2001 From: jstinhw Date: Wed, 17 Jul 2024 14:08:02 +0800 Subject: [PATCH 2/2] chore: bump @zerodev/wallet to 0.1.4 --- wallet/CHANGELOG.md | 6 ++++++ wallet/package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/wallet/CHANGELOG.md b/wallet/CHANGELOG.md index 0c87d368..77a4bdb8 100644 --- a/wallet/CHANGELOG.md +++ b/wallet/CHANGELOG.md @@ -1,5 +1,11 @@ # @zerodev/wallet +## 0.1.4 + +### Patch Changes + +- fix: policy parsing in grantPermission + ## 0.1.3 ### Patch Changes diff --git a/wallet/package.json b/wallet/package.json index 36c8dd67..e016f581 100644 --- a/wallet/package.json +++ b/wallet/package.json @@ -1,6 +1,6 @@ { "name": "@zerodev/wallet", - "version": "0.1.3", + "version": "0.1.4", "author": "ZeroDev", "main": "./_cjs/index.js", "module": "./_esm/index.js",