Skip to content

Commit

Permalink
Merge pull request #250 from pimlicolabs/encode-nonce
Browse files Browse the repository at this point in the history
Add encodeNonce util
  • Loading branch information
plusminushalf authored Jul 17, 2024
2 parents 0d96967 + be1c3de commit 6b5b366
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-vans-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"permissionless": patch
---

Added encodeNonce util function
38 changes: 38 additions & 0 deletions packages/permissionless/utils/encodeNonce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { encodeNonce } from "permissionless/utils"
import { toHex } from "viem"
import { describe, expect, test } from "vitest"

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)
})
})
8 changes: 8 additions & 0 deletions packages/permissionless/utils/encodeNonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
}
2 changes: 2 additions & 0 deletions packages/permissionless/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,6 +48,7 @@ export {
getAddressFromInitCodeOrPaymasterAndData,
getPackedUserOperation,
getEntryPointVersion,
encodeNonce,
ENTRYPOINT_ADDRESS_V06,
ENTRYPOINT_ADDRESS_V07
}

0 comments on commit 6b5b366

Please sign in to comment.