@zerodev/sdk 5.1.12 → 5.2.0-alpha-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 (122) hide show
  1. package/_cjs/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.js +738 -0
  2. package/_cjs/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.js.map +1 -0
  3. package/_cjs/accounts/kernel/abi/kernel_v_3_0_0/KernelModuleAbi.js +15 -0
  4. package/_cjs/accounts/kernel/abi/kernel_v_3_0_0/KernelModuleAbi.js.map +1 -0
  5. package/_cjs/accounts/kernel/createKernelAccount.js +231 -75
  6. package/_cjs/accounts/kernel/createKernelAccount.js.map +1 -1
  7. package/_cjs/accounts/kernel/v1/createKernelV1Account.js +29 -18
  8. package/_cjs/accounts/kernel/v1/createKernelV1Account.js.map +1 -1
  9. package/_cjs/accounts/utils/6492.js +19 -3
  10. package/_cjs/accounts/utils/6492.js.map +1 -1
  11. package/_cjs/accounts/utils/index.js +4 -3
  12. package/_cjs/accounts/utils/index.js.map +1 -1
  13. package/_cjs/accounts/utils/toKernelPluginManager.js +194 -62
  14. package/_cjs/accounts/utils/toKernelPluginManager.js.map +1 -1
  15. package/_cjs/actions/account-client/signUserOperation.js +1 -1
  16. package/_cjs/actions/account-client/signUserOperation.js.map +1 -1
  17. package/_cjs/actions/paymaster/estimateGasInERC20.js.map +1 -1
  18. package/_cjs/actions/paymaster/sponsorUserOperation.js +17 -14
  19. package/_cjs/actions/paymaster/sponsorUserOperation.js.map +1 -1
  20. package/_cjs/clients/decorators/kernel.js +8 -5
  21. package/_cjs/clients/decorators/kernel.js.map +1 -1
  22. package/_cjs/clients/kernelAccountClient.js +3 -3
  23. package/_cjs/clients/kernelAccountClient.js.map +1 -1
  24. package/_cjs/clients/paymasterClient.js +1 -1
  25. package/_cjs/clients/paymasterClient.js.map +1 -1
  26. package/_cjs/constants.js +28 -3
  27. package/_cjs/constants.js.map +1 -1
  28. package/_cjs/index.js +5 -1
  29. package/_cjs/index.js.map +1 -1
  30. package/_cjs/providers/KernelEIP1193Provider.js.map +1 -1
  31. package/_cjs/types/kernel.js.map +1 -1
  32. package/_cjs/utils.js +34 -3
  33. package/_cjs/utils.js.map +1 -1
  34. package/_esm/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.js +735 -0
  35. package/_esm/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.js.map +1 -0
  36. package/_esm/accounts/kernel/abi/kernel_v_3_0_0/KernelModuleAbi.js +12 -0
  37. package/_esm/accounts/kernel/abi/kernel_v_3_0_0/KernelModuleAbi.js.map +1 -0
  38. package/_esm/accounts/kernel/createKernelAccount.js +240 -81
  39. package/_esm/accounts/kernel/createKernelAccount.js.map +1 -1
  40. package/_esm/accounts/kernel/v1/createKernelV1Account.js +37 -20
  41. package/_esm/accounts/kernel/v1/createKernelV1Account.js.map +1 -1
  42. package/_esm/accounts/utils/6492.js +16 -1
  43. package/_esm/accounts/utils/6492.js.map +1 -1
  44. package/_esm/accounts/utils/index.js +2 -4
  45. package/_esm/accounts/utils/index.js.map +1 -1
  46. package/_esm/accounts/utils/toKernelPluginManager.js +198 -66
  47. package/_esm/accounts/utils/toKernelPluginManager.js.map +1 -1
  48. package/_esm/actions/account-client/signUserOperation.js +1 -1
  49. package/_esm/actions/account-client/signUserOperation.js.map +1 -1
  50. package/_esm/actions/paymaster/estimateGasInERC20.js.map +1 -1
  51. package/_esm/actions/paymaster/sponsorUserOperation.js +37 -15
  52. package/_esm/actions/paymaster/sponsorUserOperation.js.map +1 -1
  53. package/_esm/clients/decorators/kernel.js +9 -5
  54. package/_esm/clients/decorators/kernel.js.map +1 -1
  55. package/_esm/clients/kernelAccountClient.js +3 -3
  56. package/_esm/clients/kernelAccountClient.js.map +1 -1
  57. package/_esm/clients/paymasterClient.js +1 -1
  58. package/_esm/clients/paymasterClient.js.map +1 -1
  59. package/_esm/constants.js +27 -2
  60. package/_esm/constants.js.map +1 -1
  61. package/_esm/index.js +1 -0
  62. package/_esm/index.js.map +1 -1
  63. package/_esm/providers/KernelEIP1193Provider.js.map +1 -1
  64. package/_esm/types/kernel.js +1 -0
  65. package/_esm/types/kernel.js.map +1 -1
  66. package/_esm/utils.js +33 -4
  67. package/_esm/utils.js.map +1 -1
  68. package/_types/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.d.ts +843 -0
  69. package/_types/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.d.ts.map +1 -0
  70. package/_types/accounts/kernel/abi/kernel_v_3_0_0/KernelModuleAbi.d.ts +16 -0
  71. package/_types/accounts/kernel/abi/kernel_v_3_0_0/KernelModuleAbi.d.ts.map +1 -0
  72. package/_types/accounts/kernel/createKernelAccount.d.ts +14 -11
  73. package/_types/accounts/kernel/createKernelAccount.d.ts.map +1 -1
  74. package/_types/accounts/kernel/v1/createKernelV1Account.d.ts +5 -4
  75. package/_types/accounts/kernel/v1/createKernelV1Account.d.ts.map +1 -1
  76. package/_types/accounts/utils/6492.d.ts +3 -1
  77. package/_types/accounts/utils/6492.d.ts.map +1 -1
  78. package/_types/accounts/utils/index.d.ts +2 -4
  79. package/_types/accounts/utils/index.d.ts.map +1 -1
  80. package/_types/accounts/utils/toKernelPluginManager.d.ts +3 -2
  81. package/_types/accounts/utils/toKernelPluginManager.d.ts.map +1 -1
  82. package/_types/actions/account-client/signUserOperation.d.ts +7 -7
  83. package/_types/actions/account-client/signUserOperation.d.ts.map +1 -1
  84. package/_types/actions/paymaster/estimateGasInERC20.d.ts +3 -2
  85. package/_types/actions/paymaster/estimateGasInERC20.d.ts.map +1 -1
  86. package/_types/actions/paymaster/sponsorUserOperation.d.ts +7 -6
  87. package/_types/actions/paymaster/sponsorUserOperation.d.ts.map +1 -1
  88. package/_types/clients/decorators/kernel.d.ts +8 -7
  89. package/_types/clients/decorators/kernel.d.ts.map +1 -1
  90. package/_types/clients/kernelAccountClient.d.ts +101 -22
  91. package/_types/clients/kernelAccountClient.d.ts.map +1 -1
  92. package/_types/clients/paymasterClient.d.ts +6 -3
  93. package/_types/clients/paymasterClient.d.ts.map +1 -1
  94. package/_types/constants.d.ts +22 -1
  95. package/_types/constants.d.ts.map +1 -1
  96. package/_types/index.d.ts +1 -0
  97. package/_types/index.d.ts.map +1 -1
  98. package/_types/providers/KernelEIP1193Provider.d.ts +3 -2
  99. package/_types/providers/KernelEIP1193Provider.d.ts.map +1 -1
  100. package/_types/types/kernel.d.ts +44 -12
  101. package/_types/types/kernel.d.ts.map +1 -1
  102. package/_types/utils.d.ts +11 -2
  103. package/_types/utils.d.ts.map +1 -1
  104. package/accounts/kernel/abi/kernel_v_3_0_0/KernelAccountAbi.ts +736 -0
  105. package/accounts/kernel/abi/kernel_v_3_0_0/KernelModuleAbi.ts +11 -0
  106. package/accounts/kernel/createKernelAccount.ts +407 -104
  107. package/accounts/kernel/v1/createKernelV1Account.ts +67 -28
  108. package/accounts/utils/6492.ts +27 -1
  109. package/accounts/utils/index.ts +2 -4
  110. package/accounts/utils/toKernelPluginManager.ts +261 -80
  111. package/actions/account-client/signUserOperation.ts +25 -13
  112. package/actions/paymaster/estimateGasInERC20.ts +5 -4
  113. package/actions/paymaster/sponsorUserOperation.ts +104 -32
  114. package/clients/decorators/kernel.ts +44 -35
  115. package/clients/kernelAccountClient.ts +34 -25
  116. package/clients/paymasterClient.ts +10 -6
  117. package/constants.ts +25 -2
  118. package/index.ts +5 -0
  119. package/package.json +93 -92
  120. package/providers/KernelEIP1193Provider.ts +6 -3
  121. package/types/kernel.ts +96 -40
  122. package/utils.ts +52 -4
@@ -1,8 +1,18 @@
1
- import { getAccountNonce, getAction, getSenderAddress } from "permissionless"
1
+ import {
2
+ getAccountNonce,
3
+ getEntryPointVersion,
4
+ getSenderAddress,
5
+ isSmartAccountDeployed
6
+ } from "permissionless"
2
7
  import {
3
8
  SignTransactionNotSupportedBySmartAccount,
4
9
  type SmartAccount
5
10
  } from "permissionless/accounts"
11
+ import type {
12
+ ENTRYPOINT_ADDRESS_V06_TYPE,
13
+ ENTRYPOINT_ADDRESS_V07_TYPE,
14
+ EntryPoint
15
+ } from "permissionless/types"
6
16
  import {
7
17
  type Address,
8
18
  type Chain,
@@ -21,37 +31,63 @@ import {
21
31
  hashMessage,
22
32
  hashTypedData,
23
33
  keccak256,
34
+ pad,
24
35
  parseAbi,
36
+ parseAbiParameters,
25
37
  publicActions,
26
38
  stringToHex,
27
- validateTypedData
39
+ validateTypedData,
40
+ zeroAddress
28
41
  } from "viem"
29
42
  import { toAccount } from "viem/accounts"
30
- import { getBytecode, getStorageAt } from "viem/actions"
31
- import { KERNEL_NAME } from "../../constants.js"
43
+ import { CALL_TYPE, EXEC_TYPE, KERNEL_NAME } from "../../constants.js"
32
44
  import type {
33
45
  KernelEncodeCallDataArgs,
34
46
  KernelPluginManager,
35
47
  KernelPluginManagerParams
36
48
  } from "../../types/kernel.js"
37
- import { getKernelVersion } from "../../utils.js"
38
- import { wrapSignatureWith6492 } from "../utils/6492.js"
49
+ import {
50
+ KERNEL_FEATURES,
51
+ getExecMode,
52
+ getKernelVersion,
53
+ hasKernelFeature
54
+ } from "../../utils.js"
55
+ import {
56
+ getKernelImplementationAddress,
57
+ wrapSignatureWith6492
58
+ } from "../utils/6492.js"
39
59
  import { parseFactoryAddressAndCallDataFromAccountInitCode } from "../utils/index.js"
40
60
  import {
41
61
  isKernelPluginManager,
42
62
  toKernelPluginManager
43
63
  } from "../utils/toKernelPluginManager.js"
44
64
  import { KernelExecuteAbi, KernelInitAbi } from "./abi/KernelAccountAbi.js"
65
+ import {
66
+ KernelV3ExecuteAbi,
67
+ KernelV3InitAbi
68
+ } from "./abi/kernel_v_3_0_0/KernelAccountAbi.js"
45
69
 
46
70
  export type KernelSmartAccount<
71
+ entryPoint extends EntryPoint,
47
72
  transport extends Transport = Transport,
48
73
  chain extends Chain | undefined = Chain | undefined
49
- > = SmartAccount<"kernelSmartAccount", transport, chain> & {
50
- kernelPluginManager: KernelPluginManager
74
+ > = SmartAccount<entryPoint, "kernelSmartAccount", transport, chain> & {
75
+ kernelPluginManager: KernelPluginManager<entryPoint>
51
76
  generateInitCode: () => Promise<Hex>
52
77
  encodeCallData: (args: KernelEncodeCallDataArgs) => Promise<Hex>
53
78
  }
54
79
 
80
+ export type CreateKernelAccountParameters<entryPoint extends EntryPoint> = {
81
+ plugins:
82
+ | KernelPluginManagerParams<entryPoint>
83
+ | KernelPluginManager<entryPoint>
84
+ entryPoint: entryPoint
85
+ index?: bigint
86
+ factoryAddress?: Address
87
+ accountLogicAddress?: Address
88
+ deployedAccountAddress?: Address
89
+ }
90
+
55
91
  /**
56
92
  * The account creation ABI for a kernel smart account (from the KernelFactory)
57
93
  */
@@ -133,15 +169,51 @@ export const EIP1271ABI = [
133
169
  * Default addresses for kernel smart account
134
170
  */
135
171
  export const KERNEL_ADDRESSES: {
136
- ACCOUNT_LOGIC: Address
172
+ ACCOUNT_LOGIC_V0_6: Address
173
+ ACCOUNT_LOGIC_V0_7: Address
137
174
  FACTORY_ADDRESS: Address
138
175
  ENTRYPOINT_V0_6: Address
139
176
  } = {
140
- ACCOUNT_LOGIC: "0xd3082872F8B06073A021b4602e022d5A070d7cfC",
177
+ ACCOUNT_LOGIC_V0_6: "0xd3082872F8B06073A021b4602e022d5A070d7cfC",
178
+ ACCOUNT_LOGIC_V0_7: "0x079D5D8A3275A230AF743cB59c341Ff74B82488D",
141
179
  FACTORY_ADDRESS: "0x5de4839a76cf55d0c90e2061ef4386d962E15ae3",
142
180
  ENTRYPOINT_V0_6: "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789"
143
181
  }
144
182
 
183
+ const getKernelInitData = <entryPoint extends EntryPoint>({
184
+ validatorAddress,
185
+ enableData,
186
+ entryPoint: entryPointAddress
187
+ }: {
188
+ validatorAddress: Address
189
+ enableData: Hex
190
+ entryPoint: entryPoint
191
+ }) => {
192
+ const entryPointVersion = getEntryPointVersion(entryPointAddress)
193
+
194
+ if (entryPointVersion === "v0.6") {
195
+ return encodeFunctionData({
196
+ abi: KernelInitAbi,
197
+ functionName: "initialize",
198
+ args: [validatorAddress, enableData]
199
+ })
200
+ }
201
+
202
+ return encodeFunctionData({
203
+ abi: KernelV3InitAbi,
204
+ functionName: "initialize",
205
+ args: [
206
+ pad(concatHex(["0x1", validatorAddress]), {
207
+ size: 21,
208
+ dir: "left"
209
+ }),
210
+ zeroAddress,
211
+ enableData,
212
+ "0x"
213
+ ]
214
+ })
215
+ }
216
+
145
217
  /**
146
218
  * Get the account initialization code for a kernel smart account
147
219
  * @param index
@@ -149,24 +221,26 @@ export const KERNEL_ADDRESSES: {
149
221
  * @param accountLogicAddress
150
222
  * @param ecdsaValidatorAddress
151
223
  */
152
- const getAccountInitCode = async ({
224
+ const getAccountInitCode = async <entryPoint extends EntryPoint>({
153
225
  index,
154
226
  factoryAddress,
155
227
  accountLogicAddress,
156
228
  validatorAddress,
157
- enableData
229
+ enableData,
230
+ entryPoint: entryPointAddress
158
231
  }: {
159
232
  index: bigint
160
233
  factoryAddress: Address
161
234
  accountLogicAddress: Address
162
235
  validatorAddress: Address
163
236
  enableData: Hex
237
+ entryPoint: entryPoint
164
238
  }): Promise<Hex> => {
165
239
  // Build the account initialization data
166
- const initialisationData = encodeFunctionData({
167
- abi: KernelInitAbi,
168
- functionName: "initialize",
169
- args: [validatorAddress, enableData]
240
+ const initialisationData = getKernelInitData<entryPoint>({
241
+ validatorAddress,
242
+ enableData,
243
+ entryPoint: entryPointAddress
170
244
  })
171
245
 
172
246
  // Build the account init code
@@ -187,24 +261,35 @@ const getAccountInitCode = async ({
187
261
  * @param initCodeProvider
188
262
  */
189
263
  const getAccountAddress = async <
264
+ entryPoint extends EntryPoint,
190
265
  TTransport extends Transport = Transport,
191
266
  TChain extends Chain | undefined = Chain | undefined
192
267
  >({
193
268
  client,
194
- entryPoint,
269
+ entryPoint: entryPointAddress,
195
270
  initCodeProvider
196
271
  }: {
197
272
  client: Client<TTransport, TChain, undefined>
198
273
  initCodeProvider: () => Promise<Hex>
199
- entryPoint: Address
274
+ entryPoint: entryPoint
200
275
  }): Promise<Address> => {
276
+ const entryPointVersion = getEntryPointVersion(entryPointAddress)
277
+
201
278
  // Find the init code for this account
202
279
  const initCode = await initCodeProvider()
280
+ if (entryPointVersion === "v0.6") {
281
+ return getSenderAddress<ENTRYPOINT_ADDRESS_V06_TYPE>(client, {
282
+ initCode,
283
+ entryPoint: entryPointAddress as ENTRYPOINT_ADDRESS_V06_TYPE
284
+ })
285
+ }
203
286
 
204
287
  // Get the sender address based on the init code
205
- return getSenderAddress(client, {
206
- initCode,
207
- entryPoint
288
+ return getSenderAddress<ENTRYPOINT_ADDRESS_V07_TYPE>(client, {
289
+ factory: parseFactoryAddressAndCallDataFromAccountInitCode(initCode)[0],
290
+ factoryData:
291
+ parseFactoryAddressAndCallDataFromAccountInitCode(initCode)[1],
292
+ entryPoint: entryPointAddress as ENTRYPOINT_ADDRESS_V07_TYPE
208
293
  })
209
294
  }
210
295
 
@@ -220,72 +305,80 @@ const getAccountAddress = async <
220
305
  * @param deployedAccountAddress
221
306
  */
222
307
  export async function createKernelAccount<
308
+ entryPoint extends EntryPoint,
223
309
  TTransport extends Transport = Transport,
224
310
  TChain extends Chain | undefined = Chain | undefined
225
311
  >(
226
312
  client: Client<TTransport, TChain, undefined>,
227
313
  {
228
314
  plugins,
229
- entryPoint = KERNEL_ADDRESSES.ENTRYPOINT_V0_6,
315
+ entryPoint: entryPointAddress,
230
316
  index = 0n,
231
317
  factoryAddress = KERNEL_ADDRESSES.FACTORY_ADDRESS,
232
- accountLogicAddress = KERNEL_ADDRESSES.ACCOUNT_LOGIC,
318
+ accountLogicAddress = KERNEL_ADDRESSES.ACCOUNT_LOGIC_V0_6,
233
319
  deployedAccountAddress
234
- }: {
235
- plugins: KernelPluginManagerParams | KernelPluginManager
236
- entryPoint?: Address
237
- index?: bigint
238
- factoryAddress?: Address
239
- accountLogicAddress?: Address
240
- deployedAccountAddress?: Address
241
- }
242
- ): Promise<KernelSmartAccount<TTransport, TChain>> {
243
- const kernelPluginManager = isKernelPluginManager(plugins)
320
+ }: CreateKernelAccountParameters<entryPoint>
321
+ ): Promise<KernelSmartAccount<entryPoint, TTransport, TChain>> {
322
+ const entryPointVersion = getEntryPointVersion(entryPointAddress)
323
+ accountLogicAddress =
324
+ entryPointVersion === "v0.6"
325
+ ? KERNEL_ADDRESSES.ACCOUNT_LOGIC_V0_6
326
+ : KERNEL_ADDRESSES.ACCOUNT_LOGIC_V0_7
327
+
328
+ const kernelPluginManager = isKernelPluginManager<entryPoint>(plugins)
244
329
  ? plugins
245
- : await toKernelPluginManager(client, {
330
+ : await toKernelPluginManager<entryPoint>(client, {
246
331
  sudo: plugins.sudo,
247
332
  regular: plugins.regular,
248
333
  executorData: plugins.executorData,
249
- pluginEnableSignature: plugins.pluginEnableSignature
334
+ pluginEnableSignature: plugins.pluginEnableSignature,
335
+ entryPoint: entryPointAddress
250
336
  })
251
337
  // Helper to generate the init code for the smart account
252
338
  const generateInitCode = async () => {
253
339
  const validatorInitData =
254
340
  await kernelPluginManager.getValidatorInitData()
255
- return getAccountInitCode({
341
+ return getAccountInitCode<entryPoint>({
256
342
  index,
257
343
  factoryAddress,
258
344
  accountLogicAddress,
259
345
  validatorAddress: validatorInitData.validatorAddress,
260
- enableData: validatorInitData.enableData
346
+ enableData: validatorInitData.enableData,
347
+ entryPoint: entryPointAddress
261
348
  })
262
349
  }
263
350
 
264
351
  // Fetch account address and chain id
265
352
  const accountAddress =
266
353
  deployedAccountAddress ??
267
- (await getAccountAddress<TTransport, TChain>({
354
+ (await getAccountAddress<entryPoint, TTransport, TChain>({
268
355
  client,
269
- entryPoint,
356
+ entryPoint: entryPointAddress,
270
357
  initCodeProvider: generateInitCode
271
358
  }))
272
359
 
273
360
  if (!accountAddress) throw new Error("Account address not found")
274
361
 
362
+ let smartAccountDeployed = await isSmartAccountDeployed(
363
+ client,
364
+ accountAddress
365
+ )
366
+
275
367
  const signHashedMessage = async (messageHash: Hex): Promise<Hex> => {
276
- let kernelImplAddr: Address | undefined
277
- try {
278
- const strgAddr = await getAction(
279
- client,
280
- getStorageAt
281
- )({
282
- address: accountAddress,
283
- slot: "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc"
284
- })
285
- if (strgAddr) kernelImplAddr = `0x${strgAddr.slice(26)}` as Hex
286
- } catch (error) {}
287
- const kernelVersion = getKernelVersion(kernelImplAddr)
288
- if (kernelVersion !== "0.2.3" && kernelVersion !== "0.2.4") {
368
+ const kernelImplAddr = await getKernelImplementationAddress(
369
+ client,
370
+ accountAddress
371
+ )
372
+ const kernelVersion = getKernelVersion(
373
+ entryPointAddress,
374
+ kernelImplAddr
375
+ )
376
+ if (
377
+ !hasKernelFeature(
378
+ KERNEL_FEATURES.ERC1271_SIG_WRAPPER,
379
+ kernelVersion
380
+ )
381
+ ) {
289
382
  return kernelPluginManager.signMessage({
290
383
  message: {
291
384
  raw: messageHash
@@ -351,8 +444,27 @@ export async function createKernelAccount<
351
444
  )
352
445
 
353
446
  const domainSeparator = keccak256(encoded)
447
+
448
+ let finalMessageHash = messageHash
449
+ if (
450
+ hasKernelFeature(
451
+ KERNEL_FEATURES.ERC1271_SIG_WRAPPER_WITH_WRAPPED_HASH,
452
+ kernelVersion
453
+ )
454
+ ) {
455
+ finalMessageHash = keccak256(
456
+ encodeAbiParameters(
457
+ [{ type: "bytes32" }, { type: "bytes32" }],
458
+ [
459
+ keccak256(stringToHex("Kernel(bytes32 hash)")),
460
+ messageHash
461
+ ]
462
+ )
463
+ )
464
+ }
465
+
354
466
  const digest = keccak256(
355
- concatHex(["0x1901", domainSeparator, messageHash])
467
+ concatHex(["0x1901", domainSeparator, finalMessageHash])
356
468
  )
357
469
  return kernelPluginManager.signMessage({
358
470
  message: {
@@ -367,15 +479,54 @@ export async function createKernelAccount<
367
479
  async signMessage({ message }) {
368
480
  const messageHash = hashMessage(message)
369
481
  const [isDeployed, signature] = await Promise.all([
370
- isAccountDeployed(),
482
+ isSmartAccountDeployed(client, accountAddress),
371
483
  signHashedMessage(messageHash)
372
484
  ])
373
- return create6492Signature(isDeployed, signature)
485
+ const kernelImplAddr = await getKernelImplementationAddress(
486
+ client,
487
+ accountAddress
488
+ )
489
+ const kernelVersion = getKernelVersion(
490
+ entryPointAddress,
491
+ kernelImplAddr
492
+ )
493
+ if (
494
+ !hasKernelFeature(
495
+ KERNEL_FEATURES.ERC1271_WITH_VALIDATOR,
496
+ kernelVersion
497
+ )
498
+ ) {
499
+ return create6492Signature(isDeployed, signature)
500
+ }
501
+
502
+ const validatorInitData =
503
+ await kernelPluginManager.getValidatorInitData()
504
+ return create6492Signature(
505
+ isDeployed,
506
+ concatHex([
507
+ pad(
508
+ concatHex(["0x1", validatorInitData.validatorAddress]),
509
+ {
510
+ size: 21,
511
+ dir: "left"
512
+ }
513
+ ),
514
+ signature
515
+ ])
516
+ )
374
517
  },
375
518
  async signTransaction(_, __) {
376
519
  throw new SignTransactionNotSupportedBySmartAccount()
377
520
  },
378
521
  async signTypedData(typedData) {
522
+ const kernelImplAddr = await getKernelImplementationAddress(
523
+ client,
524
+ accountAddress
525
+ )
526
+ const kernelVersion = getKernelVersion(
527
+ entryPointAddress,
528
+ kernelImplAddr
529
+ )
379
530
  const types = {
380
531
  EIP712Domain: getTypesForEIP712Domain({
381
532
  domain: typedData.domain
@@ -394,21 +545,36 @@ export async function createKernelAccount<
394
545
 
395
546
  const typedHash = hashTypedData(typedData)
396
547
  const [isDeployed, signature] = await Promise.all([
397
- isAccountDeployed(),
548
+ isSmartAccountDeployed(client, accountAddress),
398
549
  signHashedMessage(typedHash)
399
550
  ])
400
- return create6492Signature(isDeployed, signature)
551
+ if (
552
+ !hasKernelFeature(
553
+ KERNEL_FEATURES.ERC1271_WITH_VALIDATOR,
554
+ kernelVersion
555
+ )
556
+ ) {
557
+ return create6492Signature(isDeployed, signature)
558
+ }
559
+ const validatorInitData =
560
+ await kernelPluginManager.getValidatorInitData()
561
+ return create6492Signature(
562
+ isDeployed,
563
+ concatHex([
564
+ pad(
565
+ // [TODO] - Make the Validator type dynamic to support permission validators
566
+ concatHex(["0x1", validatorInitData.validatorAddress]),
567
+ {
568
+ size: 21,
569
+ dir: "left"
570
+ }
571
+ ),
572
+ signature
573
+ ])
574
+ )
401
575
  }
402
576
  })
403
577
 
404
- const isAccountDeployed = async (): Promise<boolean> => {
405
- const contractCode = await getBytecode(client, {
406
- address: accountAddress
407
- })
408
-
409
- return (contractCode?.length ?? 0) > 2
410
- }
411
-
412
578
  const create6492Signature = async (
413
579
  isDeployed: boolean,
414
580
  signature: Hash
@@ -433,17 +599,45 @@ export async function createKernelAccount<
433
599
  ...account,
434
600
  client: client,
435
601
  publicKey: accountAddress,
436
- entryPoint: entryPoint,
602
+ entryPoint: entryPointAddress,
437
603
  source: "kernelSmartAccount",
438
604
  kernelPluginManager,
439
605
  generateInitCode,
440
606
 
607
+ async getFactory() {
608
+ if (smartAccountDeployed) return undefined
609
+
610
+ smartAccountDeployed = await isSmartAccountDeployed(
611
+ client,
612
+ accountAddress
613
+ )
614
+
615
+ if (smartAccountDeployed) return undefined
616
+
617
+ return factoryAddress
618
+ },
619
+
620
+ async getFactoryData() {
621
+ if (smartAccountDeployed) return undefined
622
+
623
+ smartAccountDeployed = await isSmartAccountDeployed(
624
+ client,
625
+ accountAddress
626
+ )
627
+
628
+ if (smartAccountDeployed) return undefined
629
+
630
+ return parseFactoryAddressAndCallDataFromAccountInitCode(
631
+ await generateInitCode()
632
+ )[1]
633
+ },
634
+
441
635
  // Get the nonce of the smart account
442
636
  async getNonce() {
443
- const key = await kernelPluginManager.getNonceKey()
637
+ const key = await kernelPluginManager.getNonceKey(accountAddress)
444
638
  return getAccountNonce(client, {
445
639
  sender: accountAddress,
446
- entryPoint: entryPoint,
640
+ entryPoint: entryPointAddress,
447
641
  key
448
642
  })
449
643
  },
@@ -455,32 +649,66 @@ export async function createKernelAccount<
455
649
 
456
650
  // Encode the init code
457
651
  async getInitCode() {
458
- if (await isAccountDeployed()) {
459
- return "0x"
460
- } else {
461
- return generateInitCode()
462
- }
652
+ if (smartAccountDeployed) return "0x"
653
+
654
+ smartAccountDeployed = await isSmartAccountDeployed(
655
+ client,
656
+ accountAddress
657
+ )
658
+
659
+ if (smartAccountDeployed) return "0x"
660
+ return generateInitCode()
463
661
  },
464
662
 
663
+ // [TODO] - CLeanup the encoding functions
664
+
465
665
  // Encode the deploy call data
466
666
  async encodeDeployCallData(_tx) {
667
+ if (entryPointVersion === "v0.6") {
668
+ return encodeFunctionData({
669
+ abi: KernelExecuteAbi,
670
+ functionName: "executeDelegateCall",
671
+ args: [
672
+ createCallAddress,
673
+ encodeFunctionData({
674
+ abi: createCallAbi,
675
+ functionName: "performCreate",
676
+ args: [
677
+ 0n,
678
+ encodeDeployData({
679
+ abi: _tx.abi,
680
+ bytecode: _tx.bytecode,
681
+ args: _tx.args
682
+ } as EncodeDeployDataParameters)
683
+ ]
684
+ })
685
+ ]
686
+ })
687
+ }
688
+
467
689
  return encodeFunctionData({
468
- abi: KernelExecuteAbi,
469
- functionName: "executeDelegateCall",
690
+ abi: KernelV3ExecuteAbi,
691
+ functionName: "execute",
470
692
  args: [
471
- createCallAddress,
472
- encodeFunctionData({
473
- abi: createCallAbi,
474
- functionName: "performCreate",
475
- args: [
476
- 0n,
477
- encodeDeployData({
478
- abi: _tx.abi,
479
- bytecode: _tx.bytecode,
480
- args: _tx.args
481
- } as EncodeDeployDataParameters)
693
+ getExecMode(CALL_TYPE.DELEGATE_CALL, EXEC_TYPE.DEFAULT),
694
+ encodeAbiParameters(
695
+ parseAbiParameters("address to, bytes data"),
696
+ [
697
+ createCallAddress,
698
+ encodeFunctionData({
699
+ abi: createCallAbi,
700
+ functionName: "performCreate",
701
+ args: [
702
+ 0n,
703
+ encodeDeployData({
704
+ abi: _tx.abi,
705
+ bytecode: _tx.bytecode,
706
+ args: _tx.args
707
+ } as EncodeDeployDataParameters)
708
+ ]
709
+ })
482
710
  ]
483
- })
711
+ )
484
712
  ]
485
713
  })
486
714
  },
@@ -490,20 +718,65 @@ export async function createKernelAccount<
490
718
  const tx = _tx as KernelEncodeCallDataArgs
491
719
  if (Array.isArray(tx)) {
492
720
  // Encode a batched call
721
+ if (entryPointVersion === "v0.6") {
722
+ return encodeFunctionData({
723
+ abi: KernelExecuteAbi,
724
+ functionName: "executeBatch",
725
+ args: [
726
+ tx.map((txn) => {
727
+ if (txn.callType === "delegatecall") {
728
+ throw new Error("Cannot batch delegatecall")
729
+ }
730
+ return {
731
+ to: txn.to,
732
+ value: txn.value,
733
+ data: txn.data
734
+ }
735
+ })
736
+ ]
737
+ })
738
+ }
493
739
  return encodeFunctionData({
494
- abi: KernelExecuteAbi,
495
- functionName: "executeBatch",
740
+ abi: KernelV3ExecuteAbi,
741
+ functionName: "execute",
496
742
  args: [
497
- tx.map((txn) => {
498
- if (txn.callType === "delegatecall") {
499
- throw new Error("Cannot batch delegatecall")
500
- }
501
- return {
502
- to: txn.to,
503
- value: txn.value,
504
- data: txn.data
505
- }
506
- })
743
+ getExecMode(CALL_TYPE.BATCH, EXEC_TYPE.DEFAULT),
744
+ encodeAbiParameters(
745
+ [
746
+ {
747
+ name: "executionBatch",
748
+ type: "tuple[]",
749
+ components: [
750
+ {
751
+ name: "target",
752
+ type: "address"
753
+ },
754
+ {
755
+ name: "value",
756
+ type: "uint256"
757
+ },
758
+ {
759
+ name: "callData",
760
+ type: "bytes"
761
+ }
762
+ ]
763
+ }
764
+ ],
765
+ [
766
+ tx.map((txn) => {
767
+ if (txn.callType === "delegatecall") {
768
+ throw new Error(
769
+ "Cannot batch delegatecall"
770
+ )
771
+ }
772
+ return {
773
+ target: txn.to,
774
+ value: txn.value,
775
+ callData: txn.data
776
+ }
777
+ })
778
+ ]
779
+ )
507
780
  ]
508
781
  })
509
782
  }
@@ -513,18 +786,48 @@ export async function createKernelAccount<
513
786
  if (tx.to.toLowerCase() === accountAddress.toLowerCase()) {
514
787
  return tx.data
515
788
  }
789
+ if (entryPointVersion === "v0.6") {
790
+ return encodeFunctionData({
791
+ abi: KernelExecuteAbi,
792
+ functionName: "execute",
793
+ args: [tx.to, tx.value, tx.data, 0]
794
+ })
795
+ }
516
796
  return encodeFunctionData({
517
- abi: KernelExecuteAbi,
797
+ abi: KernelV3ExecuteAbi,
518
798
  functionName: "execute",
519
- args: [tx.to, tx.value, tx.data, 0]
799
+ args: [
800
+ getExecMode(CALL_TYPE.SINGLE, EXEC_TYPE.DEFAULT),
801
+ encodeAbiParameters(
802
+ parseAbiParameters(
803
+ "address target, uint256 value, bytes calldata callData"
804
+ ),
805
+ [tx.to, tx.value, tx.data]
806
+ )
807
+ ]
520
808
  })
521
809
  }
522
810
 
523
811
  if (tx.callType === "delegatecall") {
812
+ if (entryPointVersion === "v0.6") {
813
+ return encodeFunctionData({
814
+ abi: KernelExecuteAbi,
815
+ functionName: "executeDelegateCall",
816
+ args: [tx.to, tx.data]
817
+ })
818
+ }
524
819
  return encodeFunctionData({
525
- abi: KernelExecuteAbi,
526
- functionName: "executeDelegateCall",
527
- args: [tx.to, tx.data]
820
+ abi: KernelV3ExecuteAbi,
821
+ functionName: "execute",
822
+ args: [
823
+ getExecMode(CALL_TYPE.DELEGATE_CALL, EXEC_TYPE.DEFAULT),
824
+ encodeAbiParameters(
825
+ parseAbiParameters(
826
+ "address target, bytes calldata callData"
827
+ ),
828
+ [tx.to, tx.data]
829
+ )
830
+ ]
528
831
  })
529
832
  }
530
833