Skip to content

Commit

Permalink
feat: add entity id nonce logic
Browse files Browse the repository at this point in the history
  • Loading branch information
howydev committed Dec 12, 2024
1 parent ed7e4b9 commit 5d1c446
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions account-kit/smart-contracts/src/ma-v2/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
concatHex,
encodeFunctionData,
getContract,
type Address,
type Chain,
type Hex,
Expand Down Expand Up @@ -49,6 +50,8 @@ export type CreateSMAV2AccountAccountParams<
initialOwner?: Address;
accountAddress?: Address;
entryPoint?: EntryPointDef<TEntryPointVersion, Chain>;
isGlobalValidation?: boolean;
entityId?: bigint;
};

export async function createSMAV2Account<
Expand All @@ -71,8 +74,14 @@ export async function createSMAV2Account(
initialOwner,
accountAddress,
entryPoint = getEntryPoint(chain, { version: "0.7.0" }),
isGlobalValidation = true,
entityId = 0n,
} = config;

if (entityId) {
throw new Error("Entity ID must be less than uint32.max");
}

const client = createBundlerClient({
transport,
chain,
Expand Down Expand Up @@ -120,8 +129,36 @@ export async function createSMAV2Account(
),
});

// TODO: add deferred action flag
const getAccountNonce = async (nonceKey?: bigint): Promise<bigint> => {
// uint32 entityId + (bytes1 options) makes a uint40
const nonceSuffix: bigint =
entityId * 256n + (isGlobalValidation ? 1n : 0n);

if (nonceKey) {
// if nonceKey is less than nonceSuffix it cannot contain it
if (nonceKey < nonceSuffix || nonceKey % 2n ** 40n != nonceSuffix) {

Check warning on line 140 in account-kit/smart-contracts/src/ma-v2/account/account.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected '!==' and instead saw '!='
throw new Error("Invalid nonceKey");
}
} else {
nonceKey = nonceSuffix;
}

const entryPointContract = getContract({
address: entryPoint.address,
abi: entryPoint.abi,
client,
});

return entryPointContract.read.getNonce([
accountAddress,
nonceKey,
]) as Promise<bigint>;
};

return {
...baseAccount,
getAccountNonce,
getSigner: () => signer,
};
}

0 comments on commit 5d1c446

Please sign in to comment.