tokamak-l2js 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE-APACHE +201 -0
  2. package/LICENSE-MIT +21 -0
  3. package/NOTICE +2 -0
  4. package/README.md +31 -0
  5. package/dist/cjs/crypto/index.js +112 -0
  6. package/dist/cjs/crypto/index.js.map +1 -0
  7. package/dist/cjs/index.js +33 -0
  8. package/dist/cjs/index.js.map +1 -0
  9. package/dist/cjs/package.json +3 -0
  10. package/dist/cjs/params/index.js +12 -0
  11. package/dist/cjs/params/index.js.map +1 -0
  12. package/dist/cjs/stateManager/TokamakL2StateManager.js +227 -0
  13. package/dist/cjs/stateManager/TokamakL2StateManager.js.map +1 -0
  14. package/dist/cjs/stateManager/constructors.js +26 -0
  15. package/dist/cjs/stateManager/constructors.js.map +1 -0
  16. package/dist/cjs/stateManager/index.js +22 -0
  17. package/dist/cjs/stateManager/index.js.map +1 -0
  18. package/dist/cjs/stateManager/types.js +3 -0
  19. package/dist/cjs/stateManager/types.js.map +1 -0
  20. package/dist/cjs/tx/TokamakL2Tx.js +148 -0
  21. package/dist/cjs/tx/TokamakL2Tx.js.map +1 -0
  22. package/dist/cjs/tx/constructors.js +55 -0
  23. package/dist/cjs/tx/constructors.js.map +1 -0
  24. package/dist/cjs/tx/index.js +21 -0
  25. package/dist/cjs/tx/index.js.map +1 -0
  26. package/dist/cjs/tx/types.js +3 -0
  27. package/dist/cjs/tx/types.js.map +1 -0
  28. package/dist/cjs/utils/utils.js +89 -0
  29. package/dist/cjs/utils/utils.js.map +1 -0
  30. package/dist/cjs/utils/web.js +35 -0
  31. package/dist/cjs/utils/web.js.map +1 -0
  32. package/dist/crypto/index.d.ts +11 -0
  33. package/dist/crypto/index.d.ts.map +1 -0
  34. package/dist/crypto/index.js +103 -0
  35. package/dist/crypto/index.js.map +1 -0
  36. package/dist/index.d.ts +7 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +7 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/params/index.d.ts +7 -0
  41. package/dist/params/index.d.ts.map +1 -0
  42. package/dist/params/index.js +9 -0
  43. package/dist/params/index.js.map +1 -0
  44. package/dist/stateManager/TokamakL2StateManager.d.ts +24 -0
  45. package/dist/stateManager/TokamakL2StateManager.d.ts.map +1 -0
  46. package/dist/stateManager/TokamakL2StateManager.js +223 -0
  47. package/dist/stateManager/TokamakL2StateManager.js.map +1 -0
  48. package/dist/stateManager/constructors.d.ts +5 -0
  49. package/dist/stateManager/constructors.d.ts.map +1 -0
  50. package/dist/stateManager/constructors.js +22 -0
  51. package/dist/stateManager/constructors.js.map +1 -0
  52. package/dist/stateManager/index.d.ts +4 -0
  53. package/dist/stateManager/index.d.ts.map +1 -0
  54. package/dist/stateManager/index.js +4 -0
  55. package/dist/stateManager/index.js.map +1 -0
  56. package/dist/stateManager/types.d.ts +27 -0
  57. package/dist/stateManager/types.d.ts.map +1 -0
  58. package/dist/stateManager/types.js +2 -0
  59. package/dist/stateManager/types.js.map +1 -0
  60. package/dist/tx/TokamakL2Tx.d.ts +23 -0
  61. package/dist/tx/TokamakL2Tx.d.ts.map +1 -0
  62. package/dist/tx/TokamakL2Tx.js +144 -0
  63. package/dist/tx/TokamakL2Tx.js.map +1 -0
  64. package/dist/tx/constructors.d.ts +18 -0
  65. package/dist/tx/constructors.d.ts.map +1 -0
  66. package/dist/tx/constructors.js +50 -0
  67. package/dist/tx/constructors.js.map +1 -0
  68. package/dist/tx/index.d.ts +4 -0
  69. package/dist/tx/index.d.ts.map +1 -0
  70. package/dist/tx/index.js +3 -0
  71. package/dist/tx/index.js.map +1 -0
  72. package/dist/tx/types.d.ts +32 -0
  73. package/dist/tx/types.d.ts.map +1 -0
  74. package/dist/tx/types.js +2 -0
  75. package/dist/tx/types.js.map +1 -0
  76. package/dist/utils/utils.d.ts +6 -0
  77. package/dist/utils/utils.d.ts.map +1 -0
  78. package/dist/utils/utils.js +84 -0
  79. package/dist/utils/utils.js.map +1 -0
  80. package/dist/utils/web.d.ts +13 -0
  81. package/dist/utils/web.d.ts.map +1 -0
  82. package/dist/utils/web.js +28 -0
  83. package/dist/utils/web.js.map +1 -0
  84. package/package.json +65 -0
@@ -0,0 +1,4 @@
1
+ export * from './constructors.js';
2
+ export * from './types.js';
3
+ export { TokamakL2StateManager } from './TokamakL2StateManager.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/stateManager/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}
@@ -0,0 +1,4 @@
1
+ export * from './constructors.js';
2
+ export * from './types.js';
3
+ export { TokamakL2StateManager } from './TokamakL2StateManager.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/stateManager/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}
@@ -0,0 +1,27 @@
1
+ import { Common } from "@ethereumjs/common";
2
+ import { Address } from "@ethereumjs/util";
3
+ export type TokamakL2StateManagerOpts = {
4
+ common: Common;
5
+ contractAddress: Address;
6
+ blockNumber?: number;
7
+ initStorageKeys?: {
8
+ L1: Uint8Array;
9
+ L2: Uint8Array;
10
+ }[];
11
+ contractCode?: string;
12
+ };
13
+ export type StateSnapshot = {
14
+ stateRoot: string;
15
+ registeredKeys: string[];
16
+ storageEntries: Array<{
17
+ key: string;
18
+ value: string;
19
+ }>;
20
+ contractAddress: string;
21
+ preAllocatedLeaves: Array<{
22
+ key: string;
23
+ value: string;
24
+ }>;
25
+ channelId: number;
26
+ };
27
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/stateManager/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,MAAM,MAAM,yBAAyB,GAAG;IACpC,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,OAAO,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE;QACd,EAAE,EAAE,UAAU,CAAC;QACf,EAAE,EAAE,UAAU,CAAC;KAClB,EAAE,CAAC;IACJ,YAAY,CAAC,EAAE,MAAM,CAAC;CACzB,CAAA;AAGD,MAAM,MAAM,aAAa,GAAG;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,cAAc,EAAE,KAAK,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACtD,eAAe,EAAE,MAAM,CAAC;IACxB,kBAAkB,EAAE,KAAK,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC1D,SAAS,EAAE,MAAM,CAAC;CACnB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/stateManager/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,23 @@
1
+ import { Address } from "@ethereumjs/util";
2
+ import { LegacyTx, TransactionInterface, TransactionType, TxValuesArray as AllTypesTxValuesArray } from '@ethereumjs/tx';
3
+ import { EdwardsPoint } from "@noble/curves/abstract/edwards.js";
4
+ export type TxValuesArray = AllTypesTxValuesArray[typeof TransactionType.Legacy];
5
+ export declare class TokamakL2Tx extends LegacyTx implements TransactionInterface<typeof TransactionType.Legacy> {
6
+ readonly to: Address;
7
+ initUnsafeSenderPubKey(key: Uint8Array): void;
8
+ get senderPubKeyUnsafe(): Uint8Array;
9
+ getFunctionSelector(): Uint8Array;
10
+ getFunctionInput(index: number): Uint8Array;
11
+ getUnsafeEddsaRandomizer(): EdwardsPoint | undefined;
12
+ getUnsafeEddsaPubKey(): EdwardsPoint;
13
+ isSigned(): boolean;
14
+ getMessageToSign(): Uint8Array[];
15
+ getSenderPublicKey(): Uint8Array;
16
+ addSignature(v: bigint, r: Uint8Array | bigint, s: Uint8Array | bigint, convertV?: boolean): TokamakL2Tx;
17
+ raw(): TxValuesArray;
18
+ serialize(): Uint8Array;
19
+ verifySignature(): boolean;
20
+ getSenderAddress(): Address;
21
+ sign(privateKey: Uint8Array): TokamakL2Tx;
22
+ }
23
+ //# sourceMappingURL=TokamakL2Tx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TokamakL2Tx.d.ts","sourceRoot":"","sources":["../../src/tx/TokamakL2Tx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAwH,MAAM,kBAAkB,CAAA;AAChK,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,eAAe,EAAE,aAAa,IAAI,qBAAqB,EAAkB,MAAM,gBAAgB,CAAA;AAGxI,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAA;AAOhE,MAAM,MAAM,aAAa,GAAG,qBAAqB,CAAC,OAAO,eAAe,CAAC,MAAM,CAAC,CAAA;AAEhF,qBAAa,WAAY,SAAQ,QAAS,YAAW,oBAAoB,CAAC,OAAO,eAAe,CAAC,MAAM,CAAC;IACpG,SAAiB,EAAE,EAAE,OAAO,CAAA;IAK5B,sBAAsB,CAAC,GAAG,EAAE,UAAU,GAAG,IAAI;IAM7C,IAAI,kBAAkB,IAAI,UAAU,CAInC;IAED,mBAAmB,IAAI,UAAU;IAOjC,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU;IAU3C,wBAAwB,IAAI,YAAY,GAAG,SAAS;IAIpD,oBAAoB,IAAI,YAAY;IAI3B,QAAQ,IAAI,OAAO;IAQnB,gBAAgB,IAAI,UAAU,EAAE;IAYhC,kBAAkB,IAAI,UAAU;IAgBhC,YAAY,CACjB,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,UAAU,GAAG,MAAM,EACtB,CAAC,EAAE,UAAU,GAAG,MAAM,EAMtB,QAAQ,GAAE,OAAe,GAC1B,WAAW;IAuBL,GAAG,IAAI,aAAa;IAYpB,SAAS,IAAI,UAAU;IAIvB,eAAe,IAAI,OAAO;IAU1B,gBAAgB,IAAI,OAAO;IAM3B,IAAI,CAAC,UAAU,EAAE,UAAU,GAAG,WAAW;CAsBrD"}
@@ -0,0 +1,144 @@
1
+ import { bigIntToBytes, bytesToBigInt, setLengthLeft, bigIntToUnpaddedBytes, unpadBytes, concatBytes, equalsBytes } from "@ethereumjs/util";
2
+ import { LegacyTx } from '@ethereumjs/tx';
3
+ import { RLP } from "@ethereumjs/rlp";
4
+ import { jubjub } from "@noble/curves/misc.js";
5
+ import { eddsaSign, eddsaVerify, getEddsaPublicKey } from "../crypto/index.js";
6
+ import { fromEdwardsToAddress } from "../utils/utils.js";
7
+ import { createTokamakL2Tx } from "./constructors.js";
8
+ // LegacyTx prohibits to add new members for extension. Bypassing this problem by the follow:
9
+ const _unsafeSenderPubKeyStorage = new WeakMap();
10
+ export class TokamakL2Tx extends LegacyTx {
11
+ // v: public key in bytes form
12
+ // r: randomizer in bytes form
13
+ // s: The EDDSA signature (in JUBJUB scalar field)
14
+ initUnsafeSenderPubKey(key) {
15
+ if (_unsafeSenderPubKeyStorage.has(this)) {
16
+ throw new Error('Overwriting the sender public key (unsafe) is not allowed');
17
+ }
18
+ _unsafeSenderPubKeyStorage.set(this, key);
19
+ }
20
+ get senderPubKeyUnsafe() {
21
+ const v = _unsafeSenderPubKeyStorage.get(this);
22
+ if (!v)
23
+ throw new Error('The sender public key (unsafe) is not initialized');
24
+ return v;
25
+ }
26
+ getFunctionSelector() {
27
+ if (this.data.length < 4) {
28
+ throw new Error('Insufficient transaction data');
29
+ }
30
+ return this.data.slice(0, 4);
31
+ }
32
+ getFunctionInput(index) {
33
+ const offset = 4 + 32 * index;
34
+ const endExclusiveIndex = offset + 32;
35
+ if (this.data.length < endExclusiveIndex) {
36
+ // throw new Error('Insufficient transaction data')
37
+ return new Uint8Array();
38
+ }
39
+ return this.data.slice(offset, endExclusiveIndex);
40
+ }
41
+ getUnsafeEddsaRandomizer() {
42
+ return this.r === undefined ? undefined : jubjub.Point.fromBytes(bigIntToBytes(this.r));
43
+ }
44
+ getUnsafeEddsaPubKey() {
45
+ return jubjub.Point.fromBytes(this.senderPubKeyUnsafe);
46
+ }
47
+ isSigned() {
48
+ const { v, r, s } = this;
49
+ if (v === undefined || r === undefined || s === undefined) {
50
+ return false;
51
+ }
52
+ return true;
53
+ }
54
+ getMessageToSign() {
55
+ const messageRaw = [
56
+ bigIntToUnpaddedBytes(this.nonce),
57
+ this.to.bytes,
58
+ this.getFunctionSelector(),
59
+ ];
60
+ for (let inputIndex = 0; inputIndex < 9; inputIndex++) {
61
+ messageRaw.push(this.getFunctionInput(inputIndex));
62
+ }
63
+ return messageRaw.map(m => setLengthLeft(m, 32));
64
+ }
65
+ getSenderPublicKey() {
66
+ if (!this.isSigned()) {
67
+ throw new Error('Public key can be recovered only from a signed transaction');
68
+ }
69
+ const recovered = getEddsaPublicKey(concatBytes(...this.getMessageToSign(), setLengthLeft(this.senderPubKeyUnsafe, 32)), this.v, bigIntToBytes(this.r), bigIntToBytes(this.s));
70
+ if (!equalsBytes(this.senderPubKeyUnsafe, recovered)) {
71
+ throw new Error('Recovered sender public key is different from the initialized one');
72
+ }
73
+ return recovered;
74
+ }
75
+ addSignature(v, r, s,
76
+ // convertV is `true` when called from `sign`
77
+ // This is used to convert the `v` output from `ecsign` (0 or 1) to the values used for legacy txs:
78
+ // 27 or 28 for non-EIP-155 protected txs
79
+ // 35 or 36 + chainId * 2 for EIP-155 protected txs
80
+ // See: https://eips.ethereum.org/EIPS/eip-155
81
+ convertV = false) {
82
+ if (convertV) {
83
+ throw new Error('convertV should be false');
84
+ }
85
+ const rBigint = typeof r === 'bigint' ? r : bytesToBigInt(r);
86
+ const sBigint = typeof s === 'bigint' ? s : bytesToBigInt(s);
87
+ const opts = { ...this.txOptions, common: this.common };
88
+ return createTokamakL2Tx({
89
+ nonce: this.nonce,
90
+ to: this.to,
91
+ data: this.data,
92
+ senderPubKey: this.senderPubKeyUnsafe,
93
+ v: 27n,
94
+ r: rBigint,
95
+ s: sBigint,
96
+ }, opts);
97
+ }
98
+ raw() {
99
+ return [
100
+ bigIntToUnpaddedBytes(this.nonce),
101
+ this.to.bytes,
102
+ this.data,
103
+ this.getSenderPublicKey(),
104
+ this.v !== undefined ? bigIntToUnpaddedBytes(this.v) : new Uint8Array(0),
105
+ this.r !== undefined ? bigIntToUnpaddedBytes(this.r) : new Uint8Array(0),
106
+ this.s !== undefined ? bigIntToUnpaddedBytes(this.s) : new Uint8Array(0),
107
+ ];
108
+ }
109
+ serialize() {
110
+ return RLP.encode(this.raw());
111
+ }
112
+ verifySignature() {
113
+ try {
114
+ // Main signature verification is done in `getSenderPublicKey()`
115
+ const publicKey = this.getSenderPublicKey();
116
+ return unpadBytes(publicKey).length !== 0;
117
+ }
118
+ catch {
119
+ return false;
120
+ }
121
+ }
122
+ getSenderAddress() {
123
+ const pubKeyByte = this.getSenderPublicKey();
124
+ const recovered = fromEdwardsToAddress(pubKeyByte);
125
+ return recovered;
126
+ }
127
+ sign(privateKey) {
128
+ const sk = bytesToBigInt(privateKey);
129
+ if (sk < 0n || sk >= jubjub.Point.Fn.ORDER) {
130
+ throw new Error('EDDSA private key must be in JubJub scalar field');
131
+ }
132
+ const msg = this.getMessageToSign();
133
+ const sig = eddsaSign(sk, msg);
134
+ const publicKey = jubjub.Point.BASE.multiply(sk);
135
+ if (!publicKey.equals(jubjub.Point.fromBytes(this.senderPubKeyUnsafe))) {
136
+ throw new Error("The public key initialized is not derived from the input private key");
137
+ }
138
+ if (!eddsaVerify(msg, publicKey, sig.R, sig.S)) {
139
+ throw new Error('Tried to sign but verification failure');
140
+ }
141
+ return this.addSignature(27n, bytesToBigInt(sig.R.toBytes()), sig.S);
142
+ }
143
+ }
144
+ //# sourceMappingURL=TokamakL2Tx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TokamakL2Tx.js","sourceRoot":"","sources":["../../src/tx/TokamakL2Tx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,aAAa,EAAE,aAAa,EAAE,aAAa,EAAE,qBAAqB,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,kBAAkB,CAAA;AAChK,OAAO,EAAE,QAAQ,EAAiG,MAAM,gBAAgB,CAAA;AACxI,OAAO,EAA8B,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAE9C,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,iBAAiB,EAAY,MAAM,oBAAoB,CAAA;AACxF,OAAO,EAA4B,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAClF,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAErD,6FAA6F;AAC7F,MAAM,0BAA0B,GAAG,IAAI,OAAO,EAA2B,CAAC;AAG1E,MAAM,OAAO,WAAY,SAAQ,QAAQ;IAErC,8BAA8B;IAC9B,8BAA8B;IAC9B,kDAAkD;IAElD,sBAAsB,CAAC,GAAe;QAClC,IAAI,0BAA0B,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;QAC7E,CAAC;QACD,0BAA0B,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC9C,CAAC;IACD,IA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CAAC;YACrE,MAAM,IAAI,KAAK,CAAC,sEAAsE,CAAC,CAAA;QAC3F,CAAC;QACD,IAAI,CAAC,WAAW,CAAC,GAAG,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7C,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAA;QAC7D,CAAC;QAED,OAAO,IAAI,CAAC,YAAY,CACpB,GAAG,EACH,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,EAC9B,GAAG,CAAC,CAAC,CACR,CAAA;IACL,CAAC;CACJ"}
@@ -0,0 +1,18 @@
1
+ import { TxOptions } from "@ethereumjs/tx";
2
+ import { TokamakL2Tx } from "./TokamakL2Tx.js";
3
+ import { TokamakL2TxData } from "./types.js";
4
+ export declare function createTokamakL2Tx(txData: TokamakL2TxData, opts: TxOptions): TokamakL2Tx;
5
+ /**
6
+ * Create a transaction from an array of byte encoded values ordered according to the devp2p network encoding - format noted below.
7
+ *
8
+ * Format: `[nonce, gasPrice, gasLimit, to, value, data, v, r, s]`
9
+ */
10
+ export declare function createTokamakL2TxFromBytesArray(values: Uint8Array[], opts: TxOptions): TokamakL2Tx;
11
+ /**
12
+ * Instantiate a transaction from a RLP serialized tx.
13
+ *
14
+ * Format: `rlp([nonce, gasPrice, gasLimit, to, value, data,
15
+ * signatureV, signatureR, signatureS])`
16
+ */
17
+ export declare function createTokamakL2TxFromRLP(serialized: Uint8Array, opts: TxOptions): TokamakL2Tx;
18
+ //# sourceMappingURL=constructors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constructors.d.ts","sourceRoot":"","sources":["../../src/tx/constructors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAiB,MAAM,gBAAgB,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAM5C,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,SAAS,GAAG,WAAW,CAQvF;AAED;;;;GAIG;AACH,wBAAgB,+BAA+B,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,IAAI,EAAE,SAAS,GAAG,WAAW,CAuBlG;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,GAAG,WAAW,CAQ7F"}
@@ -0,0 +1,50 @@
1
+ import { TokamakL2Tx } from "./TokamakL2Tx.js";
2
+ import { EthereumJSErrorWithoutCode, RLP } from "@ethereumjs/rlp";
3
+ import { Address, bytesToBigInt, toBytes, validateNoLeadingZeroes } from "@ethereumjs/util";
4
+ import { ANY_LARGE_GAS_LIMIT, ANY_LARGE_GAS_PRICE } from "../params/index.js";
5
+ export function createTokamakL2Tx(txData, opts) {
6
+ if (opts.common?.customCrypto === undefined) {
7
+ throw new Error("Required 'common.customCrypto'");
8
+ }
9
+ // Set the minimum gasLimit to execute VM._runTx
10
+ const tx = new TokamakL2Tx({ ...txData, gasLimit: ANY_LARGE_GAS_LIMIT, gasPrice: ANY_LARGE_GAS_PRICE }, opts);
11
+ tx.initUnsafeSenderPubKey(toBytes(txData.senderPubKey));
12
+ return tx;
13
+ }
14
+ /**
15
+ * Create a transaction from an array of byte encoded values ordered according to the devp2p network encoding - format noted below.
16
+ *
17
+ * Format: `[nonce, gasPrice, gasLimit, to, value, data, v, r, s]`
18
+ */
19
+ export function createTokamakL2TxFromBytesArray(values, opts) {
20
+ if (values.length !== 7) {
21
+ throw EthereumJSErrorWithoutCode('Invalid transaction. Only expecting 7 values for signed tx.');
22
+ }
23
+ const [nonce, to, data, senderPubKey, v, r, s] = values;
24
+ const txDataRaw = { nonce, to, data, senderPubKey, v, r, s };
25
+ validateNoLeadingZeroes(txDataRaw);
26
+ const txDataFormat = {
27
+ nonce: bytesToBigInt(nonce),
28
+ to: new Address(to),
29
+ data,
30
+ senderPubKey,
31
+ v: bytesToBigInt(v),
32
+ r: bytesToBigInt(r),
33
+ s: bytesToBigInt(s),
34
+ };
35
+ return createTokamakL2Tx(txDataFormat, opts);
36
+ }
37
+ /**
38
+ * Instantiate a transaction from a RLP serialized tx.
39
+ *
40
+ * Format: `rlp([nonce, gasPrice, gasLimit, to, value, data,
41
+ * signatureV, signatureR, signatureS])`
42
+ */
43
+ export function createTokamakL2TxFromRLP(serialized, opts) {
44
+ const values = RLP.decode(serialized);
45
+ if (!Array.isArray(values)) {
46
+ throw EthereumJSErrorWithoutCode('Invalid serialized tx input. Must be array');
47
+ }
48
+ return createTokamakL2TxFromBytesArray(values, opts);
49
+ }
50
+ //# sourceMappingURL=constructors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constructors.js","sourceRoot":"","sources":["../../src/tx/constructors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAE9C,OAAO,EAAE,0BAA0B,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AACjE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAc,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AACvG,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AAG7E,MAAM,UAAU,iBAAiB,CAAC,MAAuB,EAAE,IAAe;IACtE,IAAI,IAAI,CAAC,MAAM,EAAE,YAAY,KAAK,SAAS,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;IACrD,CAAC;IACD,gDAAgD;IAChD,MAAM,EAAE,GAAI,IAAI,WAAW,CAAC,EAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,mBAAmB,EAAE,QAAQ,EAAE,mBAAmB,EAAC,EAAE,IAAI,CAAC,CAAA;IAC5G,EAAE,CAAC,sBAAsB,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,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'./TokamakL2Tx.js';
2
+ export * from './constructors.js';
3
+ export { type TokamakL2TxData } from './types.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tx/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,cAAc,mBAAmB,CAAA;AACjC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,YAAY,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { TokamakL2Tx } from './TokamakL2Tx.js';
2
+ export * from './constructors.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tx/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,cAAc,mBAAmB,CAAA"}
@@ -0,0 +1,32 @@
1
+ import { Address } from "@ethereumjs/util";
2
+ /**
3
+ * Legacy {@link Transaction} Data
4
+ */
5
+ export type TokamakL2TxData = {
6
+ /**
7
+ * The transaction's nonce.
8
+ */
9
+ nonce: bigint;
10
+ /**
11
+ * The transaction's the address is sent to.
12
+ */
13
+ to: Address;
14
+ /**
15
+ * This will contain the data of the message or the init of a contract.
16
+ */
17
+ data: Uint8Array;
18
+ senderPubKey: Uint8Array;
19
+ /**
20
+ * Fixed for TokamakL2JS
21
+ */
22
+ v?: bigint;
23
+ /**
24
+ * EDDSA randomizer.
25
+ */
26
+ r?: bigint;
27
+ /**
28
+ * EDDSA signature.
29
+ */
30
+ s?: bigint;
31
+ };
32
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/tx/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAE1C;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B;;OAEG;IACH,KAAK,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,EAAE,EAAE,OAAO,CAAA;IAEX;;OAEG;IACH,IAAI,EAAE,UAAU,CAAA;IAEhB,YAAY,EAAE,UAAU,CAAA;IAExB;;OAEG;IACH,CAAC,CAAC,EAAE,MAAM,CAAA;IAEV;;OAEG;IACH,CAAC,CAAC,EAAE,MAAM,CAAA;IAEV;;OAEG;IACH,CAAC,CAAC,EAAE,MAAM,CAAA;CACX,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/tx/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ import { Address } from "@ethereumjs/util";
2
+ import { EdwardsPoint } from "@noble/curves/abstract/edwards.js";
3
+ export declare function batchBigIntTo32BytesEach(...inVals: bigint[]): Uint8Array;
4
+ export declare function fromEdwardsToAddress(point: EdwardsPoint | Uint8Array): Address;
5
+ export declare function getUserStorageKey(parts: Array<Address | number | bigint | string>, layer: 'L1' | 'TokamakL2'): Uint8Array;
6
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAiG,MAAM,kBAAkB,CAAA;AACvJ,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAA;AAOhE,wBAAgB,wBAAwB,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAExE;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,GAAG,OAAO,CAwB9E;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,WAAW,GAAG,UAAU,CAoCzH"}
@@ -0,0 +1,84 @@
1
+ import { addHexPrefix, Address, bigIntToBytes, concatBytes, hexToBytes, setLengthLeft } from "@ethereumjs/util";
2
+ import { jubjub } from "@noble/curves/misc.js";
3
+ import { poseidon } from "../crypto/index.js";
4
+ import { keccak256 } from "ethereum-cryptography/keccak";
5
+ export function batchBigIntTo32BytesEach(...inVals) {
6
+ return concatBytes(...inVals.map(x => setLengthLeft(bigIntToBytes(x), 32)));
7
+ }
8
+ export function fromEdwardsToAddress(point) {
9
+ const edwardsToAffineBytes = (point) => {
10
+ const affine = point.toAffine();
11
+ return batchBigIntTo32BytesEach(affine.x, affine.y);
12
+ };
13
+ let pointBytes;
14
+ if (point instanceof Uint8Array) {
15
+ if (point.length === 32) {
16
+ // compressed point
17
+ pointBytes = edwardsToAffineBytes(jubjub.Point.fromBytes(point));
18
+ }
19
+ else if (point.length === 64) {
20
+ // Uncompressed Affine coordinates
21
+ pointBytes = point;
22
+ }
23
+ else {
24
+ throw new Error('Invalid EdwardsPoint format');
25
+ }
26
+ }
27
+ else {
28
+ pointBytes = edwardsToAffineBytes(point);
29
+ }
30
+ const addressByte = poseidon(pointBytes).subarray(-20);
31
+ return new Address(addressByte);
32
+ }
33
+ export function getUserStorageKey(parts, layer) {
34
+ const bytesArray = [];
35
+ for (const p of parts) {
36
+ let b;
37
+ if (p instanceof Address) {
38
+ b = p.toBytes();
39
+ }
40
+ else if (typeof p === 'number') {
41
+ b = bigIntToBytes(BigInt(p));
42
+ }
43
+ else if (typeof p === 'bigint') {
44
+ b = bigIntToBytes(p);
45
+ }
46
+ else if (typeof p === 'string') {
47
+ b = hexToBytes(addHexPrefix(p));
48
+ }
49
+ else {
50
+ throw new Error('getStorageKey accepts only Address | number | bigint | string');
51
+ }
52
+ bytesArray.push(setLengthLeft(b, 32));
53
+ }
54
+ const packed = concatBytes(...bytesArray);
55
+ let hash;
56
+ switch (layer) {
57
+ case 'L1':
58
+ {
59
+ hash = keccak256;
60
+ }
61
+ break;
62
+ case 'TokamakL2':
63
+ {
64
+ hash = poseidon;
65
+ }
66
+ break;
67
+ default: {
68
+ throw new Error(`Error while making a user's storage key: Undefined layer "${layer}"`);
69
+ }
70
+ }
71
+ return hash(packed);
72
+ }
73
+ // export function compressJubJubPoint(point: EdwardsPoint): bigint {
74
+ // return point.X * jubjub.Point.Fp.ORDER + point.Y
75
+ // }
76
+ // export function recoverJubJubPoint(raw: bigint): EdwardsPoint {
77
+ // const Y = raw % jubjub.Point.Fp.ORDER
78
+ // const X = (raw - Y) / jubjub.Point.Fp.ORDER
79
+ // if (X >= jubjub.Point.Fp.ORDER) {
80
+ // throw new Error('Invalid format for a JubJub point')
81
+ // }
82
+ // return jubjub.Point.fromAffine({x: X, y: Y})
83
+ // }
84
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,aAAa,EAA6B,WAAW,EAAe,UAAU,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAEvJ,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAIxD,MAAM,UAAU,wBAAwB,CAAC,GAAG,MAAgB;IACxD,OAAO,WAAW,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAA;AAC/E,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,KAAgC;IACjE,MAAM,oBAAoB,GAAG,CAAC,KAAmB,EAAc,EAAE;QAC7D,MAAM,MAAM,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAA;QAC/B,OA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C,CAAC,CAAA;QACpB,CAAC;aAAM,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YACnC,CAAC,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAA;QAC/B,CAAC;aAAM,CAAC;YACR,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;QACjF,CAAC;QAED,UAAU,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;IACzC,CAAC;IACD,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,UAAU,CAAC,CAAA;IACzC,IAAI,IAAI,CAAA;IACR,QAAQ,KAAK,EAAE,CAAC;QACZ,KAAK,IAAI;YAAE,CAAC;gBACR,IAAI,GAAG,SAAS,CAAC;YACrB,CAAC;YACD,MAAM;QACN,KAAK,WAAW;YAAE,CAAC;gBACf,IAAI,GAAG,QAAQ,CAAC;YACpB,CAAC;YACD,MAAM;QACN,OAAO,CAAC,CAAC,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,6DAA6D,KAAK,GAAG,CAAC,CAAA;QAC1F,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAA;AACvB,CAAC;AAED,qEAAqE;AACrE,uDAAuD;AACvD,IAAI;AAEJ,kEAAkE;AAClE,4CAA4C;AAC5C,kDAAkD;AAClD,wCAAwC;AACxC,+DAA+D;AAC/D,QAAQ;AACR,mDAAmD;AACnD,IAAI"}
@@ -0,0 +1,13 @@
1
+ export declare const L2_PRV_KEY_MESSAGE = "Tokamak-Private-App-Channel-";
2
+ type L2KeyPair = {
3
+ privateKey: Uint8Array;
4
+ publicKey: Uint8Array;
5
+ };
6
+ export declare const deriveSignatureFromMetaMask: (signMessageAsync: (args: {
7
+ message: string;
8
+ }) => Promise<`0x${string}`>, channelId: `0x${string}`) => Promise<`0x${string}`>;
9
+ export declare const deriveL2KeysFromSignature: (signature: `0x${string}`) => L2KeyPair;
10
+ export declare const deriveL2AddressFromKeys: (keys: L2KeyPair) => `0x${string}`;
11
+ export declare const deriveL2MptKeyFromAddress: (address: `0x${string}`, slotIndex: number) => `0x${string}`;
12
+ export {};
13
+ //# sourceMappingURL=web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../src/utils/web.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,kBAAkB,iCAA+B,CAAA;AAE9D,KAAK,SAAS,GAAG;IACf,UAAU,EAAE,UAAU,CAAA;IACtB,SAAS,EAAE,UAAU,CAAA;CACtB,CAAA;AAMD,eAAO,MAAM,2BAA2B,GAAU,kBAAkB,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC,KAAK,MAAM,EAAE,CAAC,EAAE,WAAW,KAAK,MAAM,EAAE,KAAG,OAAO,CAAC,KAAK,MAAM,EAAE,CAG1K,CAAC;AAEF,eAAO,MAAM,yBAAyB,GAAI,WAAW,KAAK,MAAM,EAAE,KAAI,SAKrE,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,MAAM,SAAS,KAAG,KAAK,MAAM,EAGpE,CAAC;AAEF,eAAO,MAAM,yBAAyB,GAAI,SAAS,KAAK,MAAM,EAAE,EAAE,WAAW,MAAM,KAAG,KAAK,MAAM,EAGhG,CAAC"}
@@ -0,0 +1,28 @@
1
+ import { utf8ToBytes } from "ethereum-cryptography/utils";
2
+ import { poseidon } from "../crypto/index.js";
3
+ import { jubjub } from "@noble/curves/misc.js";
4
+ import { bigIntToBytes, bytesToBigInt, bytesToHex, setLengthLeft } from "@ethereumjs/util";
5
+ import { fromEdwardsToAddress, getUserStorageKey } from "./utils.js";
6
+ export const L2_PRV_KEY_MESSAGE = 'Tokamak-Private-App-Channel-';
7
+ // ##Usage##
8
+ // import { useSignMessage } from 'wagmi';
9
+ // const { signMessageAsync } = useSignMessage();
10
+ export const deriveSignatureFromMetaMask = async (signMessageAsync, channelId) => {
11
+ const messageToSign = `Tokamak-Private-App-Channel-${channelId}`;
12
+ return await signMessageAsync({ message: messageToSign });
13
+ };
14
+ export const deriveL2KeysFromSignature = (signature) => {
15
+ const rngStringRaw = jubjub.utils.randomPrivateKey(poseidon(utf8ToBytes(signature)));
16
+ const privateKey = setLengthLeft(bigIntToBytes(bytesToBigInt(rngStringRaw) % jubjub.Point.Fn.ORDER), 32);
17
+ const publicKey = jubjub.Point.BASE.multiply(bytesToBigInt(privateKey)).toBytes();
18
+ return { privateKey, publicKey };
19
+ };
20
+ export const deriveL2AddressFromKeys = (keys) => {
21
+ const address = fromEdwardsToAddress(keys.publicKey);
22
+ return address.toString();
23
+ };
24
+ export const deriveL2MptKeyFromAddress = (address, slotIndex) => {
25
+ const mptKey = getUserStorageKey([address, slotIndex], 'TokamakL2');
26
+ return bytesToHex(mptKey);
27
+ };
28
+ //# sourceMappingURL=web.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/utils/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAC3F,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAErE,MAAM,CAAC,MAAM,kBAAkB,GAAC,8BAA8B,CAAA;AAO9D,YAAY;AACZ,0CAA0C;AAC1C,iDAAiD;AAEjD,MAAM,CAAC,MAAM,2BAA2B,GAAG,KAAK,EAAE,gBAAuE,EAAE,SAAwB,EAA0B,EAAE;IAC7K,MAAM,aAAa,GAAG,+BAA+B,SAAS,EAAE,CAAC;IACjE,OAAO,MAAM,gBAAgB,CAAC,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC,CAAA;AAC3D,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,SAAwB,EAAc,EAAE;IAChF,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,QAAQ,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACrF,MAAM,UAAU,GAAG,aAAa,CAAC,aAAa,CAAC,aAAa,CAAC,YAAY,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;IACzG,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IAClF,OAAO,EAAC,UAAU,EAAE,SAAS,EAAC,CAAC;AACjC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,IAAe,EAAiB,EAAE;IACxE,MAAM,OAAO,GAAG,oBAAoB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACrD,OAAO,OAAO,CAAC,QAAQ,EAAE,CAAA;AAC3B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,OAAsB,EAAE,SAAiB,EAAiB,EAAE;IACpG,MAAM,MAAM,GAAG,iBAAiB,CAAC,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,WAAW,CAAC,CAAC;IACpE,OAAO,UAAU,CAAC,MAAM,CAAC,CAAA;AAC3B,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "tokamak-l2js",
3
+ "version": "0.0.1",
4
+ "description": "TokamakL2JS - Tokamak Network L2 transaction and state utilities.",
5
+ "keywords": [
6
+ "tokamak",
7
+ "layer2",
8
+ "ethereum",
9
+ "tokamak-network"
10
+ ],
11
+ "homepage": "https://github.com/tokamak-network/TokamakL2JS#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/tokamak-network/TokamakL2JS/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/tokamak-network/TokamakL2JS.git"
18
+ },
19
+ "license": "MIT OR Apache-2.0",
20
+ "author": "Tokamak Network",
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "main": "dist/index.js",
24
+ "types": "dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "import": "./dist/index.js",
28
+ "require": "./dist/cjs/index.js",
29
+ "types": "./dist/index.d.ts"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "LICENSE-MIT",
35
+ "LICENSE-APACHE",
36
+ "NOTICE",
37
+ "README.md"
38
+ ],
39
+ "scripts": {
40
+ "build": "npm run build:esm && npm run build:cjs",
41
+ "build:esm": "tsc -p tsconfig.json",
42
+ "build:cjs": "tsc -p tsconfig.cjs.json && node ./scripts/write-cjs-package.cjs",
43
+ "clean": "rm -rf dist",
44
+ "prepublishOnly": "npm run build"
45
+ },
46
+ "dependencies": {
47
+ "@ethereumjs/common": "^10.0.0",
48
+ "@ethereumjs/rlp": "^10.0.0",
49
+ "@ethereumjs/statemanager": "^10.0.0",
50
+ "@ethereumjs/tx": "^10.0.0",
51
+ "@ethereumjs/util": "^10.0.0",
52
+ "@noble/curves": "^1.9.0",
53
+ "@zk-kit/imt": "2.0.0-beta.8",
54
+ "dotenv": "^17.2.0",
55
+ "ethereum-cryptography": "^3.0.0",
56
+ "ethers": "^6.14.3",
57
+ "poseidon-bls12381": "^1.0.2"
58
+ },
59
+ "devDependencies": {
60
+ "typescript": "^5.8.3"
61
+ },
62
+ "engines": {
63
+ "node": ">=18"
64
+ }
65
+ }