-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from pimlicolabs/encode-nonce
Add encodeNonce util
- Loading branch information
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"permissionless": patch | ||
--- | ||
|
||
Added encodeNonce util function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters