From 80ee41e96776f688ba6adf8e97723e7fcdb353e1 Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Thu, 18 Jul 2024 18:10:54 +0100 Subject: [PATCH 1/4] Add decodeNonce --- .../permissionless/utils/decodeNonce.test.ts | 47 +++++++++++++++++++ packages/permissionless/utils/decodeNonce.ts | 10 ++++ packages/permissionless/utils/index.ts | 2 + 3 files changed, 59 insertions(+) create mode 100644 packages/permissionless/utils/decodeNonce.test.ts create mode 100644 packages/permissionless/utils/decodeNonce.ts diff --git a/packages/permissionless/utils/decodeNonce.test.ts b/packages/permissionless/utils/decodeNonce.test.ts new file mode 100644 index 00000000..7f135f54 --- /dev/null +++ b/packages/permissionless/utils/decodeNonce.test.ts @@ -0,0 +1,47 @@ +import { decodeNonce, encodeNonce } from "permissionless/utils" +import { toHex } from "viem" +import { describe, expect, test } from "vitest" + +describe("decodeNonce", () => { + test("should encode key and sequence correctly", async () => { + const key = 123456789012345678901234n + const sequence = 9876543210n + const expectedKey = BigInt(toHex(key, { size: 24 })) + const expectedSequence = BigInt(toHex(sequence, { size: 8 })) + + 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 expectedKey = BigInt(toHex(key, { size: 24 })) + const expectedSequence = BigInt(toHex(sequence, { size: 8 })) + + 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 expectedKey = BigInt(toHex(key, { size: 24 })) + const expectedSequence = BigInt(toHex(sequence, { size: 8 })) + + 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/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 } From 299513dfee5054a3d59591f4bf11cb145783988d Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Thu, 18 Jul 2024 18:11:58 +0100 Subject: [PATCH 2/4] Add changeset --- .changeset/pink-mayflies-sparkle.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pink-mayflies-sparkle.md 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 From b7596d1baec302cb0ac88d3429fb846980a869c1 Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Thu, 18 Jul 2024 18:29:08 +0100 Subject: [PATCH 3/4] improve coverage --- packages/permissionless/utils/decodeNonce.test.ts | 9 +-------- packages/permissionless/utils/encodeNonce.test.ts | 2 +- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/permissionless/utils/decodeNonce.test.ts b/packages/permissionless/utils/decodeNonce.test.ts index 7f135f54..e066aa01 100644 --- a/packages/permissionless/utils/decodeNonce.test.ts +++ b/packages/permissionless/utils/decodeNonce.test.ts @@ -1,13 +1,10 @@ -import { decodeNonce, encodeNonce } from "permissionless/utils" -import { toHex } from "viem" +import { decodeNonce, encodeNonce } from "./index" import { describe, expect, test } from "vitest" describe("decodeNonce", () => { test("should encode key and sequence correctly", async () => { const key = 123456789012345678901234n const sequence = 9876543210n - const expectedKey = BigInt(toHex(key, { size: 24 })) - const expectedSequence = BigInt(toHex(sequence, { size: 8 })) const result = encodeNonce({ key, sequence }) @@ -20,8 +17,6 @@ describe("decodeNonce", () => { test("should handle zero values correctly", () => { const key = BigInt(0) const sequence = BigInt(0) - const expectedKey = BigInt(toHex(key, { size: 24 })) - const expectedSequence = BigInt(toHex(sequence, { size: 8 })) const result = encodeNonce({ key, sequence }) @@ -34,8 +29,6 @@ describe("decodeNonce", () => { test("should handle large values correctly", () => { const key = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFF") const sequence = BigInt("0xFFFFFFFF") - const expectedKey = BigInt(toHex(key, { size: 24 })) - const expectedSequence = BigInt(toHex(sequence, { size: 8 })) const result = encodeNonce({ key, sequence }) diff --git a/packages/permissionless/utils/encodeNonce.test.ts b/packages/permissionless/utils/encodeNonce.test.ts index f3214f2e..a4f6ed44 100644 --- a/packages/permissionless/utils/encodeNonce.test.ts +++ b/packages/permissionless/utils/encodeNonce.test.ts @@ -1,4 +1,4 @@ -import { encodeNonce } from "permissionless/utils" +import { encodeNonce } from "./index" import { toHex } from "viem" import { describe, expect, test } from "vitest" From 1ba5eadab19875d897d3d1bd9f289b3d0a9e3395 Mon Sep 17 00:00:00 2001 From: plusminushalf Date: Thu, 18 Jul 2024 17:29:49 +0000 Subject: [PATCH 4/4] chore: format --- packages/permissionless/utils/decodeNonce.test.ts | 2 +- packages/permissionless/utils/encodeNonce.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/permissionless/utils/decodeNonce.test.ts b/packages/permissionless/utils/decodeNonce.test.ts index e066aa01..d15fc0d6 100644 --- a/packages/permissionless/utils/decodeNonce.test.ts +++ b/packages/permissionless/utils/decodeNonce.test.ts @@ -1,5 +1,5 @@ -import { decodeNonce, encodeNonce } from "./index" import { describe, expect, test } from "vitest" +import { decodeNonce, encodeNonce } from "./index" describe("decodeNonce", () => { test("should encode key and sequence correctly", async () => { diff --git a/packages/permissionless/utils/encodeNonce.test.ts b/packages/permissionless/utils/encodeNonce.test.ts index a4f6ed44..7d2cc099 100644 --- a/packages/permissionless/utils/encodeNonce.test.ts +++ b/packages/permissionless/utils/encodeNonce.test.ts @@ -1,6 +1,6 @@ -import { encodeNonce } from "./index" import { toHex } from "viem" import { describe, expect, test } from "vitest" +import { encodeNonce } from "./index" describe("encodeNonce", () => { test("should encode key and sequence correctly", async () => {