Skip to content

Commit

Permalink
fix: use ep constant from permissionless and have nonce and signature…
Browse files Browse the repository at this point in the history
… methods use validator address from either user input or defaults
  • Loading branch information
SahilVasava committed Apr 19, 2024
1 parent a272576 commit 3f5b084
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ describe("ECDSA kernel Account", () => {

test("Account address", async () => {
const ecdsaSmartAccount = await getSignerToEcdsaKernelAccount()
console.log({ address: ecdsaSmartAccount.address })

expectTypeOf(ecdsaSmartAccount.address).toBeString()
expect(ecdsaSmartAccount.address).toHaveLength(42)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {
Prettify
} from "../../types"
import type { ENTRYPOINT_ADDRESS_V06_TYPE } from "../../types/entrypoint"
import { getEntryPointVersion } from "../../utils"
import { ENTRYPOINT_ADDRESS_V06, getEntryPointVersion } from "../../utils"
import { getUserOperationHash } from "../../utils/getUserOperationHash"
import { isSmartAccountDeployed } from "../../utils/isSmartAccountDeployed"
import { toSmartAccount } from "../toSmartAccount"
Expand Down Expand Up @@ -120,9 +120,7 @@ export const KERNEL_VERSION_TO_ADDRESSES_MAP: {
* @param entryPoint
*/
const getKernelVersion = (entryPoint: EntryPoint): KernelVersion => {
return entryPoint === "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789"
? "0.2.2"
: "0.3.0-beta"
return entryPoint === ENTRYPOINT_ADDRESS_V06 ? "0.2.2" : "0.3.0-beta"
}

type KERNEL_ADDRESSES = {
Expand Down Expand Up @@ -166,11 +164,10 @@ const getDefaultAddresses = <entryPoint extends EntryPoint>(
}
}

export const getEcdsaRootIdentifierForKernelV3 = () => {
const ecdsaValidatorAddress =
KERNEL_VERSION_TO_ADDRESSES_MAP["0.3.0-beta"].ECDSA_VALIDATOR

return concatHex([VALIDATOR_TYPE.VALIDATOR, ecdsaValidatorAddress])
export const getEcdsaRootIdentifierForKernelV3 = (
validatorAddress: Address
) => {
return concatHex([VALIDATOR_TYPE.VALIDATOR, validatorAddress])
}

/**
Expand Down Expand Up @@ -202,7 +199,7 @@ const getInitialisationData = <entryPoint extends EntryPoint>({
abi: KernelV3InitAbi,
functionName: "initialize",
args: [
getEcdsaRootIdentifierForKernelV3(),
getEcdsaRootIdentifierForKernelV3(ecdsaValidatorAddress),
zeroAddress /* hookAddress */,
owner,
"0x" /* hookData */
Expand Down Expand Up @@ -456,13 +453,18 @@ export async function signerToEcdsaKernelSmartAccount<
return toSmartAccount({
address: accountAddress,
async signMessage({ message }) {
return signMessage(client, {
const signature = await signMessage(client, {
account: viemSigner,
message,
accountAddress,
accountVersion: kernelVersion,
chainId
})

return concatHex([
getEcdsaRootIdentifierForKernelV3(ecdsaValidatorAddress),
signature
])
},
async signTransaction(_, __) {
throw new SignTransactionNotSupportedBySmartAccount()
Expand All @@ -473,16 +475,22 @@ export async function signerToEcdsaKernelSmartAccount<
| keyof TTypedData
| "EIP712Domain" = keyof TTypedData
>(typedData: TypedDataDefinition<TTypedData, TPrimaryType>) {
return signTypedData<TTypedData, TPrimaryType, TChain, undefined>(
client,
{
account: viemSigner,
...typedData,
accountAddress,
accountVersion: kernelVersion,
chainId
}
)
const signature = await signTypedData<
TTypedData,
TPrimaryType,
TChain,
undefined
>(client, {
account: viemSigner,
...typedData,
accountAddress,
accountVersion: kernelVersion,
chainId
})
return concatHex([
getEcdsaRootIdentifierForKernelV3(ecdsaValidatorAddress),
signature
])
},
client: client,
publicKey: accountAddress,
Expand All @@ -492,7 +500,8 @@ export async function signerToEcdsaKernelSmartAccount<
// Get the nonce of the smart account
async getNonce() {
const key = getNonceKeyWithEncoding(
kernelVersion
kernelVersion,
ecdsaValidatorAddress
// @dev specify the custom nonceKey here when integrating the said feature
/*, nonceKey */
)
Expand Down
12 changes: 4 additions & 8 deletions packages/permissionless/accounts/kernel/utils/getNonceKey.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { concatHex, maxUint16, pad, toHex } from "viem"
import { type Address, concatHex, maxUint16, pad, toHex } from "viem"
import { VALIDATOR_MODE, VALIDATOR_TYPE } from "../constants"
import {
KERNEL_VERSION_TO_ADDRESSES_MAP,
type KernelVersion
} from "../signerToEcdsaKernelSmartAccount"
import { type KernelVersion } from "../signerToEcdsaKernelSmartAccount"

export const getNonceKeyWithEncoding = (
accountVerion: KernelVersion,
validatorAddress: Address,
nonceKey = 0n
) => {
if (accountVerion === "0.2.2") {
Expand All @@ -18,15 +16,13 @@ export const getNonceKeyWithEncoding = (
`nonce key must be equal or less than 2 bytes(maxUint16) for Kernel version ${accountVerion}`
)

const ecdsaValidatorAddress =
KERNEL_VERSION_TO_ADDRESSES_MAP["0.3.0-beta"].ECDSA_VALIDATOR // Just use ECDSA as root validator
const validatorMode = VALIDATOR_MODE.DEFAULT
const validatorType = VALIDATOR_TYPE.ROOT
const encoding = pad(
concatHex([
validatorMode, // 1 byte
validatorType, // 1 byte
ecdsaValidatorAddress, // 20 bytes
validatorAddress, // 20 bytes
toHex(nonceKey, { size: 2 }) // 2 byte
]),
{ size: 24 }
Expand Down
6 changes: 2 additions & 4 deletions packages/permissionless/accounts/kernel/utils/signMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import {
type SignMessageParameters,
type SignMessageReturnType,
type Transport,
concatHex,
hashMessage,
publicActions
} from "viem"
import { signMessage as _signMessage } from "viem/actions"
import { getEcdsaRootIdentifierForKernelV3 } from "../signerToEcdsaKernelSmartAccount"
import { type WrapMessageHashParams, wrapMessageHash } from "./wrapMessageHash"

export async function signMessage<
Expand Down Expand Up @@ -41,10 +39,10 @@ export async function signMessage<
: await client.extend(publicActions).getChainId()
})

const _signature = await _signMessage(client, {
const signature = await _signMessage(client, {
account: account_ as LocalAccount,
message: { raw: wrappedMessageHash }
})

return concatHex([getEcdsaRootIdentifierForKernelV3(), _signature])
return signature
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
type SignTypedDataParameters,
type SignTypedDataReturnType,
type Transport,
concatHex,
getTypesForEIP712Domain,
hashTypedData,
publicActions,
Expand All @@ -17,7 +16,6 @@ import {
signMessage as _signMessage,
signTypedData as _signTypedData
} from "viem/actions"
import { getEcdsaRootIdentifierForKernelV3 } from "../signerToEcdsaKernelSmartAccount"
import { type WrapMessageHashParams, wrapMessageHash } from "./wrapMessageHash"

export async function signTypedData<
Expand Down Expand Up @@ -66,10 +64,10 @@ export async function signTypedData<
: await client.extend(publicActions).getChainId()
})

const _signature = await _signMessage(client, {
const signature = await _signMessage(client, {
account: account_ as LocalAccount,
message: { raw: wrappedMessageHash }
})

return concatHex([getEcdsaRootIdentifierForKernelV3(), _signature])
return signature
}

0 comments on commit 3f5b084

Please sign in to comment.