From c442a914f160ba407d2f77cc5d970ef37d6c98f9 Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Wed, 17 Jul 2024 13:08:42 +0100 Subject: [PATCH 1/5] Add encodeNonce util --- packages/permissionless/utils/encodeNonce.ts | 6 ++++++ packages/permissionless/utils/index.ts | 2 ++ 2 files changed, 8 insertions(+) create mode 100644 packages/permissionless/utils/encodeNonce.ts diff --git a/packages/permissionless/utils/encodeNonce.ts b/packages/permissionless/utils/encodeNonce.ts new file mode 100644 index 00000000..ce801f56 --- /dev/null +++ b/packages/permissionless/utils/encodeNonce.ts @@ -0,0 +1,6 @@ +export function encodeNonce({ + key, + sequence +}: { key: bigint; sequence: bigint }): bigint { + return (key << BigInt(64)) + sequence +} diff --git a/packages/permissionless/utils/index.ts b/packages/permissionless/utils/index.ts index 99d5896c..ef3fee6a 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 { encodeNonce } from "./encodeNonce" import { ENTRYPOINT_ADDRESS_V06, ENTRYPOINT_ADDRESS_V07, @@ -47,6 +48,7 @@ export { getAddressFromInitCodeOrPaymasterAndData, getPackedUserOperation, getEntryPointVersion, + encodeNonce, ENTRYPOINT_ADDRESS_V06, ENTRYPOINT_ADDRESS_V07 } From c7585b4d41714126fef5933575b5702c7694f6ba Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Wed, 17 Jul 2024 13:17:20 +0100 Subject: [PATCH 2/5] convert to proper size --- packages/permissionless/utils/encodeNonce.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/permissionless/utils/encodeNonce.ts b/packages/permissionless/utils/encodeNonce.ts index ce801f56..89387fa5 100644 --- a/packages/permissionless/utils/encodeNonce.ts +++ b/packages/permissionless/utils/encodeNonce.ts @@ -1,6 +1,8 @@ -export function encodeNonce({ - key, - sequence -}: { key: bigint; sequence: bigint }): bigint { +import { toHex } from "viem" + +export function encodeNonce(args: { key: bigint; sequence: bigint }): bigint { + const key = BigInt(toHex(args.key, { size: 24 })) + const sequence = BigInt(toHex(args.sequence, { size: 8 })) + return (key << BigInt(64)) + sequence } From cc5a5c28f8aaa3b6fe63a69c72777fd32dd8bd43 Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Wed, 17 Jul 2024 13:27:23 +0100 Subject: [PATCH 3/5] Add tests --- .../permissionless/utils/encodeNonce.test.ts | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 packages/permissionless/utils/encodeNonce.test.ts diff --git a/packages/permissionless/utils/encodeNonce.test.ts b/packages/permissionless/utils/encodeNonce.test.ts new file mode 100644 index 00000000..93571042 --- /dev/null +++ b/packages/permissionless/utils/encodeNonce.test.ts @@ -0,0 +1,38 @@ +import { toHex } from "viem" +import { describe, expect, test } from "vitest" +import { encodeNonce } from "../utils" + +describe("encodeNonce", () => { + 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 }) + + expect(result).toBe((expectedKey << BigInt(64)) + expectedSequence) + }) + + 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 }) + + expect(result).toBe((expectedKey << BigInt(64)) + expectedSequence) + }) + + 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 }) + + expect(result).toBe((expectedKey << BigInt(64)) + expectedSequence) + }) +}) From f0536c3d05c0a88426c0726eceb8e33abb2ba97e Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Wed, 17 Jul 2024 13:29:12 +0100 Subject: [PATCH 4/5] use proper imports --- packages/permissionless/utils/encodeNonce.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/permissionless/utils/encodeNonce.test.ts b/packages/permissionless/utils/encodeNonce.test.ts index 93571042..f3214f2e 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 "../utils" describe("encodeNonce", () => { test("should encode key and sequence correctly", async () => { From be1c3dede45bdfbffe168735cbd48fd967684818 Mon Sep 17 00:00:00 2001 From: Garvit Khatri Date: Wed, 17 Jul 2024 13:31:16 +0100 Subject: [PATCH 5/5] Add changeset --- .changeset/strange-vans-sit.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strange-vans-sit.md diff --git a/.changeset/strange-vans-sit.md b/.changeset/strange-vans-sit.md new file mode 100644 index 00000000..483011fe --- /dev/null +++ b/.changeset/strange-vans-sit.md @@ -0,0 +1,5 @@ +--- +"permissionless": patch +--- + +Added encodeNonce util function