Skip to content

Commit

Permalink
Hex check (#8)
Browse files Browse the repository at this point in the history
* update jest lib

* adding increment and toHexNonce methods and tests, adding more readme

* adding 0x check
  • Loading branch information
StoyanD authored Feb 12, 2024
1 parent 92ab90e commit 74e6b9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beamnetwork/nonce-2d",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class Nonce2D {
private _address: string

private constructor(hexNonce2D: string) {
hexNonce2D = include0x(hexNonce2D)
const { key, chain, address } = this.getKeyHex(BigInt(hexNonce2D))
this._key = key
this._chain = chain
Expand Down Expand Up @@ -192,6 +193,18 @@ function stripOx(hex: string): string {
return hex
}

/**
* helper function to include the 0x prefix in a hex string
* @param hex the hex string to include the 0x prefix
* @returns
*/
function include0x(hex: string): string {
if (!hex.startsWith('0x')) {
return '0x' + hex
}
return hex
}

/**
* Helper function to padd a string with leading 0s
* @param hexInput string to pad
Expand Down
6 changes: 6 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ describe('Nonce2D tests', () => {
expect(n.toHexNonce()).toBe(testsHexNonce)
})

it('should support lack of 0x infrom of string', () => {
const missing = testsHexNonce.substring(2)
const n = Nonce2D.fromHexNonce(missing)
expect(n.toHexNonce()).toBe(testsHexNonce)
})

it('should be able to increment and reconstitute a new hex nonce', () => {
// construct new nonce by incrementing the sequence number
const nextNonce =
Expand Down

0 comments on commit 74e6b9c

Please sign in to comment.