@zerodev/sdk 4.1.5 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,7 +38,11 @@ import { Operation } from "../provider.js";
38
38
  import { getChainId } from "../api/index.js";
39
39
  import { fixSignedData } from "../utils.js";
40
40
  import type { GetAbiItemReturnType } from "viem/dist/types/utils/abi/getAbiItem.js";
41
- import type { GeneratePermissionFromArgsParameters } from "./types.js";
41
+ import { type AbiFunction } from "abitype";
42
+ import type {
43
+ CombinedArgs,
44
+ GeneratePermissionFromArgsParameters,
45
+ } from "./types.js";
42
46
 
43
47
  // We need to be able to serialize bigint to transmit session key over
44
48
  // the network.
@@ -62,7 +66,7 @@ export type SessionKeyParams = Pick<
62
66
  accountAddress?: Address;
63
67
  };
64
68
 
65
- export enum ParamCondition {
69
+ export enum ParamOperator {
66
70
  EQUAL = 0,
67
71
  GREATER_THAN = 1,
68
72
  LESS_THAN = 2,
@@ -73,14 +77,14 @@ export enum ParamCondition {
73
77
 
74
78
  export interface ParamRules {
75
79
  offset: number;
76
- condition: ParamCondition;
80
+ condition: ParamOperator;
77
81
  param: Hex;
78
82
  }
79
83
 
80
84
  export type Permission = {
81
85
  target: Address;
82
- sig: Hex;
83
- rules: ParamRules[];
86
+ rules?: ParamRules[];
87
+ sig?: Hex;
84
88
  valueLimit?: bigint;
85
89
  operation?: Operation;
86
90
  };
@@ -136,7 +140,6 @@ export function getPermissionFromABI<
136
140
  abi,
137
141
  target,
138
142
  args,
139
- conditions,
140
143
  functionName,
141
144
  operation,
142
145
  valueLimit,
@@ -151,24 +154,22 @@ export function getPermissionFromABI<
151
154
  }
152
155
  const functionSelector = getFunctionSelector(abiItem);
153
156
  let paramRules: ParamRules[] = [];
154
- if (
155
- args &&
156
- Array.isArray(args) &&
157
- conditions &&
158
- Array.isArray(conditions) &&
159
- args.length === conditions.length
160
- ) {
161
- paramRules = args
162
- .map<ParamRules>(
157
+ if (args && Array.isArray(args)) {
158
+ paramRules = (args as CombinedArgs<AbiFunction["inputs"]>)
159
+ .map(
163
160
  (arg, i) =>
164
- arg &&
165
- conditions && {
166
- param: pad(isHex(arg) ? arg : toHex(arg), { size: 32 }),
161
+ arg && {
162
+ param: pad(
163
+ isHex(arg.value)
164
+ ? arg.value
165
+ : toHex(arg.value as Parameters<typeof toHex>[0]),
166
+ { size: 32 }
167
+ ),
167
168
  offset: i * 32,
168
- condition: conditions[i],
169
+ condition: arg.operator,
169
170
  }
170
171
  )
171
- .filter((rule) => rule);
172
+ .filter((rule) => rule) as ParamRules[];
172
173
  }
173
174
  return {
174
175
  sig: functionSelector,
@@ -198,6 +199,8 @@ export class SessionKeyValidator extends KernelBaseValidator {
198
199
  ...perm,
199
200
  operation: perm.operation ?? Operation.Call,
200
201
  valueLimit: perm.valueLimit ?? 0n,
202
+ sig: perm.sig ?? pad("0x", { size: 4 }),
203
+ rules: perm.rules ?? [],
201
204
  })
202
205
  );
203
206
  this.merkleTree = this.getMerkleTree();
@@ -305,7 +308,8 @@ export class SessionKeyValidator extends KernelBaseValidator {
305
308
  signature: string
306
309
  ): Permission[] {
307
310
  return permissions.filter(
308
- (permission) => permission.sig.toLowerCase() === signature
311
+ (permission) =>
312
+ (permission.sig ?? pad("0x", { size: 4 })).toLowerCase() === signature
309
313
  );
310
314
  }
311
315
 
@@ -314,7 +318,7 @@ export class SessionKeyValidator extends KernelBaseValidator {
314
318
  data: string
315
319
  ): Permission | undefined {
316
320
  return permissions.find((permission) => {
317
- for (const rule of permission.rules) {
321
+ for (const rule of permission.rules ?? []) {
318
322
  const dataParam: Hex = this.getFormattedHex(
319
323
  "0x" + data.slice(10 + rule.offset * 2, 10 + rule.offset * 2 + 64)
320
324
  );
@@ -337,20 +341,20 @@ export class SessionKeyValidator extends KernelBaseValidator {
337
341
  private evaluateRuleCondition(
338
342
  dataParam: Hex,
339
343
  ruleParam: Hex,
340
- condition: ParamCondition
344
+ condition: ParamOperator
341
345
  ): boolean {
342
346
  switch (condition) {
343
- case ParamCondition.EQUAL:
347
+ case ParamOperator.EQUAL:
344
348
  return dataParam === ruleParam;
345
- case ParamCondition.GREATER_THAN:
349
+ case ParamOperator.GREATER_THAN:
346
350
  return dataParam > ruleParam;
347
- case ParamCondition.LESS_THAN:
351
+ case ParamOperator.LESS_THAN:
348
352
  return dataParam < ruleParam;
349
- case ParamCondition.GREATER_THAN_OR_EQUAL:
353
+ case ParamOperator.GREATER_THAN_OR_EQUAL:
350
354
  return dataParam >= ruleParam;
351
- case ParamCondition.LESS_THAN_OR_EQUAL:
355
+ case ParamOperator.LESS_THAN_OR_EQUAL:
352
356
  return dataParam <= ruleParam;
353
- case ParamCondition.NOT_EQUAL:
357
+ case ParamOperator.NOT_EQUAL:
354
358
  return dataParam !== ruleParam;
355
359
  default:
356
360
  return false;
@@ -30,7 +30,7 @@ import type {
30
30
  Permission,
31
31
  SessionKeyData,
32
32
  SessionKeyParams,
33
- ParamCondition,
33
+ ParamOperator,
34
34
  } from "./session-key-validator.js";
35
35
  import type { Abi, InferFunctionName, Narrow } from "viem";
36
36
 
@@ -75,36 +75,43 @@ export type AbiParametersToPrimitiveTypes<
75
75
  [K in keyof TAbiParameters]: AbiParameterToPrimitiveType<
76
76
  TAbiParameters[K],
77
77
  TAbiParameterKind
78
- > | null;
78
+ >;
79
79
  }>;
80
80
 
81
81
  export type AbiParametersToConditons<
82
82
  TAbiParameters extends readonly AbiParameter[]
83
83
  > = Pretty<{
84
- [K in keyof TAbiParameters]: ParamCondition | null;
84
+ [K in keyof TAbiParameters]: ParamOperator;
85
85
  }>;
86
86
 
87
+ export type CombinedArgs<
88
+ TAbiParameters extends readonly AbiParameter[],
89
+ TAbiParameterKind extends AbiParameterKind = AbiParameterKind
90
+ > = {
91
+ [K in keyof TAbiParameters]: {
92
+ operator: ParamOperator;
93
+ value: AbiParameterToPrimitiveType<TAbiParameters[K], TAbiParameterKind>;
94
+ } | null;
95
+ };
96
+
87
97
  export type GetFunctionArgs<
88
98
  TAbi extends Abi | readonly unknown[],
89
99
  TFunctionName extends string,
90
100
  TAbiFunction extends AbiFunction = TAbi extends Abi
91
101
  ? ExtractAbiFunction<TAbi, TFunctionName>
92
102
  : AbiFunction,
93
- TArgs = AbiParametersToPrimitiveTypes<TAbiFunction["inputs"]>,
94
- TConditions = AbiParametersToConditons<TAbiFunction["inputs"]>,
103
+ TArgs = CombinedArgs<TAbiFunction["inputs"]>,
95
104
  FailedToParseArgs =
96
105
  | ([TArgs] extends [never] ? true : false)
97
106
  | (readonly unknown[] extends TArgs ? true : false)
98
107
  > = true extends FailedToParseArgs
99
108
  ? {
100
109
  args?: readonly unknown[];
101
- conditions?: readonly unknown[];
102
110
  }
103
111
  : TArgs extends readonly []
104
- ? { args?: never; conditions?: never }
112
+ ? { args?: never }
105
113
  : {
106
114
  args?: TArgs;
107
- conditions?: TConditions;
108
115
  };
109
116
 
110
117
  export type GeneratePermissionFromArgsParameters<