Skip to content

Commit e778db3

Browse files
committed
use actual kernel version for deployed contracts
1 parent 10543a6 commit e778db3

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

.changeset/olive-boats-rest.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"permissionless": patch
3+
---
4+
5+
Now supports using deployed Kernel accounts with a different version

packages/permissionless/accounts/kernel/toEcdsaKernelSmartAccount.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
getUserOperationHash,
2828
toSmartAccount
2929
} from "viem/account-abstraction"
30-
import { signMessage as _signMessage, getChainId } from "viem/actions"
30+
import { getChainId } from "viem/actions"
3131
import { getAction } from "viem/utils"
3232
import { getAccountNonce } from "../../actions/public/getAccountNonce.js"
3333
import { getSenderAddress } from "../../actions/public/getSenderAddress.js"
@@ -44,6 +44,7 @@ import {
4444
VALIDATOR_TYPE
4545
} from "./constants.js"
4646
import { encodeCallData } from "./utils/encodeCallData.js"
47+
import { getActualKernelVersion } from "./utils/getActualKernelVersion.js"
4748
import { getNonceKeyWithEncoding } from "./utils/getNonceKey.js"
4849
import { isKernelV2 } from "./utils/isKernelV2.js"
4950
import { signMessage } from "./utils/signMessage.js"
@@ -479,15 +480,22 @@ export async function toEcdsaKernelSmartAccount<
479480
return this.signMessage({ message: hash })
480481
},
481482
async signMessage({ message }) {
483+
const accountAddress = await this.getAddress()
484+
const actualKernelVersion = await getActualKernelVersion(
485+
client,
486+
accountAddress,
487+
kernelVersion
488+
)
489+
482490
const signature = await signMessage({
483491
owner: localOwner,
484492
message,
485493
accountAddress: await this.getAddress(),
486-
kernelVersion,
494+
kernelVersion: actualKernelVersion,
487495
chainId: await getMemoizedChainId()
488496
})
489497

490-
if (isKernelV2(kernelVersion)) {
498+
if (isKernelV2(actualKernelVersion)) {
491499
return signature
492500
}
493501

@@ -497,15 +505,22 @@ export async function toEcdsaKernelSmartAccount<
497505
])
498506
},
499507
async signTypedData(typedData) {
508+
const accountAddress = await this.getAddress()
509+
const actualKernelVersion = await getActualKernelVersion(
510+
client,
511+
accountAddress,
512+
kernelVersion
513+
)
514+
500515
const signature = await signTypedData({
501516
owner: localOwner,
502517
chainId: await getMemoizedChainId(),
503518
...(typedData as TypedDataDefinition),
504519
accountAddress: await this.getAddress(),
505-
kernelVersion
520+
kernelVersion: actualKernelVersion
506521
})
507522

508-
if (isKernelV2(kernelVersion)) {
523+
if (isKernelV2(actualKernelVersion)) {
509524
return signature
510525
}
511526

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { type Address, type Client, getContract } from "viem"
2+
import type { KernelVersion } from "../toEcdsaKernelSmartAccount"
3+
4+
// If the kernel contract is already deployed, we can get the actual version from the contract
5+
export async function getActualKernelVersion(
6+
client: Client,
7+
address: Address,
8+
version: KernelVersion<"0.6" | "0.7">
9+
): Promise<KernelVersion<"0.6" | "0.7">> {
10+
try {
11+
const contract = getContract({
12+
address,
13+
abi: [
14+
{
15+
type: "function",
16+
name: "eip712Domain",
17+
inputs: [],
18+
outputs: [
19+
{
20+
name: "fields",
21+
type: "bytes1",
22+
internalType: "bytes1"
23+
},
24+
{
25+
name: "name",
26+
type: "string",
27+
internalType: "string"
28+
},
29+
{
30+
name: "version",
31+
type: "string",
32+
internalType: "string"
33+
},
34+
{
35+
name: "chainId",
36+
type: "uint256",
37+
internalType: "uint256"
38+
},
39+
{
40+
name: "verifyingContract",
41+
type: "address",
42+
internalType: "address"
43+
},
44+
{
45+
name: "salt",
46+
type: "bytes32",
47+
internalType: "bytes32"
48+
},
49+
{
50+
name: "extensions",
51+
type: "uint256[]",
52+
internalType: "uint256[]"
53+
}
54+
],
55+
stateMutability: "view"
56+
}
57+
],
58+
client
59+
})
60+
61+
const [, , version] = await contract.read.eip712Domain()
62+
63+
return version as KernelVersion<"0.6" | "0.7">
64+
} catch {
65+
return version
66+
}
67+
}

0 commit comments

Comments
 (0)