Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/wallet permission #169

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @zerodev/wallet

## 0.1.4

### Patch Changes

- fix: policy parsing in grantPermission

## 0.1.3

### Patch Changes
Expand Down
3 changes: 2 additions & 1 deletion wallet/KernelEIP1193Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion wallet/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions wallet/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type {
Permission,
PaymasterServiceCapability,
SendCallsParams,
SendCallsResult,
Expand Down
25 changes: 25 additions & 0 deletions wallet/types/policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { OneOf } from "viem"

/** @internal */
export type GasLimitPolicy<uint256 = bigint> = {
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<amount = bigint> = OneOf<
GasLimitPolicy<amount> | RateLimitPolicy
>
16 changes: 10 additions & 6 deletions wallet/types/provider.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -44,13 +45,16 @@ export type GetCallsResult = {
export type ShowCallsParams = string

// wallet_issuePermissions
export type Permission = {
type: string
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
data: Record<string, any>
required: boolean
policies: Policy[]
}

export type GrantPermissionsParams = {
permissions: {
type: string
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
data: Record<string, any>
required: boolean
}[]
permissions: Permission[]
expiry: number
}

Expand Down
65 changes: 41 additions & 24 deletions wallet/utils/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
type RateLimitPolicyParams,
type SignatureCallerPolicyParams,
toCallPolicy,
toGasPolicy,
Expand All @@ -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,
Expand All @@ -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 = (
Expand Down
Loading