x402-proxy 0.10.12 → 0.11.0
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.
- package/README.md +3 -3
- package/dist/{Credential-0iRzpJkF.js → Mcp-CKylLH8N.js} +209 -211
- package/dist/accounts-D5u2KBgr.js +4 -0
- package/dist/{hashTypedData-CtEdfx4y.js → accounts-D9KsVVpJ.js} +228 -3
- package/dist/{auth-J9VuNMoG.js → auth-CaCZiNCk.js} +3 -3
- package/dist/bin/cli.js +46 -51
- package/dist/{decodeFunctionData-JPOUdvil.js → call-CNxk2xpg.js} +2084 -144
- package/dist/{decodeFunctionData-DuFcwhC_.js → call-DdUUzMfg.js} +11 -16
- package/dist/{ccip-C6CQOJYv.js → ccip-Cc1Fdazz.js} +1 -1
- package/dist/{ccip-Bx-zoUCJ.js → ccip-IumNC20V.js} +13 -3
- package/dist/{ccip-C2k1DD1T.js → ccip-JDVib6lf.js} +1 -1
- package/dist/{ccip-RZzsZ5Mv.js → ccip-RzF9aX_O.js} +3 -4
- package/dist/{chain-CafcHffR.js → chain-C_3Km9Dx.js} +1 -1
- package/dist/{client-Bcl2uC6u.js → client-C2DwSbYN.js} +1 -2
- package/dist/{Sse-DGR-tH7K.js → client-DqtxTnu6.js} +10249 -6224
- package/dist/client-DuqEc8Iw.js +3 -0
- package/dist/{client-DZV1cVsW.js → client-uAaF6lv2.js} +2 -2
- package/dist/{derive-DNUl8LU9.js → config-cyNyKO4-.js} +1024 -919
- package/dist/{dist-DM5_F3r5.js → dist-CFt2UZ4W.js} +1 -1
- package/dist/{dist-DxJCYyL5.js → dist-Cwt2qrTK.js} +2 -2
- package/dist/{helpers-ChcFbdC0.js → helpers-DebIs_21.js} +2 -2
- package/dist/{sendRawTransactionSync-DvSkhZtW.js → http-J0KYM-2Y.js} +2186 -169
- package/dist/index.d.ts +0 -1
- package/dist/index.js +30107 -771
- package/dist/{index.node-l8K03uWn.js → index.node-xc-jRJ78.js} +1 -1
- package/dist/{localBatchGatewayRequest-DOdQ9bR7.js → localBatchGatewayRequest-0gr8vzH3.js} +14 -2
- package/dist/{localBatchGatewayRequest-DQkbZaSy.js → localBatchGatewayRequest-CBsFXIAU.js} +2 -2
- package/dist/{localBatchGatewayRequest-C-RPJyDO.js → localBatchGatewayRequest-CPqqSJcB.js} +2 -2
- package/dist/{schemas-BxMFYNbH.js → schemas-DzZjufD1.js} +1 -1
- package/dist/{secp256k1-BZpiyffY.js → secp256k1-Bp7LcNvI.js} +2 -7
- package/dist/secp256k1-BsaSkXlH.js +2 -0
- package/dist/{secp256k1-BjenrLl5.js → secp256k1-BwSAYyif.js} +2 -2
- package/dist/secp256k1-CHAXu2gP.js +3 -0
- package/dist/{server-D2apWWY9.js → server-Dbsk9dql.js} +2 -2
- package/dist/setup-BdSfEdx4.js +3 -0
- package/dist/{setup-BY4J49Lv.js → setup-C10O8N36.js} +16 -33
- package/dist/{hmac-59IlS_by.js → sha2-XLg3s2a9.js} +81 -81
- package/dist/{sha256-FAs0qeni.js → sha256-DCLdwsRU.js} +1 -1
- package/dist/{sha3-DbMJRJ3C.js → sha3-03id956K.js} +1 -1
- package/dist/{sha3-CYkWM8Xa.js → sha3-VSia5eGE.js} +1 -1
- package/dist/{sse-qIEKrJEZ.js → sse-kba68iqF.js} +2 -2
- package/dist/status-B2xUewrq.js +3 -0
- package/dist/{status-CD_fCzGf.js → status-DjZdIeES.js} +2 -3
- package/dist/{stdio-BfDjLLnd.js → stdio-CdmkLt8r.js} +3 -3
- package/dist/{streamableHttp-BkayeoEQ.js → streamableHttp-DRntYTFN.js} +2 -2
- package/dist/{types-BmyZIQ8G.js → types-BQ17ZF7g.js} +1 -1
- package/dist/{types-DlqDf-zE.js → types-DH9rDVeP.js} +1 -1
- package/dist/{utils-SeGHMW9O.js → utils-psFtUKLa.js} +53 -53
- package/dist/wallet-BsCgqUun.js +3 -0
- package/dist/{wallet-C4RiduwM.js → wallet-CUCoLEqs.js} +179 -147
- package/package.json +1 -1
- package/dist/Mcp-CSyD7Hxz.js +0 -10
- package/dist/Sse-C-KDPYh0.js +0 -16482
- package/dist/accounts-DsuvWwph.js +0 -232
- package/dist/accounts-DzvAlQRn.js +0 -5
- package/dist/accounts-IG-Cmrwy.js +0 -229
- package/dist/chain-DwfP5RGZ.js +0 -1968
- package/dist/chunk-DBEY4PJZ.js +0 -16
- package/dist/client-1XQsO-Eh.js +0 -5152
- package/dist/client-Co7Fg1lv.js +0 -6388
- package/dist/config-BUQsit4s.js +0 -3
- package/dist/config-DR1Fs_wL.js +0 -6600
- package/dist/config-rvKA3SYT.js +0 -96
- package/dist/hashTypedData-BHmP9dBd.js +0 -859
- package/dist/http-BAtucMbS.js +0 -2060
- package/dist/isAddressEqual-BLrd1Hg1.js +0 -9
- package/dist/isAddressEqual-DsAqfQOD.js +0 -10
- package/dist/parseUnits-CApwcKSD.js +0 -49
- package/dist/parseUnits-cMO2udMe.js +0 -48
- package/dist/secp256k1-CLPUX17u.js +0 -3
- package/dist/setup-wMOAgrsN.js +0 -3
- package/dist/status-_TcPMyzr.js +0 -3
- package/dist/tempo-BcTkftHK.js +0 -17
- package/dist/tempo-Bs_aydgm.js +0 -18
- package/dist/wallet-BymEHYms.js +0 -3
- /package/dist/{api-CUzmQvTQ.js → api-B5mo8tpE.js} +0 -0
- /package/dist/{dist-C2YO6HSQ.js → dist-CvV4mGO6.js} +0 -0
- /package/dist/{index.node-DPDV8w2j.js → index.node-DCvOPMK7.js} +0 -0
- /package/dist/{utils-BYjkXZDF.js → utils-B1qOlJzL.js} +0 -0
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as __commonJSMin } from "./chunk-DjEMn6fM.js";
|
|
2
|
+
import { i as __toESM, t as __commonJSMin } from "./chunk-DjEMn6fM.js";
|
|
3
|
+
import { t as require_dist } from "./dist-CvV4mGO6.js";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
3
7
|
//#region node_modules/.pnpm/@stricli+core@1.2.6/node_modules/@stricli/core/dist/index.js
|
|
4
8
|
function checkEnvironmentVariable(process, varName) {
|
|
5
9
|
const value = process.env?.[varName];
|
|
@@ -4571,794 +4575,395 @@ createORPF({
|
|
|
4571
4575
|
hashToScalar: ristretto255_hasher.hashToScalar
|
|
4572
4576
|
});
|
|
4573
4577
|
//#endregion
|
|
4574
|
-
//#region node_modules/.pnpm/@
|
|
4575
|
-
/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
4576
|
-
function isBytes(a) {
|
|
4577
|
-
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
4578
|
-
}
|
|
4579
|
-
function isArrayOf(isString, arr) {
|
|
4580
|
-
if (!Array.isArray(arr)) return false;
|
|
4581
|
-
if (arr.length === 0) return true;
|
|
4582
|
-
if (isString) return arr.every((item) => typeof item === "string");
|
|
4583
|
-
else return arr.every((item) => Number.isSafeInteger(item));
|
|
4584
|
-
}
|
|
4585
|
-
function afn(input) {
|
|
4586
|
-
if (typeof input !== "function") throw new Error("function expected");
|
|
4587
|
-
return true;
|
|
4588
|
-
}
|
|
4589
|
-
function astr(label, input) {
|
|
4590
|
-
if (typeof input !== "string") throw new Error(`${label}: string expected`);
|
|
4591
|
-
return true;
|
|
4592
|
-
}
|
|
4593
|
-
function anumber(n) {
|
|
4594
|
-
if (!Number.isSafeInteger(n)) throw new Error(`invalid integer: ${n}`);
|
|
4595
|
-
}
|
|
4596
|
-
function aArr(input) {
|
|
4597
|
-
if (!Array.isArray(input)) throw new Error("array expected");
|
|
4598
|
-
}
|
|
4599
|
-
function astrArr(label, input) {
|
|
4600
|
-
if (!isArrayOf(true, input)) throw new Error(`${label}: array of strings expected`);
|
|
4601
|
-
}
|
|
4602
|
-
function anumArr(label, input) {
|
|
4603
|
-
if (!isArrayOf(false, input)) throw new Error(`${label}: array of numbers expected`);
|
|
4604
|
-
}
|
|
4578
|
+
//#region node_modules/.pnpm/@noble+hashes@2.0.1/node_modules/@noble/hashes/hmac.js
|
|
4605
4579
|
/**
|
|
4606
|
-
*
|
|
4580
|
+
* HMAC: RFC2104 message authentication code.
|
|
4581
|
+
* @module
|
|
4607
4582
|
*/
|
|
4608
|
-
|
|
4609
|
-
|
|
4610
|
-
|
|
4611
|
-
|
|
4612
|
-
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
4583
|
+
/** Internal class for HMAC. */
|
|
4584
|
+
var _HMAC = class {
|
|
4585
|
+
oHash;
|
|
4586
|
+
iHash;
|
|
4587
|
+
blockLen;
|
|
4588
|
+
outputLen;
|
|
4589
|
+
finished = false;
|
|
4590
|
+
destroyed = false;
|
|
4591
|
+
constructor(hash, key) {
|
|
4592
|
+
ahash(hash);
|
|
4593
|
+
abytes$1(key, void 0, "key");
|
|
4594
|
+
this.iHash = hash.create();
|
|
4595
|
+
if (typeof this.iHash.update !== "function") throw new Error("Expected instance of class which extends utils.Hash");
|
|
4596
|
+
this.blockLen = this.iHash.blockLen;
|
|
4597
|
+
this.outputLen = this.iHash.outputLen;
|
|
4598
|
+
const blockLen = this.blockLen;
|
|
4599
|
+
const pad = new Uint8Array(blockLen);
|
|
4600
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
4601
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 54;
|
|
4602
|
+
this.iHash.update(pad);
|
|
4603
|
+
this.oHash = hash.create();
|
|
4604
|
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 106;
|
|
4605
|
+
this.oHash.update(pad);
|
|
4606
|
+
clean(pad);
|
|
4607
|
+
}
|
|
4608
|
+
update(buf) {
|
|
4609
|
+
aexists(this);
|
|
4610
|
+
this.iHash.update(buf);
|
|
4611
|
+
return this;
|
|
4612
|
+
}
|
|
4613
|
+
digestInto(out) {
|
|
4614
|
+
aexists(this);
|
|
4615
|
+
abytes$1(out, this.outputLen, "output");
|
|
4616
|
+
this.finished = true;
|
|
4617
|
+
this.iHash.digestInto(out);
|
|
4618
|
+
this.oHash.update(out);
|
|
4619
|
+
this.oHash.digestInto(out);
|
|
4620
|
+
this.destroy();
|
|
4621
|
+
}
|
|
4622
|
+
digest() {
|
|
4623
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
4624
|
+
this.digestInto(out);
|
|
4625
|
+
return out;
|
|
4626
|
+
}
|
|
4627
|
+
_cloneInto(to) {
|
|
4628
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
4629
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
4630
|
+
to = to;
|
|
4631
|
+
to.finished = finished;
|
|
4632
|
+
to.destroyed = destroyed;
|
|
4633
|
+
to.blockLen = blockLen;
|
|
4634
|
+
to.outputLen = outputLen;
|
|
4635
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
4636
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
4637
|
+
return to;
|
|
4638
|
+
}
|
|
4639
|
+
clone() {
|
|
4640
|
+
return this._cloneInto();
|
|
4641
|
+
}
|
|
4642
|
+
destroy() {
|
|
4643
|
+
this.destroyed = true;
|
|
4644
|
+
this.oHash.destroy();
|
|
4645
|
+
this.iHash.destroy();
|
|
4646
|
+
}
|
|
4647
|
+
};
|
|
4616
4648
|
/**
|
|
4617
|
-
*
|
|
4618
|
-
*
|
|
4619
|
-
* @
|
|
4649
|
+
* HMAC: RFC2104 message authentication code.
|
|
4650
|
+
* @param hash - function that would be used e.g. sha256
|
|
4651
|
+
* @param key - message key
|
|
4652
|
+
* @param message - message data
|
|
4653
|
+
* @example
|
|
4654
|
+
* import { hmac } from '@noble/hashes/hmac';
|
|
4655
|
+
* import { sha256 } from '@noble/hashes/sha2';
|
|
4656
|
+
* const mac1 = hmac(sha256, 'key', 'message');
|
|
4620
4657
|
*/
|
|
4621
|
-
|
|
4622
|
-
|
|
4623
|
-
|
|
4624
|
-
|
|
4625
|
-
const indexes = new Map(lettersA.map((l, i) => [l, i]));
|
|
4626
|
-
return {
|
|
4627
|
-
encode: (digits) => {
|
|
4628
|
-
aArr(digits);
|
|
4629
|
-
return digits.map((i) => {
|
|
4630
|
-
if (!Number.isSafeInteger(i) || i < 0 || i >= len) throw new Error(`alphabet.encode: digit index outside alphabet "${i}". Allowed: ${letters}`);
|
|
4631
|
-
return lettersA[i];
|
|
4632
|
-
});
|
|
4633
|
-
},
|
|
4634
|
-
decode: (input) => {
|
|
4635
|
-
aArr(input);
|
|
4636
|
-
return input.map((letter) => {
|
|
4637
|
-
astr("alphabet.decode", letter);
|
|
4638
|
-
const i = indexes.get(letter);
|
|
4639
|
-
if (i === void 0) throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
|
|
4640
|
-
return i;
|
|
4641
|
-
});
|
|
4642
|
-
}
|
|
4643
|
-
};
|
|
4644
|
-
}
|
|
4658
|
+
const hmac = (hash, key, message) => new _HMAC(hash, key).update(message).digest();
|
|
4659
|
+
hmac.create = (hash, key) => new _HMAC(hash, key);
|
|
4660
|
+
//#endregion
|
|
4661
|
+
//#region node_modules/.pnpm/@noble+curves@2.0.1/node_modules/@noble/curves/abstract/weierstrass.js
|
|
4645
4662
|
/**
|
|
4646
|
-
*
|
|
4663
|
+
* Short Weierstrass curve methods. The formula is: y² = x³ + ax + b.
|
|
4664
|
+
*
|
|
4665
|
+
* ### Design rationale for types
|
|
4666
|
+
*
|
|
4667
|
+
* * Interaction between classes from different curves should fail:
|
|
4668
|
+
* `k256.Point.BASE.add(p256.Point.BASE)`
|
|
4669
|
+
* * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
|
|
4670
|
+
* * Different calls of `curve()` would return different classes -
|
|
4671
|
+
* `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
|
|
4672
|
+
* it won't affect others
|
|
4673
|
+
*
|
|
4674
|
+
* TypeScript can't infer types for classes created inside a function. Classes is one instance
|
|
4675
|
+
* of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
|
|
4676
|
+
* unique type for every function call.
|
|
4677
|
+
*
|
|
4678
|
+
* We can use generic types via some param, like curve opts, but that would:
|
|
4679
|
+
* 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
|
|
4680
|
+
* which is hard to debug.
|
|
4681
|
+
* 2. Params can be generic and we can't enforce them to be constant value:
|
|
4682
|
+
* if somebody creates curve from non-constant params,
|
|
4683
|
+
* it would be allowed to interact with other curves with non-constant params
|
|
4684
|
+
*
|
|
4685
|
+
* @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
|
|
4686
|
+
* @module
|
|
4647
4687
|
*/
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
return {
|
|
4651
|
-
encode: (from) => {
|
|
4652
|
-
astrArr("join.decode", from);
|
|
4653
|
-
return from.join(separator);
|
|
4654
|
-
},
|
|
4655
|
-
decode: (to) => {
|
|
4656
|
-
astr("join.decode", to);
|
|
4657
|
-
return to.split(separator);
|
|
4658
|
-
}
|
|
4659
|
-
};
|
|
4660
|
-
}
|
|
4688
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
4689
|
+
const divNearest = (num, den) => (num + (num >= 0 ? den : -den) / _2n$2) / den;
|
|
4661
4690
|
/**
|
|
4662
|
-
*
|
|
4663
|
-
* @__NO_SIDE_EFFECTS__
|
|
4691
|
+
* Splits scalar for GLV endomorphism.
|
|
4664
4692
|
*/
|
|
4665
|
-
function
|
|
4666
|
-
|
|
4667
|
-
|
|
4693
|
+
function _splitEndoScalar(k, basis, n) {
|
|
4694
|
+
const [[a1, b1], [a2, b2]] = basis;
|
|
4695
|
+
const c1 = divNearest(b2 * k, n);
|
|
4696
|
+
const c2 = divNearest(-b1 * k, n);
|
|
4697
|
+
let k1 = k - c1 * a1 - c2 * a2;
|
|
4698
|
+
let k2 = -c1 * b1 - c2 * b2;
|
|
4699
|
+
const k1neg = k1 < _0n$1;
|
|
4700
|
+
const k2neg = k2 < _0n$1;
|
|
4701
|
+
if (k1neg) k1 = -k1;
|
|
4702
|
+
if (k2neg) k2 = -k2;
|
|
4703
|
+
const MAX_NUM = bitMask(Math.ceil(bitLen(n) / 2)) + _1n$1;
|
|
4704
|
+
if (k1 < _0n$1 || k1 >= MAX_NUM || k2 < _0n$1 || k2 >= MAX_NUM) throw new Error("splitScalar (endomorphism): failed, k=" + k);
|
|
4668
4705
|
return {
|
|
4669
|
-
|
|
4670
|
-
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
},
|
|
4674
|
-
decode(input) {
|
|
4675
|
-
astrArr("padding.decode", input);
|
|
4676
|
-
let end = input.length;
|
|
4677
|
-
if (end * bits % 8) throw new Error("padding: invalid, string should have whole number of bytes");
|
|
4678
|
-
for (; end > 0 && input[end - 1] === chr; end--) if ((end - 1) * bits % 8 === 0) throw new Error("padding: invalid, string has too much padding");
|
|
4679
|
-
return input.slice(0, end);
|
|
4680
|
-
}
|
|
4706
|
+
k1neg,
|
|
4707
|
+
k1,
|
|
4708
|
+
k2neg,
|
|
4709
|
+
k2
|
|
4681
4710
|
};
|
|
4682
4711
|
}
|
|
4683
|
-
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4712
|
+
function validateSigFormat(format) {
|
|
4713
|
+
if (![
|
|
4714
|
+
"compact",
|
|
4715
|
+
"recovered",
|
|
4716
|
+
"der"
|
|
4717
|
+
].includes(format)) throw new Error("Signature format must be \"compact\", \"recovered\", or \"der\"");
|
|
4718
|
+
return format;
|
|
4719
|
+
}
|
|
4720
|
+
function validateSigOpts(opts, def) {
|
|
4721
|
+
const optsn = {};
|
|
4722
|
+
for (let optName of Object.keys(def)) optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
|
|
4723
|
+
abool(optsn.lowS, "lowS");
|
|
4724
|
+
abool(optsn.prehash, "prehash");
|
|
4725
|
+
if (optsn.format !== void 0) validateSigFormat(optsn.format);
|
|
4726
|
+
return optsn;
|
|
4692
4727
|
}
|
|
4728
|
+
var DERErr = class extends Error {
|
|
4729
|
+
constructor(m = "") {
|
|
4730
|
+
super(m);
|
|
4731
|
+
}
|
|
4732
|
+
};
|
|
4693
4733
|
/**
|
|
4694
|
-
*
|
|
4734
|
+
* ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
|
|
4735
|
+
*
|
|
4736
|
+
* [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
|
|
4737
|
+
*
|
|
4738
|
+
* Docs: https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/, https://luca.ntop.org/Teaching/Appunti/asn1.html
|
|
4695
4739
|
*/
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
const dlen = digits.length;
|
|
4709
|
-
while (true) {
|
|
4710
|
-
let carry = 0;
|
|
4711
|
-
let done = true;
|
|
4712
|
-
for (let i = pos; i < dlen; i++) {
|
|
4713
|
-
const digit = digits[i];
|
|
4714
|
-
const fromCarry = from * carry;
|
|
4715
|
-
const digitBase = fromCarry + digit;
|
|
4716
|
-
if (!Number.isSafeInteger(digitBase) || fromCarry / from !== carry || digitBase - digit !== fromCarry) throw new Error("convertRadix: carry overflow");
|
|
4717
|
-
const div = digitBase / to;
|
|
4718
|
-
carry = digitBase % to;
|
|
4719
|
-
const rounded = Math.floor(div);
|
|
4720
|
-
digits[i] = rounded;
|
|
4721
|
-
if (!Number.isSafeInteger(rounded) || rounded * to + carry !== digitBase) throw new Error("convertRadix: carry overflow");
|
|
4722
|
-
if (!done) continue;
|
|
4723
|
-
else if (!rounded) pos = i;
|
|
4724
|
-
else done = false;
|
|
4725
|
-
}
|
|
4726
|
-
res.push(carry);
|
|
4727
|
-
if (done) break;
|
|
4728
|
-
}
|
|
4729
|
-
for (let i = 0; i < data.length - 1 && data[i] === 0; i++) res.push(0);
|
|
4730
|
-
return res.reverse();
|
|
4731
|
-
}
|
|
4732
|
-
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
|
|
4733
|
-
const radix2carry = /* @__NO_SIDE_EFFECTS__ */ (from, to) => from + (to - gcd(from, to));
|
|
4734
|
-
const powers = /* @__PURE__ */ (() => {
|
|
4735
|
-
let res = [];
|
|
4736
|
-
for (let i = 0; i < 40; i++) res.push(2 ** i);
|
|
4737
|
-
return res;
|
|
4738
|
-
})();
|
|
4739
|
-
/**
|
|
4740
|
-
* Implemented with numbers, because BigInt is 5x slower
|
|
4741
|
-
*/
|
|
4742
|
-
function convertRadix2(data, from, to, padding) {
|
|
4743
|
-
aArr(data);
|
|
4744
|
-
if (from <= 0 || from > 32) throw new Error(`convertRadix2: wrong from=${from}`);
|
|
4745
|
-
if (to <= 0 || to > 32) throw new Error(`convertRadix2: wrong to=${to}`);
|
|
4746
|
-
if (/* @__PURE__ */ radix2carry(from, to) > 32) throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${/* @__PURE__ */ radix2carry(from, to)}`);
|
|
4747
|
-
let carry = 0;
|
|
4748
|
-
let pos = 0;
|
|
4749
|
-
const max = powers[from];
|
|
4750
|
-
const mask = powers[to] - 1;
|
|
4751
|
-
const res = [];
|
|
4752
|
-
for (const n of data) {
|
|
4753
|
-
anumber(n);
|
|
4754
|
-
if (n >= max) throw new Error(`convertRadix2: invalid data word=${n} from=${from}`);
|
|
4755
|
-
carry = carry << from | n;
|
|
4756
|
-
if (pos + from > 32) throw new Error(`convertRadix2: carry overflow pos=${pos} from=${from}`);
|
|
4757
|
-
pos += from;
|
|
4758
|
-
for (; pos >= to; pos -= to) res.push((carry >> pos - to & mask) >>> 0);
|
|
4759
|
-
const pow = powers[pos];
|
|
4760
|
-
if (pow === void 0) throw new Error("invalid carry");
|
|
4761
|
-
carry &= pow - 1;
|
|
4762
|
-
}
|
|
4763
|
-
carry = carry << to - pos & mask;
|
|
4764
|
-
if (!padding && pos >= from) throw new Error("Excess padding");
|
|
4765
|
-
if (!padding && carry > 0) throw new Error(`Non-zero padding: ${carry}`);
|
|
4766
|
-
if (padding && pos > 0) res.push(carry >>> 0);
|
|
4767
|
-
return res;
|
|
4768
|
-
}
|
|
4769
|
-
/**
|
|
4770
|
-
* @__NO_SIDE_EFFECTS__
|
|
4771
|
-
*/
|
|
4772
|
-
function radix(num) {
|
|
4773
|
-
anumber(num);
|
|
4774
|
-
const _256 = 2 ** 8;
|
|
4775
|
-
return {
|
|
4776
|
-
encode: (bytes) => {
|
|
4777
|
-
if (!isBytes(bytes)) throw new Error("radix.encode input should be Uint8Array");
|
|
4778
|
-
return convertRadix(Array.from(bytes), _256, num);
|
|
4779
|
-
},
|
|
4780
|
-
decode: (digits) => {
|
|
4781
|
-
anumArr("radix.decode", digits);
|
|
4782
|
-
return Uint8Array.from(convertRadix(digits, num, _256));
|
|
4783
|
-
}
|
|
4784
|
-
};
|
|
4785
|
-
}
|
|
4786
|
-
/**
|
|
4787
|
-
* If both bases are power of same number (like `2**8 <-> 2**64`),
|
|
4788
|
-
* there is a linear algorithm. For now we have implementation for power-of-two bases only.
|
|
4789
|
-
* @__NO_SIDE_EFFECTS__
|
|
4790
|
-
*/
|
|
4791
|
-
function radix2(bits, revPadding = false) {
|
|
4792
|
-
anumber(bits);
|
|
4793
|
-
if (bits <= 0 || bits > 32) throw new Error("radix2: bits should be in (0..32]");
|
|
4794
|
-
if (/* @__PURE__ */ radix2carry(8, bits) > 32 || /* @__PURE__ */ radix2carry(bits, 8) > 32) throw new Error("radix2: carry overflow");
|
|
4795
|
-
return {
|
|
4796
|
-
encode: (bytes) => {
|
|
4797
|
-
if (!isBytes(bytes)) throw new Error("radix2.encode input should be Uint8Array");
|
|
4798
|
-
return convertRadix2(Array.from(bytes), 8, bits, !revPadding);
|
|
4740
|
+
const DER = {
|
|
4741
|
+
Err: DERErr,
|
|
4742
|
+
_tlv: {
|
|
4743
|
+
encode: (tag, data) => {
|
|
4744
|
+
const { Err: E } = DER;
|
|
4745
|
+
if (tag < 0 || tag > 256) throw new E("tlv.encode: wrong tag");
|
|
4746
|
+
if (data.length & 1) throw new E("tlv.encode: unpadded data");
|
|
4747
|
+
const dataLen = data.length / 2;
|
|
4748
|
+
const len = numberToHexUnpadded(dataLen);
|
|
4749
|
+
if (len.length / 2 & 128) throw new E("tlv.encode: long form length too big");
|
|
4750
|
+
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
4751
|
+
return numberToHexUnpadded(tag) + lenLen + len + data;
|
|
4799
4752
|
},
|
|
4800
|
-
decode
|
|
4801
|
-
|
|
4802
|
-
|
|
4753
|
+
decode(tag, data) {
|
|
4754
|
+
const { Err: E } = DER;
|
|
4755
|
+
let pos = 0;
|
|
4756
|
+
if (tag < 0 || tag > 256) throw new E("tlv.encode: wrong tag");
|
|
4757
|
+
if (data.length < 2 || data[pos++] !== tag) throw new E("tlv.decode: wrong tlv");
|
|
4758
|
+
const first = data[pos++];
|
|
4759
|
+
const isLong = !!(first & 128);
|
|
4760
|
+
let length = 0;
|
|
4761
|
+
if (!isLong) length = first;
|
|
4762
|
+
else {
|
|
4763
|
+
const lenLen = first & 127;
|
|
4764
|
+
if (!lenLen) throw new E("tlv.decode(long): indefinite length not supported");
|
|
4765
|
+
if (lenLen > 4) throw new E("tlv.decode(long): byte length is too big");
|
|
4766
|
+
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
4767
|
+
if (lengthBytes.length !== lenLen) throw new E("tlv.decode: length bytes not complete");
|
|
4768
|
+
if (lengthBytes[0] === 0) throw new E("tlv.decode(long): zero leftmost byte");
|
|
4769
|
+
for (const b of lengthBytes) length = length << 8 | b;
|
|
4770
|
+
pos += lenLen;
|
|
4771
|
+
if (length < 128) throw new E("tlv.decode(long): not minimal encoding");
|
|
4772
|
+
}
|
|
4773
|
+
const v = data.subarray(pos, pos + length);
|
|
4774
|
+
if (v.length !== length) throw new E("tlv.decode: wrong value length");
|
|
4775
|
+
return {
|
|
4776
|
+
v,
|
|
4777
|
+
l: data.subarray(pos + length)
|
|
4778
|
+
};
|
|
4803
4779
|
}
|
|
4804
|
-
}
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
}
|
|
4814
|
-
function checksum(len, fn) {
|
|
4815
|
-
anumber(len);
|
|
4816
|
-
afn(fn);
|
|
4817
|
-
return {
|
|
4818
|
-
encode(data) {
|
|
4819
|
-
if (!isBytes(data)) throw new Error("checksum.encode: input should be Uint8Array");
|
|
4820
|
-
const sum = fn(data).slice(0, len);
|
|
4821
|
-
const res = new Uint8Array(data.length + len);
|
|
4822
|
-
res.set(data);
|
|
4823
|
-
res.set(sum, data.length);
|
|
4824
|
-
return res;
|
|
4780
|
+
},
|
|
4781
|
+
_int: {
|
|
4782
|
+
encode(num) {
|
|
4783
|
+
const { Err: E } = DER;
|
|
4784
|
+
if (num < _0n$1) throw new E("integer: negative integers are not allowed");
|
|
4785
|
+
let hex = numberToHexUnpadded(num);
|
|
4786
|
+
if (Number.parseInt(hex[0], 16) & 8) hex = "00" + hex;
|
|
4787
|
+
if (hex.length & 1) throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
4788
|
+
return hex;
|
|
4825
4789
|
},
|
|
4826
4790
|
decode(data) {
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
|
|
4831
|
-
for (let i = 0; i < len; i++) if (newChecksum[i] !== oldChecksum[i]) throw new Error("Invalid checksum");
|
|
4832
|
-
return payload;
|
|
4791
|
+
const { Err: E } = DER;
|
|
4792
|
+
if (data[0] & 128) throw new E("invalid signature integer: negative");
|
|
4793
|
+
if (data[0] === 0 && !(data[1] & 128)) throw new E("invalid signature integer: unnecessary leading zero");
|
|
4794
|
+
return bytesToNumberBE(data);
|
|
4833
4795
|
}
|
|
4834
|
-
}
|
|
4835
|
-
|
|
4836
|
-
const
|
|
4837
|
-
|
|
4838
|
-
|
|
4839
|
-
|
|
4840
|
-
|
|
4841
|
-
|
|
4842
|
-
|
|
4843
|
-
|
|
4844
|
-
|
|
4845
|
-
|
|
4796
|
+
},
|
|
4797
|
+
toSig(bytes) {
|
|
4798
|
+
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
4799
|
+
const data = abytes$1(bytes, void 0, "signature");
|
|
4800
|
+
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
4801
|
+
if (seqLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
4802
|
+
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
4803
|
+
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
4804
|
+
if (sLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
4805
|
+
return {
|
|
4806
|
+
r: int.decode(rBytes),
|
|
4807
|
+
s: int.decode(sBytes)
|
|
4808
|
+
};
|
|
4809
|
+
},
|
|
4810
|
+
hexFromSig(sig) {
|
|
4811
|
+
const { _tlv: tlv, _int: int } = DER;
|
|
4812
|
+
const seq = tlv.encode(2, int.encode(sig.r)) + tlv.encode(2, int.encode(sig.s));
|
|
4813
|
+
return tlv.encode(48, seq);
|
|
4814
|
+
}
|
|
4846
4815
|
};
|
|
4847
|
-
|
|
4848
|
-
chain(radix2(5), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"), padding(5), join(""));
|
|
4849
|
-
chain(radix2(5), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"), join(""));
|
|
4850
|
-
chain(radix2(5), alphabet("0123456789ABCDEFGHIJKLMNOPQRSTUV"), padding(5), join(""));
|
|
4851
|
-
chain(radix2(5), alphabet("0123456789ABCDEFGHIJKLMNOPQRSTUV"), join(""));
|
|
4852
|
-
chain(radix2(5), alphabet("0123456789ABCDEFGHJKMNPQRSTVWXYZ"), join(""), normalize$1((s) => s.toUpperCase().replace(/O/g, "0").replace(/[IL]/g, "1")));
|
|
4853
|
-
const hasBase64Builtin = typeof Uint8Array.from([]).toBase64 === "function" && typeof Uint8Array.fromBase64 === "function";
|
|
4854
|
-
hasBase64Builtin || chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), padding(6), join(""));
|
|
4855
|
-
chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), join(""));
|
|
4856
|
-
hasBase64Builtin || chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), padding(6), join(""));
|
|
4857
|
-
chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), join(""));
|
|
4858
|
-
const genBase58 = /* @__NO_SIDE_EFFECTS__ */ (abc) => chain(radix(58), alphabet(abc), join(""));
|
|
4816
|
+
const _0n$1 = BigInt(0), _1n$1 = BigInt(1), _2n$2 = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
|
|
4859
4817
|
/**
|
|
4860
|
-
*
|
|
4861
|
-
*
|
|
4818
|
+
* Creates weierstrass Point constructor, based on specified curve options.
|
|
4819
|
+
*
|
|
4820
|
+
* See {@link WeierstrassOpts}.
|
|
4821
|
+
*
|
|
4862
4822
|
* @example
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4823
|
+
```js
|
|
4824
|
+
const opts = {
|
|
4825
|
+
p: 0xfffffffffffffffffffffffffffffffeffffac73n,
|
|
4826
|
+
n: 0x100000000000000000001b8fa16dfab9aca16b6b3n,
|
|
4827
|
+
h: 1n,
|
|
4828
|
+
a: 0n,
|
|
4829
|
+
b: 7n,
|
|
4830
|
+
Gx: 0x3b4c382ce37aa192a4019e763036f4f5dd4d7ebbn,
|
|
4831
|
+
Gy: 0x938cf935318fdced6bc28286531733c3f03c4feen,
|
|
4832
|
+
};
|
|
4833
|
+
const secp160k1_Point = weierstrass(opts);
|
|
4834
|
+
```
|
|
4872
4835
|
*/
|
|
4873
|
-
|
|
4874
|
-
const
|
|
4875
|
-
const
|
|
4876
|
-
|
|
4877
|
-
|
|
4878
|
-
|
|
4879
|
-
|
|
4880
|
-
|
|
4881
|
-
|
|
4882
|
-
function
|
|
4883
|
-
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
const len = prefix.length;
|
|
4890
|
-
let chk = 1;
|
|
4891
|
-
for (let i = 0; i < len; i++) {
|
|
4892
|
-
const c = prefix.charCodeAt(i);
|
|
4893
|
-
if (c < 33 || c > 126) throw new Error(`Invalid prefix (${prefix})`);
|
|
4894
|
-
chk = bech32Polymod(chk) ^ c >> 5;
|
|
4836
|
+
function weierstrass(params, extraOpts = {}) {
|
|
4837
|
+
const validated = createCurveFields("weierstrass", params, extraOpts);
|
|
4838
|
+
const { Fp, Fn } = validated;
|
|
4839
|
+
let CURVE = validated.CURVE;
|
|
4840
|
+
const { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
4841
|
+
validateObject(extraOpts, {}, {
|
|
4842
|
+
allowInfinityPoint: "boolean",
|
|
4843
|
+
clearCofactor: "function",
|
|
4844
|
+
isTorsionFree: "function",
|
|
4845
|
+
fromBytes: "function",
|
|
4846
|
+
toBytes: "function",
|
|
4847
|
+
endo: "object"
|
|
4848
|
+
});
|
|
4849
|
+
const { endo } = extraOpts;
|
|
4850
|
+
if (endo) {
|
|
4851
|
+
if (!Fp.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) throw new Error("invalid endo: expected \"beta\": bigint and \"basises\": array");
|
|
4895
4852
|
}
|
|
4896
|
-
|
|
4897
|
-
|
|
4898
|
-
|
|
4899
|
-
for (let i = 0; i < 6; i++) chk = bech32Polymod(chk);
|
|
4900
|
-
chk ^= encodingConst;
|
|
4901
|
-
return BECH_ALPHABET.encode(convertRadix2([chk % powers[30]], 30, 5, false));
|
|
4902
|
-
}
|
|
4903
|
-
/**
|
|
4904
|
-
* @__NO_SIDE_EFFECTS__
|
|
4905
|
-
*/
|
|
4906
|
-
function genBech32(encoding) {
|
|
4907
|
-
const ENCODING_CONST = encoding === "bech32" ? 1 : 734539939;
|
|
4908
|
-
const _words = radix2(5);
|
|
4909
|
-
const fromWords = _words.decode;
|
|
4910
|
-
const toWords = _words.encode;
|
|
4911
|
-
const fromWordsUnsafe = unsafeWrapper(fromWords);
|
|
4912
|
-
function encode(prefix, words, limit = 90) {
|
|
4913
|
-
astr("bech32.encode prefix", prefix);
|
|
4914
|
-
if (isBytes(words)) words = Array.from(words);
|
|
4915
|
-
anumArr("bech32.encode", words);
|
|
4916
|
-
const plen = prefix.length;
|
|
4917
|
-
if (plen === 0) throw new TypeError(`Invalid prefix length ${plen}`);
|
|
4918
|
-
const actualLength = plen + 7 + words.length;
|
|
4919
|
-
if (limit !== false && actualLength > limit) throw new TypeError(`Length ${actualLength} exceeds limit ${limit}`);
|
|
4920
|
-
const lowered = prefix.toLowerCase();
|
|
4921
|
-
const sum = bechChecksum(lowered, words, ENCODING_CONST);
|
|
4922
|
-
return `${lowered}1${BECH_ALPHABET.encode(words)}${sum}`;
|
|
4853
|
+
const lengths = getWLengths(Fp, Fn);
|
|
4854
|
+
function assertCompressionIsSupported() {
|
|
4855
|
+
if (!Fp.isOdd) throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
4923
4856
|
}
|
|
4924
|
-
function
|
|
4925
|
-
|
|
4926
|
-
const
|
|
4927
|
-
|
|
4928
|
-
|
|
4929
|
-
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
const prefix = lowered.slice(0, sepIndex);
|
|
4933
|
-
const data = lowered.slice(sepIndex + 1);
|
|
4934
|
-
if (data.length < 6) throw new Error("Data must be at least 6 characters long");
|
|
4935
|
-
const words = BECH_ALPHABET.decode(data).slice(0, -6);
|
|
4936
|
-
const sum = bechChecksum(prefix, words, ENCODING_CONST);
|
|
4937
|
-
if (!data.endsWith(sum)) throw new Error(`Invalid checksum in ${str}: expected "${sum}"`);
|
|
4938
|
-
return {
|
|
4939
|
-
prefix,
|
|
4940
|
-
words
|
|
4941
|
-
};
|
|
4857
|
+
function pointToBytes(_c, point, isCompressed) {
|
|
4858
|
+
const { x, y } = point.toAffine();
|
|
4859
|
+
const bx = Fp.toBytes(x);
|
|
4860
|
+
abool(isCompressed, "isCompressed");
|
|
4861
|
+
if (isCompressed) {
|
|
4862
|
+
assertCompressionIsSupported();
|
|
4863
|
+
return concatBytes(pprefix(!Fp.isOdd(y)), bx);
|
|
4864
|
+
} else return concatBytes(Uint8Array.of(4), bx, Fp.toBytes(y));
|
|
4942
4865
|
}
|
|
4943
|
-
|
|
4944
|
-
|
|
4945
|
-
const {
|
|
4946
|
-
|
|
4947
|
-
|
|
4948
|
-
|
|
4949
|
-
|
|
4950
|
-
|
|
4866
|
+
function pointFromBytes(bytes) {
|
|
4867
|
+
abytes$1(bytes, void 0, "Point");
|
|
4868
|
+
const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
|
|
4869
|
+
const length = bytes.length;
|
|
4870
|
+
const head = bytes[0];
|
|
4871
|
+
const tail = bytes.subarray(1);
|
|
4872
|
+
if (length === comp && (head === 2 || head === 3)) {
|
|
4873
|
+
const x = Fp.fromBytes(tail);
|
|
4874
|
+
if (!Fp.isValid(x)) throw new Error("bad point: is not on curve, wrong x");
|
|
4875
|
+
const y2 = weierstrassEquation(x);
|
|
4876
|
+
let y;
|
|
4877
|
+
try {
|
|
4878
|
+
y = Fp.sqrt(y2);
|
|
4879
|
+
} catch (sqrtError) {
|
|
4880
|
+
const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
4881
|
+
throw new Error("bad point: is not on curve, sqrt error" + err);
|
|
4882
|
+
}
|
|
4883
|
+
assertCompressionIsSupported();
|
|
4884
|
+
const evenY = Fp.isOdd(y);
|
|
4885
|
+
if ((head & 1) === 1 !== evenY) y = Fp.neg(y);
|
|
4886
|
+
return {
|
|
4887
|
+
x,
|
|
4888
|
+
y
|
|
4889
|
+
};
|
|
4890
|
+
} else if (length === uncomp && head === 4) {
|
|
4891
|
+
const L = Fp.BYTES;
|
|
4892
|
+
const x = Fp.fromBytes(tail.subarray(0, L));
|
|
4893
|
+
const y = Fp.fromBytes(tail.subarray(L, L * 2));
|
|
4894
|
+
if (!isValidXY(x, y)) throw new Error("bad point: is not on curve");
|
|
4895
|
+
return {
|
|
4896
|
+
x,
|
|
4897
|
+
y
|
|
4898
|
+
};
|
|
4899
|
+
} else throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
4951
4900
|
}
|
|
4952
|
-
|
|
4953
|
-
|
|
4901
|
+
const encodePoint = extraOpts.toBytes || pointToBytes;
|
|
4902
|
+
const decodePoint = extraOpts.fromBytes || pointFromBytes;
|
|
4903
|
+
function weierstrassEquation(x) {
|
|
4904
|
+
const x2 = Fp.sqr(x);
|
|
4905
|
+
const x3 = Fp.mul(x2, x);
|
|
4906
|
+
return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b);
|
|
4954
4907
|
}
|
|
4955
|
-
|
|
4956
|
-
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
decodeUnsafe,
|
|
4961
|
-
fromWords,
|
|
4962
|
-
fromWordsUnsafe,
|
|
4963
|
-
toWords
|
|
4964
|
-
};
|
|
4965
|
-
}
|
|
4966
|
-
genBech32("bech32");
|
|
4967
|
-
genBech32("bech32m");
|
|
4968
|
-
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function" || chain(radix2(4), alphabet("0123456789abcdef"), join(""), normalize$1((s) => {
|
|
4969
|
-
if (typeof s !== "string" || s.length % 2 !== 0) throw new TypeError(`hex.decode: expected string, got ${typeof s} with length ${s.length}`);
|
|
4970
|
-
return s.toLowerCase();
|
|
4971
|
-
}));
|
|
4972
|
-
//#endregion
|
|
4973
|
-
//#region node_modules/.pnpm/@noble+hashes@2.0.1/node_modules/@noble/hashes/hmac.js
|
|
4974
|
-
/**
|
|
4975
|
-
* HMAC: RFC2104 message authentication code.
|
|
4976
|
-
* @module
|
|
4977
|
-
*/
|
|
4978
|
-
/** Internal class for HMAC. */
|
|
4979
|
-
var _HMAC = class {
|
|
4980
|
-
oHash;
|
|
4981
|
-
iHash;
|
|
4982
|
-
blockLen;
|
|
4983
|
-
outputLen;
|
|
4984
|
-
finished = false;
|
|
4985
|
-
destroyed = false;
|
|
4986
|
-
constructor(hash, key) {
|
|
4987
|
-
ahash(hash);
|
|
4988
|
-
abytes$1(key, void 0, "key");
|
|
4989
|
-
this.iHash = hash.create();
|
|
4990
|
-
if (typeof this.iHash.update !== "function") throw new Error("Expected instance of class which extends utils.Hash");
|
|
4991
|
-
this.blockLen = this.iHash.blockLen;
|
|
4992
|
-
this.outputLen = this.iHash.outputLen;
|
|
4993
|
-
const blockLen = this.blockLen;
|
|
4994
|
-
const pad = new Uint8Array(blockLen);
|
|
4995
|
-
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
4996
|
-
for (let i = 0; i < pad.length; i++) pad[i] ^= 54;
|
|
4997
|
-
this.iHash.update(pad);
|
|
4998
|
-
this.oHash = hash.create();
|
|
4999
|
-
for (let i = 0; i < pad.length; i++) pad[i] ^= 106;
|
|
5000
|
-
this.oHash.update(pad);
|
|
5001
|
-
clean(pad);
|
|
4908
|
+
/** Checks whether equation holds for given x, y: y² == x³ + ax + b */
|
|
4909
|
+
function isValidXY(x, y) {
|
|
4910
|
+
const left = Fp.sqr(y);
|
|
4911
|
+
const right = weierstrassEquation(x);
|
|
4912
|
+
return Fp.eql(left, right);
|
|
5002
4913
|
}
|
|
5003
|
-
|
|
5004
|
-
|
|
5005
|
-
|
|
5006
|
-
|
|
4914
|
+
if (!isValidXY(CURVE.Gx, CURVE.Gy)) throw new Error("bad curve params: generator point");
|
|
4915
|
+
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n), _4n);
|
|
4916
|
+
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
4917
|
+
if (Fp.is0(Fp.add(_4a3, _27b2))) throw new Error("bad curve params: a or b");
|
|
4918
|
+
/** Asserts coordinate is valid: 0 <= n < Fp.ORDER. */
|
|
4919
|
+
function acoord(title, n, banZero = false) {
|
|
4920
|
+
if (!Fp.isValid(n) || banZero && Fp.is0(n)) throw new Error(`bad point coordinate ${title}`);
|
|
4921
|
+
return n;
|
|
5007
4922
|
}
|
|
5008
|
-
|
|
5009
|
-
|
|
5010
|
-
abytes$1(out, this.outputLen, "output");
|
|
5011
|
-
this.finished = true;
|
|
5012
|
-
this.iHash.digestInto(out);
|
|
5013
|
-
this.oHash.update(out);
|
|
5014
|
-
this.oHash.digestInto(out);
|
|
5015
|
-
this.destroy();
|
|
4923
|
+
function aprjpoint(other) {
|
|
4924
|
+
if (!(other instanceof Point)) throw new Error("Weierstrass Point expected");
|
|
5016
4925
|
}
|
|
5017
|
-
|
|
5018
|
-
|
|
5019
|
-
|
|
5020
|
-
return out;
|
|
4926
|
+
function splitEndoScalarN(k) {
|
|
4927
|
+
if (!endo || !endo.basises) throw new Error("no endo");
|
|
4928
|
+
return _splitEndoScalar(k, endo.basises, Fn.ORDER);
|
|
5021
4929
|
}
|
|
5022
|
-
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
|
|
5032
|
-
|
|
5033
|
-
|
|
5034
|
-
|
|
5035
|
-
|
|
5036
|
-
|
|
5037
|
-
|
|
5038
|
-
|
|
5039
|
-
|
|
5040
|
-
|
|
5041
|
-
|
|
5042
|
-
};
|
|
5043
|
-
|
|
5044
|
-
|
|
5045
|
-
|
|
5046
|
-
|
|
5047
|
-
|
|
5048
|
-
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
*
|
|
5060
|
-
* ### Design rationale for types
|
|
5061
|
-
*
|
|
5062
|
-
* * Interaction between classes from different curves should fail:
|
|
5063
|
-
* `k256.Point.BASE.add(p256.Point.BASE)`
|
|
5064
|
-
* * For this purpose we want to use `instanceof` operator, which is fast and works during runtime
|
|
5065
|
-
* * Different calls of `curve()` would return different classes -
|
|
5066
|
-
* `curve(params) !== curve(params)`: if somebody decided to monkey-patch their curve,
|
|
5067
|
-
* it won't affect others
|
|
5068
|
-
*
|
|
5069
|
-
* TypeScript can't infer types for classes created inside a function. Classes is one instance
|
|
5070
|
-
* of nominative types in TypeScript and interfaces only check for shape, so it's hard to create
|
|
5071
|
-
* unique type for every function call.
|
|
5072
|
-
*
|
|
5073
|
-
* We can use generic types via some param, like curve opts, but that would:
|
|
5074
|
-
* 1. Enable interaction between `curve(params)` and `curve(params)` (curves of same params)
|
|
5075
|
-
* which is hard to debug.
|
|
5076
|
-
* 2. Params can be generic and we can't enforce them to be constant value:
|
|
5077
|
-
* if somebody creates curve from non-constant params,
|
|
5078
|
-
* it would be allowed to interact with other curves with non-constant params
|
|
5079
|
-
*
|
|
5080
|
-
* @todo https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#unique-symbol
|
|
5081
|
-
* @module
|
|
5082
|
-
*/
|
|
5083
|
-
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
5084
|
-
const divNearest = (num, den) => (num + (num >= 0 ? den : -den) / _2n$2) / den;
|
|
5085
|
-
/**
|
|
5086
|
-
* Splits scalar for GLV endomorphism.
|
|
5087
|
-
*/
|
|
5088
|
-
function _splitEndoScalar(k, basis, n) {
|
|
5089
|
-
const [[a1, b1], [a2, b2]] = basis;
|
|
5090
|
-
const c1 = divNearest(b2 * k, n);
|
|
5091
|
-
const c2 = divNearest(-b1 * k, n);
|
|
5092
|
-
let k1 = k - c1 * a1 - c2 * a2;
|
|
5093
|
-
let k2 = -c1 * b1 - c2 * b2;
|
|
5094
|
-
const k1neg = k1 < _0n$1;
|
|
5095
|
-
const k2neg = k2 < _0n$1;
|
|
5096
|
-
if (k1neg) k1 = -k1;
|
|
5097
|
-
if (k2neg) k2 = -k2;
|
|
5098
|
-
const MAX_NUM = bitMask(Math.ceil(bitLen(n) / 2)) + _1n$1;
|
|
5099
|
-
if (k1 < _0n$1 || k1 >= MAX_NUM || k2 < _0n$1 || k2 >= MAX_NUM) throw new Error("splitScalar (endomorphism): failed, k=" + k);
|
|
5100
|
-
return {
|
|
5101
|
-
k1neg,
|
|
5102
|
-
k1,
|
|
5103
|
-
k2neg,
|
|
5104
|
-
k2
|
|
5105
|
-
};
|
|
5106
|
-
}
|
|
5107
|
-
function validateSigFormat(format) {
|
|
5108
|
-
if (![
|
|
5109
|
-
"compact",
|
|
5110
|
-
"recovered",
|
|
5111
|
-
"der"
|
|
5112
|
-
].includes(format)) throw new Error("Signature format must be \"compact\", \"recovered\", or \"der\"");
|
|
5113
|
-
return format;
|
|
5114
|
-
}
|
|
5115
|
-
function validateSigOpts(opts, def) {
|
|
5116
|
-
const optsn = {};
|
|
5117
|
-
for (let optName of Object.keys(def)) optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
|
|
5118
|
-
abool(optsn.lowS, "lowS");
|
|
5119
|
-
abool(optsn.prehash, "prehash");
|
|
5120
|
-
if (optsn.format !== void 0) validateSigFormat(optsn.format);
|
|
5121
|
-
return optsn;
|
|
5122
|
-
}
|
|
5123
|
-
var DERErr = class extends Error {
|
|
5124
|
-
constructor(m = "") {
|
|
5125
|
-
super(m);
|
|
5126
|
-
}
|
|
5127
|
-
};
|
|
5128
|
-
/**
|
|
5129
|
-
* ASN.1 DER encoding utilities. ASN is very complex & fragile. Format:
|
|
5130
|
-
*
|
|
5131
|
-
* [0x30 (SEQUENCE), bytelength, 0x02 (INTEGER), intLength, R, 0x02 (INTEGER), intLength, S]
|
|
5132
|
-
*
|
|
5133
|
-
* Docs: https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/, https://luca.ntop.org/Teaching/Appunti/asn1.html
|
|
5134
|
-
*/
|
|
5135
|
-
const DER = {
|
|
5136
|
-
Err: DERErr,
|
|
5137
|
-
_tlv: {
|
|
5138
|
-
encode: (tag, data) => {
|
|
5139
|
-
const { Err: E } = DER;
|
|
5140
|
-
if (tag < 0 || tag > 256) throw new E("tlv.encode: wrong tag");
|
|
5141
|
-
if (data.length & 1) throw new E("tlv.encode: unpadded data");
|
|
5142
|
-
const dataLen = data.length / 2;
|
|
5143
|
-
const len = numberToHexUnpadded(dataLen);
|
|
5144
|
-
if (len.length / 2 & 128) throw new E("tlv.encode: long form length too big");
|
|
5145
|
-
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
5146
|
-
return numberToHexUnpadded(tag) + lenLen + len + data;
|
|
5147
|
-
},
|
|
5148
|
-
decode(tag, data) {
|
|
5149
|
-
const { Err: E } = DER;
|
|
5150
|
-
let pos = 0;
|
|
5151
|
-
if (tag < 0 || tag > 256) throw new E("tlv.encode: wrong tag");
|
|
5152
|
-
if (data.length < 2 || data[pos++] !== tag) throw new E("tlv.decode: wrong tlv");
|
|
5153
|
-
const first = data[pos++];
|
|
5154
|
-
const isLong = !!(first & 128);
|
|
5155
|
-
let length = 0;
|
|
5156
|
-
if (!isLong) length = first;
|
|
5157
|
-
else {
|
|
5158
|
-
const lenLen = first & 127;
|
|
5159
|
-
if (!lenLen) throw new E("tlv.decode(long): indefinite length not supported");
|
|
5160
|
-
if (lenLen > 4) throw new E("tlv.decode(long): byte length is too big");
|
|
5161
|
-
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
5162
|
-
if (lengthBytes.length !== lenLen) throw new E("tlv.decode: length bytes not complete");
|
|
5163
|
-
if (lengthBytes[0] === 0) throw new E("tlv.decode(long): zero leftmost byte");
|
|
5164
|
-
for (const b of lengthBytes) length = length << 8 | b;
|
|
5165
|
-
pos += lenLen;
|
|
5166
|
-
if (length < 128) throw new E("tlv.decode(long): not minimal encoding");
|
|
5167
|
-
}
|
|
5168
|
-
const v = data.subarray(pos, pos + length);
|
|
5169
|
-
if (v.length !== length) throw new E("tlv.decode: wrong value length");
|
|
5170
|
-
return {
|
|
5171
|
-
v,
|
|
5172
|
-
l: data.subarray(pos + length)
|
|
5173
|
-
};
|
|
5174
|
-
}
|
|
5175
|
-
},
|
|
5176
|
-
_int: {
|
|
5177
|
-
encode(num) {
|
|
5178
|
-
const { Err: E } = DER;
|
|
5179
|
-
if (num < _0n$1) throw new E("integer: negative integers are not allowed");
|
|
5180
|
-
let hex = numberToHexUnpadded(num);
|
|
5181
|
-
if (Number.parseInt(hex[0], 16) & 8) hex = "00" + hex;
|
|
5182
|
-
if (hex.length & 1) throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
5183
|
-
return hex;
|
|
5184
|
-
},
|
|
5185
|
-
decode(data) {
|
|
5186
|
-
const { Err: E } = DER;
|
|
5187
|
-
if (data[0] & 128) throw new E("invalid signature integer: negative");
|
|
5188
|
-
if (data[0] === 0 && !(data[1] & 128)) throw new E("invalid signature integer: unnecessary leading zero");
|
|
5189
|
-
return bytesToNumberBE(data);
|
|
5190
|
-
}
|
|
5191
|
-
},
|
|
5192
|
-
toSig(bytes) {
|
|
5193
|
-
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
5194
|
-
const data = abytes$1(bytes, void 0, "signature");
|
|
5195
|
-
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
5196
|
-
if (seqLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
5197
|
-
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
5198
|
-
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
5199
|
-
if (sLeftBytes.length) throw new E("invalid signature: left bytes after parsing");
|
|
5200
|
-
return {
|
|
5201
|
-
r: int.decode(rBytes),
|
|
5202
|
-
s: int.decode(sBytes)
|
|
5203
|
-
};
|
|
5204
|
-
},
|
|
5205
|
-
hexFromSig(sig) {
|
|
5206
|
-
const { _tlv: tlv, _int: int } = DER;
|
|
5207
|
-
const seq = tlv.encode(2, int.encode(sig.r)) + tlv.encode(2, int.encode(sig.s));
|
|
5208
|
-
return tlv.encode(48, seq);
|
|
5209
|
-
}
|
|
5210
|
-
};
|
|
5211
|
-
const _0n$1 = BigInt(0), _1n$1 = BigInt(1), _2n$2 = BigInt(2), _3n = BigInt(3), _4n = BigInt(4);
|
|
5212
|
-
/**
|
|
5213
|
-
* Creates weierstrass Point constructor, based on specified curve options.
|
|
5214
|
-
*
|
|
5215
|
-
* See {@link WeierstrassOpts}.
|
|
5216
|
-
*
|
|
5217
|
-
* @example
|
|
5218
|
-
```js
|
|
5219
|
-
const opts = {
|
|
5220
|
-
p: 0xfffffffffffffffffffffffffffffffeffffac73n,
|
|
5221
|
-
n: 0x100000000000000000001b8fa16dfab9aca16b6b3n,
|
|
5222
|
-
h: 1n,
|
|
5223
|
-
a: 0n,
|
|
5224
|
-
b: 7n,
|
|
5225
|
-
Gx: 0x3b4c382ce37aa192a4019e763036f4f5dd4d7ebbn,
|
|
5226
|
-
Gy: 0x938cf935318fdced6bc28286531733c3f03c4feen,
|
|
5227
|
-
};
|
|
5228
|
-
const secp160k1_Point = weierstrass(opts);
|
|
5229
|
-
```
|
|
5230
|
-
*/
|
|
5231
|
-
function weierstrass(params, extraOpts = {}) {
|
|
5232
|
-
const validated = createCurveFields("weierstrass", params, extraOpts);
|
|
5233
|
-
const { Fp, Fn } = validated;
|
|
5234
|
-
let CURVE = validated.CURVE;
|
|
5235
|
-
const { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
5236
|
-
validateObject(extraOpts, {}, {
|
|
5237
|
-
allowInfinityPoint: "boolean",
|
|
5238
|
-
clearCofactor: "function",
|
|
5239
|
-
isTorsionFree: "function",
|
|
5240
|
-
fromBytes: "function",
|
|
5241
|
-
toBytes: "function",
|
|
5242
|
-
endo: "object"
|
|
5243
|
-
});
|
|
5244
|
-
const { endo } = extraOpts;
|
|
5245
|
-
if (endo) {
|
|
5246
|
-
if (!Fp.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) throw new Error("invalid endo: expected \"beta\": bigint and \"basises\": array");
|
|
5247
|
-
}
|
|
5248
|
-
const lengths = getWLengths(Fp, Fn);
|
|
5249
|
-
function assertCompressionIsSupported() {
|
|
5250
|
-
if (!Fp.isOdd) throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
5251
|
-
}
|
|
5252
|
-
function pointToBytes(_c, point, isCompressed) {
|
|
5253
|
-
const { x, y } = point.toAffine();
|
|
5254
|
-
const bx = Fp.toBytes(x);
|
|
5255
|
-
abool(isCompressed, "isCompressed");
|
|
5256
|
-
if (isCompressed) {
|
|
5257
|
-
assertCompressionIsSupported();
|
|
5258
|
-
return concatBytes(pprefix(!Fp.isOdd(y)), bx);
|
|
5259
|
-
} else return concatBytes(Uint8Array.of(4), bx, Fp.toBytes(y));
|
|
5260
|
-
}
|
|
5261
|
-
function pointFromBytes(bytes) {
|
|
5262
|
-
abytes$1(bytes, void 0, "Point");
|
|
5263
|
-
const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
|
|
5264
|
-
const length = bytes.length;
|
|
5265
|
-
const head = bytes[0];
|
|
5266
|
-
const tail = bytes.subarray(1);
|
|
5267
|
-
if (length === comp && (head === 2 || head === 3)) {
|
|
5268
|
-
const x = Fp.fromBytes(tail);
|
|
5269
|
-
if (!Fp.isValid(x)) throw new Error("bad point: is not on curve, wrong x");
|
|
5270
|
-
const y2 = weierstrassEquation(x);
|
|
5271
|
-
let y;
|
|
5272
|
-
try {
|
|
5273
|
-
y = Fp.sqrt(y2);
|
|
5274
|
-
} catch (sqrtError) {
|
|
5275
|
-
const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
5276
|
-
throw new Error("bad point: is not on curve, sqrt error" + err);
|
|
5277
|
-
}
|
|
5278
|
-
assertCompressionIsSupported();
|
|
5279
|
-
const evenY = Fp.isOdd(y);
|
|
5280
|
-
if ((head & 1) === 1 !== evenY) y = Fp.neg(y);
|
|
5281
|
-
return {
|
|
5282
|
-
x,
|
|
5283
|
-
y
|
|
5284
|
-
};
|
|
5285
|
-
} else if (length === uncomp && head === 4) {
|
|
5286
|
-
const L = Fp.BYTES;
|
|
5287
|
-
const x = Fp.fromBytes(tail.subarray(0, L));
|
|
5288
|
-
const y = Fp.fromBytes(tail.subarray(L, L * 2));
|
|
5289
|
-
if (!isValidXY(x, y)) throw new Error("bad point: is not on curve");
|
|
5290
|
-
return {
|
|
5291
|
-
x,
|
|
5292
|
-
y
|
|
5293
|
-
};
|
|
5294
|
-
} else throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
5295
|
-
}
|
|
5296
|
-
const encodePoint = extraOpts.toBytes || pointToBytes;
|
|
5297
|
-
const decodePoint = extraOpts.fromBytes || pointFromBytes;
|
|
5298
|
-
function weierstrassEquation(x) {
|
|
5299
|
-
const x2 = Fp.sqr(x);
|
|
5300
|
-
const x3 = Fp.mul(x2, x);
|
|
5301
|
-
return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b);
|
|
5302
|
-
}
|
|
5303
|
-
/** Checks whether equation holds for given x, y: y² == x³ + ax + b */
|
|
5304
|
-
function isValidXY(x, y) {
|
|
5305
|
-
const left = Fp.sqr(y);
|
|
5306
|
-
const right = weierstrassEquation(x);
|
|
5307
|
-
return Fp.eql(left, right);
|
|
5308
|
-
}
|
|
5309
|
-
if (!isValidXY(CURVE.Gx, CURVE.Gy)) throw new Error("bad curve params: generator point");
|
|
5310
|
-
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n), _4n);
|
|
5311
|
-
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
5312
|
-
if (Fp.is0(Fp.add(_4a3, _27b2))) throw new Error("bad curve params: a or b");
|
|
5313
|
-
/** Asserts coordinate is valid: 0 <= n < Fp.ORDER. */
|
|
5314
|
-
function acoord(title, n, banZero = false) {
|
|
5315
|
-
if (!Fp.isValid(n) || banZero && Fp.is0(n)) throw new Error(`bad point coordinate ${title}`);
|
|
5316
|
-
return n;
|
|
5317
|
-
}
|
|
5318
|
-
function aprjpoint(other) {
|
|
5319
|
-
if (!(other instanceof Point)) throw new Error("Weierstrass Point expected");
|
|
5320
|
-
}
|
|
5321
|
-
function splitEndoScalarN(k) {
|
|
5322
|
-
if (!endo || !endo.basises) throw new Error("no endo");
|
|
5323
|
-
return _splitEndoScalar(k, endo.basises, Fn.ORDER);
|
|
5324
|
-
}
|
|
5325
|
-
const toAffineMemo = memoized((p, iz) => {
|
|
5326
|
-
const { X, Y, Z } = p;
|
|
5327
|
-
if (Fp.eql(Z, Fp.ONE)) return {
|
|
5328
|
-
x: X,
|
|
5329
|
-
y: Y
|
|
5330
|
-
};
|
|
5331
|
-
const is0 = p.is0();
|
|
5332
|
-
if (iz == null) iz = is0 ? Fp.ONE : Fp.inv(Z);
|
|
5333
|
-
const x = Fp.mul(X, iz);
|
|
5334
|
-
const y = Fp.mul(Y, iz);
|
|
5335
|
-
const zz = Fp.mul(Z, iz);
|
|
5336
|
-
if (is0) return {
|
|
5337
|
-
x: Fp.ZERO,
|
|
5338
|
-
y: Fp.ZERO
|
|
5339
|
-
};
|
|
5340
|
-
if (!Fp.eql(zz, Fp.ONE)) throw new Error("invZ was invalid");
|
|
5341
|
-
return {
|
|
5342
|
-
x,
|
|
5343
|
-
y
|
|
5344
|
-
};
|
|
5345
|
-
});
|
|
5346
|
-
const assertValidMemo = memoized((p) => {
|
|
5347
|
-
if (p.is0()) {
|
|
5348
|
-
if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y)) return;
|
|
5349
|
-
throw new Error("bad point: ZERO");
|
|
5350
|
-
}
|
|
5351
|
-
const { x, y } = p.toAffine();
|
|
5352
|
-
if (!Fp.isValid(x) || !Fp.isValid(y)) throw new Error("bad point: x or y not field elements");
|
|
5353
|
-
if (!isValidXY(x, y)) throw new Error("bad point: equation left != right");
|
|
5354
|
-
if (!p.isTorsionFree()) throw new Error("bad point: not in prime-order subgroup");
|
|
5355
|
-
return true;
|
|
5356
|
-
});
|
|
5357
|
-
function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
|
|
5358
|
-
k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
|
|
5359
|
-
k1p = negateCt(k1neg, k1p);
|
|
5360
|
-
k2p = negateCt(k2neg, k2p);
|
|
5361
|
-
return k1p.add(k2p);
|
|
4930
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
4931
|
+
const { X, Y, Z } = p;
|
|
4932
|
+
if (Fp.eql(Z, Fp.ONE)) return {
|
|
4933
|
+
x: X,
|
|
4934
|
+
y: Y
|
|
4935
|
+
};
|
|
4936
|
+
const is0 = p.is0();
|
|
4937
|
+
if (iz == null) iz = is0 ? Fp.ONE : Fp.inv(Z);
|
|
4938
|
+
const x = Fp.mul(X, iz);
|
|
4939
|
+
const y = Fp.mul(Y, iz);
|
|
4940
|
+
const zz = Fp.mul(Z, iz);
|
|
4941
|
+
if (is0) return {
|
|
4942
|
+
x: Fp.ZERO,
|
|
4943
|
+
y: Fp.ZERO
|
|
4944
|
+
};
|
|
4945
|
+
if (!Fp.eql(zz, Fp.ONE)) throw new Error("invZ was invalid");
|
|
4946
|
+
return {
|
|
4947
|
+
x,
|
|
4948
|
+
y
|
|
4949
|
+
};
|
|
4950
|
+
});
|
|
4951
|
+
const assertValidMemo = memoized((p) => {
|
|
4952
|
+
if (p.is0()) {
|
|
4953
|
+
if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y)) return;
|
|
4954
|
+
throw new Error("bad point: ZERO");
|
|
4955
|
+
}
|
|
4956
|
+
const { x, y } = p.toAffine();
|
|
4957
|
+
if (!Fp.isValid(x) || !Fp.isValid(y)) throw new Error("bad point: x or y not field elements");
|
|
4958
|
+
if (!isValidXY(x, y)) throw new Error("bad point: equation left != right");
|
|
4959
|
+
if (!p.isTorsionFree()) throw new Error("bad point: not in prime-order subgroup");
|
|
4960
|
+
return true;
|
|
4961
|
+
});
|
|
4962
|
+
function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
|
|
4963
|
+
k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
|
|
4964
|
+
k1p = negateCt(k1neg, k1p);
|
|
4965
|
+
k2p = negateCt(k2neg, k2p);
|
|
4966
|
+
return k1p.add(k2p);
|
|
5362
4967
|
}
|
|
5363
4968
|
/**
|
|
5364
4969
|
* Projective Point works in 3d / projective (homogeneous) coordinates:(X, Y, Z) ∋ (x=X/Z, y=Y/Z).
|
|
@@ -6223,179 +5828,578 @@ createHasher(Pointk1, (scalars) => {
|
|
|
6223
5828
|
* Check out `sha3-addons` module for cSHAKE, k12, and others.
|
|
6224
5829
|
* @module
|
|
6225
5830
|
*/
|
|
6226
|
-
const _0n = BigInt(0);
|
|
6227
|
-
const _1n = BigInt(1);
|
|
6228
|
-
const _2n = BigInt(2);
|
|
6229
|
-
const _7n = BigInt(7);
|
|
6230
|
-
const _256n = BigInt(256);
|
|
6231
|
-
const _0x71n = BigInt(113);
|
|
6232
|
-
const SHA3_PI = [];
|
|
6233
|
-
const SHA3_ROTL = [];
|
|
6234
|
-
const _SHA3_IOTA = [];
|
|
6235
|
-
for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
|
6236
|
-
[x, y] = [y, (2 * x + 3 * y) % 5];
|
|
6237
|
-
SHA3_PI.push(2 * (5 * y + x));
|
|
6238
|
-
SHA3_ROTL.push((round + 1) * (round + 2) / 2 % 64);
|
|
6239
|
-
let t = _0n;
|
|
6240
|
-
for (let j = 0; j < 7; j++) {
|
|
6241
|
-
R = (R << _1n ^ (R >> _7n) * _0x71n) % _256n;
|
|
6242
|
-
if (R & _2n) t ^= _1n << (_1n << BigInt(j)) - _1n;
|
|
6243
|
-
}
|
|
6244
|
-
_SHA3_IOTA.push(t);
|
|
5831
|
+
const _0n = BigInt(0);
|
|
5832
|
+
const _1n = BigInt(1);
|
|
5833
|
+
const _2n = BigInt(2);
|
|
5834
|
+
const _7n = BigInt(7);
|
|
5835
|
+
const _256n = BigInt(256);
|
|
5836
|
+
const _0x71n = BigInt(113);
|
|
5837
|
+
const SHA3_PI = [];
|
|
5838
|
+
const SHA3_ROTL = [];
|
|
5839
|
+
const _SHA3_IOTA = [];
|
|
5840
|
+
for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
|
5841
|
+
[x, y] = [y, (2 * x + 3 * y) % 5];
|
|
5842
|
+
SHA3_PI.push(2 * (5 * y + x));
|
|
5843
|
+
SHA3_ROTL.push((round + 1) * (round + 2) / 2 % 64);
|
|
5844
|
+
let t = _0n;
|
|
5845
|
+
for (let j = 0; j < 7; j++) {
|
|
5846
|
+
R = (R << _1n ^ (R >> _7n) * _0x71n) % _256n;
|
|
5847
|
+
if (R & _2n) t ^= _1n << (_1n << BigInt(j)) - _1n;
|
|
5848
|
+
}
|
|
5849
|
+
_SHA3_IOTA.push(t);
|
|
5850
|
+
}
|
|
5851
|
+
const IOTAS = split(_SHA3_IOTA, true);
|
|
5852
|
+
const SHA3_IOTA_H = IOTAS[0];
|
|
5853
|
+
const SHA3_IOTA_L = IOTAS[1];
|
|
5854
|
+
const rotlH = (h, l, s) => s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s);
|
|
5855
|
+
const rotlL = (h, l, s) => s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s);
|
|
5856
|
+
/** `keccakf1600` internal function, additionally allows to adjust round count. */
|
|
5857
|
+
function keccakP(s, rounds = 24) {
|
|
5858
|
+
const B = new Uint32Array(10);
|
|
5859
|
+
for (let round = 24 - rounds; round < 24; round++) {
|
|
5860
|
+
for (let x = 0; x < 10; x++) B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
|
|
5861
|
+
for (let x = 0; x < 10; x += 2) {
|
|
5862
|
+
const idx1 = (x + 8) % 10;
|
|
5863
|
+
const idx0 = (x + 2) % 10;
|
|
5864
|
+
const B0 = B[idx0];
|
|
5865
|
+
const B1 = B[idx0 + 1];
|
|
5866
|
+
const Th = rotlH(B0, B1, 1) ^ B[idx1];
|
|
5867
|
+
const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
|
|
5868
|
+
for (let y = 0; y < 50; y += 10) {
|
|
5869
|
+
s[x + y] ^= Th;
|
|
5870
|
+
s[x + y + 1] ^= Tl;
|
|
5871
|
+
}
|
|
5872
|
+
}
|
|
5873
|
+
let curH = s[2];
|
|
5874
|
+
let curL = s[3];
|
|
5875
|
+
for (let t = 0; t < 24; t++) {
|
|
5876
|
+
const shift = SHA3_ROTL[t];
|
|
5877
|
+
const Th = rotlH(curH, curL, shift);
|
|
5878
|
+
const Tl = rotlL(curH, curL, shift);
|
|
5879
|
+
const PI = SHA3_PI[t];
|
|
5880
|
+
curH = s[PI];
|
|
5881
|
+
curL = s[PI + 1];
|
|
5882
|
+
s[PI] = Th;
|
|
5883
|
+
s[PI + 1] = Tl;
|
|
5884
|
+
}
|
|
5885
|
+
for (let y = 0; y < 50; y += 10) {
|
|
5886
|
+
for (let x = 0; x < 10; x++) B[x] = s[y + x];
|
|
5887
|
+
for (let x = 0; x < 10; x++) s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
|
|
5888
|
+
}
|
|
5889
|
+
s[0] ^= SHA3_IOTA_H[round];
|
|
5890
|
+
s[1] ^= SHA3_IOTA_L[round];
|
|
5891
|
+
}
|
|
5892
|
+
clean(B);
|
|
5893
|
+
}
|
|
5894
|
+
/** Keccak sponge function. */
|
|
5895
|
+
var Keccak = class Keccak {
|
|
5896
|
+
state;
|
|
5897
|
+
pos = 0;
|
|
5898
|
+
posOut = 0;
|
|
5899
|
+
finished = false;
|
|
5900
|
+
state32;
|
|
5901
|
+
destroyed = false;
|
|
5902
|
+
blockLen;
|
|
5903
|
+
suffix;
|
|
5904
|
+
outputLen;
|
|
5905
|
+
enableXOF = false;
|
|
5906
|
+
rounds;
|
|
5907
|
+
constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
|
|
5908
|
+
this.blockLen = blockLen;
|
|
5909
|
+
this.suffix = suffix;
|
|
5910
|
+
this.outputLen = outputLen;
|
|
5911
|
+
this.enableXOF = enableXOF;
|
|
5912
|
+
this.rounds = rounds;
|
|
5913
|
+
anumber$1(outputLen, "outputLen");
|
|
5914
|
+
if (!(0 < blockLen && blockLen < 200)) throw new Error("only keccak-f1600 function is supported");
|
|
5915
|
+
this.state = new Uint8Array(200);
|
|
5916
|
+
this.state32 = u32(this.state);
|
|
5917
|
+
}
|
|
5918
|
+
clone() {
|
|
5919
|
+
return this._cloneInto();
|
|
5920
|
+
}
|
|
5921
|
+
keccak() {
|
|
5922
|
+
swap32IfBE(this.state32);
|
|
5923
|
+
keccakP(this.state32, this.rounds);
|
|
5924
|
+
swap32IfBE(this.state32);
|
|
5925
|
+
this.posOut = 0;
|
|
5926
|
+
this.pos = 0;
|
|
5927
|
+
}
|
|
5928
|
+
update(data) {
|
|
5929
|
+
aexists(this);
|
|
5930
|
+
abytes$1(data);
|
|
5931
|
+
const { blockLen, state } = this;
|
|
5932
|
+
const len = data.length;
|
|
5933
|
+
for (let pos = 0; pos < len;) {
|
|
5934
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
5935
|
+
for (let i = 0; i < take; i++) state[this.pos++] ^= data[pos++];
|
|
5936
|
+
if (this.pos === blockLen) this.keccak();
|
|
5937
|
+
}
|
|
5938
|
+
return this;
|
|
5939
|
+
}
|
|
5940
|
+
finish() {
|
|
5941
|
+
if (this.finished) return;
|
|
5942
|
+
this.finished = true;
|
|
5943
|
+
const { state, suffix, pos, blockLen } = this;
|
|
5944
|
+
state[pos] ^= suffix;
|
|
5945
|
+
if ((suffix & 128) !== 0 && pos === blockLen - 1) this.keccak();
|
|
5946
|
+
state[blockLen - 1] ^= 128;
|
|
5947
|
+
this.keccak();
|
|
5948
|
+
}
|
|
5949
|
+
writeInto(out) {
|
|
5950
|
+
aexists(this, false);
|
|
5951
|
+
abytes$1(out);
|
|
5952
|
+
this.finish();
|
|
5953
|
+
const bufferOut = this.state;
|
|
5954
|
+
const { blockLen } = this;
|
|
5955
|
+
for (let pos = 0, len = out.length; pos < len;) {
|
|
5956
|
+
if (this.posOut >= blockLen) this.keccak();
|
|
5957
|
+
const take = Math.min(blockLen - this.posOut, len - pos);
|
|
5958
|
+
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
|
5959
|
+
this.posOut += take;
|
|
5960
|
+
pos += take;
|
|
5961
|
+
}
|
|
5962
|
+
return out;
|
|
5963
|
+
}
|
|
5964
|
+
xofInto(out) {
|
|
5965
|
+
if (!this.enableXOF) throw new Error("XOF is not possible for this instance");
|
|
5966
|
+
return this.writeInto(out);
|
|
5967
|
+
}
|
|
5968
|
+
xof(bytes) {
|
|
5969
|
+
anumber$1(bytes);
|
|
5970
|
+
return this.xofInto(new Uint8Array(bytes));
|
|
5971
|
+
}
|
|
5972
|
+
digestInto(out) {
|
|
5973
|
+
aoutput(out, this);
|
|
5974
|
+
if (this.finished) throw new Error("digest() was already called");
|
|
5975
|
+
this.writeInto(out);
|
|
5976
|
+
this.destroy();
|
|
5977
|
+
return out;
|
|
5978
|
+
}
|
|
5979
|
+
digest() {
|
|
5980
|
+
return this.digestInto(new Uint8Array(this.outputLen));
|
|
5981
|
+
}
|
|
5982
|
+
destroy() {
|
|
5983
|
+
this.destroyed = true;
|
|
5984
|
+
clean(this.state);
|
|
5985
|
+
}
|
|
5986
|
+
_cloneInto(to) {
|
|
5987
|
+
const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
|
|
5988
|
+
to ||= new Keccak(blockLen, suffix, outputLen, enableXOF, rounds);
|
|
5989
|
+
to.state32.set(this.state32);
|
|
5990
|
+
to.pos = this.pos;
|
|
5991
|
+
to.posOut = this.posOut;
|
|
5992
|
+
to.finished = this.finished;
|
|
5993
|
+
to.rounds = rounds;
|
|
5994
|
+
to.suffix = suffix;
|
|
5995
|
+
to.outputLen = outputLen;
|
|
5996
|
+
to.enableXOF = enableXOF;
|
|
5997
|
+
to.destroyed = this.destroyed;
|
|
5998
|
+
return to;
|
|
5999
|
+
}
|
|
6000
|
+
};
|
|
6001
|
+
const genKeccak = (suffix, blockLen, outputLen, info = {}) => createHasher$1(() => new Keccak(blockLen, suffix, outputLen), info);
|
|
6002
|
+
/** keccak-256 hash function. Different from SHA3-256. */
|
|
6003
|
+
const keccak_256 = /* @__PURE__ */ genKeccak(1, 136, 32);
|
|
6004
|
+
//#endregion
|
|
6005
|
+
//#region node_modules/.pnpm/@scure+base@2.0.0/node_modules/@scure/base/index.js
|
|
6006
|
+
/*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
6007
|
+
function isBytes(a) {
|
|
6008
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
6009
|
+
}
|
|
6010
|
+
function isArrayOf(isString, arr) {
|
|
6011
|
+
if (!Array.isArray(arr)) return false;
|
|
6012
|
+
if (arr.length === 0) return true;
|
|
6013
|
+
if (isString) return arr.every((item) => typeof item === "string");
|
|
6014
|
+
else return arr.every((item) => Number.isSafeInteger(item));
|
|
6015
|
+
}
|
|
6016
|
+
function afn(input) {
|
|
6017
|
+
if (typeof input !== "function") throw new Error("function expected");
|
|
6018
|
+
return true;
|
|
6019
|
+
}
|
|
6020
|
+
function astr(label, input) {
|
|
6021
|
+
if (typeof input !== "string") throw new Error(`${label}: string expected`);
|
|
6022
|
+
return true;
|
|
6023
|
+
}
|
|
6024
|
+
function anumber(n) {
|
|
6025
|
+
if (!Number.isSafeInteger(n)) throw new Error(`invalid integer: ${n}`);
|
|
6026
|
+
}
|
|
6027
|
+
function aArr(input) {
|
|
6028
|
+
if (!Array.isArray(input)) throw new Error("array expected");
|
|
6029
|
+
}
|
|
6030
|
+
function astrArr(label, input) {
|
|
6031
|
+
if (!isArrayOf(true, input)) throw new Error(`${label}: array of strings expected`);
|
|
6032
|
+
}
|
|
6033
|
+
function anumArr(label, input) {
|
|
6034
|
+
if (!isArrayOf(false, input)) throw new Error(`${label}: array of numbers expected`);
|
|
6035
|
+
}
|
|
6036
|
+
/**
|
|
6037
|
+
* @__NO_SIDE_EFFECTS__
|
|
6038
|
+
*/
|
|
6039
|
+
function chain(...args) {
|
|
6040
|
+
const id = (a) => a;
|
|
6041
|
+
const wrap = (a, b) => (c) => a(b(c));
|
|
6042
|
+
return {
|
|
6043
|
+
encode: args.map((x) => x.encode).reduceRight(wrap, id),
|
|
6044
|
+
decode: args.map((x) => x.decode).reduce(wrap, id)
|
|
6045
|
+
};
|
|
6046
|
+
}
|
|
6047
|
+
/**
|
|
6048
|
+
* Encodes integer radix representation to array of strings using alphabet and back.
|
|
6049
|
+
* Could also be array of strings.
|
|
6050
|
+
* @__NO_SIDE_EFFECTS__
|
|
6051
|
+
*/
|
|
6052
|
+
function alphabet(letters) {
|
|
6053
|
+
const lettersA = typeof letters === "string" ? letters.split("") : letters;
|
|
6054
|
+
const len = lettersA.length;
|
|
6055
|
+
astrArr("alphabet", lettersA);
|
|
6056
|
+
const indexes = new Map(lettersA.map((l, i) => [l, i]));
|
|
6057
|
+
return {
|
|
6058
|
+
encode: (digits) => {
|
|
6059
|
+
aArr(digits);
|
|
6060
|
+
return digits.map((i) => {
|
|
6061
|
+
if (!Number.isSafeInteger(i) || i < 0 || i >= len) throw new Error(`alphabet.encode: digit index outside alphabet "${i}". Allowed: ${letters}`);
|
|
6062
|
+
return lettersA[i];
|
|
6063
|
+
});
|
|
6064
|
+
},
|
|
6065
|
+
decode: (input) => {
|
|
6066
|
+
aArr(input);
|
|
6067
|
+
return input.map((letter) => {
|
|
6068
|
+
astr("alphabet.decode", letter);
|
|
6069
|
+
const i = indexes.get(letter);
|
|
6070
|
+
if (i === void 0) throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
|
|
6071
|
+
return i;
|
|
6072
|
+
});
|
|
6073
|
+
}
|
|
6074
|
+
};
|
|
6245
6075
|
}
|
|
6246
|
-
|
|
6247
|
-
|
|
6248
|
-
|
|
6249
|
-
|
|
6250
|
-
|
|
6251
|
-
|
|
6252
|
-
|
|
6253
|
-
|
|
6254
|
-
|
|
6255
|
-
|
|
6256
|
-
|
|
6257
|
-
|
|
6258
|
-
|
|
6259
|
-
const B0 = B[idx0];
|
|
6260
|
-
const B1 = B[idx0 + 1];
|
|
6261
|
-
const Th = rotlH(B0, B1, 1) ^ B[idx1];
|
|
6262
|
-
const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
|
|
6263
|
-
for (let y = 0; y < 50; y += 10) {
|
|
6264
|
-
s[x + y] ^= Th;
|
|
6265
|
-
s[x + y + 1] ^= Tl;
|
|
6266
|
-
}
|
|
6076
|
+
/**
|
|
6077
|
+
* @__NO_SIDE_EFFECTS__
|
|
6078
|
+
*/
|
|
6079
|
+
function join$1(separator = "") {
|
|
6080
|
+
astr("join", separator);
|
|
6081
|
+
return {
|
|
6082
|
+
encode: (from) => {
|
|
6083
|
+
astrArr("join.decode", from);
|
|
6084
|
+
return from.join(separator);
|
|
6085
|
+
},
|
|
6086
|
+
decode: (to) => {
|
|
6087
|
+
astr("join.decode", to);
|
|
6088
|
+
return to.split(separator);
|
|
6267
6089
|
}
|
|
6268
|
-
|
|
6269
|
-
|
|
6270
|
-
|
|
6271
|
-
|
|
6272
|
-
|
|
6273
|
-
|
|
6274
|
-
|
|
6275
|
-
|
|
6276
|
-
|
|
6277
|
-
|
|
6278
|
-
|
|
6090
|
+
};
|
|
6091
|
+
}
|
|
6092
|
+
/**
|
|
6093
|
+
* Pad strings array so it has integer number of bits
|
|
6094
|
+
* @__NO_SIDE_EFFECTS__
|
|
6095
|
+
*/
|
|
6096
|
+
function padding(bits, chr = "=") {
|
|
6097
|
+
anumber(bits);
|
|
6098
|
+
astr("padding", chr);
|
|
6099
|
+
return {
|
|
6100
|
+
encode(data) {
|
|
6101
|
+
astrArr("padding.encode", data);
|
|
6102
|
+
while (data.length * bits % 8) data.push(chr);
|
|
6103
|
+
return data;
|
|
6104
|
+
},
|
|
6105
|
+
decode(input) {
|
|
6106
|
+
astrArr("padding.decode", input);
|
|
6107
|
+
let end = input.length;
|
|
6108
|
+
if (end * bits % 8) throw new Error("padding: invalid, string should have whole number of bytes");
|
|
6109
|
+
for (; end > 0 && input[end - 1] === chr; end--) if ((end - 1) * bits % 8 === 0) throw new Error("padding: invalid, string has too much padding");
|
|
6110
|
+
return input.slice(0, end);
|
|
6279
6111
|
}
|
|
6280
|
-
|
|
6281
|
-
|
|
6282
|
-
|
|
6112
|
+
};
|
|
6113
|
+
}
|
|
6114
|
+
/**
|
|
6115
|
+
* @__NO_SIDE_EFFECTS__
|
|
6116
|
+
*/
|
|
6117
|
+
function normalize$2(fn) {
|
|
6118
|
+
afn(fn);
|
|
6119
|
+
return {
|
|
6120
|
+
encode: (from) => from,
|
|
6121
|
+
decode: (to) => fn(to)
|
|
6122
|
+
};
|
|
6123
|
+
}
|
|
6124
|
+
/**
|
|
6125
|
+
* Slow: O(n^2) time complexity
|
|
6126
|
+
*/
|
|
6127
|
+
function convertRadix(data, from, to) {
|
|
6128
|
+
if (from < 2) throw new Error(`convertRadix: invalid from=${from}, base cannot be less than 2`);
|
|
6129
|
+
if (to < 2) throw new Error(`convertRadix: invalid to=${to}, base cannot be less than 2`);
|
|
6130
|
+
aArr(data);
|
|
6131
|
+
if (!data.length) return [];
|
|
6132
|
+
let pos = 0;
|
|
6133
|
+
const res = [];
|
|
6134
|
+
const digits = Array.from(data, (d) => {
|
|
6135
|
+
anumber(d);
|
|
6136
|
+
if (d < 0 || d >= from) throw new Error(`invalid integer: ${d}`);
|
|
6137
|
+
return d;
|
|
6138
|
+
});
|
|
6139
|
+
const dlen = digits.length;
|
|
6140
|
+
while (true) {
|
|
6141
|
+
let carry = 0;
|
|
6142
|
+
let done = true;
|
|
6143
|
+
for (let i = pos; i < dlen; i++) {
|
|
6144
|
+
const digit = digits[i];
|
|
6145
|
+
const fromCarry = from * carry;
|
|
6146
|
+
const digitBase = fromCarry + digit;
|
|
6147
|
+
if (!Number.isSafeInteger(digitBase) || fromCarry / from !== carry || digitBase - digit !== fromCarry) throw new Error("convertRadix: carry overflow");
|
|
6148
|
+
const div = digitBase / to;
|
|
6149
|
+
carry = digitBase % to;
|
|
6150
|
+
const rounded = Math.floor(div);
|
|
6151
|
+
digits[i] = rounded;
|
|
6152
|
+
if (!Number.isSafeInteger(rounded) || rounded * to + carry !== digitBase) throw new Error("convertRadix: carry overflow");
|
|
6153
|
+
if (!done) continue;
|
|
6154
|
+
else if (!rounded) pos = i;
|
|
6155
|
+
else done = false;
|
|
6283
6156
|
}
|
|
6284
|
-
|
|
6285
|
-
|
|
6157
|
+
res.push(carry);
|
|
6158
|
+
if (done) break;
|
|
6286
6159
|
}
|
|
6287
|
-
|
|
6160
|
+
for (let i = 0; i < data.length - 1 && data[i] === 0; i++) res.push(0);
|
|
6161
|
+
return res.reverse();
|
|
6288
6162
|
}
|
|
6289
|
-
|
|
6290
|
-
|
|
6291
|
-
|
|
6292
|
-
|
|
6293
|
-
|
|
6294
|
-
|
|
6295
|
-
|
|
6296
|
-
|
|
6297
|
-
|
|
6298
|
-
|
|
6299
|
-
|
|
6300
|
-
|
|
6301
|
-
|
|
6302
|
-
|
|
6303
|
-
|
|
6304
|
-
|
|
6305
|
-
|
|
6306
|
-
|
|
6307
|
-
|
|
6308
|
-
|
|
6309
|
-
|
|
6310
|
-
|
|
6311
|
-
|
|
6312
|
-
|
|
6313
|
-
|
|
6314
|
-
|
|
6315
|
-
|
|
6316
|
-
|
|
6317
|
-
|
|
6318
|
-
|
|
6319
|
-
swap32IfBE(this.state32);
|
|
6320
|
-
this.posOut = 0;
|
|
6321
|
-
this.pos = 0;
|
|
6163
|
+
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
|
|
6164
|
+
const radix2carry = /* @__NO_SIDE_EFFECTS__ */ (from, to) => from + (to - gcd(from, to));
|
|
6165
|
+
const powers = /* @__PURE__ */ (() => {
|
|
6166
|
+
let res = [];
|
|
6167
|
+
for (let i = 0; i < 40; i++) res.push(2 ** i);
|
|
6168
|
+
return res;
|
|
6169
|
+
})();
|
|
6170
|
+
/**
|
|
6171
|
+
* Implemented with numbers, because BigInt is 5x slower
|
|
6172
|
+
*/
|
|
6173
|
+
function convertRadix2(data, from, to, padding) {
|
|
6174
|
+
aArr(data);
|
|
6175
|
+
if (from <= 0 || from > 32) throw new Error(`convertRadix2: wrong from=${from}`);
|
|
6176
|
+
if (to <= 0 || to > 32) throw new Error(`convertRadix2: wrong to=${to}`);
|
|
6177
|
+
if (/* @__PURE__ */ radix2carry(from, to) > 32) throw new Error(`convertRadix2: carry overflow from=${from} to=${to} carryBits=${/* @__PURE__ */ radix2carry(from, to)}`);
|
|
6178
|
+
let carry = 0;
|
|
6179
|
+
let pos = 0;
|
|
6180
|
+
const max = powers[from];
|
|
6181
|
+
const mask = powers[to] - 1;
|
|
6182
|
+
const res = [];
|
|
6183
|
+
for (const n of data) {
|
|
6184
|
+
anumber(n);
|
|
6185
|
+
if (n >= max) throw new Error(`convertRadix2: invalid data word=${n} from=${from}`);
|
|
6186
|
+
carry = carry << from | n;
|
|
6187
|
+
if (pos + from > 32) throw new Error(`convertRadix2: carry overflow pos=${pos} from=${from}`);
|
|
6188
|
+
pos += from;
|
|
6189
|
+
for (; pos >= to; pos -= to) res.push((carry >> pos - to & mask) >>> 0);
|
|
6190
|
+
const pow = powers[pos];
|
|
6191
|
+
if (pow === void 0) throw new Error("invalid carry");
|
|
6192
|
+
carry &= pow - 1;
|
|
6322
6193
|
}
|
|
6323
|
-
|
|
6324
|
-
|
|
6325
|
-
|
|
6326
|
-
|
|
6327
|
-
|
|
6328
|
-
|
|
6329
|
-
|
|
6330
|
-
|
|
6331
|
-
|
|
6194
|
+
carry = carry << to - pos & mask;
|
|
6195
|
+
if (!padding && pos >= from) throw new Error("Excess padding");
|
|
6196
|
+
if (!padding && carry > 0) throw new Error(`Non-zero padding: ${carry}`);
|
|
6197
|
+
if (padding && pos > 0) res.push(carry >>> 0);
|
|
6198
|
+
return res;
|
|
6199
|
+
}
|
|
6200
|
+
/**
|
|
6201
|
+
* @__NO_SIDE_EFFECTS__
|
|
6202
|
+
*/
|
|
6203
|
+
function radix(num) {
|
|
6204
|
+
anumber(num);
|
|
6205
|
+
const _256 = 2 ** 8;
|
|
6206
|
+
return {
|
|
6207
|
+
encode: (bytes) => {
|
|
6208
|
+
if (!isBytes(bytes)) throw new Error("radix.encode input should be Uint8Array");
|
|
6209
|
+
return convertRadix(Array.from(bytes), _256, num);
|
|
6210
|
+
},
|
|
6211
|
+
decode: (digits) => {
|
|
6212
|
+
anumArr("radix.decode", digits);
|
|
6213
|
+
return Uint8Array.from(convertRadix(digits, num, _256));
|
|
6332
6214
|
}
|
|
6333
|
-
|
|
6334
|
-
|
|
6335
|
-
|
|
6336
|
-
|
|
6337
|
-
|
|
6338
|
-
|
|
6339
|
-
|
|
6340
|
-
|
|
6341
|
-
|
|
6342
|
-
|
|
6343
|
-
|
|
6344
|
-
|
|
6345
|
-
|
|
6346
|
-
|
|
6347
|
-
|
|
6348
|
-
|
|
6349
|
-
|
|
6350
|
-
|
|
6351
|
-
|
|
6352
|
-
const take = Math.min(blockLen - this.posOut, len - pos);
|
|
6353
|
-
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
|
6354
|
-
this.posOut += take;
|
|
6355
|
-
pos += take;
|
|
6215
|
+
};
|
|
6216
|
+
}
|
|
6217
|
+
/**
|
|
6218
|
+
* If both bases are power of same number (like `2**8 <-> 2**64`),
|
|
6219
|
+
* there is a linear algorithm. For now we have implementation for power-of-two bases only.
|
|
6220
|
+
* @__NO_SIDE_EFFECTS__
|
|
6221
|
+
*/
|
|
6222
|
+
function radix2(bits, revPadding = false) {
|
|
6223
|
+
anumber(bits);
|
|
6224
|
+
if (bits <= 0 || bits > 32) throw new Error("radix2: bits should be in (0..32]");
|
|
6225
|
+
if (/* @__PURE__ */ radix2carry(8, bits) > 32 || /* @__PURE__ */ radix2carry(bits, 8) > 32) throw new Error("radix2: carry overflow");
|
|
6226
|
+
return {
|
|
6227
|
+
encode: (bytes) => {
|
|
6228
|
+
if (!isBytes(bytes)) throw new Error("radix2.encode input should be Uint8Array");
|
|
6229
|
+
return convertRadix2(Array.from(bytes), 8, bits, !revPadding);
|
|
6230
|
+
},
|
|
6231
|
+
decode: (digits) => {
|
|
6232
|
+
anumArr("radix2.decode", digits);
|
|
6233
|
+
return Uint8Array.from(convertRadix2(digits, bits, 8, revPadding));
|
|
6356
6234
|
}
|
|
6357
|
-
|
|
6358
|
-
|
|
6359
|
-
|
|
6360
|
-
|
|
6361
|
-
|
|
6362
|
-
|
|
6363
|
-
|
|
6364
|
-
|
|
6365
|
-
|
|
6235
|
+
};
|
|
6236
|
+
}
|
|
6237
|
+
function unsafeWrapper(fn) {
|
|
6238
|
+
afn(fn);
|
|
6239
|
+
return function(...args) {
|
|
6240
|
+
try {
|
|
6241
|
+
return fn.apply(null, args);
|
|
6242
|
+
} catch (e) {}
|
|
6243
|
+
};
|
|
6244
|
+
}
|
|
6245
|
+
function checksum(len, fn) {
|
|
6246
|
+
anumber(len);
|
|
6247
|
+
afn(fn);
|
|
6248
|
+
return {
|
|
6249
|
+
encode(data) {
|
|
6250
|
+
if (!isBytes(data)) throw new Error("checksum.encode: input should be Uint8Array");
|
|
6251
|
+
const sum = fn(data).slice(0, len);
|
|
6252
|
+
const res = new Uint8Array(data.length + len);
|
|
6253
|
+
res.set(data);
|
|
6254
|
+
res.set(sum, data.length);
|
|
6255
|
+
return res;
|
|
6256
|
+
},
|
|
6257
|
+
decode(data) {
|
|
6258
|
+
if (!isBytes(data)) throw new Error("checksum.decode: input should be Uint8Array");
|
|
6259
|
+
const payload = data.slice(0, -len);
|
|
6260
|
+
const oldChecksum = data.slice(-len);
|
|
6261
|
+
const newChecksum = fn(payload).slice(0, len);
|
|
6262
|
+
for (let i = 0; i < len; i++) if (newChecksum[i] !== oldChecksum[i]) throw new Error("Invalid checksum");
|
|
6263
|
+
return payload;
|
|
6264
|
+
}
|
|
6265
|
+
};
|
|
6266
|
+
}
|
|
6267
|
+
const utils = {
|
|
6268
|
+
alphabet,
|
|
6269
|
+
chain,
|
|
6270
|
+
checksum,
|
|
6271
|
+
convertRadix,
|
|
6272
|
+
convertRadix2,
|
|
6273
|
+
radix,
|
|
6274
|
+
radix2,
|
|
6275
|
+
join: join$1,
|
|
6276
|
+
padding
|
|
6277
|
+
};
|
|
6278
|
+
chain(radix2(4), alphabet("0123456789ABCDEF"), join$1(""));
|
|
6279
|
+
chain(radix2(5), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"), padding(5), join$1(""));
|
|
6280
|
+
chain(radix2(5), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"), join$1(""));
|
|
6281
|
+
chain(radix2(5), alphabet("0123456789ABCDEFGHIJKLMNOPQRSTUV"), padding(5), join$1(""));
|
|
6282
|
+
chain(radix2(5), alphabet("0123456789ABCDEFGHIJKLMNOPQRSTUV"), join$1(""));
|
|
6283
|
+
chain(radix2(5), alphabet("0123456789ABCDEFGHJKMNPQRSTVWXYZ"), join$1(""), normalize$2((s) => s.toUpperCase().replace(/O/g, "0").replace(/[IL]/g, "1")));
|
|
6284
|
+
const hasBase64Builtin = typeof Uint8Array.from([]).toBase64 === "function" && typeof Uint8Array.fromBase64 === "function";
|
|
6285
|
+
hasBase64Builtin || chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), padding(6), join$1(""));
|
|
6286
|
+
chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"), join$1(""));
|
|
6287
|
+
hasBase64Builtin || chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), padding(6), join$1(""));
|
|
6288
|
+
chain(radix2(6), alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"), join$1(""));
|
|
6289
|
+
const genBase58 = /* @__NO_SIDE_EFFECTS__ */ (abc) => chain(radix(58), alphabet(abc), join$1(""));
|
|
6290
|
+
/**
|
|
6291
|
+
* base58: base64 without ambigous characters +, /, 0, O, I, l.
|
|
6292
|
+
* Quadratic (O(n^2)) - so, can't be used on large inputs.
|
|
6293
|
+
* @example
|
|
6294
|
+
* ```js
|
|
6295
|
+
* base58.decode('01abcdef');
|
|
6296
|
+
* // => '3UhJW'
|
|
6297
|
+
* ```
|
|
6298
|
+
*/
|
|
6299
|
+
const base58 = /* @__PURE__ */ genBase58("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
|
|
6300
|
+
/**
|
|
6301
|
+
* Method, which creates base58check encoder.
|
|
6302
|
+
* Requires function, calculating sha256.
|
|
6303
|
+
*/
|
|
6304
|
+
const createBase58check = (sha256) => chain(checksum(4, (data) => sha256(sha256(data))), base58);
|
|
6305
|
+
const BECH_ALPHABET = chain(alphabet("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), join$1(""));
|
|
6306
|
+
const POLYMOD_GENERATORS = [
|
|
6307
|
+
996825010,
|
|
6308
|
+
642813549,
|
|
6309
|
+
513874426,
|
|
6310
|
+
1027748829,
|
|
6311
|
+
705979059
|
|
6312
|
+
];
|
|
6313
|
+
function bech32Polymod(pre) {
|
|
6314
|
+
const b = pre >> 25;
|
|
6315
|
+
let chk = (pre & 33554431) << 5;
|
|
6316
|
+
for (let i = 0; i < POLYMOD_GENERATORS.length; i++) if ((b >> i & 1) === 1) chk ^= POLYMOD_GENERATORS[i];
|
|
6317
|
+
return chk;
|
|
6318
|
+
}
|
|
6319
|
+
function bechChecksum(prefix, words, encodingConst = 1) {
|
|
6320
|
+
const len = prefix.length;
|
|
6321
|
+
let chk = 1;
|
|
6322
|
+
for (let i = 0; i < len; i++) {
|
|
6323
|
+
const c = prefix.charCodeAt(i);
|
|
6324
|
+
if (c < 33 || c > 126) throw new Error(`Invalid prefix (${prefix})`);
|
|
6325
|
+
chk = bech32Polymod(chk) ^ c >> 5;
|
|
6366
6326
|
}
|
|
6367
|
-
|
|
6368
|
-
|
|
6369
|
-
|
|
6370
|
-
|
|
6371
|
-
|
|
6372
|
-
|
|
6327
|
+
chk = bech32Polymod(chk);
|
|
6328
|
+
for (let i = 0; i < len; i++) chk = bech32Polymod(chk) ^ prefix.charCodeAt(i) & 31;
|
|
6329
|
+
for (let v of words) chk = bech32Polymod(chk) ^ v;
|
|
6330
|
+
for (let i = 0; i < 6; i++) chk = bech32Polymod(chk);
|
|
6331
|
+
chk ^= encodingConst;
|
|
6332
|
+
return BECH_ALPHABET.encode(convertRadix2([chk % powers[30]], 30, 5, false));
|
|
6333
|
+
}
|
|
6334
|
+
/**
|
|
6335
|
+
* @__NO_SIDE_EFFECTS__
|
|
6336
|
+
*/
|
|
6337
|
+
function genBech32(encoding) {
|
|
6338
|
+
const ENCODING_CONST = encoding === "bech32" ? 1 : 734539939;
|
|
6339
|
+
const _words = radix2(5);
|
|
6340
|
+
const fromWords = _words.decode;
|
|
6341
|
+
const toWords = _words.encode;
|
|
6342
|
+
const fromWordsUnsafe = unsafeWrapper(fromWords);
|
|
6343
|
+
function encode(prefix, words, limit = 90) {
|
|
6344
|
+
astr("bech32.encode prefix", prefix);
|
|
6345
|
+
if (isBytes(words)) words = Array.from(words);
|
|
6346
|
+
anumArr("bech32.encode", words);
|
|
6347
|
+
const plen = prefix.length;
|
|
6348
|
+
if (plen === 0) throw new TypeError(`Invalid prefix length ${plen}`);
|
|
6349
|
+
const actualLength = plen + 7 + words.length;
|
|
6350
|
+
if (limit !== false && actualLength > limit) throw new TypeError(`Length ${actualLength} exceeds limit ${limit}`);
|
|
6351
|
+
const lowered = prefix.toLowerCase();
|
|
6352
|
+
const sum = bechChecksum(lowered, words, ENCODING_CONST);
|
|
6353
|
+
return `${lowered}1${BECH_ALPHABET.encode(words)}${sum}`;
|
|
6373
6354
|
}
|
|
6374
|
-
|
|
6375
|
-
|
|
6355
|
+
function decode(str, limit = 90) {
|
|
6356
|
+
astr("bech32.decode input", str);
|
|
6357
|
+
const slen = str.length;
|
|
6358
|
+
if (slen < 8 || limit !== false && slen > limit) throw new TypeError(`invalid string length: ${slen} (${str}). Expected (8..${limit})`);
|
|
6359
|
+
const lowered = str.toLowerCase();
|
|
6360
|
+
if (str !== lowered && str !== str.toUpperCase()) throw new Error(`String must be lowercase or uppercase`);
|
|
6361
|
+
const sepIndex = lowered.lastIndexOf("1");
|
|
6362
|
+
if (sepIndex === 0 || sepIndex === -1) throw new Error(`Letter "1" must be present between prefix and data only`);
|
|
6363
|
+
const prefix = lowered.slice(0, sepIndex);
|
|
6364
|
+
const data = lowered.slice(sepIndex + 1);
|
|
6365
|
+
if (data.length < 6) throw new Error("Data must be at least 6 characters long");
|
|
6366
|
+
const words = BECH_ALPHABET.decode(data).slice(0, -6);
|
|
6367
|
+
const sum = bechChecksum(prefix, words, ENCODING_CONST);
|
|
6368
|
+
if (!data.endsWith(sum)) throw new Error(`Invalid checksum in ${str}: expected "${sum}"`);
|
|
6369
|
+
return {
|
|
6370
|
+
prefix,
|
|
6371
|
+
words
|
|
6372
|
+
};
|
|
6376
6373
|
}
|
|
6377
|
-
|
|
6378
|
-
|
|
6379
|
-
|
|
6374
|
+
const decodeUnsafe = unsafeWrapper(decode);
|
|
6375
|
+
function decodeToBytes(str) {
|
|
6376
|
+
const { prefix, words } = decode(str, false);
|
|
6377
|
+
return {
|
|
6378
|
+
prefix,
|
|
6379
|
+
words,
|
|
6380
|
+
bytes: fromWords(words)
|
|
6381
|
+
};
|
|
6380
6382
|
}
|
|
6381
|
-
|
|
6382
|
-
|
|
6383
|
-
to ||= new Keccak(blockLen, suffix, outputLen, enableXOF, rounds);
|
|
6384
|
-
to.state32.set(this.state32);
|
|
6385
|
-
to.pos = this.pos;
|
|
6386
|
-
to.posOut = this.posOut;
|
|
6387
|
-
to.finished = this.finished;
|
|
6388
|
-
to.rounds = rounds;
|
|
6389
|
-
to.suffix = suffix;
|
|
6390
|
-
to.outputLen = outputLen;
|
|
6391
|
-
to.enableXOF = enableXOF;
|
|
6392
|
-
to.destroyed = this.destroyed;
|
|
6393
|
-
return to;
|
|
6383
|
+
function encodeFromBytes(prefix, bytes) {
|
|
6384
|
+
return encode(prefix, toWords(bytes));
|
|
6394
6385
|
}
|
|
6395
|
-
|
|
6396
|
-
|
|
6397
|
-
|
|
6398
|
-
|
|
6386
|
+
return {
|
|
6387
|
+
encode,
|
|
6388
|
+
decode,
|
|
6389
|
+
encodeFromBytes,
|
|
6390
|
+
decodeToBytes,
|
|
6391
|
+
decodeUnsafe,
|
|
6392
|
+
fromWords,
|
|
6393
|
+
fromWordsUnsafe,
|
|
6394
|
+
toWords
|
|
6395
|
+
};
|
|
6396
|
+
}
|
|
6397
|
+
genBech32("bech32");
|
|
6398
|
+
genBech32("bech32m");
|
|
6399
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function" || chain(radix2(4), alphabet("0123456789abcdef"), join$1(""), normalize$2((s) => {
|
|
6400
|
+
if (typeof s !== "string" || s.length % 2 !== 0) throw new TypeError(`hex.decode: expected string, got ${typeof s} with length ${s.length}`);
|
|
6401
|
+
return s.toLowerCase();
|
|
6402
|
+
}));
|
|
6399
6403
|
//#endregion
|
|
6400
6404
|
//#region node_modules/.pnpm/@noble+hashes@2.0.1/node_modules/@noble/hashes/legacy.js
|
|
6401
6405
|
/**
|
|
@@ -6902,7 +6906,7 @@ function nfkd(str) {
|
|
|
6902
6906
|
if (typeof str !== "string") throw new TypeError("invalid mnemonic type: " + typeof str);
|
|
6903
6907
|
return str.normalize("NFKD");
|
|
6904
6908
|
}
|
|
6905
|
-
function normalize(str) {
|
|
6909
|
+
function normalize$1(str) {
|
|
6906
6910
|
const norm = nfkd(str);
|
|
6907
6911
|
const words = norm.split(" ");
|
|
6908
6912
|
if (![
|
|
@@ -6980,7 +6984,7 @@ const psalt = (passphrase) => nfkd("mnemonic" + passphrase);
|
|
|
6980
6984
|
* // new Uint8Array([...64 bytes])
|
|
6981
6985
|
*/
|
|
6982
6986
|
function mnemonicToSeedSync(mnemonic, passphrase = "") {
|
|
6983
|
-
return pbkdf2(sha512, normalize(mnemonic).nfkd, psalt(passphrase), {
|
|
6987
|
+
return pbkdf2(sha512, normalize$1(mnemonic).nfkd, psalt(passphrase), {
|
|
6984
6988
|
c: 2048,
|
|
6985
6989
|
dkLen: 64
|
|
6986
6990
|
});
|
|
@@ -9106,4 +9110,105 @@ function checksumAddress(addr) {
|
|
|
9106
9110
|
return out;
|
|
9107
9111
|
}
|
|
9108
9112
|
//#endregion
|
|
9109
|
-
|
|
9113
|
+
//#region packages/x402-proxy/src/lib/config.ts
|
|
9114
|
+
var import_dist = /* @__PURE__ */ __toESM(require_dist(), 1);
|
|
9115
|
+
const APP_NAME = "x402-proxy";
|
|
9116
|
+
function getConfigDir() {
|
|
9117
|
+
const xdg = process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config");
|
|
9118
|
+
return path.join(xdg, APP_NAME);
|
|
9119
|
+
}
|
|
9120
|
+
/** Config dir with $HOME replaced by ~ for display */
|
|
9121
|
+
function getConfigDirShort() {
|
|
9122
|
+
const dir = getConfigDir();
|
|
9123
|
+
const home = os.homedir();
|
|
9124
|
+
return dir.startsWith(home) ? `~${dir.slice(home.length)}` : dir;
|
|
9125
|
+
}
|
|
9126
|
+
function getWalletPath() {
|
|
9127
|
+
return path.join(getConfigDir(), "wallet.json");
|
|
9128
|
+
}
|
|
9129
|
+
function getHistoryPath() {
|
|
9130
|
+
return path.join(getConfigDir(), "history.jsonl");
|
|
9131
|
+
}
|
|
9132
|
+
function getDebugLogPath() {
|
|
9133
|
+
return path.join(getConfigDir(), "debug.log");
|
|
9134
|
+
}
|
|
9135
|
+
function ensureConfigDir() {
|
|
9136
|
+
fs.mkdirSync(getConfigDir(), { recursive: true });
|
|
9137
|
+
}
|
|
9138
|
+
function loadWalletFile() {
|
|
9139
|
+
const p = getWalletPath();
|
|
9140
|
+
if (!fs.existsSync(p)) return null;
|
|
9141
|
+
try {
|
|
9142
|
+
const data = JSON.parse(fs.readFileSync(p, "utf-8"));
|
|
9143
|
+
if (data.version === 1 && typeof data.mnemonic === "string") return data;
|
|
9144
|
+
return null;
|
|
9145
|
+
} catch {
|
|
9146
|
+
return null;
|
|
9147
|
+
}
|
|
9148
|
+
}
|
|
9149
|
+
function createWalletFile(mnemonic) {
|
|
9150
|
+
const evm = deriveEvmKeypair(mnemonic);
|
|
9151
|
+
const sol = deriveSolanaKeypair(mnemonic);
|
|
9152
|
+
return {
|
|
9153
|
+
version: 1,
|
|
9154
|
+
mnemonic,
|
|
9155
|
+
addresses: {
|
|
9156
|
+
evm: evm.address,
|
|
9157
|
+
solana: sol.address
|
|
9158
|
+
}
|
|
9159
|
+
};
|
|
9160
|
+
}
|
|
9161
|
+
function saveWalletFile(wallet) {
|
|
9162
|
+
ensureConfigDir();
|
|
9163
|
+
fs.writeFileSync(getWalletPath(), JSON.stringify(wallet, null, 2), {
|
|
9164
|
+
mode: 384,
|
|
9165
|
+
encoding: "utf-8"
|
|
9166
|
+
});
|
|
9167
|
+
}
|
|
9168
|
+
function loadConfig() {
|
|
9169
|
+
const dir = getConfigDir();
|
|
9170
|
+
for (const name of [
|
|
9171
|
+
"config.yaml",
|
|
9172
|
+
"config.yml",
|
|
9173
|
+
"config.jsonc",
|
|
9174
|
+
"config.json"
|
|
9175
|
+
]) {
|
|
9176
|
+
const p = path.join(dir, name);
|
|
9177
|
+
if (!fs.existsSync(p)) continue;
|
|
9178
|
+
try {
|
|
9179
|
+
const raw = fs.readFileSync(p, "utf-8");
|
|
9180
|
+
if (name.endsWith(".yaml") || name.endsWith(".yml")) return (0, import_dist.parse)(raw);
|
|
9181
|
+
if (name.endsWith(".jsonc")) {
|
|
9182
|
+
const stripped = raw.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
|
9183
|
+
return JSON.parse(stripped);
|
|
9184
|
+
}
|
|
9185
|
+
return JSON.parse(raw);
|
|
9186
|
+
} catch {}
|
|
9187
|
+
}
|
|
9188
|
+
return null;
|
|
9189
|
+
}
|
|
9190
|
+
function saveConfig(config) {
|
|
9191
|
+
ensureConfigDir();
|
|
9192
|
+
const p = path.join(getConfigDir(), "config.yaml");
|
|
9193
|
+
fs.writeFileSync(p, (0, import_dist.stringify)(config), "utf-8");
|
|
9194
|
+
}
|
|
9195
|
+
function getSessionPath() {
|
|
9196
|
+
return path.join(getConfigDir(), "session.json");
|
|
9197
|
+
}
|
|
9198
|
+
function saveSession(session) {
|
|
9199
|
+
ensureConfigDir();
|
|
9200
|
+
fs.writeFileSync(getSessionPath(), JSON.stringify(session, null, 2), "utf-8");
|
|
9201
|
+
}
|
|
9202
|
+
function clearSession() {
|
|
9203
|
+
try {
|
|
9204
|
+
fs.unlinkSync(getSessionPath());
|
|
9205
|
+
} catch {}
|
|
9206
|
+
}
|
|
9207
|
+
function isConfigured() {
|
|
9208
|
+
if (process.env.X402_PROXY_WALLET_MNEMONIC) return true;
|
|
9209
|
+
if (process.env.X402_PROXY_WALLET_EVM_KEY) return true;
|
|
9210
|
+
if (process.env.X402_PROXY_WALLET_SOLANA_KEY) return true;
|
|
9211
|
+
return loadWalletFile() !== null;
|
|
9212
|
+
}
|
|
9213
|
+
//#endregion
|
|
9214
|
+
export { run as C, buildRouteMap as S, base58 as _, getDebugLogPath as a, buildApplication as b, isConfigured as c, saveConfig as d, saveSession as f, generateMnemonic as g, deriveSolanaKeypair as h, getConfigDirShort as i, loadConfig as l, deriveEvmKeypair as m, createWalletFile as n, getHistoryPath as o, saveWalletFile as p, ensureConfigDir as r, getWalletPath as s, clearSession as t, loadWalletFile as u, ed25519 as v, buildCommand as x, require_picocolors as y };
|