@account-kit/smart-contracts 4.23.1 → 4.25.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.
Files changed (49) hide show
  1. package/dist/esm/src/ma-v2/account/common/modularAccountV2Base.d.ts +1 -0
  2. package/dist/esm/src/ma-v2/account/common/modularAccountV2Base.js +35 -12
  3. package/dist/esm/src/ma-v2/account/common/modularAccountV2Base.js.map +1 -1
  4. package/dist/esm/src/ma-v2/account/modularAccountV2.d.ts +1 -0
  5. package/dist/esm/src/ma-v2/account/modularAccountV2.js +2 -1
  6. package/dist/esm/src/ma-v2/account/modularAccountV2.js.map +1 -1
  7. package/dist/esm/src/ma-v2/account/nativeSMASigner.d.ts +2 -2
  8. package/dist/esm/src/ma-v2/account/nativeSMASigner.js +11 -8
  9. package/dist/esm/src/ma-v2/account/nativeSMASigner.js.map +1 -1
  10. package/dist/esm/src/ma-v2/actions/deferralActions.d.ts +9 -7
  11. package/dist/esm/src/ma-v2/actions/deferralActions.js +21 -35
  12. package/dist/esm/src/ma-v2/actions/deferralActions.js.map +1 -1
  13. package/dist/esm/src/ma-v2/index.d.ts +4 -0
  14. package/dist/esm/src/ma-v2/index.js +2 -0
  15. package/dist/esm/src/ma-v2/index.js.map +1 -1
  16. package/dist/esm/src/ma-v2/modules/single-signer-validation/signer.d.ts +2 -1
  17. package/dist/esm/src/ma-v2/modules/single-signer-validation/signer.js +10 -6
  18. package/dist/esm/src/ma-v2/modules/single-signer-validation/signer.js.map +1 -1
  19. package/dist/esm/src/ma-v2/permissionBuilder.d.ts +113 -0
  20. package/dist/esm/src/ma-v2/permissionBuilder.js +487 -0
  21. package/dist/esm/src/ma-v2/permissionBuilder.js.map +1 -0
  22. package/dist/esm/src/ma-v2/utils.d.ts +42 -0
  23. package/dist/esm/src/ma-v2/utils.js +15 -1
  24. package/dist/esm/src/ma-v2/utils.js.map +1 -1
  25. package/dist/types/src/ma-v2/account/common/modularAccountV2Base.d.ts +1 -0
  26. package/dist/types/src/ma-v2/account/common/modularAccountV2Base.d.ts.map +1 -1
  27. package/dist/types/src/ma-v2/account/modularAccountV2.d.ts +1 -0
  28. package/dist/types/src/ma-v2/account/modularAccountV2.d.ts.map +1 -1
  29. package/dist/types/src/ma-v2/account/nativeSMASigner.d.ts +2 -2
  30. package/dist/types/src/ma-v2/account/nativeSMASigner.d.ts.map +1 -1
  31. package/dist/types/src/ma-v2/actions/deferralActions.d.ts +9 -7
  32. package/dist/types/src/ma-v2/actions/deferralActions.d.ts.map +1 -1
  33. package/dist/types/src/ma-v2/index.d.ts +4 -0
  34. package/dist/types/src/ma-v2/index.d.ts.map +1 -1
  35. package/dist/types/src/ma-v2/modules/single-signer-validation/signer.d.ts +2 -1
  36. package/dist/types/src/ma-v2/modules/single-signer-validation/signer.d.ts.map +1 -1
  37. package/dist/types/src/ma-v2/permissionBuilder.d.ts +114 -0
  38. package/dist/types/src/ma-v2/permissionBuilder.d.ts.map +1 -0
  39. package/dist/types/src/ma-v2/utils.d.ts +42 -0
  40. package/dist/types/src/ma-v2/utils.d.ts.map +1 -1
  41. package/package.json +6 -5
  42. package/src/ma-v2/account/common/modularAccountV2Base.ts +49 -11
  43. package/src/ma-v2/account/modularAccountV2.ts +3 -0
  44. package/src/ma-v2/account/nativeSMASigner.ts +20 -14
  45. package/src/ma-v2/actions/deferralActions.ts +38 -51
  46. package/src/ma-v2/index.ts +4 -0
  47. package/src/ma-v2/modules/single-signer-validation/signer.ts +19 -13
  48. package/src/ma-v2/permissionBuilder.ts +716 -0
  49. package/src/ma-v2/utils.ts +25 -0
@@ -0,0 +1,716 @@
1
+ import { toHex, zeroAddress, type Address, type Hex } from "viem";
2
+ import {
3
+ HookType,
4
+ type HookConfig,
5
+ type ValidationConfig,
6
+ } from "./actions/common/types.js";
7
+ import {
8
+ installValidationActions,
9
+ type InstallValidationParams,
10
+ } from "./actions/install-validation/installValidation.js";
11
+ import type { ModularAccountV2Client } from "./client/client.js";
12
+ import {
13
+ deferralActions,
14
+ type DeferredActionTypedData,
15
+ } from "./actions/deferralActions.js";
16
+ import { NativeTokenLimitModule } from "./modules/native-token-limit-module/module.js";
17
+ import {
18
+ getDefaultAllowlistModuleAddress,
19
+ getDefaultNativeTokenLimitModuleAddress,
20
+ getDefaultSingleSignerValidationModuleAddress,
21
+ getDefaultTimeRangeModuleAddress,
22
+ } from "./modules/utils.js";
23
+ import { SingleSignerValidationModule } from "./modules/single-signer-validation/module.js";
24
+ import { AllowlistModule } from "./modules/allowlist-module/module.js";
25
+ import { TimeRangeModule } from "./modules/time-range-module/module.js";
26
+
27
+ // We use this to offset the ERC20 spend limit entityId
28
+ const HALF_UINT32 = 2147483647;
29
+ const ERC20_APPROVE_SELECTOR = "0x095ea7b3";
30
+ const ERC20_TRANSFER_SELECTOR = "0xa9059cbb";
31
+ const ACCOUNT_EXECUTE_SELECTOR = "0xb61d27f6";
32
+ const ACCOUNT_EXECUTEBATCH_SELECTOR = "0x34fcd5be";
33
+
34
+ export enum PermissionType {
35
+ NATIVE_TOKEN_TRANSFER = "native-token-transfer",
36
+ ERC20_TOKEN_TRANSFER = "erc20-token-transfer",
37
+ // ERC721_TOKEN_TRANSFER = "erc721-token-transfer", //Unimplemented
38
+ // ERC1155_TOKEN_TRANSFER = "erc1155-token-transfer", //Unimplemented
39
+ GAS_LIMIT = "gas-limit",
40
+ // CALL_LIMIT = "call-limit", //Unimplemented
41
+ // RATE_LIMIT = "rate-limit", //Unimplemented
42
+ CONTRACT_ACCESS = "contract-access",
43
+ ACCOUNT_FUNCTIONS = "account-functions",
44
+ FUNCTIONS_ON_ALL_CONTRACTS = "functions-on-all-contracts",
45
+ FUNCTIONS_ON_CONTRACT = "functions-on-contract",
46
+ ROOT = "root",
47
+ }
48
+
49
+ enum HookIdentifier {
50
+ NATIVE_TOKEN_TRANSFER,
51
+ ERC20_TOKEN_TRANSFER,
52
+ GAS_LIMIT,
53
+ PREVAL_ALLOWLIST, // aggregate of CONTRACT_ACCESS, ACCOUNT_FUNCTIONS, FUNCTIONS_ON_ALL_CONTRACTS, FUNCTIONS_ON_CONTRACT
54
+ }
55
+
56
+ type PreExecutionHookConfig = {
57
+ address: Address;
58
+ entityId: number;
59
+ hookType: HookType.EXECUTION;
60
+ hasPreHooks: true;
61
+ hasPostHooks: false;
62
+ };
63
+
64
+ type PreValidationHookConfig = {
65
+ address: Address;
66
+ entityId: number;
67
+ hookType: HookType.VALIDATION;
68
+ hasPreHooks: true;
69
+ hasPostHooks: false;
70
+ };
71
+
72
+ type RawHooks = {
73
+ [HookIdentifier.NATIVE_TOKEN_TRANSFER]:
74
+ | {
75
+ hookConfig: PreExecutionHookConfig;
76
+ initData: {
77
+ entityId: number;
78
+ spendLimit: bigint;
79
+ };
80
+ }
81
+ | undefined;
82
+ [HookIdentifier.ERC20_TOKEN_TRANSFER]:
83
+ | {
84
+ hookConfig: PreExecutionHookConfig;
85
+ initData: {
86
+ entityId: number;
87
+ inputs: Array<{
88
+ target: Address;
89
+ hasSelectorAllowlist: boolean;
90
+ hasERC20SpendLimit: boolean;
91
+ erc20SpendLimit: bigint;
92
+ selectors: Array<Hex>;
93
+ }>;
94
+ };
95
+ }
96
+ | undefined;
97
+ [HookIdentifier.GAS_LIMIT]:
98
+ | {
99
+ hookConfig: PreValidationHookConfig;
100
+ initData: {
101
+ entityId: number;
102
+ spendLimit: bigint;
103
+ };
104
+ }
105
+ | undefined;
106
+ [HookIdentifier.PREVAL_ALLOWLIST]:
107
+ | {
108
+ hookConfig: PreValidationHookConfig;
109
+
110
+ initData: {
111
+ entityId: number;
112
+ inputs: Array<{
113
+ target: Address;
114
+ hasSelectorAllowlist: boolean;
115
+ hasERC20SpendLimit: boolean;
116
+ erc20SpendLimit: bigint;
117
+ selectors: Array<Hex>;
118
+ }>;
119
+ };
120
+ }
121
+ | undefined;
122
+ };
123
+
124
+ type OneOf<T extends {}[]> = T[number];
125
+
126
+ type Key = {
127
+ publicKey: Hex;
128
+ type: "secp256k1" | "contract";
129
+ };
130
+
131
+ export type Permission = OneOf<
132
+ [
133
+ {
134
+ // this permission allows transfer of native tokens from the account
135
+ type: PermissionType.NATIVE_TOKEN_TRANSFER;
136
+ data: {
137
+ allowance: Hex;
138
+ };
139
+ },
140
+ {
141
+ // this permission allows transfer or approval of erc20 tokens from the account
142
+ type: PermissionType.ERC20_TOKEN_TRANSFER;
143
+ data: {
144
+ address: Address; // erc20 token contract address
145
+ allowance: Hex;
146
+ };
147
+ },
148
+ {
149
+ // this permissions allows the key to spend gas for UOs
150
+ type: PermissionType.GAS_LIMIT;
151
+ data: {
152
+ limit: Hex;
153
+ };
154
+ },
155
+ {
156
+ // this permission grants access to all functions in a contract
157
+ type: PermissionType.CONTRACT_ACCESS;
158
+ data: {
159
+ address: Address;
160
+ };
161
+ },
162
+ {
163
+ // this permission grants access to functions in the account
164
+ type: PermissionType.ACCOUNT_FUNCTIONS;
165
+ data: {
166
+ functions: Hex[]; // function signatures
167
+ };
168
+ },
169
+ {
170
+ // this permission grants access to a function selector in any address or contract
171
+ type: PermissionType.FUNCTIONS_ON_ALL_CONTRACTS;
172
+ data: {
173
+ functions: Hex[]; // function signatures
174
+ };
175
+ },
176
+ {
177
+ // this permission grants access to specified functions on a specific contract
178
+ type: PermissionType.FUNCTIONS_ON_CONTRACT;
179
+ data: {
180
+ address: Address;
181
+ functions: Hex[];
182
+ };
183
+ },
184
+ {
185
+ // this permission grants full access to everything
186
+ type: PermissionType.ROOT;
187
+ data?: never;
188
+ }
189
+ ]
190
+ >;
191
+
192
+ type Hook = {
193
+ hookConfig: HookConfig;
194
+ initData: Hex;
195
+ };
196
+
197
+ export class PermissionBuilder {
198
+ private client: ModularAccountV2Client;
199
+ private validationConfig: ValidationConfig = {
200
+ moduleAddress: zeroAddress,
201
+ entityId: 0, // uint32
202
+ isGlobal: false,
203
+ isSignatureValidation: false,
204
+ isUserOpValidation: false,
205
+ };
206
+ private selectors: Hex[] = [];
207
+ private installData: Hex = "0x";
208
+ private permissions: Permission[] = [];
209
+ private hooks: Hook[] = [];
210
+ private nonce: bigint = 0n;
211
+ private hasAssociatedExecHooks: boolean = false;
212
+ private deadline: number = 0;
213
+
214
+ constructor({
215
+ client,
216
+ key,
217
+ entityId,
218
+ nonce,
219
+ selectors,
220
+ hooks,
221
+ deadline,
222
+ }: {
223
+ client: ModularAccountV2Client;
224
+ key: Key;
225
+ entityId: number;
226
+ nonce: bigint;
227
+ selectors?: Hex[];
228
+ hooks?: Hook[];
229
+ deadline?: number;
230
+ }) {
231
+ this.client = client;
232
+ this.validationConfig = {
233
+ moduleAddress: getDefaultSingleSignerValidationModuleAddress(
234
+ this.client.chain
235
+ ),
236
+ entityId,
237
+ isUserOpValidation: true,
238
+ isGlobal: false,
239
+ isSignatureValidation: false,
240
+ };
241
+ this.installData = SingleSignerValidationModule.encodeOnInstallData({
242
+ entityId: entityId,
243
+ signer: key.publicKey,
244
+ });
245
+ this.nonce = nonce;
246
+ if (selectors) this.selectors = selectors;
247
+ if (hooks) this.hooks = hooks;
248
+ if (deadline) this.deadline = deadline;
249
+ }
250
+
251
+ addSelector({ selector }: { selector: Hex }): this {
252
+ this.selectors.push(selector);
253
+ return this;
254
+ }
255
+
256
+ addPermission({ permission }: { permission: Permission }): this {
257
+ // Check 1: If we're adding root, we can't have any other permissions
258
+ if (permission.type === PermissionType.ROOT) {
259
+ if (this.permissions.length !== 0) {
260
+ throw new Error(
261
+ "PERMISSION: ROOT: Cannot add ROOT permission with other permissions"
262
+ );
263
+ }
264
+ this.permissions.push(permission);
265
+ // Set isGlobal to true
266
+ this.validationConfig.isGlobal = true;
267
+ return this;
268
+ }
269
+
270
+ // Check 2: If the permission is NOT ROOT (guaranteed), ensure there is no ROOT permission set
271
+ // Will resolve to undefined if ROOT is not found
272
+ // NOTE: Technically this could be replaced by checking permissions[0] since it should not be possible
273
+ // to have >1 permission with root among them
274
+ if (this.permissions.find((p) => p.type === PermissionType.ROOT)) {
275
+ throw new Error(
276
+ `PERMISSION: ${permission.type} => Cannot add permissions with ROOT enabled`
277
+ );
278
+ }
279
+
280
+ // Check 3: If the permission is either CONTRACT_ACCESS or FUNCTIONS_ON_CONTRACT, ensure it doesn't collide with another like it.
281
+ if (
282
+ permission.type === PermissionType.CONTRACT_ACCESS ||
283
+ permission.type === PermissionType.FUNCTIONS_ON_CONTRACT
284
+ ) {
285
+ // Check 3.1: address must not be the account address, or the user should use the ACCOUNT_FUNCTIONS permission
286
+ if (permission.data.address === this.client.account.address) {
287
+ throw new Error(
288
+ `PERMISSION: ${permission.type} => Account address as target, use ACCOUNT_FUNCTIONS for account address`
289
+ );
290
+ }
291
+
292
+ // Check 3.2: there must not be an existing permission with this address as a target
293
+ const targetAddress = permission.data.address;
294
+ const existingPermissionWithSameAddress = this.permissions.find(
295
+ (p) =>
296
+ (p.type === PermissionType.CONTRACT_ACCESS &&
297
+ "address" in p.data &&
298
+ p.data.address === targetAddress) ||
299
+ (p.type === PermissionType.FUNCTIONS_ON_CONTRACT &&
300
+ "address" in p.data &&
301
+ p.data.address === targetAddress)
302
+ );
303
+
304
+ if (existingPermissionWithSameAddress) {
305
+ throw new Error(
306
+ `PERMISSION: ${permission.type} => Address ${targetAddress} already has a permission. Cannot add multiple CONTRACT_ACCESS or FUNCTIONS_ON_CONTRACT permissions for the same target address.`
307
+ );
308
+ }
309
+ }
310
+
311
+ // Check 4: If the permission is ACCOUNT_FUNCTIONS, add selectors
312
+ if (permission.type === PermissionType.ACCOUNT_FUNCTIONS) {
313
+ this.selectors = [...this.selectors, ...permission.data.functions];
314
+ return this;
315
+ }
316
+
317
+ this.permissions.push(permission);
318
+
319
+ return this;
320
+ }
321
+
322
+ addPermissions({ permissions }: { permissions: Permission[] }): this {
323
+ // We could validate each permission here, but for simplicity we'll just add them
324
+ // A better approach would be to call addPermission for each one
325
+ permissions.forEach((permission) => {
326
+ this.addPermission({ permission });
327
+ });
328
+ return this;
329
+ }
330
+
331
+ // Use for building deferred action typed data to sign
332
+ async compileDeferred(): Promise<{
333
+ typedData: DeferredActionTypedData;
334
+ fullPreSignatureDeferredActionDigest: Hex;
335
+ }> {
336
+ // Add time range module hook via expiry
337
+ if (this.deadline !== 0) {
338
+ if (this.deadline < Date.now() / 1000) {
339
+ throw new Error(
340
+ `PERMISSION: compileDeferred(): deadline ${
341
+ this.deadline
342
+ } cannot be before now (${Date.now() / 1000})`
343
+ );
344
+ }
345
+ this.hooks.push(
346
+ TimeRangeModule.buildHook(
347
+ {
348
+ entityId: this.validationConfig.entityId, // will be timerange entityId
349
+ validUntil: this.deadline,
350
+ validAfter: 0,
351
+ },
352
+ getDefaultTimeRangeModuleAddress(this.client.chain)
353
+ )
354
+ );
355
+ }
356
+
357
+ const installValidationCall = await this.compileRaw();
358
+
359
+ const { typedData } = await deferralActions(
360
+ this.client
361
+ ).createDeferredActionTypedDataObject({
362
+ callData: installValidationCall,
363
+ deadline: this.deadline,
364
+ nonce: this.nonce,
365
+ });
366
+
367
+ const preSignatureDigest = deferralActions(
368
+ this.client
369
+ ).buildPreSignatureDeferredActionDigest({ typedData });
370
+
371
+ // Encode additional information to build the full pre-signature digest
372
+ const fullPreSignatureDeferredActionDigest: `0x${string}` = `0x0${
373
+ this.hasAssociatedExecHooks ? "1" : "0"
374
+ }${toHex(this.nonce, {
375
+ size: 32,
376
+ }).slice(2)}${preSignatureDigest.slice(2)}`;
377
+
378
+ return {
379
+ typedData,
380
+ fullPreSignatureDeferredActionDigest,
381
+ };
382
+ }
383
+
384
+ // Use for direct `installValidation()` low-level calls (maybe useless)
385
+ async compileRaw(): Promise<Hex> {
386
+ // Translate all permissions into raw hooks if >0
387
+ if (this.permissions.length > 0) {
388
+ const rawHooks = this.translatePermissions(
389
+ this.validationConfig.entityId
390
+ );
391
+ // Add the translated permissions as hooks
392
+ this.addHooks(rawHooks);
393
+ }
394
+ this.validateConfiguration();
395
+
396
+ return await installValidationActions(this.client).encodeInstallValidation({
397
+ validationConfig: this.validationConfig,
398
+ selectors: this.selectors,
399
+ installData: this.installData,
400
+ hooks: this.hooks,
401
+ account: this.client.account,
402
+ });
403
+ }
404
+
405
+ // Use for compiling args to installValidation
406
+ async compileInstallArgs(): Promise<InstallValidationParams> {
407
+ this.validateConfiguration();
408
+
409
+ return {
410
+ validationConfig: this.validationConfig,
411
+ selectors: this.selectors,
412
+ installData: this.installData,
413
+ hooks: this.hooks,
414
+ account: this.client.account,
415
+ };
416
+ }
417
+
418
+ private validateConfiguration(): void {
419
+ if (
420
+ this.validationConfig.isGlobal === false &&
421
+ this.selectors.length === 0
422
+ ) {
423
+ throw new Error(
424
+ "Validation config unset, use permissionBuilder.configure(...)"
425
+ );
426
+ }
427
+ }
428
+
429
+ // Used to translate consolidated permissions into raw unencoded hooks
430
+ // Note entityId will be a member object later
431
+ private translatePermissions(entityId: number): RawHooks {
432
+ const rawHooks: RawHooks = {
433
+ [HookIdentifier.NATIVE_TOKEN_TRANSFER]: undefined,
434
+ [HookIdentifier.ERC20_TOKEN_TRANSFER]: undefined,
435
+ [HookIdentifier.GAS_LIMIT]: undefined,
436
+ [HookIdentifier.PREVAL_ALLOWLIST]: undefined,
437
+ };
438
+
439
+ this.permissions.forEach((permission) => {
440
+ switch (permission.type) {
441
+ case PermissionType.NATIVE_TOKEN_TRANSFER:
442
+ // Should never be added twice, check is on addPermission(s) too
443
+ if (rawHooks[HookIdentifier.NATIVE_TOKEN_TRANSFER] !== undefined) {
444
+ throw new Error(
445
+ "PERMISSION: NATIVE_TOKEN_TRANSFER => Must have at most ONE native token transfer permission"
446
+ );
447
+ }
448
+ rawHooks[HookIdentifier.NATIVE_TOKEN_TRANSFER] = {
449
+ hookConfig: {
450
+ address: getDefaultNativeTokenLimitModuleAddress(
451
+ this.client.chain
452
+ ),
453
+ entityId,
454
+ hookType: HookType.EXECUTION,
455
+ hasPreHooks: true,
456
+ hasPostHooks: false,
457
+ },
458
+ initData: {
459
+ entityId,
460
+ spendLimit: BigInt(permission.data.allowance),
461
+ },
462
+ };
463
+ this.hasAssociatedExecHooks = true;
464
+ break;
465
+ case PermissionType.ERC20_TOKEN_TRANSFER:
466
+ if (permission.data.address === zeroAddress) {
467
+ throw new Error(
468
+ "PERMISSION: ERC20_TOKEN_TRANSFER => Zero address provided"
469
+ );
470
+ }
471
+ rawHooks[HookIdentifier.ERC20_TOKEN_TRANSFER] = {
472
+ hookConfig: {
473
+ address: getDefaultAllowlistModuleAddress(this.client.chain),
474
+ entityId: entityId + HALF_UINT32,
475
+ hookType: HookType.EXECUTION,
476
+ hasPreHooks: true,
477
+ hasPostHooks: false,
478
+ },
479
+ initData: {
480
+ entityId: entityId + HALF_UINT32,
481
+ inputs: [
482
+ // Add previous inputs if they exist
483
+ ...(rawHooks[HookIdentifier.ERC20_TOKEN_TRANSFER]?.initData
484
+ .inputs || []),
485
+ {
486
+ target: permission.data.address,
487
+ hasSelectorAllowlist: false,
488
+ hasERC20SpendLimit: true,
489
+ erc20SpendLimit: BigInt(permission.data.allowance),
490
+ selectors: [],
491
+ },
492
+ ],
493
+ },
494
+ };
495
+ this.hasAssociatedExecHooks = true;
496
+ // Also allow `approve` and `transfer` for the erc20
497
+ rawHooks[HookIdentifier.PREVAL_ALLOWLIST] = {
498
+ hookConfig: {
499
+ address: getDefaultAllowlistModuleAddress(this.client.chain),
500
+ entityId,
501
+ hookType: HookType.VALIDATION,
502
+ hasPreHooks: true,
503
+ hasPostHooks: false,
504
+ },
505
+ initData: {
506
+ entityId,
507
+ inputs: [
508
+ // Add previous inputs if they exist
509
+ ...(rawHooks[HookIdentifier.PREVAL_ALLOWLIST]?.initData
510
+ .inputs || []),
511
+ {
512
+ target: permission.data.address,
513
+ hasSelectorAllowlist: true,
514
+ hasERC20SpendLimit: false,
515
+ erc20SpendLimit: 0n,
516
+ selectors: [ERC20_APPROVE_SELECTOR, ERC20_TRANSFER_SELECTOR], // approve, transfer
517
+ },
518
+ ],
519
+ },
520
+ };
521
+ break;
522
+ case PermissionType.GAS_LIMIT:
523
+ // Should only ever be added once, check is also on addPermission(s)
524
+ if (rawHooks[HookIdentifier.GAS_LIMIT] !== undefined) {
525
+ throw new Error(
526
+ "PERMISSION: GAS_LIMIT => Must have at most ONE gas limit permission"
527
+ );
528
+ }
529
+ rawHooks[HookIdentifier.GAS_LIMIT] = {
530
+ hookConfig: {
531
+ address: getDefaultNativeTokenLimitModuleAddress(
532
+ this.client.chain
533
+ ),
534
+ entityId,
535
+ hookType: HookType.VALIDATION,
536
+ hasPreHooks: true,
537
+ hasPostHooks: false,
538
+ },
539
+ initData: {
540
+ entityId,
541
+ spendLimit: BigInt(permission.data.limit),
542
+ },
543
+ };
544
+ break;
545
+ case PermissionType.CONTRACT_ACCESS:
546
+ if (permission.data.address === zeroAddress) {
547
+ throw new Error(
548
+ "PERMISSION: CONTRACT_ACCESS => Zero address provided"
549
+ );
550
+ }
551
+ rawHooks[HookIdentifier.PREVAL_ALLOWLIST] = {
552
+ hookConfig: {
553
+ address: getDefaultAllowlistModuleAddress(this.client.chain),
554
+ entityId,
555
+ hookType: HookType.VALIDATION,
556
+ hasPreHooks: true,
557
+ hasPostHooks: false,
558
+ },
559
+ initData: {
560
+ entityId,
561
+ inputs: [
562
+ // Add previous inputs if they exist
563
+ ...(rawHooks[HookIdentifier.PREVAL_ALLOWLIST]?.initData
564
+ .inputs || []),
565
+ {
566
+ target: permission.data.address,
567
+ hasSelectorAllowlist: false,
568
+ hasERC20SpendLimit: false,
569
+ erc20SpendLimit: 0n,
570
+ selectors: [],
571
+ },
572
+ ],
573
+ },
574
+ };
575
+ break;
576
+ case PermissionType.ACCOUNT_FUNCTIONS:
577
+ if (permission.data.functions.length === 0) {
578
+ throw new Error(
579
+ "PERMISSION: ACCOUNT_FUNCTION => No functions provided"
580
+ ); // should be in add perm
581
+ }
582
+ break;
583
+ case PermissionType.FUNCTIONS_ON_ALL_CONTRACTS:
584
+ if (permission.data.functions.length === 0) {
585
+ throw new Error(
586
+ "PERMISSION: FUNCTIONS_ON_ALL_CONTRACTS => No functions provided"
587
+ );
588
+ }
589
+ rawHooks[HookIdentifier.PREVAL_ALLOWLIST] = {
590
+ hookConfig: {
591
+ address: getDefaultAllowlistModuleAddress(this.client.chain),
592
+ entityId,
593
+ hookType: HookType.VALIDATION,
594
+ hasPreHooks: true,
595
+ hasPostHooks: false,
596
+ },
597
+ initData: {
598
+ entityId,
599
+ inputs: [
600
+ // Add previous inputs if they exist
601
+ ...(rawHooks[HookIdentifier.PREVAL_ALLOWLIST]?.initData
602
+ .inputs || []),
603
+ {
604
+ target: zeroAddress,
605
+ hasSelectorAllowlist: false,
606
+ hasERC20SpendLimit: false,
607
+ erc20SpendLimit: 0n,
608
+ selectors: permission.data.functions,
609
+ },
610
+ ],
611
+ },
612
+ };
613
+ break;
614
+ case PermissionType.FUNCTIONS_ON_CONTRACT:
615
+ if (permission.data.functions.length === 0) {
616
+ throw new Error(
617
+ "PERMISSION: FUNCTIONS_ON_CONTRACT => No functions provided"
618
+ );
619
+ }
620
+ if (permission.data.address === zeroAddress) {
621
+ throw new Error(
622
+ "PERMISSION: FUNCTIONS_ON_CONTRACT => Zero address provided"
623
+ );
624
+ }
625
+ rawHooks[HookIdentifier.PREVAL_ALLOWLIST] = {
626
+ hookConfig: {
627
+ address: getDefaultAllowlistModuleAddress(this.client.chain),
628
+ entityId,
629
+ hookType: HookType.VALIDATION,
630
+ hasPreHooks: true,
631
+ hasPostHooks: false,
632
+ },
633
+ initData: {
634
+ entityId,
635
+ inputs: [
636
+ // Add previous inputs if they exist
637
+ ...(rawHooks[HookIdentifier.PREVAL_ALLOWLIST]?.initData
638
+ .inputs || []),
639
+ {
640
+ target: permission.data.address,
641
+ hasSelectorAllowlist: true,
642
+ hasERC20SpendLimit: false,
643
+ erc20SpendLimit: 0n,
644
+ selectors: permission.data.functions,
645
+ },
646
+ ],
647
+ },
648
+ };
649
+ break;
650
+ case PermissionType.ROOT:
651
+ // Root permission handled in addPermission
652
+ break;
653
+ default:
654
+ throw new Error(
655
+ `Unsupported permission type: ${(permission as any).type}`
656
+ );
657
+ }
658
+
659
+ // isGlobal guaranteed to be false since it's only set with root permissions,
660
+ // we must add access to execute & executeBatch if there's a preVal allowlist hook set.
661
+ if (rawHooks[HookIdentifier.PREVAL_ALLOWLIST] !== undefined) {
662
+ const selectorsToAdd: `0x${string}`[] = [
663
+ ACCOUNT_EXECUTE_SELECTOR,
664
+ ACCOUNT_EXECUTEBATCH_SELECTOR,
665
+ ]; // execute, executeBatch
666
+
667
+ // Only add the selectors if they aren't already in this.selectors
668
+ const newSelectors = selectorsToAdd.filter(
669
+ (selector) => !this.selectors.includes(selector)
670
+ );
671
+
672
+ this.selectors = [...this.selectors, ...newSelectors];
673
+ }
674
+ });
675
+
676
+ return rawHooks;
677
+ }
678
+
679
+ private addHooks(rawHooks: RawHooks) {
680
+ if (rawHooks[HookIdentifier.NATIVE_TOKEN_TRANSFER]) {
681
+ this.hooks.push({
682
+ hookConfig: rawHooks[HookIdentifier.NATIVE_TOKEN_TRANSFER].hookConfig,
683
+ initData: NativeTokenLimitModule.encodeOnInstallData(
684
+ rawHooks[HookIdentifier.NATIVE_TOKEN_TRANSFER].initData
685
+ ),
686
+ });
687
+ }
688
+
689
+ if (rawHooks[HookIdentifier.ERC20_TOKEN_TRANSFER]) {
690
+ this.hooks.push({
691
+ hookConfig: rawHooks[HookIdentifier.ERC20_TOKEN_TRANSFER].hookConfig,
692
+ initData: AllowlistModule.encodeOnInstallData(
693
+ rawHooks[HookIdentifier.ERC20_TOKEN_TRANSFER].initData
694
+ ),
695
+ });
696
+ }
697
+
698
+ if (rawHooks[HookIdentifier.GAS_LIMIT]) {
699
+ this.hooks.push({
700
+ hookConfig: rawHooks[HookIdentifier.GAS_LIMIT].hookConfig,
701
+ initData: NativeTokenLimitModule.encodeOnInstallData(
702
+ rawHooks[HookIdentifier.GAS_LIMIT].initData
703
+ ),
704
+ });
705
+ }
706
+
707
+ if (rawHooks[HookIdentifier.PREVAL_ALLOWLIST]) {
708
+ this.hooks.push({
709
+ hookConfig: rawHooks[HookIdentifier.PREVAL_ALLOWLIST].hookConfig,
710
+ initData: AllowlistModule.encodeOnInstallData(
711
+ rawHooks[HookIdentifier.PREVAL_ALLOWLIST].initData
712
+ ),
713
+ });
714
+ }
715
+ }
716
+ }