diff --git a/.changeset/pink-mayflies-sparkle.md b/.changeset/pink-mayflies-sparkle.md new file mode 100644 index 00000000..b016b402 --- /dev/null +++ b/.changeset/pink-mayflies-sparkle.md @@ -0,0 +1,5 @@ +--- +"permissionless": patch +--- + +Added decodeNonce util function diff --git a/packages/permissionless/utils/decodeNonce.test.ts b/packages/permissionless/utils/decodeNonce.test.ts new file mode 100644 index 00000000..d15fc0d6 --- /dev/null +++ b/packages/permissionless/utils/decodeNonce.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, test } from "vitest" +import { decodeNonce, encodeNonce } from "./index" + +describe("decodeNonce", () => { + test("should encode key and sequence correctly", async () => { + const key = 123456789012345678901234n + const sequence = 9876543210n + + const result = encodeNonce({ key, sequence }) + + const decodedNonce = decodeNonce(result) + + expect(decodedNonce.key).toBe(key) + expect(decodedNonce.sequence).toBe(sequence) + }) + + test("should handle zero values correctly", () => { + const key = BigInt(0) + const sequence = BigInt(0) + + const result = encodeNonce({ key, sequence }) + + const decodedNonce = decodeNonce(result) + + expect(decodedNonce.key).toBe(key) + expect(decodedNonce.sequence).toBe(sequence) + }) + + test("should handle large values correctly", () => { + const key = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFF") + const sequence = BigInt("0xFFFFFFFF") + + const result = encodeNonce({ key, sequence }) + + const decodedNonce = decodeNonce(result) + + expect(decodedNonce.key).toBe(key) + expect(decodedNonce.sequence).toBe(sequence) + }) +}) diff --git a/packages/permissionless/utils/decodeNonce.ts b/packages/permissionless/utils/decodeNonce.ts new file mode 100644 index 00000000..e38a9e39 --- /dev/null +++ b/packages/permissionless/utils/decodeNonce.ts @@ -0,0 +1,10 @@ +import { toHex } from "viem" + +export function decodeNonce(nonce: bigint): { key: bigint; sequence: bigint } { + const parsedNonce = BigInt(toHex(nonce, { size: 32 })) + + const key = parsedNonce >> BigInt(64) + const sequence = parsedNonce & BigInt("0xFFFFFFFFFFFFFFFF") + + return { key, sequence } +} diff --git a/packages/permissionless/utils/encodeNonce.test.ts b/packages/permissionless/utils/encodeNonce.test.ts index f3214f2e..7d2cc099 100644 --- a/packages/permissionless/utils/encodeNonce.test.ts +++ b/packages/permissionless/utils/encodeNonce.test.ts @@ -1,6 +1,6 @@ -import { encodeNonce } from "permissionless/utils" import { toHex } from "viem" import { describe, expect, test } from "vitest" +import { encodeNonce } from "./index" describe("encodeNonce", () => { test("should encode key and sequence correctly", async () => { diff --git a/packages/permissionless/utils/index.ts b/packages/permissionless/utils/index.ts index ef3fee6a..55b42674 100644 --- a/packages/permissionless/utils/index.ts +++ b/packages/permissionless/utils/index.ts @@ -23,6 +23,7 @@ export function parseAccount(account: Address | Account): Account { return { address: account, type: "json-rpc" } return account } +import { decodeNonce } from "./decodeNonce" import { encodeNonce } from "./encodeNonce" import { ENTRYPOINT_ADDRESS_V06, @@ -49,6 +50,7 @@ export { getPackedUserOperation, getEntryPointVersion, encodeNonce, + decodeNonce, ENTRYPOINT_ADDRESS_V06, ENTRYPOINT_ADDRESS_V07 }