@1money/protocol-ts-sdk 1.0.14

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 (65) hide show
  1. package/.claude/settings.local.json +14 -0
  2. package/CLAUDE.md +77 -0
  3. package/README.md +600 -0
  4. package/es/api/accounts/index.d.ts +20 -0
  5. package/es/api/accounts/types.d.ts +9 -0
  6. package/es/api/chain/index.d.ts +12 -0
  7. package/es/api/chain/types.d.ts +3 -0
  8. package/es/api/checkpoints/index.d.ts +26 -0
  9. package/es/api/checkpoints/types.d.ts +33 -0
  10. package/es/api/constants.d.ts +9 -0
  11. package/es/api/index.d.ts +31 -0
  12. package/es/api/index.js +593 -0
  13. package/es/api/state/index.d.ts +12 -0
  14. package/es/api/state/types.d.ts +7 -0
  15. package/es/api/tokens/index.d.ts +62 -0
  16. package/es/api/tokens/types.d.ts +130 -0
  17. package/es/api/transactions/index.d.ts +35 -0
  18. package/es/api/transactions/types.d.ts +25 -0
  19. package/es/api/types.d.ts +18 -0
  20. package/es/client/core.d.ts +109 -0
  21. package/es/client/index.d.ts +21 -0
  22. package/es/client/index.js +373 -0
  23. package/es/index.d.ts +20 -0
  24. package/es/index.js +1490 -0
  25. package/es/utils/address.d.ts +15 -0
  26. package/es/utils/index.d.ts +6 -0
  27. package/es/utils/index.js +841 -0
  28. package/es/utils/interface.d.ts +7 -0
  29. package/es/utils/safePromise.d.ts +4 -0
  30. package/es/utils/sign.d.ts +15 -0
  31. package/es/utils/txHash.d.ts +2 -0
  32. package/es/utils/typeof.d.ts +2 -0
  33. package/lib/api/accounts/index.d.ts +20 -0
  34. package/lib/api/accounts/types.d.ts +9 -0
  35. package/lib/api/chain/index.d.ts +12 -0
  36. package/lib/api/chain/types.d.ts +3 -0
  37. package/lib/api/checkpoints/index.d.ts +26 -0
  38. package/lib/api/checkpoints/types.d.ts +33 -0
  39. package/lib/api/constants.d.ts +9 -0
  40. package/lib/api/index.d.ts +31 -0
  41. package/lib/api/index.js +692 -0
  42. package/lib/api/state/index.d.ts +12 -0
  43. package/lib/api/state/types.d.ts +7 -0
  44. package/lib/api/tokens/index.d.ts +62 -0
  45. package/lib/api/tokens/types.d.ts +130 -0
  46. package/lib/api/transactions/index.d.ts +35 -0
  47. package/lib/api/transactions/types.d.ts +25 -0
  48. package/lib/api/types.d.ts +18 -0
  49. package/lib/client/core.d.ts +109 -0
  50. package/lib/client/index.d.ts +21 -0
  51. package/lib/client/index.js +434 -0
  52. package/lib/index.d.ts +20 -0
  53. package/lib/index.js +1591 -0
  54. package/lib/utils/address.d.ts +15 -0
  55. package/lib/utils/index.d.ts +6 -0
  56. package/lib/utils/index.js +937 -0
  57. package/lib/utils/interface.d.ts +7 -0
  58. package/lib/utils/safePromise.d.ts +4 -0
  59. package/lib/utils/sign.d.ts +15 -0
  60. package/lib/utils/txHash.d.ts +2 -0
  61. package/lib/utils/typeof.d.ts +2 -0
  62. package/package.json +111 -0
  63. package/public/favicon.ico +0 -0
  64. package/public/logo.png +0 -0
  65. package/umd/1money-protocol-ts-sdk.min.js +2 -0
@@ -0,0 +1,841 @@
1
+ import {hexToBytes as hexToBytes$1,stringToBytes,keccak256,bytesToHex as bytesToHex$1,numberToHex,stringToHex,boolToHex}from'viem';import {encode}from'@ethereumjs/rlp';/**
2
+ * Derives the token account address given the wallet address and mint address.
3
+ *
4
+ * Address is 20 byte, 160 bits. Let's say if we want to support 50 billion
5
+ * accounts on 1money. That's about 36 bits. There are 124 bits remaining. In
6
+ * other words, the collision probability is 1/2^124, which is very very low.
7
+ * So, we will be fine to just use the hash of the wallet address and mint
8
+ * address to derive the token account address.
9
+ *
10
+ * @param walletAddress - The wallet address (20 bytes)
11
+ * @param mintAddress - The mint address (20 bytes)
12
+ * @returns The derived token account address
13
+ */
14
+ function deriveTokenAddress(walletAddress, mintAddress) {
15
+ const walletBytes = walletAddress.startsWith('0x')
16
+ ? hexToBytes$1(walletAddress)
17
+ : stringToBytes(walletAddress);
18
+ const mintBytes = mintAddress.startsWith('0x')
19
+ ? hexToBytes$1(mintAddress)
20
+ : stringToBytes(mintAddress);
21
+ const combined = new Uint8Array(walletBytes.length + mintBytes.length);
22
+ combined.set(walletBytes, 0);
23
+ combined.set(mintBytes, walletBytes.length);
24
+ const hashHex = keccak256(combined);
25
+ const hashBytes = hexToBytes$1(hashHex);
26
+ const addressBytes = hashBytes.slice(12);
27
+ return bytesToHex$1(addressBytes);
28
+ }// concurrent
29
+ function safePromiseAll(arr) {
30
+ // @ts-expect-error
31
+ if (!arr || !arr.length)
32
+ return Promise.resolve([]);
33
+ return Promise.all(arr);
34
+ }
35
+ // serial
36
+ async function safePromiseLine(arr) {
37
+ if (!arr || !arr.length)
38
+ return [];
39
+ const res = [];
40
+ for (let i = 0; i < arr.length; i++) {
41
+ try {
42
+ res.push(await arr[i](i));
43
+ }
44
+ catch (e) {
45
+ // ignore
46
+ }
47
+ }
48
+ return res;
49
+ }function _typeof(ele) {
50
+ if (typeof ele !== 'object')
51
+ return (typeof ele).toLowerCase();
52
+ const typeStr = Object.prototype.toString.call(ele);
53
+ return typeStr.slice(8, typeStr.length - 1).toLowerCase();
54
+ }/*! noble-secp256k1 - MIT License (c) 2019 Paul Miller (paulmillr.com) */
55
+ /**
56
+ * 4KB JS implementation of secp256k1 ECDSA / Schnorr signatures & ECDH.
57
+ * Compliant with RFC6979 & BIP340.
58
+ * @module
59
+ */
60
+ /**
61
+ * Curve params. secp256k1 is short weierstrass / koblitz curve. Equation is y² == x³ + ax + b.
62
+ * * P = `2n**256n-2n**32n-2n**977n` // field over which calculations are done
63
+ * * N = `2n**256n - 0x14551231950b75fc4402da1732fc9bebfn` // group order, amount of curve points
64
+ * * h = `1n` // cofactor
65
+ * * a = `0n` // equation param
66
+ * * b = `7n` // equation param
67
+ * * Gx, Gy are coordinates of Generator / base point
68
+ */
69
+ const secp256k1_CURVE = {
70
+ p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn,
71
+ n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,
72
+ b: 7n,
73
+ Gx: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,
74
+ Gy: 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n,
75
+ };
76
+ const { p: P, n: N, Gx, Gy, b: _b } = secp256k1_CURVE;
77
+ const L = 32; // field / group byte length
78
+ const L2 = 64;
79
+ // Helpers and Precomputes sections are reused between libraries
80
+ // ## Helpers
81
+ // ----------
82
+ // error helper, messes-up stack trace
83
+ const err = (m = '') => {
84
+ throw new Error(m);
85
+ };
86
+ const isBig = (n) => typeof n === 'bigint'; // is big integer
87
+ const isStr = (s) => typeof s === 'string'; // is string
88
+ const isBytes = (a) => a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
89
+ /** assert is Uint8Array (of specific length) */
90
+ const abytes = (a, l) => !isBytes(a) || (typeof l === 'number' && l > 0 && a.length !== l)
91
+ ? err('Uint8Array expected')
92
+ : a;
93
+ /** create Uint8Array */
94
+ const u8n = (len) => new Uint8Array(len);
95
+ const u8fr = (buf) => Uint8Array.from(buf);
96
+ const padh = (n, pad) => n.toString(16).padStart(pad, '0');
97
+ const bytesToHex = (b) => Array.from(abytes(b))
98
+ .map((e) => padh(e, 2))
99
+ .join('');
100
+ const C = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 }; // ASCII characters
101
+ const _ch = (ch) => {
102
+ if (ch >= C._0 && ch <= C._9)
103
+ return ch - C._0; // '2' => 50-48
104
+ if (ch >= C.A && ch <= C.F)
105
+ return ch - (C.A - 10); // 'B' => 66-(65-10)
106
+ if (ch >= C.a && ch <= C.f)
107
+ return ch - (C.a - 10); // 'b' => 98-(97-10)
108
+ return;
109
+ };
110
+ const hexToBytes = (hex) => {
111
+ const e = 'hex invalid';
112
+ if (!isStr(hex))
113
+ return err(e);
114
+ const hl = hex.length;
115
+ const al = hl / 2;
116
+ if (hl % 2)
117
+ return err(e);
118
+ const array = u8n(al);
119
+ for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
120
+ // treat each char as ASCII
121
+ const n1 = _ch(hex.charCodeAt(hi)); // parse first char, multiply it by 16
122
+ const n2 = _ch(hex.charCodeAt(hi + 1)); // parse second char
123
+ if (n1 === undefined || n2 === undefined)
124
+ return err(e);
125
+ array[ai] = n1 * 16 + n2; // example: 'A9' => 10*16 + 9
126
+ }
127
+ return array;
128
+ };
129
+ /** normalize hex or ui8a to ui8a */
130
+ const toU8 = (a, len) => abytes(isStr(a) ? hexToBytes(a) : u8fr(abytes(a)), len);
131
+ const cr = () => globalThis?.crypto; // WebCrypto is available in all modern environments
132
+ const subtle = () => cr()?.subtle ?? err('crypto.subtle must be defined');
133
+ // prettier-ignore
134
+ const concatBytes = (...arrs) => {
135
+ const r = u8n(arrs.reduce((sum, a) => sum + abytes(a).length, 0)); // create u8a of summed length
136
+ let pad = 0; // walk through each array,
137
+ arrs.forEach(a => { r.set(a, pad); pad += a.length; }); // ensure they have proper type
138
+ return r;
139
+ };
140
+ /** WebCrypto OS-level CSPRNG (random number generator). Will throw when not available. */
141
+ const randomBytes = (len = L) => {
142
+ const c = cr();
143
+ return c.getRandomValues(u8n(len));
144
+ };
145
+ const big = BigInt;
146
+ const arange = (n, min, max, msg = 'bad number: out of range') => isBig(n) && min <= n && n < max ? n : err(msg);
147
+ /** modular division */
148
+ const M = (a, b = P) => {
149
+ const r = a % b;
150
+ return r >= 0n ? r : b + r;
151
+ };
152
+ const modN = (a) => M(a, N);
153
+ /** Modular inversion using eucledian GCD (non-CT). No negative exponent for now. */
154
+ // prettier-ignore
155
+ const invert = (num, md) => {
156
+ if (num === 0n || md <= 0n)
157
+ err('no inverse n=' + num + ' mod=' + md);
158
+ let a = M(num, md), b = md, x = 0n, u = 1n;
159
+ while (a !== 0n) {
160
+ const q = b / a, r = b % a;
161
+ const m = x - u * q;
162
+ b = a, a = r, x = u, u = m;
163
+ }
164
+ return b === 1n ? M(x, md) : err('no inverse'); // b is gcd at this point
165
+ };
166
+ const apoint = (p) => (p instanceof Point ? p : err('Point expected'));
167
+ // ## End of Helpers
168
+ // -----------------
169
+ /** secp256k1 formula. Koblitz curves are subclass of weierstrass curves with a=0, making it x³+b */
170
+ const koblitz = (x) => M(M(x * x) * x + _b);
171
+ /** assert is field element or 0 */
172
+ const afield0 = (n) => arange(n, 0n, P);
173
+ /** assert is field element */
174
+ const afield = (n) => arange(n, 1n, P);
175
+ /** assert is group elem */
176
+ const agroup = (n) => arange(n, 1n, N);
177
+ const isEven = (y) => (y & 1n) === 0n;
178
+ /** create Uint8Array of byte n */
179
+ const u8of = (n) => Uint8Array.of(n);
180
+ const getPrefix = (y) => u8of(isEven(y) ? 0x02 : 0x03);
181
+ /** lift_x from BIP340 calculates square root. Validates x, then validates root*root. */
182
+ const lift_x = (x) => {
183
+ // Let c = x³ + 7 mod p. Fail if x ≥ p. (also fail if x < 1)
184
+ const c = koblitz(afield(x));
185
+ // c = √y
186
+ // y = c^((p+1)/4) mod p
187
+ // This formula works for fields p = 3 mod 4 -- a special, fast case.
188
+ // Paper: "Square Roots from 1;24,51,10 to Dan Shanks".
189
+ let r = 1n;
190
+ for (let num = c, e = (P + 1n) / 4n; e > 0n; e >>= 1n) {
191
+ // powMod: modular exponentiation.
192
+ if (e & 1n)
193
+ r = (r * num) % P; // Uses exponentiation by squaring.
194
+ num = (num * num) % P; // Not constant-time.
195
+ }
196
+ return M(r * r) === c ? r : err('sqrt invalid'); // check if result is valid
197
+ };
198
+ /** Point in 3d xyz projective coordinates. 3d takes less inversions than 2d. */
199
+ class Point {
200
+ static BASE;
201
+ static ZERO;
202
+ px;
203
+ py;
204
+ pz;
205
+ constructor(px, py, pz) {
206
+ this.px = afield0(px);
207
+ this.py = afield(py); // y can't be 0 in Projective
208
+ this.pz = afield0(pz);
209
+ Object.freeze(this);
210
+ }
211
+ /** Convert Uint8Array or hex string to Point. */
212
+ static fromBytes(bytes) {
213
+ abytes(bytes);
214
+ let p = undefined;
215
+ // First byte is prefix, rest is data. There are 2 kinds: compressed & uncompressed:
216
+ // * [0x02 or 0x03][32-byte x coordinate]
217
+ // * [0x04] [32-byte x coordinate][32-byte y coordinate]
218
+ const head = bytes[0];
219
+ const tail = bytes.subarray(1);
220
+ const x = sliceBytesNumBE(tail, 0, L);
221
+ const len = bytes.length;
222
+ // Compressed 33-byte point, 0x02 or 0x03 prefix
223
+ if (len === L + 1 && [0x02, 0x03].includes(head)) {
224
+ // Equation is y² == x³ + ax + b. We calculate y from x.
225
+ // y = √y²; there are two solutions: y, -y. Determine proper solution based on prefix
226
+ let y = lift_x(x);
227
+ const evenY = isEven(y);
228
+ const evenH = isEven(big(head));
229
+ if (evenH !== evenY)
230
+ y = M(-y);
231
+ p = new Point(x, y, 1n);
232
+ }
233
+ // Uncompressed 65-byte point, 0x04 prefix
234
+ if (len === L2 + 1 && head === 0x04)
235
+ p = new Point(x, sliceBytesNumBE(tail, L, L2), 1n);
236
+ // Validate point
237
+ return p ? p.assertValidity() : err('bad point: not on curve');
238
+ }
239
+ /** Equality check: compare points P&Q. */
240
+ equals(other) {
241
+ const { px: X1, py: Y1, pz: Z1 } = this;
242
+ const { px: X2, py: Y2, pz: Z2 } = apoint(other); // checks class equality
243
+ const X1Z2 = M(X1 * Z2);
244
+ const X2Z1 = M(X2 * Z1);
245
+ const Y1Z2 = M(Y1 * Z2);
246
+ const Y2Z1 = M(Y2 * Z1);
247
+ return X1Z2 === X2Z1 && Y1Z2 === Y2Z1;
248
+ }
249
+ is0() {
250
+ return this.equals(I);
251
+ }
252
+ /** Flip point over y coordinate. */
253
+ negate() {
254
+ return new Point(this.px, M(-this.py), this.pz);
255
+ }
256
+ /** Point doubling: P+P, complete formula. */
257
+ double() {
258
+ return this.add(this);
259
+ }
260
+ /**
261
+ * Point addition: P+Q, complete, exception-free formula
262
+ * (Renes-Costello-Batina, algo 1 of [2015/1060](https://eprint.iacr.org/2015/1060)).
263
+ * Cost: `12M + 0S + 3*a + 3*b3 + 23add`.
264
+ */
265
+ // prettier-ignore
266
+ add(other) {
267
+ const { px: X1, py: Y1, pz: Z1 } = this;
268
+ const { px: X2, py: Y2, pz: Z2 } = apoint(other);
269
+ const a = 0n;
270
+ const b = _b;
271
+ let X3 = 0n, Y3 = 0n, Z3 = 0n;
272
+ const b3 = M(b * 3n);
273
+ let t0 = M(X1 * X2), t1 = M(Y1 * Y2), t2 = M(Z1 * Z2), t3 = M(X1 + Y1); // step 1
274
+ let t4 = M(X2 + Y2); // step 5
275
+ t3 = M(t3 * t4);
276
+ t4 = M(t0 + t1);
277
+ t3 = M(t3 - t4);
278
+ t4 = M(X1 + Z1);
279
+ let t5 = M(X2 + Z2); // step 10
280
+ t4 = M(t4 * t5);
281
+ t5 = M(t0 + t2);
282
+ t4 = M(t4 - t5);
283
+ t5 = M(Y1 + Z1);
284
+ X3 = M(Y2 + Z2); // step 15
285
+ t5 = M(t5 * X3);
286
+ X3 = M(t1 + t2);
287
+ t5 = M(t5 - X3);
288
+ Z3 = M(a * t4);
289
+ X3 = M(b3 * t2); // step 20
290
+ Z3 = M(X3 + Z3);
291
+ X3 = M(t1 - Z3);
292
+ Z3 = M(t1 + Z3);
293
+ Y3 = M(X3 * Z3);
294
+ t1 = M(t0 + t0); // step 25
295
+ t1 = M(t1 + t0);
296
+ t2 = M(a * t2);
297
+ t4 = M(b3 * t4);
298
+ t1 = M(t1 + t2);
299
+ t2 = M(t0 - t2); // step 30
300
+ t2 = M(a * t2);
301
+ t4 = M(t4 + t2);
302
+ t0 = M(t1 * t4);
303
+ Y3 = M(Y3 + t0);
304
+ t0 = M(t5 * t4); // step 35
305
+ X3 = M(t3 * X3);
306
+ X3 = M(X3 - t0);
307
+ t0 = M(t3 * t1);
308
+ Z3 = M(t5 * Z3);
309
+ Z3 = M(Z3 + t0); // step 40
310
+ return new Point(X3, Y3, Z3);
311
+ }
312
+ /**
313
+ * Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
314
+ * Uses {@link wNAF} for base point.
315
+ * Uses fake point to mitigate side-channel leakage.
316
+ * @param n scalar by which point is multiplied
317
+ * @param safe safe mode guards against timing attacks; unsafe mode is faster
318
+ */
319
+ multiply(n, safe = true) {
320
+ if (!safe && n === 0n)
321
+ return I;
322
+ agroup(n);
323
+ if (n === 1n)
324
+ return this;
325
+ if (this.equals(G))
326
+ return wNAF(n).p;
327
+ // init result point & fake point
328
+ let p = I;
329
+ let f = G;
330
+ for (let d = this; n > 0n; d = d.double(), n >>= 1n) {
331
+ // if bit is present, add to point
332
+ // if not present, add to fake, for timing safety
333
+ if (n & 1n)
334
+ p = p.add(d);
335
+ else if (safe)
336
+ f = f.add(d);
337
+ }
338
+ return p;
339
+ }
340
+ /** Convert point to 2d xy affine point. (X, Y, Z) ∋ (x=X/Z, y=Y/Z) */
341
+ toAffine() {
342
+ const { px: x, py: y, pz: z } = this;
343
+ // fast-paths for ZERO point OR Z=1
344
+ if (this.equals(I))
345
+ return { x: 0n, y: 0n };
346
+ if (z === 1n)
347
+ return { x, y };
348
+ const iz = invert(z, P);
349
+ // (Z * Z^-1) must be 1, otherwise bad math
350
+ if (M(z * iz) !== 1n)
351
+ err('inverse invalid');
352
+ // x = X*Z^-1; y = Y*Z^-1
353
+ return { x: M(x * iz), y: M(y * iz) };
354
+ }
355
+ /** Checks if the point is valid and on-curve. */
356
+ assertValidity() {
357
+ const { x, y } = this.toAffine(); // convert to 2d xy affine point.
358
+ afield(x); // must be in range 1 <= x,y < P
359
+ afield(y);
360
+ // y² == x³ + ax + b, equation sides must be equal
361
+ return M(y * y) === koblitz(x) ? this : err('bad point: not on curve');
362
+ }
363
+ /** Converts point to 33/65-byte Uint8Array. */
364
+ toBytes(isCompressed = true) {
365
+ const { x, y } = this.assertValidity().toAffine();
366
+ const x32b = numTo32b(x);
367
+ if (isCompressed)
368
+ return concatBytes(getPrefix(y), x32b);
369
+ return concatBytes(u8of(0x04), x32b, numTo32b(y));
370
+ }
371
+ /** Create 3d xyz point from 2d xy. (0, 0) => (0, 1, 0), not (0, 0, 1) */
372
+ static fromAffine(ap) {
373
+ const { x, y } = ap;
374
+ return x === 0n && y === 0n ? I : new Point(x, y, 1n);
375
+ }
376
+ toHex(isCompressed) {
377
+ return bytesToHex(this.toBytes(isCompressed));
378
+ }
379
+ static fromPrivateKey(k) {
380
+ return G.multiply(toPrivScalar(k));
381
+ }
382
+ static fromHex(hex) {
383
+ return Point.fromBytes(toU8(hex));
384
+ }
385
+ get x() {
386
+ return this.toAffine().x;
387
+ }
388
+ get y() {
389
+ return this.toAffine().y;
390
+ }
391
+ toRawBytes(isCompressed) {
392
+ return this.toBytes(isCompressed);
393
+ }
394
+ }
395
+ /** Generator / base point */
396
+ const G = new Point(Gx, Gy, 1n);
397
+ /** Identity / zero point */
398
+ const I = new Point(0n, 1n, 0n);
399
+ // Static aliases
400
+ Point.BASE = G;
401
+ Point.ZERO = I;
402
+ /** `Q = u1⋅G + u2⋅R`. Verifies Q is not ZERO. Unsafe: non-CT. */
403
+ const doubleScalarMulUns = (R, u1, u2) => {
404
+ return G.multiply(u1, false).add(R.multiply(u2, false)).assertValidity();
405
+ };
406
+ const bytesToNumBE = (b) => big('0x' + (bytesToHex(b) || '0'));
407
+ const sliceBytesNumBE = (b, from, to) => bytesToNumBE(b.subarray(from, to));
408
+ const B256 = 2n ** 256n; // secp256k1 is weierstrass curve. Equation is x³ + ax + b.
409
+ /** Number to 32b. Must be 0 <= num < B256. validate, pad, to bytes. */
410
+ const numTo32b = (num) => hexToBytes(padh(arange(num, 0n, B256), L2));
411
+ /** Normalize private key to scalar (bigint). Verifies scalar is in range 1<s<N */
412
+ const toPrivScalar = (pr) => {
413
+ const num = isBig(pr) ? pr : bytesToNumBE(toU8(pr, L));
414
+ return arange(num, 1n, N, 'private key invalid 3');
415
+ };
416
+ /** For Signature malleability, validates sig.s is bigger than N/2. */
417
+ const highS = (n) => n > N >> 1n;
418
+ /** ECDSA Signature class. Supports only compact 64-byte representation, not DER. */
419
+ class Signature {
420
+ r;
421
+ s;
422
+ recovery;
423
+ constructor(r, s, recovery) {
424
+ this.r = agroup(r); // 1 <= r < N
425
+ this.s = agroup(s); // 1 <= s < N
426
+ if (recovery != null)
427
+ this.recovery = recovery;
428
+ Object.freeze(this);
429
+ }
430
+ /** Create signature from 64b compact (r || s) representation. */
431
+ static fromBytes(b) {
432
+ abytes(b, L2);
433
+ const r = sliceBytesNumBE(b, 0, L);
434
+ const s = sliceBytesNumBE(b, L, L2);
435
+ return new Signature(r, s);
436
+ }
437
+ toBytes() {
438
+ const { r, s } = this;
439
+ return concatBytes(numTo32b(r), numTo32b(s));
440
+ }
441
+ /** Copy signature, with newly added recovery bit. */
442
+ addRecoveryBit(bit) {
443
+ return new Signature(this.r, this.s, bit);
444
+ }
445
+ hasHighS() {
446
+ return highS(this.s);
447
+ }
448
+ toCompactRawBytes() {
449
+ return this.toBytes();
450
+ }
451
+ toCompactHex() {
452
+ return bytesToHex(this.toBytes());
453
+ }
454
+ recoverPublicKey(msg) {
455
+ return recoverPublicKey(this, msg);
456
+ }
457
+ static fromCompact(hex) {
458
+ return Signature.fromBytes(toU8(hex, L2));
459
+ }
460
+ assertValidity() {
461
+ return this;
462
+ }
463
+ normalizeS() {
464
+ const { r, s, recovery } = this;
465
+ return highS(s) ? new Signature(r, modN(-s), recovery) : this;
466
+ }
467
+ }
468
+ /**
469
+ * RFC6979: ensure ECDSA msg is X bytes, convert to BigInt.
470
+ * RFC suggests optional truncating via bits2octets.
471
+ * FIPS 186-4 4.6 suggests the leftmost min(nBitLen, outLen) bits,
472
+ * which matches bits2int. bits2int can produce res>N.
473
+ */
474
+ const bits2int = (bytes) => {
475
+ const delta = bytes.length * 8 - 256;
476
+ if (delta > 1024)
477
+ err('msg invalid'); // our CUSTOM check, "just-in-case": prohibit long inputs
478
+ const num = bytesToNumBE(bytes);
479
+ return delta > 0 ? num >> big(delta) : num;
480
+ };
481
+ /** int2octets can't be used; pads small msgs with 0: BAD for truncation as per RFC vectors */
482
+ const bits2int_modN = (bytes) => modN(bits2int(abytes(bytes)));
483
+ const signOpts = { lowS: true };
484
+ // RFC6979 signature generation, preparation step.
485
+ const prepSig = (msgh, priv, opts = signOpts) => {
486
+ if (['der', 'recovered', 'canonical'].some((k) => k in opts))
487
+ // legacy opts
488
+ err('option not supported');
489
+ let { lowS, extraEntropy } = opts; // generates low-s sigs by default
490
+ if (lowS == null)
491
+ lowS = true; // RFC6979 3.2: we skip step A
492
+ const i2o = numTo32b; // int to octets
493
+ const h1i = bits2int_modN(toU8(msgh)); // msg bigint
494
+ const h1o = i2o(h1i); // msg octets
495
+ const d = toPrivScalar(priv); // validate private key, convert to bigint
496
+ const seed = [i2o(d), h1o]; // Step D of RFC6979 3.2
497
+ /** RFC6979 3.6: additional k' (optional). See {@link ExtraEntropy}. */
498
+ // K = HMAC_K(V || 0x00 || int2octets(x) || bits2octets(h1) || k')
499
+ if (extraEntropy)
500
+ seed.push(extraEntropy === true ? randomBytes(L) : toU8(extraEntropy));
501
+ const m = h1i; // convert msg to bigint
502
+ // Converts signature params into point w r/s, checks result for validity.
503
+ // To transform k => Signature:
504
+ // q = k⋅G
505
+ // r = q.x mod n
506
+ // s = k^-1(m + rd) mod n
507
+ const k2sig = (kBytes) => {
508
+ // RFC 6979 Section 3.2, step 3: k = bits2int(T)
509
+ // Important: all mod() calls here must be done over N
510
+ const k = bits2int(kBytes);
511
+ if (!(1n <= k && k < N))
512
+ return; // Check 0 < k < CURVE.n
513
+ const q = G.multiply(k).toAffine(); // q = k⋅G
514
+ const r = modN(q.x); // r = q.x mod n
515
+ if (r === 0n)
516
+ return;
517
+ const ik = invert(k, N); // k^-1 mod n, NOT mod P
518
+ const s = modN(ik * modN(m + modN(d * r))); // s = k^-1(m + rd) mod n
519
+ if (s === 0n)
520
+ return;
521
+ let normS = s; // normalized S
522
+ let recovery = (q.x === r ? 0 : 2) | Number(q.y & 1n); // recovery bit (2 or 3, when q.x > n)
523
+ if (lowS && highS(s)) {
524
+ // if lowS was passed, ensure s is always
525
+ normS = modN(-s); // in the bottom half of CURVE.n
526
+ recovery ^= 1;
527
+ }
528
+ return new Signature(r, normS, recovery); // use normS, not s
529
+ };
530
+ return { seed: concatBytes(...seed), k2sig };
531
+ };
532
+ // HMAC-DRBG from NIST 800-90. Minimal, non-full-spec - used for RFC6979 signatures.
533
+ const hmacDrbg = (asynchronous) => {
534
+ let v = u8n(L); // Steps B, C of RFC6979 3.2: set hashLen
535
+ let k = u8n(L); // In our case, it's always equal to L
536
+ let i = 0; // Iterations counter, will throw when over max
537
+ const NULL = u8n(0);
538
+ const reset = () => {
539
+ v.fill(1);
540
+ k.fill(0);
541
+ i = 0;
542
+ };
543
+ const max = 1000;
544
+ const _e = 'drbg: tried 1000 values';
545
+ {
546
+ // asynchronous=true
547
+ // h = hmac(K || V || ...)
548
+ const h = (...b) => etc.hmacSha256Async(k, v, ...b);
549
+ const reseed = async (seed = NULL) => {
550
+ // HMAC-DRBG reseed() function. Steps D-G
551
+ k = await h(u8of(0x00), seed); // k = hmac(K || V || 0x00 || seed)
552
+ v = await h(); // v = hmac(K || V)
553
+ if (seed.length === 0)
554
+ return;
555
+ k = await h(u8of(0x01), seed); // k = hmac(K || V || 0x01 || seed)
556
+ v = await h(); // v = hmac(K || V)
557
+ };
558
+ // HMAC-DRBG generate() function
559
+ const gen = async () => {
560
+ if (i++ >= max)
561
+ err(_e);
562
+ v = await h(); // v = hmac(K || V)
563
+ return v; // this diverges from noble-curves: we don't allow arbitrary output len!
564
+ };
565
+ // Do not reuse returned fn for more than 1 sig:
566
+ // 1) it's slower (JIT screws up). 2. unsafe (async race conditions)
567
+ return async (seed, pred) => {
568
+ reset();
569
+ await reseed(seed); // Steps D-G
570
+ let res = undefined; // Step H: grind until k is in [1..n-1]
571
+ while (!(res = pred(await gen())))
572
+ await reseed(); // test predicate until it returns ok
573
+ reset();
574
+ return res;
575
+ };
576
+ }
577
+ };
578
+ /**
579
+ * Sign a msg hash using secp256k1. Async.
580
+ * Follows [SEC1](https://secg.org/sec1-v2.pdf) 4.1.2 & RFC6979.
581
+ * It's suggested to enable hedging ({@link ExtraEntropy}) to prevent fault attacks.
582
+ * @param msgh - message HASH, not message itself e.g. sha256(message)
583
+ * @param priv - private key
584
+ * @param opts - `lowS: true` prevents malleability, `extraEntropy: true` enables hedging
585
+ */
586
+ const signAsync = async (msgh, priv, opts = signOpts) => {
587
+ // Re-run drbg until k2sig returns ok
588
+ const { seed, k2sig } = prepSig(msgh, priv, opts);
589
+ const sig = await hmacDrbg()(seed, k2sig);
590
+ return sig;
591
+ };
592
+ /**
593
+ * ECDSA public key recovery. Requires msg hash and recovery id.
594
+ * Follows [SEC1](https://secg.org/sec1-v2.pdf) 4.1.6.
595
+ */
596
+ const recoverPublicKey = (sig, msgh) => {
597
+ const { r, s, recovery } = sig;
598
+ // 0 or 1 recovery id determines sign of "y" coordinate.
599
+ // 2 or 3 means q.x was >N.
600
+ if (![0, 1, 2, 3].includes(recovery))
601
+ err('recovery id invalid');
602
+ const h = bits2int_modN(toU8(msgh, L)); // Truncate hash
603
+ const radj = recovery === 2 || recovery === 3 ? r + N : r;
604
+ afield(radj); // ensure q.x is still a field element
605
+ const head = getPrefix(big(recovery)); // head is 0x02 or 0x03
606
+ const Rb = concatBytes(head, numTo32b(radj)); // concat head + r
607
+ const R = Point.fromBytes(Rb);
608
+ const ir = invert(radj, N); // r^-1
609
+ const u1 = modN(-h * ir); // -hr^-1
610
+ const u2 = modN(s * ir); // sr^-1
611
+ return doubleScalarMulUns(R, u1, u2); // (sr^-1)R-(hr^-1)G = -(hr^-1)G + (sr^-1)
612
+ };
613
+ // FIPS 186 B.4.1 compliant key generation produces private keys with modulo bias being neglible.
614
+ // takes >N+8 bytes, returns (hash mod n-1)+1
615
+ const hashToPrivateKey = (hash) => {
616
+ hash = toU8(hash);
617
+ if (hash.length < L + 8 || hash.length > 1024)
618
+ err('expected 40-1024b');
619
+ const num = M(bytesToNumBE(hash), N - 1n);
620
+ return numTo32b(num + 1n);
621
+ };
622
+ const _sha = 'SHA-256';
623
+ /** Math, hex, byte helpers. Not in `utils` because utils share API with noble-curves. */
624
+ const etc = {
625
+ hexToBytes: hexToBytes,
626
+ bytesToHex: bytesToHex,
627
+ concatBytes: concatBytes,
628
+ bytesToNumberBE: bytesToNumBE,
629
+ numberToBytesBE: numTo32b,
630
+ mod: M,
631
+ invert: invert, // math utilities
632
+ hmacSha256Async: async (key, ...msgs) => {
633
+ const s = subtle();
634
+ const name = 'HMAC';
635
+ const k = await s.importKey('raw', key, { name, hash: { name: _sha } }, false, ['sign']);
636
+ return u8n(await s.sign(name, k, concatBytes(...msgs)));
637
+ },
638
+ hmacSha256Sync: undefined, // For TypeScript. Actual logic is below
639
+ hashToPrivateKey: hashToPrivateKey,
640
+ randomBytes: randomBytes,
641
+ };
642
+ // ## Precomputes
643
+ // --------------
644
+ const W = 8; // W is window size
645
+ const scalarBits = 256;
646
+ const pwindows = Math.ceil(scalarBits / W) + 1; // 33 for W=8
647
+ const pwindowSize = 2 ** (W - 1); // 128 for W=8
648
+ const precompute = () => {
649
+ const points = [];
650
+ let p = G;
651
+ let b = p;
652
+ for (let w = 0; w < pwindows; w++) {
653
+ b = p;
654
+ points.push(b);
655
+ for (let i = 1; i < pwindowSize; i++) {
656
+ b = b.add(p);
657
+ points.push(b);
658
+ } // i=1, bc we skip 0
659
+ p = b.double();
660
+ }
661
+ return points;
662
+ };
663
+ let Gpows = undefined; // precomputes for base point G
664
+ // const-time negate
665
+ const ctneg = (cnd, p) => {
666
+ const n = p.negate();
667
+ return cnd ? n : p;
668
+ };
669
+ /**
670
+ * Precomputes give 12x faster getPublicKey(), 10x sign(), 2x verify() by
671
+ * caching multiples of G (base point). Cache is stored in 32MB of RAM.
672
+ * Any time `G.multiply` is done, precomputes are used.
673
+ * Not used for getSharedSecret, which instead multiplies random pubkey `P.multiply`.
674
+ *
675
+ * w-ary non-adjacent form (wNAF) precomputation method is 10% slower than windowed method,
676
+ * but takes 2x less RAM. RAM reduction is possible by utilizing `.subtract`.
677
+ *
678
+ * !! Precomputes can be disabled by commenting-out call of the wNAF() inside Point#multiply().
679
+ */
680
+ const wNAF = (n) => {
681
+ const comp = Gpows || (Gpows = precompute());
682
+ let p = I;
683
+ let f = G; // f must be G, or could become I in the end
684
+ const pow_2_w = 2 ** W; // 256 for W=8
685
+ const maxNum = pow_2_w; // 256 for W=8
686
+ const mask = big(pow_2_w - 1); // 255 for W=8 == mask 0b11111111
687
+ const shiftBy = big(W); // 8 for W=8
688
+ for (let w = 0; w < pwindows; w++) {
689
+ let wbits = Number(n & mask); // extract W bits.
690
+ n >>= shiftBy; // shift number by W bits.
691
+ if (wbits > pwindowSize) {
692
+ wbits -= maxNum;
693
+ n += 1n;
694
+ } // split if bits > max: +224 => 256-32
695
+ const off = w * pwindowSize;
696
+ const offF = off; // offsets, evaluate both
697
+ const offP = off + Math.abs(wbits) - 1;
698
+ const isEven = w % 2 !== 0; // conditions, evaluate both
699
+ const isNeg = wbits < 0;
700
+ if (wbits === 0) {
701
+ // off == I: can't add it. Adding random offF instead.
702
+ f = f.add(ctneg(isEven, comp[offF])); // bits are 0: add garbage to fake point
703
+ }
704
+ else {
705
+ p = p.add(ctneg(isNeg, comp[offP])); // bits are 1: add to result point
706
+ }
707
+ }
708
+ return { p, f }; // return both real and fake points for JIT
709
+ };/**
710
+ * RLP encode a payload into a digest
711
+ * @param payload Payload to encode
712
+ * @returns RLP encoded payload
713
+ */
714
+ function encodePayload(payload) {
715
+ if (_typeof(payload) === 'array') {
716
+ const formatted = payload.map((v) => {
717
+ if (_typeof(v) === 'string') {
718
+ if (/^0x[0-9a-fA-F]+$/.test(v)) {
719
+ // hex-encoded data → raw bytes
720
+ return hexToBytes$1(v);
721
+ }
722
+ else if (!isNaN(+v)) {
723
+ // number-like string → hex → bytes
724
+ return v === '0' ? new Uint8Array([]) : hexToBytes$1(numberToHex(+v));
725
+ }
726
+ else {
727
+ // plain string → UTF-8 bytes
728
+ return new TextEncoder().encode(v);
729
+ }
730
+ }
731
+ else if (_typeof(v) === 'number' || _typeof(v) === 'bigint') {
732
+ // produce minimal hex, then arrayify
733
+ return v === 0 || v === BigInt(0) ? new Uint8Array([]) : hexToBytes$1(numberToHex(v));
734
+ }
735
+ else if (_typeof(v) === 'boolean') {
736
+ return v ? Uint8Array.from([1]) : new Uint8Array([]);
737
+ }
738
+ else {
739
+ return v;
740
+ }
741
+ });
742
+ return encode(formatted);
743
+ }
744
+ if (_typeof(payload) === 'boolean') {
745
+ return encode(payload ? Uint8Array.from([1]) : new Uint8Array([]));
746
+ }
747
+ return encode(payload);
748
+ }
749
+ /**
750
+ * Sign a message using the provided private key
751
+ * @param payload Payload to sign
752
+ * @param privateKey Private key to sign with
753
+ * @returns Signature object with r, s, v components
754
+ */
755
+ async function signMessage(payload, privateKey) {
756
+ const encoded = encodePayload(payload);
757
+ const digestHex = keccak256(encoded);
758
+ const digest = hexToBytes$1(digestHex);
759
+ const privateKeyBytes = hexToBytes$1(privateKey);
760
+ const signature = await signAsync(digest, privateKeyBytes, { lowS: true });
761
+ const compact = signature.toCompactRawBytes();
762
+ const rBytes = compact.subarray(0, 32);
763
+ const sBytes = compact.subarray(32, 64);
764
+ return {
765
+ r: bytesToHex$1(rBytes),
766
+ s: bytesToHex$1(sBytes),
767
+ v: signature.recovery,
768
+ };
769
+ }
770
+ function toHex(value) {
771
+ const type = _typeof(value);
772
+ try {
773
+ switch (type) {
774
+ case 'boolean':
775
+ return boolToHex(value);
776
+ case 'number':
777
+ case 'bigint':
778
+ return numberToHex(value);
779
+ case 'string':
780
+ if (!isNaN(+value))
781
+ return numberToHex(+value);
782
+ return stringToHex(value);
783
+ case 'uint8array':
784
+ case 'uint16array':
785
+ case 'uint32array':
786
+ case 'int8array':
787
+ case 'int16array':
788
+ case 'int32array':
789
+ case 'arraybuffer':
790
+ return bytesToHex$1(value);
791
+ case 'array':
792
+ if (value.length === 0)
793
+ return '0x';
794
+ else if (value.every(item => typeof item === 'number'))
795
+ return bytesToHex$1(Uint8Array.from(value));
796
+ else
797
+ return bytesToHex$1(stringToBytes(JSON.stringify(value)));
798
+ default:
799
+ return bytesToHex$1(stringToBytes(JSON.stringify(value)));
800
+ }
801
+ }
802
+ catch (e) {
803
+ console.error('[1Money toHex]: ', e);
804
+ return '0x';
805
+ }
806
+ }function encodeRlpListHeader(length) {
807
+ if (length < 56) {
808
+ // Short list: single byte prefix
809
+ return Uint8Array.from([0xc0 + length]);
810
+ }
811
+ else {
812
+ // Long list: prefix 0xf7 + length of length + actual length bytes
813
+ const lenBytes = [];
814
+ let temp = length;
815
+ while (temp > 0) {
816
+ lenBytes.unshift(temp & 0xff);
817
+ temp >>= 8;
818
+ }
819
+ return Uint8Array.from([0xf7 + lenBytes.length, ...lenBytes]);
820
+ }
821
+ }
822
+ function calcTxHash(payload, signature) {
823
+ const pEncode = encodePayload(payload);
824
+ const vEncode = encode(typeof signature.v === 'boolean'
825
+ ? signature.v
826
+ ? Uint8Array.from([1])
827
+ : new Uint8Array([])
828
+ : BigInt(signature.v));
829
+ const rEncode = encode(hexToBytes$1(signature.r));
830
+ const sEncode = encode(hexToBytes$1(signature.s));
831
+ const vrsBytes = new Uint8Array(vEncode.length + rEncode.length + sEncode.length);
832
+ vrsBytes.set(vEncode, 0);
833
+ vrsBytes.set(rEncode, vEncode.length);
834
+ vrsBytes.set(sEncode, vEncode.length + rEncode.length);
835
+ const header = encodeRlpListHeader(pEncode.length + vrsBytes.length);
836
+ const encoded = new Uint8Array(header.length + pEncode.length + vrsBytes.length);
837
+ encoded.set(header, 0);
838
+ encoded.set(pEncode, header.length);
839
+ encoded.set(vrsBytes, header.length + pEncode.length);
840
+ return keccak256(encoded);
841
+ }export{_typeof,calcTxHash,deriveTokenAddress,encodePayload,safePromiseAll,safePromiseLine,signMessage,toHex};