Skip to content

Commit 92ab90e

Browse files
authored
Pub (#7)
* update jest lib * adding increment and toHexNonce methods and tests, adding more readme * updating for distribution * updating package * clean types * adding needed lib for github actions build * removing explicit chain id
1 parent a988611 commit 92ab90e

7 files changed

+681
-36
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ This utility library exists to create and work with 2D nonces as discribed in th
44

55
This lib has been checked against what is currently deployed by Eth-Infinitism which is [Release v0.6](https://github.com/eth-infinitism/account-abstraction/releases/tag/v0.6.0) on ethereum at [0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789](https://optimistic.etherscan.io/address/0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789#code)
66

7-
To use the library, install it.
7+
To use the library, install it:
8+
9+
```
10+
yarn add @beamnetwork/nonce-2d
11+
```
812

913
The 2D nonce is devided into 2 parts. It is 256 bits long, with 192 bits reserved for a key and 64 reserved for numerical sequence that increments for each tx.
1014

package.json

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{
2-
"name": "nonce-2d",
3-
"version": "0.0.1",
2+
"name": "@beamnetwork/nonce-2d",
3+
"version": "0.0.5",
44
"description": "",
5-
"main": "index.js",
6-
"types": "index.d.ts",
5+
"main": "./dist/index.js",
6+
"module": "./dist/index.mjs",
7+
"types": "./dist/index.d.ts",
8+
"files": [
9+
"dist",
10+
"package.json",
11+
"README.md"
12+
],
713
"repository": {
814
"type": "git",
915
"url": "git+https://github.com/eco/nonce-2d.git"
@@ -31,12 +37,18 @@
3137
"prettier:fix": "prettier . --write",
3238
"format": "yarn lint:fix && yarn prettier:fix",
3339
"format:check": "yarn lint && yarn prettier",
34-
"build": "npx tsc",
35-
"clean": "rm -rf ./dist",
36-
"buildLib": "build",
37-
"publish": "npm publish --access public"
40+
"build": "tsup",
41+
"clean": "rm -rf ./dist"
42+
},
43+
"exports": {
44+
".": {
45+
"require": "./dist/index.js",
46+
"import": "./dist/index.mjs",
47+
"types": "./dist/index.d.ts"
48+
}
3849
},
3950
"devDependencies": {
51+
"@swc/core": "^1.3.105",
4052
"@types/jest": "^29.5.11",
4153
"@typescript-eslint/eslint-plugin": "^6.14.0",
4254
"@typescript-eslint/parser": "^6.14.0",
@@ -56,6 +68,7 @@
5668
"prettier": "^3.1.1",
5769
"ts-jest": "^29.1.1",
5870
"ts-node": "^10.9.2",
71+
"tsup": "^8.0.1",
5972
"typescript": "^5.3.3"
6073
},
6174
"dependencies": {},

index.ts src/index.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ export const NONCE_2D_KEY_CHAIN_BIT_LENGTH = 32 // 4*8
1616
// Radix for hex
1717
const HEX_RADIS = 16
1818

19-
export enum ChainID {
20-
EthereumMainnet = 1,
21-
EthereumGoerli = 5,
22-
Optimism = 10,
23-
OptimismGoerli = 420,
24-
}
2519
/**
2620
* The structure of the Nonce2D, [key, seq] == [192|64] == 256 bits
2721
* @param key The key as a hex string
@@ -81,10 +75,10 @@ export class Nonce2D {
8175
* Generates a sequence number for a given hex key and sequence number
8276
*
8377
* @param hexKey the 192 bit hex key containing the chain and address
84-
* @param seq the 64 bit sequence number or 1 if not provided
78+
* @param seq the 64 bit sequence number or defaults to 0 if not provided
8579
* @returns
8680
*/
87-
static fromHexKey(hexKey: string, seq: number = 1): Nonce2D {
81+
static fromHexKey(hexKey: string, seq: number = 0): Nonce2D {
8882
const hexNonce = hexKey + hexPad(seq.toString(HEX_RADIS), 16)
8983
return new Nonce2D(hexNonce)
9084
}
@@ -94,12 +88,12 @@ export class Nonce2D {
9488
* to look up the full nonce on-chain for the wallet.
9589
*
9690
* @param ethAddress the destination address in the key(not the address of the 4337 wallet)
97-
* @param chain the destination chain in the key
91+
* @param chainID the destination chain id in the key
9892
* @returns the 192 bit hex key for the destination address-chain pair
9993
*/
100-
static getHexKeyForDestination(ethAddress: string, chain: ChainID): string {
94+
static getHexKeyForDestination(ethAddress: string, chainID: number): string {
10195
ethAddress = stripOx(ethAddress)
102-
return ('0x' + chain.toString(HEX_RADIS) + ethAddress).toLocaleLowerCase()
96+
return ('0x' + chainID.toString(HEX_RADIS) + ethAddress).toLocaleLowerCase()
10397
}
10498

10599
/**

test/index.spec.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { ChainID, Nonce2D } from '../index'
1+
import { Nonce2D } from '../src/index'
2+
3+
enum ChainID {
4+
EthereumMainnet = 1,
5+
EthereumGoerli = 5,
6+
Optimism = 10,
7+
OptimismGoerli = 420,
8+
}
9+
210
// Test chain id
311
const testChain = ChainID.EthereumGoerli
412
// 256 bit nonce = 32 bytes
@@ -31,7 +39,7 @@ describe('Nonce2D tests', () => {
3139

3240
describe('fromHexKey tests', () => {
3341
it('should create a Nonce2D object from a valid key', () => {
34-
const n = Nonce2D.fromHexKey(testsKey)
42+
const n = Nonce2D.fromHexKey(testsKey, 1)
3543
expect(Number(n.chain)).toBe(testChain)
3644
expect(n.address).toBe(testsAddress)
3745
expect(n.seq).toBe(testsSeq)

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"strict": true
88
},
99
"types": ["jest"],
10-
"include": ["index.ts"],
10+
"include": ["src/index.ts"],
1111
"exclude": ["node_modules", "dist"]
1212
}

tsup.config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from 'tsup'
2+
3+
export default defineConfig({
4+
entry: ['src/index.ts'],
5+
format: ['cjs', 'esm'], // Build for commonJS and ESmodules
6+
dts: true, // Generate declaration file (.d.ts)
7+
splitting: false,
8+
sourcemap: true,
9+
clean: true,
10+
})

0 commit comments

Comments
 (0)