x402-proxy 0.10.6 → 0.10.9
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/CHANGELOG.md +30 -1
- package/README.md +7 -1
- package/dist/Credential-COZQnr1-.js +2055 -0
- package/dist/Mcp-CrCEqLqO.js +10 -0
- package/dist/Sse-ChldYgU7.js +9742 -0
- package/dist/Sse-kCB38G56.js +16482 -0
- package/dist/accounts-DsuvWwph.js +232 -0
- package/dist/accounts-DzvAlQRn.js +5 -0
- package/dist/accounts-IG-Cmrwy.js +229 -0
- package/dist/api-CUzmQvTQ.js +2802 -0
- package/dist/auth-DTzQmnZ_.js +1196 -0
- package/dist/bin/cli.js +608 -244
- package/dist/ccip-Bx-zoUCJ.js +240 -0
- package/dist/ccip-C2k1DD1T.js +153 -0
- package/dist/ccip-C6CQOJYv.js +152 -0
- package/dist/ccip-RZzsZ5Mv.js +156 -0
- package/dist/chain-CafcHffR.js +1997 -0
- package/dist/chain-DwfP5RGZ.js +1968 -0
- package/dist/chunk-DBEY4PJZ.js +16 -0
- package/dist/chunk-DjEMn6fM.js +36 -0
- package/dist/client-Blw2V7LF.js +657 -0
- package/dist/client-C37gWJOZ.js +102 -0
- package/dist/client-CEc4NYAA.js +6388 -0
- package/dist/client-CVDTUY0l.js +5152 -0
- package/dist/config-BUQsit4s.js +3 -0
- package/dist/config-DR1Fs_wL.js +6600 -0
- package/dist/{config-D9wIR3xc.js → config-rvKA3SYT.js} +10 -5
- package/dist/decodeFunctionData-DuFcwhC_.js +4510 -0
- package/dist/decodeFunctionData-JPOUdvil.js +4394 -0
- package/dist/derive-DNUl8LU9.js +9109 -0
- package/dist/dist-C2YO6HSQ.js +6581 -0
- package/dist/dist-DM5_F3r5.js +4 -0
- package/dist/dist-DxJCYyL5.js +1388 -0
- package/dist/hashTypedData-BHmP9dBd.js +859 -0
- package/dist/hashTypedData-CtEdfx4y.js +846 -0
- package/dist/helpers-CuUSw-tH.js +7125 -0
- package/dist/hmac-59IlS_by.js +648 -0
- package/dist/http-BAtucMbS.js +2060 -0
- package/dist/index.d.ts +1903 -9
- package/dist/index.js +18006 -50
- package/dist/index.node-CxkL0OFh.js +3592 -0
- package/dist/index.node-DvmeuZBj.js +3 -0
- package/dist/isAddressEqual-BLrd1Hg1.js +9 -0
- package/dist/isAddressEqual-DsAqfQOD.js +10 -0
- package/dist/localBatchGatewayRequest-C-RPJyDO.js +6260 -0
- package/dist/localBatchGatewayRequest-DOdQ9bR7.js +93 -0
- package/dist/localBatchGatewayRequest-DQkbZaSy.js +6261 -0
- package/dist/parseUnits-CApwcKSD.js +49 -0
- package/dist/parseUnits-cMO2udMe.js +48 -0
- package/dist/schemas-BxMFYNbH.js +1270 -0
- package/dist/secp256k1-BZpiyffY.js +2525 -0
- package/dist/secp256k1-BjenrLl5.js +1877 -0
- package/dist/secp256k1-CLPUX17u.js +3 -0
- package/dist/sendRawTransactionSync-DvSkhZtW.js +3612 -0
- package/dist/server-CSq0IuUq.js +565 -0
- package/dist/setup-BY4J49Lv.js +1110 -0
- package/dist/setup-wMOAgrsN.js +3 -0
- package/dist/sha256-FAs0qeni.js +17 -0
- package/dist/sha3-CYkWM8Xa.js +195 -0
- package/dist/sha3-DbMJRJ3C.js +194 -0
- package/dist/sse-B4LLqBQm.js +408 -0
- package/dist/status-Bu23RjW6.js +3 -0
- package/dist/{status-DihAcUSC.js → status-X21VnGUO.js} +16 -15
- package/dist/stdio-BADqxZdZ.js +85 -0
- package/dist/streamableHttp-BHkJypcI.js +358 -0
- package/dist/tempo-3nttrxgQ.js +17 -0
- package/dist/tempo-DER0P-ul.js +18 -0
- package/dist/types-BEKUz-Mf.js +1240 -0
- package/dist/types-DatK5vR5.js +3 -0
- package/dist/utils-BYjkXZDF.js +444 -0
- package/dist/utils-SeGHMW9O.js +445 -0
- package/dist/wallet-DKVlrR1S.js +3 -0
- package/dist/wallet-DSyht15_.js +17759 -0
- package/package.json +18 -71
- package/dist/config-B_upkJeK.js +0 -66
- package/dist/config-Be35NM5s.js +0 -3
- package/dist/config-J1m-CWXT.js +0 -27
- package/dist/derive-CL6e8K0Z.js +0 -81
- package/dist/openclaw/plugin.d.ts +0 -15
- package/dist/openclaw/plugin.js +0 -2067
- package/dist/openclaw.plugin.json +0 -93
- package/dist/setup-CNyMLnM-.js +0 -197
- package/dist/setup-DTIxPe58.js +0 -3
- package/dist/status-DZlJ4pS7.js +0 -3
- package/dist/wallet-B0S-rma9.js +0 -544
- package/dist/wallet-DBrVZJqe.js +0 -3
- package/openclaw.plugin.json +0 -93
- package/skills/SKILL.md +0 -183
- package/skills/references/library.md +0 -85
- package/skills/references/openclaw-plugin.md +0 -145
|
@@ -0,0 +1,2055 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { t as keccak_256 } from "./sha3-CYkWM8Xa.js";
|
|
3
|
+
import { t as hmac } from "./hmac-59IlS_by.js";
|
|
4
|
+
import { t as sha256 } from "./sha256-FAs0qeni.js";
|
|
5
|
+
import { B as _number, Cn as safeParseAsync, Dn as clone, Fn as normalizeParams, Gt as $ZodObject, H as _refine, Jt as $ZodPipe, K as _string, Lt as $ZodLiteral, On as defineLazy, Q as _unknown, Qt as $ZodString, Sn as safeParse, Tt as $ZodEnum, U as _regex, Un as $constructor, Ut as $ZodNumber, Zt as $ZodRecord, a as _boolean, an as $ZodUnion, bn as parse, d as _custom, en as $ZodTransform, on as $ZodUnknown, pt as $ZodBoolean, qt as $ZodOptional, tn as $ZodType, ut as $ZodArray, xn as parseAsync, xt as $ZodDiscriminatedUnion, yt as $ZodCustom } from "./api-CUzmQvTQ.js";
|
|
6
|
+
//#region node_modules/.pnpm/zod@4.3.6/node_modules/zod/v4/mini/schemas.js
|
|
7
|
+
const ZodMiniType = /* @__PURE__ */ $constructor("ZodMiniType", (inst, def) => {
|
|
8
|
+
if (!inst._zod) throw new Error("Uninitialized schema in ZodMiniType.");
|
|
9
|
+
$ZodType.init(inst, def);
|
|
10
|
+
inst.def = def;
|
|
11
|
+
inst.type = def.type;
|
|
12
|
+
inst.parse = (data, params) => parse(inst, data, params, { callee: inst.parse });
|
|
13
|
+
inst.safeParse = (data, params) => safeParse(inst, data, params);
|
|
14
|
+
inst.parseAsync = async (data, params) => parseAsync(inst, data, params, { callee: inst.parseAsync });
|
|
15
|
+
inst.safeParseAsync = async (data, params) => safeParseAsync(inst, data, params);
|
|
16
|
+
inst.check = (...checks) => {
|
|
17
|
+
return inst.clone({
|
|
18
|
+
...def,
|
|
19
|
+
checks: [...def.checks ?? [], ...checks.map((ch) => typeof ch === "function" ? { _zod: {
|
|
20
|
+
check: ch,
|
|
21
|
+
def: { check: "custom" },
|
|
22
|
+
onattach: []
|
|
23
|
+
} } : ch)]
|
|
24
|
+
}, { parent: true });
|
|
25
|
+
};
|
|
26
|
+
inst.with = inst.check;
|
|
27
|
+
inst.clone = (_def, params) => clone(inst, _def, params);
|
|
28
|
+
inst.brand = () => inst;
|
|
29
|
+
inst.register = ((reg, meta) => {
|
|
30
|
+
reg.add(inst, meta);
|
|
31
|
+
return inst;
|
|
32
|
+
});
|
|
33
|
+
inst.apply = (fn) => fn(inst);
|
|
34
|
+
});
|
|
35
|
+
const ZodMiniString = /* @__PURE__ */ $constructor("ZodMiniString", (inst, def) => {
|
|
36
|
+
$ZodString.init(inst, def);
|
|
37
|
+
ZodMiniType.init(inst, def);
|
|
38
|
+
});
|
|
39
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
40
|
+
function string(params) {
|
|
41
|
+
return _string(ZodMiniString, params);
|
|
42
|
+
}
|
|
43
|
+
const ZodMiniNumber = /* @__PURE__ */ $constructor("ZodMiniNumber", (inst, def) => {
|
|
44
|
+
$ZodNumber.init(inst, def);
|
|
45
|
+
ZodMiniType.init(inst, def);
|
|
46
|
+
});
|
|
47
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
48
|
+
function number(params) {
|
|
49
|
+
return _number(ZodMiniNumber, params);
|
|
50
|
+
}
|
|
51
|
+
const ZodMiniBoolean = /* @__PURE__ */ $constructor("ZodMiniBoolean", (inst, def) => {
|
|
52
|
+
$ZodBoolean.init(inst, def);
|
|
53
|
+
ZodMiniType.init(inst, def);
|
|
54
|
+
});
|
|
55
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
56
|
+
function boolean(params) {
|
|
57
|
+
return _boolean(ZodMiniBoolean, params);
|
|
58
|
+
}
|
|
59
|
+
const ZodMiniUnknown = /* @__PURE__ */ $constructor("ZodMiniUnknown", (inst, def) => {
|
|
60
|
+
$ZodUnknown.init(inst, def);
|
|
61
|
+
ZodMiniType.init(inst, def);
|
|
62
|
+
});
|
|
63
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
64
|
+
function unknown() {
|
|
65
|
+
return _unknown(ZodMiniUnknown);
|
|
66
|
+
}
|
|
67
|
+
const ZodMiniArray = /* @__PURE__ */ $constructor("ZodMiniArray", (inst, def) => {
|
|
68
|
+
$ZodArray.init(inst, def);
|
|
69
|
+
ZodMiniType.init(inst, def);
|
|
70
|
+
});
|
|
71
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
72
|
+
function array(element, params) {
|
|
73
|
+
return new ZodMiniArray({
|
|
74
|
+
type: "array",
|
|
75
|
+
element,
|
|
76
|
+
...normalizeParams(params)
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const ZodMiniObject = /* @__PURE__ */ $constructor("ZodMiniObject", (inst, def) => {
|
|
80
|
+
$ZodObject.init(inst, def);
|
|
81
|
+
ZodMiniType.init(inst, def);
|
|
82
|
+
defineLazy(inst, "shape", () => def.shape);
|
|
83
|
+
});
|
|
84
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
85
|
+
function object(shape, params) {
|
|
86
|
+
return new ZodMiniObject({
|
|
87
|
+
type: "object",
|
|
88
|
+
shape: shape ?? {},
|
|
89
|
+
...normalizeParams(params)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const ZodMiniUnion = /* @__PURE__ */ $constructor("ZodMiniUnion", (inst, def) => {
|
|
93
|
+
$ZodUnion.init(inst, def);
|
|
94
|
+
ZodMiniType.init(inst, def);
|
|
95
|
+
});
|
|
96
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
97
|
+
function union(options, params) {
|
|
98
|
+
return new ZodMiniUnion({
|
|
99
|
+
type: "union",
|
|
100
|
+
options,
|
|
101
|
+
...normalizeParams(params)
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const ZodMiniDiscriminatedUnion = /* @__PURE__ */ $constructor("ZodMiniDiscriminatedUnion", (inst, def) => {
|
|
105
|
+
$ZodDiscriminatedUnion.init(inst, def);
|
|
106
|
+
ZodMiniType.init(inst, def);
|
|
107
|
+
});
|
|
108
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
109
|
+
function discriminatedUnion(discriminator, options, params) {
|
|
110
|
+
return new ZodMiniDiscriminatedUnion({
|
|
111
|
+
type: "union",
|
|
112
|
+
options,
|
|
113
|
+
discriminator,
|
|
114
|
+
...normalizeParams(params)
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const ZodMiniRecord = /* @__PURE__ */ $constructor("ZodMiniRecord", (inst, def) => {
|
|
118
|
+
$ZodRecord.init(inst, def);
|
|
119
|
+
ZodMiniType.init(inst, def);
|
|
120
|
+
});
|
|
121
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
122
|
+
function record(keyType, valueType, params) {
|
|
123
|
+
return new ZodMiniRecord({
|
|
124
|
+
type: "record",
|
|
125
|
+
keyType,
|
|
126
|
+
valueType,
|
|
127
|
+
...normalizeParams(params)
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const ZodMiniEnum = /* @__PURE__ */ $constructor("ZodMiniEnum", (inst, def) => {
|
|
131
|
+
$ZodEnum.init(inst, def);
|
|
132
|
+
ZodMiniType.init(inst, def);
|
|
133
|
+
inst.options = Object.values(def.entries);
|
|
134
|
+
});
|
|
135
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
136
|
+
function _enum(values, params) {
|
|
137
|
+
return new ZodMiniEnum({
|
|
138
|
+
type: "enum",
|
|
139
|
+
entries: Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values,
|
|
140
|
+
...normalizeParams(params)
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const ZodMiniLiteral = /* @__PURE__ */ $constructor("ZodMiniLiteral", (inst, def) => {
|
|
144
|
+
$ZodLiteral.init(inst, def);
|
|
145
|
+
ZodMiniType.init(inst, def);
|
|
146
|
+
});
|
|
147
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
148
|
+
function literal(value, params) {
|
|
149
|
+
return new ZodMiniLiteral({
|
|
150
|
+
type: "literal",
|
|
151
|
+
values: Array.isArray(value) ? value : [value],
|
|
152
|
+
...normalizeParams(params)
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const ZodMiniTransform = /* @__PURE__ */ $constructor("ZodMiniTransform", (inst, def) => {
|
|
156
|
+
$ZodTransform.init(inst, def);
|
|
157
|
+
ZodMiniType.init(inst, def);
|
|
158
|
+
});
|
|
159
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
160
|
+
function transform(fn) {
|
|
161
|
+
return new ZodMiniTransform({
|
|
162
|
+
type: "transform",
|
|
163
|
+
transform: fn
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
const ZodMiniOptional = /* @__PURE__ */ $constructor("ZodMiniOptional", (inst, def) => {
|
|
167
|
+
$ZodOptional.init(inst, def);
|
|
168
|
+
ZodMiniType.init(inst, def);
|
|
169
|
+
});
|
|
170
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
171
|
+
function optional(innerType) {
|
|
172
|
+
return new ZodMiniOptional({
|
|
173
|
+
type: "optional",
|
|
174
|
+
innerType
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
const ZodMiniPipe = /* @__PURE__ */ $constructor("ZodMiniPipe", (inst, def) => {
|
|
178
|
+
$ZodPipe.init(inst, def);
|
|
179
|
+
ZodMiniType.init(inst, def);
|
|
180
|
+
});
|
|
181
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
182
|
+
function pipe(in_, out) {
|
|
183
|
+
return new ZodMiniPipe({
|
|
184
|
+
type: "pipe",
|
|
185
|
+
in: in_,
|
|
186
|
+
out
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
const ZodMiniCustom = /* @__PURE__ */ $constructor("ZodMiniCustom", (inst, def) => {
|
|
190
|
+
$ZodCustom.init(inst, def);
|
|
191
|
+
ZodMiniType.init(inst, def);
|
|
192
|
+
});
|
|
193
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
194
|
+
function custom(fn, _params) {
|
|
195
|
+
return _custom(ZodMiniCustom, fn ?? (() => true), _params);
|
|
196
|
+
}
|
|
197
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
198
|
+
function refine(fn, _params = {}) {
|
|
199
|
+
return _refine(ZodMiniCustom, fn, _params);
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/version.js
|
|
203
|
+
/** @internal */
|
|
204
|
+
const version = "0.1.1";
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/internal/errors.js
|
|
207
|
+
/** @internal */
|
|
208
|
+
function getVersion() {
|
|
209
|
+
return version;
|
|
210
|
+
}
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/Errors.js
|
|
213
|
+
/**
|
|
214
|
+
* Base error class inherited by all errors thrown by ox.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* import { Errors } from 'ox'
|
|
219
|
+
* throw new Errors.BaseError('An error occurred')
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
var BaseError = class BaseError extends Error {
|
|
223
|
+
static setStaticOptions(options) {
|
|
224
|
+
BaseError.prototype.docsOrigin = options.docsOrigin;
|
|
225
|
+
BaseError.prototype.showVersion = options.showVersion;
|
|
226
|
+
BaseError.prototype.version = options.version;
|
|
227
|
+
}
|
|
228
|
+
constructor(shortMessage, options = {}) {
|
|
229
|
+
const details = (() => {
|
|
230
|
+
if (options.cause instanceof BaseError) {
|
|
231
|
+
if (options.cause.details) return options.cause.details;
|
|
232
|
+
if (options.cause.shortMessage) return options.cause.shortMessage;
|
|
233
|
+
}
|
|
234
|
+
if (options.cause && "details" in options.cause && typeof options.cause.details === "string") return options.cause.details;
|
|
235
|
+
if (options.cause?.message) return options.cause.message;
|
|
236
|
+
return options.details;
|
|
237
|
+
})();
|
|
238
|
+
const docsPath = (() => {
|
|
239
|
+
if (options.cause instanceof BaseError) return options.cause.docsPath || options.docsPath;
|
|
240
|
+
return options.docsPath;
|
|
241
|
+
})();
|
|
242
|
+
const docsBaseUrl = options.docsOrigin ?? BaseError.prototype.docsOrigin;
|
|
243
|
+
const docs = `${docsBaseUrl}${docsPath ?? ""}`;
|
|
244
|
+
const showVersion = Boolean(options.version ?? BaseError.prototype.showVersion);
|
|
245
|
+
const version = options.version ?? BaseError.prototype.version;
|
|
246
|
+
const message = [
|
|
247
|
+
shortMessage || "An error occurred.",
|
|
248
|
+
...options.metaMessages ? ["", ...options.metaMessages] : [],
|
|
249
|
+
...details || docsPath || showVersion ? [
|
|
250
|
+
"",
|
|
251
|
+
details ? `Details: ${details}` : void 0,
|
|
252
|
+
docsPath ? `See: ${docs}` : void 0,
|
|
253
|
+
showVersion ? `Version: ${version}` : void 0
|
|
254
|
+
] : []
|
|
255
|
+
].filter((x) => typeof x === "string").join("\n");
|
|
256
|
+
super(message, options.cause ? { cause: options.cause } : void 0);
|
|
257
|
+
Object.defineProperty(this, "details", {
|
|
258
|
+
enumerable: true,
|
|
259
|
+
configurable: true,
|
|
260
|
+
writable: true,
|
|
261
|
+
value: void 0
|
|
262
|
+
});
|
|
263
|
+
Object.defineProperty(this, "docs", {
|
|
264
|
+
enumerable: true,
|
|
265
|
+
configurable: true,
|
|
266
|
+
writable: true,
|
|
267
|
+
value: void 0
|
|
268
|
+
});
|
|
269
|
+
Object.defineProperty(this, "docsOrigin", {
|
|
270
|
+
enumerable: true,
|
|
271
|
+
configurable: true,
|
|
272
|
+
writable: true,
|
|
273
|
+
value: void 0
|
|
274
|
+
});
|
|
275
|
+
Object.defineProperty(this, "docsPath", {
|
|
276
|
+
enumerable: true,
|
|
277
|
+
configurable: true,
|
|
278
|
+
writable: true,
|
|
279
|
+
value: void 0
|
|
280
|
+
});
|
|
281
|
+
Object.defineProperty(this, "shortMessage", {
|
|
282
|
+
enumerable: true,
|
|
283
|
+
configurable: true,
|
|
284
|
+
writable: true,
|
|
285
|
+
value: void 0
|
|
286
|
+
});
|
|
287
|
+
Object.defineProperty(this, "showVersion", {
|
|
288
|
+
enumerable: true,
|
|
289
|
+
configurable: true,
|
|
290
|
+
writable: true,
|
|
291
|
+
value: void 0
|
|
292
|
+
});
|
|
293
|
+
Object.defineProperty(this, "version", {
|
|
294
|
+
enumerable: true,
|
|
295
|
+
configurable: true,
|
|
296
|
+
writable: true,
|
|
297
|
+
value: void 0
|
|
298
|
+
});
|
|
299
|
+
Object.defineProperty(this, "cause", {
|
|
300
|
+
enumerable: true,
|
|
301
|
+
configurable: true,
|
|
302
|
+
writable: true,
|
|
303
|
+
value: void 0
|
|
304
|
+
});
|
|
305
|
+
Object.defineProperty(this, "name", {
|
|
306
|
+
enumerable: true,
|
|
307
|
+
configurable: true,
|
|
308
|
+
writable: true,
|
|
309
|
+
value: "BaseError"
|
|
310
|
+
});
|
|
311
|
+
this.cause = options.cause;
|
|
312
|
+
this.details = details;
|
|
313
|
+
this.docs = docs;
|
|
314
|
+
this.docsOrigin = docsBaseUrl;
|
|
315
|
+
this.docsPath = docsPath;
|
|
316
|
+
this.shortMessage = shortMessage;
|
|
317
|
+
this.showVersion = showVersion;
|
|
318
|
+
this.version = version;
|
|
319
|
+
}
|
|
320
|
+
walk(fn) {
|
|
321
|
+
return walk(this, fn);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
Object.defineProperty(BaseError, "defaultStaticOptions", {
|
|
325
|
+
enumerable: true,
|
|
326
|
+
configurable: true,
|
|
327
|
+
writable: true,
|
|
328
|
+
value: {
|
|
329
|
+
docsOrigin: "https://oxlib.sh",
|
|
330
|
+
showVersion: false,
|
|
331
|
+
version: `ox@${getVersion()}`
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
BaseError.setStaticOptions(BaseError.defaultStaticOptions);
|
|
335
|
+
/** @internal */
|
|
336
|
+
function walk(err, fn) {
|
|
337
|
+
if (fn?.(err)) return err;
|
|
338
|
+
if (err && typeof err === "object" && "cause" in err && err.cause) return walk(err.cause, fn);
|
|
339
|
+
return fn ? null : err;
|
|
340
|
+
}
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/internal/bytes.js
|
|
343
|
+
/** @internal */
|
|
344
|
+
function assertSize$1(bytes, size_) {
|
|
345
|
+
if (size(bytes) > size_) throw new SizeOverflowError({
|
|
346
|
+
givenSize: size(bytes),
|
|
347
|
+
maxSize: size_
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
/** @internal */
|
|
351
|
+
const charCodeMap = {
|
|
352
|
+
zero: 48,
|
|
353
|
+
nine: 57,
|
|
354
|
+
A: 65,
|
|
355
|
+
F: 70,
|
|
356
|
+
a: 97,
|
|
357
|
+
f: 102
|
|
358
|
+
};
|
|
359
|
+
/** @internal */
|
|
360
|
+
function charCodeToBase16(char) {
|
|
361
|
+
if (char >= charCodeMap.zero && char <= charCodeMap.nine) return char - charCodeMap.zero;
|
|
362
|
+
if (char >= charCodeMap.A && char <= charCodeMap.F) return char - (charCodeMap.A - 10);
|
|
363
|
+
if (char >= charCodeMap.a && char <= charCodeMap.f) return char - (charCodeMap.a - 10);
|
|
364
|
+
}
|
|
365
|
+
/** @internal */
|
|
366
|
+
function pad$1(bytes, options = {}) {
|
|
367
|
+
const { dir, size = 32 } = options;
|
|
368
|
+
if (size === 0) return bytes;
|
|
369
|
+
if (bytes.length > size) throw new SizeExceedsPaddingSizeError({
|
|
370
|
+
size: bytes.length,
|
|
371
|
+
targetSize: size,
|
|
372
|
+
type: "Bytes"
|
|
373
|
+
});
|
|
374
|
+
const paddedBytes = new Uint8Array(size);
|
|
375
|
+
for (let i = 0; i < size; i++) {
|
|
376
|
+
const padEnd = dir === "right";
|
|
377
|
+
paddedBytes[padEnd ? i : size - i - 1] = bytes[padEnd ? i : bytes.length - i - 1];
|
|
378
|
+
}
|
|
379
|
+
return paddedBytes;
|
|
380
|
+
}
|
|
381
|
+
/** @internal */
|
|
382
|
+
function trim(value, options = {}) {
|
|
383
|
+
const { dir = "left" } = options;
|
|
384
|
+
let data = value;
|
|
385
|
+
let sliceLength = 0;
|
|
386
|
+
for (let i = 0; i < data.length - 1; i++) if (data[dir === "left" ? i : data.length - i - 1].toString() === "0") sliceLength++;
|
|
387
|
+
else break;
|
|
388
|
+
data = dir === "left" ? data.slice(sliceLength) : data.slice(0, data.length - sliceLength);
|
|
389
|
+
return data;
|
|
390
|
+
}
|
|
391
|
+
//#endregion
|
|
392
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/internal/hex.js
|
|
393
|
+
/** @internal */
|
|
394
|
+
function assertSize(hex, size_) {
|
|
395
|
+
if (size$1(hex) > size_) throw new SizeOverflowError$1({
|
|
396
|
+
givenSize: size$1(hex),
|
|
397
|
+
maxSize: size_
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
/** @internal */
|
|
401
|
+
function assertStartOffset(value, start) {
|
|
402
|
+
if (typeof start === "number" && start > 0 && start > size$1(value) - 1) throw new SliceOffsetOutOfBoundsError({
|
|
403
|
+
offset: start,
|
|
404
|
+
position: "start",
|
|
405
|
+
size: size$1(value)
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
/** @internal */
|
|
409
|
+
function assertEndOffset(value, start, end) {
|
|
410
|
+
if (typeof start === "number" && typeof end === "number" && size$1(value) !== end - start) throw new SliceOffsetOutOfBoundsError({
|
|
411
|
+
offset: end,
|
|
412
|
+
position: "end",
|
|
413
|
+
size: size$1(value)
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
/** @internal */
|
|
417
|
+
function pad(hex_, options = {}) {
|
|
418
|
+
const { dir, size = 32 } = options;
|
|
419
|
+
if (size === 0) return hex_;
|
|
420
|
+
const hex = hex_.replace("0x", "");
|
|
421
|
+
if (hex.length > size * 2) throw new SizeExceedsPaddingSizeError$1({
|
|
422
|
+
size: Math.ceil(hex.length / 2),
|
|
423
|
+
targetSize: size,
|
|
424
|
+
type: "Hex"
|
|
425
|
+
});
|
|
426
|
+
return `0x${hex[dir === "right" ? "padEnd" : "padStart"](size * 2, "0")}`;
|
|
427
|
+
}
|
|
428
|
+
//#endregion
|
|
429
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/Json.js
|
|
430
|
+
const bigIntSuffix = "#__bigint";
|
|
431
|
+
/**
|
|
432
|
+
* Serializes a value to a canonical JSON string as defined by
|
|
433
|
+
* [RFC 8785 (JSON Canonicalization Scheme)](https://www.rfc-editor.org/rfc/rfc8785).
|
|
434
|
+
*
|
|
435
|
+
* - Object keys are sorted recursively by UTF-16 code unit comparison.
|
|
436
|
+
* - Primitives are serialized per ECMAScript rules (no trailing zeros on numbers, etc.).
|
|
437
|
+
* - No whitespace is inserted.
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```ts twoslash
|
|
441
|
+
* import { Json } from 'ox'
|
|
442
|
+
*
|
|
443
|
+
* const json = Json.canonicalize({ b: 2, a: 1 })
|
|
444
|
+
* // @log: '{"a":1,"b":2}'
|
|
445
|
+
* ```
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts twoslash
|
|
449
|
+
* import { Json } from 'ox'
|
|
450
|
+
*
|
|
451
|
+
* const json = Json.canonicalize({ z: [3, { y: 1, x: 2 }], a: 'hello' })
|
|
452
|
+
* // @log: '{"a":"hello","z":[3,{"x":2,"y":1}]}'
|
|
453
|
+
* ```
|
|
454
|
+
*
|
|
455
|
+
* @param value - The value to canonicalize.
|
|
456
|
+
* @returns The canonical JSON string.
|
|
457
|
+
*/
|
|
458
|
+
function canonicalize(value) {
|
|
459
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") return JSON.stringify(value);
|
|
460
|
+
if (typeof value === "number") {
|
|
461
|
+
if (!Number.isFinite(value)) throw new TypeError("Cannot canonicalize non-finite number");
|
|
462
|
+
return Object.is(value, -0) ? "0" : JSON.stringify(value);
|
|
463
|
+
}
|
|
464
|
+
if (typeof value === "bigint") throw new TypeError("Cannot canonicalize bigint");
|
|
465
|
+
if (Array.isArray(value)) return `[${value.map((item) => canonicalize(item)).join(",")}]`;
|
|
466
|
+
if (typeof value === "object") return `{${Object.keys(value).sort().reduce((acc, key) => {
|
|
467
|
+
const v = value[key];
|
|
468
|
+
if (v !== void 0) acc.push(`${JSON.stringify(key)}:${canonicalize(v)}`);
|
|
469
|
+
return acc;
|
|
470
|
+
}, []).join(",")}}`;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Stringifies a value to its JSON representation, with support for `bigint`.
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```ts twoslash
|
|
477
|
+
* import { Json } from 'ox'
|
|
478
|
+
*
|
|
479
|
+
* const json = Json.stringify({
|
|
480
|
+
* foo: 'bar',
|
|
481
|
+
* baz: 69420694206942069420694206942069420694206942069420n,
|
|
482
|
+
* })
|
|
483
|
+
* // @log: '{"foo":"bar","baz":"69420694206942069420694206942069420694206942069420#__bigint"}'
|
|
484
|
+
* ```
|
|
485
|
+
*
|
|
486
|
+
* @param value - The value to stringify.
|
|
487
|
+
* @param replacer - A function that transforms the results. It is passed the key and value of the property, and must return the value to be used in the JSON string. If this function returns `undefined`, the property is not included in the resulting JSON string.
|
|
488
|
+
* @param space - A string or number that determines the indentation of the JSON string. If it is a number, it indicates the number of spaces to use as indentation; if it is a string (e.g. `'\t'`), it uses the string as the indentation character.
|
|
489
|
+
* @returns The JSON string.
|
|
490
|
+
*/
|
|
491
|
+
function stringify(value, replacer, space) {
|
|
492
|
+
return JSON.stringify(value, (key, value) => {
|
|
493
|
+
if (typeof replacer === "function") return replacer(key, value);
|
|
494
|
+
if (typeof value === "bigint") return value.toString() + bigIntSuffix;
|
|
495
|
+
return value;
|
|
496
|
+
}, space);
|
|
497
|
+
}
|
|
498
|
+
//#endregion
|
|
499
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/Hex.js
|
|
500
|
+
const encoder$2 = /* @__PURE__ */ new TextEncoder();
|
|
501
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_v, i) => i.toString(16).padStart(2, "0"));
|
|
502
|
+
/**
|
|
503
|
+
* Asserts if the given value is {@link ox#Hex.Hex}.
|
|
504
|
+
*
|
|
505
|
+
* @example
|
|
506
|
+
* ```ts twoslash
|
|
507
|
+
* import { Hex } from 'ox'
|
|
508
|
+
*
|
|
509
|
+
* Hex.assert('abc')
|
|
510
|
+
* // @error: InvalidHexValueTypeError:
|
|
511
|
+
* // @error: Value `"abc"` of type `string` is an invalid hex type.
|
|
512
|
+
* // @error: Hex types must be represented as `"0x\${string}"`.
|
|
513
|
+
* ```
|
|
514
|
+
*
|
|
515
|
+
* @param value - The value to assert.
|
|
516
|
+
* @param options - Options.
|
|
517
|
+
*/
|
|
518
|
+
function assert$1(value, options = {}) {
|
|
519
|
+
const { strict = false } = options;
|
|
520
|
+
if (!value) throw new InvalidHexTypeError(value);
|
|
521
|
+
if (typeof value !== "string") throw new InvalidHexTypeError(value);
|
|
522
|
+
if (strict) {
|
|
523
|
+
if (!/^0x[0-9a-fA-F]*$/.test(value)) throw new InvalidHexValueError(value);
|
|
524
|
+
}
|
|
525
|
+
if (!value.startsWith("0x")) throw new InvalidHexValueError(value);
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Concatenates two or more {@link ox#Hex.Hex}.
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```ts twoslash
|
|
532
|
+
* import { Hex } from 'ox'
|
|
533
|
+
*
|
|
534
|
+
* Hex.concat('0x123', '0x456')
|
|
535
|
+
* // @log: '0x123456'
|
|
536
|
+
* ```
|
|
537
|
+
*
|
|
538
|
+
* @param values - The {@link ox#Hex.Hex} values to concatenate.
|
|
539
|
+
* @returns The concatenated {@link ox#Hex.Hex} value.
|
|
540
|
+
*/
|
|
541
|
+
function concat(...values) {
|
|
542
|
+
return `0x${values.reduce((acc, x) => acc + x.replace("0x", ""), "")}`;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Instantiates a {@link ox#Hex.Hex} value from a hex string or {@link ox#Bytes.Bytes} value.
|
|
546
|
+
*
|
|
547
|
+
* :::tip
|
|
548
|
+
*
|
|
549
|
+
* To instantiate from a **Boolean**, **String**, or **Number**, use one of the following:
|
|
550
|
+
*
|
|
551
|
+
* - `Hex.fromBoolean`
|
|
552
|
+
*
|
|
553
|
+
* - `Hex.fromString`
|
|
554
|
+
*
|
|
555
|
+
* - `Hex.fromNumber`
|
|
556
|
+
*
|
|
557
|
+
* :::
|
|
558
|
+
*
|
|
559
|
+
* @example
|
|
560
|
+
* ```ts twoslash
|
|
561
|
+
* import { Bytes, Hex } from 'ox'
|
|
562
|
+
*
|
|
563
|
+
* Hex.from('0x48656c6c6f20576f726c6421')
|
|
564
|
+
* // @log: '0x48656c6c6f20576f726c6421'
|
|
565
|
+
*
|
|
566
|
+
* Hex.from(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
|
|
567
|
+
* // @log: '0x48656c6c6f20576f726c6421'
|
|
568
|
+
* ```
|
|
569
|
+
*
|
|
570
|
+
* @param value - The {@link ox#Bytes.Bytes} value to encode.
|
|
571
|
+
* @returns The encoded {@link ox#Hex.Hex} value.
|
|
572
|
+
*/
|
|
573
|
+
function from$2(value) {
|
|
574
|
+
if (value instanceof Uint8Array) return fromBytes$1(value);
|
|
575
|
+
if (Array.isArray(value)) return fromBytes$1(new Uint8Array(value));
|
|
576
|
+
return value;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Encodes a boolean into a {@link ox#Hex.Hex} value.
|
|
580
|
+
*
|
|
581
|
+
* @example
|
|
582
|
+
* ```ts twoslash
|
|
583
|
+
* import { Hex } from 'ox'
|
|
584
|
+
*
|
|
585
|
+
* Hex.fromBoolean(true)
|
|
586
|
+
* // @log: '0x1'
|
|
587
|
+
*
|
|
588
|
+
* Hex.fromBoolean(false)
|
|
589
|
+
* // @log: '0x0'
|
|
590
|
+
*
|
|
591
|
+
* Hex.fromBoolean(true, { size: 32 })
|
|
592
|
+
* // @log: '0x0000000000000000000000000000000000000000000000000000000000000001'
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* @param value - The boolean value to encode.
|
|
596
|
+
* @param options - Options.
|
|
597
|
+
* @returns The encoded {@link ox#Hex.Hex} value.
|
|
598
|
+
*/
|
|
599
|
+
function fromBoolean(value, options = {}) {
|
|
600
|
+
const hex = `0x${Number(value)}`;
|
|
601
|
+
if (typeof options.size === "number") {
|
|
602
|
+
assertSize(hex, options.size);
|
|
603
|
+
return padLeft(hex, options.size);
|
|
604
|
+
}
|
|
605
|
+
return hex;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Encodes a {@link ox#Bytes.Bytes} value into a {@link ox#Hex.Hex} value.
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* ```ts twoslash
|
|
612
|
+
* import { Bytes, Hex } from 'ox'
|
|
613
|
+
*
|
|
614
|
+
* Hex.fromBytes(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
|
|
615
|
+
* // @log: '0x48656c6c6f20576f726c6421'
|
|
616
|
+
* ```
|
|
617
|
+
*
|
|
618
|
+
* @param value - The {@link ox#Bytes.Bytes} value to encode.
|
|
619
|
+
* @param options - Options.
|
|
620
|
+
* @returns The encoded {@link ox#Hex.Hex} value.
|
|
621
|
+
*/
|
|
622
|
+
function fromBytes$1(value, options = {}) {
|
|
623
|
+
let string = "";
|
|
624
|
+
for (let i = 0; i < value.length; i++) string += hexes[value[i]];
|
|
625
|
+
const hex = `0x${string}`;
|
|
626
|
+
if (typeof options.size === "number") {
|
|
627
|
+
assertSize(hex, options.size);
|
|
628
|
+
return padRight$1(hex, options.size);
|
|
629
|
+
}
|
|
630
|
+
return hex;
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Encodes a number or bigint into a {@link ox#Hex.Hex} value.
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```ts twoslash
|
|
637
|
+
* import { Hex } from 'ox'
|
|
638
|
+
*
|
|
639
|
+
* Hex.fromNumber(420)
|
|
640
|
+
* // @log: '0x1a4'
|
|
641
|
+
*
|
|
642
|
+
* Hex.fromNumber(420, { size: 32 })
|
|
643
|
+
* // @log: '0x00000000000000000000000000000000000000000000000000000000000001a4'
|
|
644
|
+
* ```
|
|
645
|
+
*
|
|
646
|
+
* @param value - The number or bigint value to encode.
|
|
647
|
+
* @param options - Options.
|
|
648
|
+
* @returns The encoded {@link ox#Hex.Hex} value.
|
|
649
|
+
*/
|
|
650
|
+
function fromNumber(value, options = {}) {
|
|
651
|
+
const { signed, size } = options;
|
|
652
|
+
const value_ = BigInt(value);
|
|
653
|
+
let maxValue;
|
|
654
|
+
if (size) if (signed) maxValue = (1n << BigInt(size) * 8n - 1n) - 1n;
|
|
655
|
+
else maxValue = 2n ** (BigInt(size) * 8n) - 1n;
|
|
656
|
+
else if (typeof value === "number") maxValue = BigInt(Number.MAX_SAFE_INTEGER);
|
|
657
|
+
const minValue = typeof maxValue === "bigint" && signed ? -maxValue - 1n : 0;
|
|
658
|
+
if (maxValue && value_ > maxValue || value_ < minValue) {
|
|
659
|
+
const suffix = typeof value === "bigint" ? "n" : "";
|
|
660
|
+
throw new IntegerOutOfRangeError({
|
|
661
|
+
max: maxValue ? `${maxValue}${suffix}` : void 0,
|
|
662
|
+
min: `${minValue}${suffix}`,
|
|
663
|
+
signed,
|
|
664
|
+
size,
|
|
665
|
+
value: `${value}${suffix}`
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
const hex = `0x${(signed && value_ < 0 ? BigInt.asUintN(size * 8, BigInt(value_)) : value_).toString(16)}`;
|
|
669
|
+
if (size) return padLeft(hex, size);
|
|
670
|
+
return hex;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Encodes a string into a {@link ox#Hex.Hex} value.
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* ```ts twoslash
|
|
677
|
+
* import { Hex } from 'ox'
|
|
678
|
+
* Hex.fromString('Hello World!')
|
|
679
|
+
* // '0x48656c6c6f20576f726c6421'
|
|
680
|
+
*
|
|
681
|
+
* Hex.fromString('Hello World!', { size: 32 })
|
|
682
|
+
* // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
|
|
683
|
+
* ```
|
|
684
|
+
*
|
|
685
|
+
* @param value - The string value to encode.
|
|
686
|
+
* @param options - Options.
|
|
687
|
+
* @returns The encoded {@link ox#Hex.Hex} value.
|
|
688
|
+
*/
|
|
689
|
+
function fromString$2(value, options = {}) {
|
|
690
|
+
return fromBytes$1(encoder$2.encode(value), options);
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Pads a {@link ox#Hex.Hex} value to the left with zero bytes until it reaches the given `size` (default: 32 bytes).
|
|
694
|
+
*
|
|
695
|
+
* @example
|
|
696
|
+
* ```ts twoslash
|
|
697
|
+
* import { Hex } from 'ox'
|
|
698
|
+
*
|
|
699
|
+
* Hex.padLeft('0x1234', 4)
|
|
700
|
+
* // @log: '0x00001234'
|
|
701
|
+
* ```
|
|
702
|
+
*
|
|
703
|
+
* @param value - The {@link ox#Hex.Hex} value to pad.
|
|
704
|
+
* @param size - The size (in bytes) of the output hex value.
|
|
705
|
+
* @returns The padded {@link ox#Hex.Hex} value.
|
|
706
|
+
*/
|
|
707
|
+
function padLeft(value, size) {
|
|
708
|
+
return pad(value, {
|
|
709
|
+
dir: "left",
|
|
710
|
+
size
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Pads a {@link ox#Hex.Hex} value to the right with zero bytes until it reaches the given `size` (default: 32 bytes).
|
|
715
|
+
*
|
|
716
|
+
* @example
|
|
717
|
+
* ```ts
|
|
718
|
+
* import { Hex } from 'ox'
|
|
719
|
+
*
|
|
720
|
+
* Hex.padRight('0x1234', 4)
|
|
721
|
+
* // @log: '0x12340000'
|
|
722
|
+
* ```
|
|
723
|
+
*
|
|
724
|
+
* @param value - The {@link ox#Hex.Hex} value to pad.
|
|
725
|
+
* @param size - The size (in bytes) of the output hex value.
|
|
726
|
+
* @returns The padded {@link ox#Hex.Hex} value.
|
|
727
|
+
*/
|
|
728
|
+
function padRight$1(value, size) {
|
|
729
|
+
return pad(value, {
|
|
730
|
+
dir: "right",
|
|
731
|
+
size
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Generates a random {@link ox#Hex.Hex} value of the specified length.
|
|
736
|
+
*
|
|
737
|
+
* @example
|
|
738
|
+
* ```ts twoslash
|
|
739
|
+
* import { Hex } from 'ox'
|
|
740
|
+
*
|
|
741
|
+
* const hex = Hex.random(32)
|
|
742
|
+
* // @log: '0x...'
|
|
743
|
+
* ```
|
|
744
|
+
*
|
|
745
|
+
* @returns Random {@link ox#Hex.Hex} value.
|
|
746
|
+
*/
|
|
747
|
+
function random$1(length) {
|
|
748
|
+
return fromBytes$1(random(length));
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Returns a section of a {@link ox#Bytes.Bytes} value given a start/end bytes offset.
|
|
752
|
+
*
|
|
753
|
+
* @example
|
|
754
|
+
* ```ts twoslash
|
|
755
|
+
* import { Hex } from 'ox'
|
|
756
|
+
*
|
|
757
|
+
* Hex.slice('0x0123456789', 1, 4)
|
|
758
|
+
* // @log: '0x234567'
|
|
759
|
+
* ```
|
|
760
|
+
*
|
|
761
|
+
* @param value - The {@link ox#Hex.Hex} value to slice.
|
|
762
|
+
* @param start - The start offset (in bytes).
|
|
763
|
+
* @param end - The end offset (in bytes).
|
|
764
|
+
* @param options - Options.
|
|
765
|
+
* @returns The sliced {@link ox#Hex.Hex} value.
|
|
766
|
+
*/
|
|
767
|
+
function slice(value, start, end, options = {}) {
|
|
768
|
+
const { strict } = options;
|
|
769
|
+
assertStartOffset(value, start);
|
|
770
|
+
const value_ = `0x${value.replace("0x", "").slice((start ?? 0) * 2, (end ?? value.length) * 2)}`;
|
|
771
|
+
if (strict) assertEndOffset(value_, start, end);
|
|
772
|
+
return value_;
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Retrieves the size of a {@link ox#Hex.Hex} value (in bytes).
|
|
776
|
+
*
|
|
777
|
+
* @example
|
|
778
|
+
* ```ts twoslash
|
|
779
|
+
* import { Hex } from 'ox'
|
|
780
|
+
*
|
|
781
|
+
* Hex.size('0xdeadbeef')
|
|
782
|
+
* // @log: 4
|
|
783
|
+
* ```
|
|
784
|
+
*
|
|
785
|
+
* @param value - The {@link ox#Hex.Hex} value to get the size of.
|
|
786
|
+
* @returns The size of the {@link ox#Hex.Hex} value (in bytes).
|
|
787
|
+
*/
|
|
788
|
+
function size$1(value) {
|
|
789
|
+
return Math.ceil((value.length - 2) / 2);
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Decodes a {@link ox#Hex.Hex} value into a BigInt.
|
|
793
|
+
*
|
|
794
|
+
* @example
|
|
795
|
+
* ```ts twoslash
|
|
796
|
+
* import { Hex } from 'ox'
|
|
797
|
+
*
|
|
798
|
+
* Hex.toBigInt('0x1a4')
|
|
799
|
+
* // @log: 420n
|
|
800
|
+
*
|
|
801
|
+
* Hex.toBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
|
|
802
|
+
* // @log: 420n
|
|
803
|
+
* ```
|
|
804
|
+
*
|
|
805
|
+
* @param hex - The {@link ox#Hex.Hex} value to decode.
|
|
806
|
+
* @param options - Options.
|
|
807
|
+
* @returns The decoded BigInt.
|
|
808
|
+
*/
|
|
809
|
+
function toBigInt(hex, options = {}) {
|
|
810
|
+
const { signed } = options;
|
|
811
|
+
if (options.size) assertSize(hex, options.size);
|
|
812
|
+
const value = BigInt(hex);
|
|
813
|
+
if (!signed) return value;
|
|
814
|
+
const size = (hex.length - 2) / 2;
|
|
815
|
+
const max_unsigned = (1n << BigInt(size) * 8n) - 1n;
|
|
816
|
+
if (value <= max_unsigned >> 1n) return value;
|
|
817
|
+
return value - max_unsigned - 1n;
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Decodes a {@link ox#Hex.Hex} value into a {@link ox#Bytes.Bytes}.
|
|
821
|
+
*
|
|
822
|
+
* @example
|
|
823
|
+
* ```ts twoslash
|
|
824
|
+
* import { Hex } from 'ox'
|
|
825
|
+
*
|
|
826
|
+
* const data = Hex.toBytes('0x48656c6c6f20776f726c6421')
|
|
827
|
+
* // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
|
828
|
+
* ```
|
|
829
|
+
*
|
|
830
|
+
* @param hex - The {@link ox#Hex.Hex} value to decode.
|
|
831
|
+
* @param options - Options.
|
|
832
|
+
* @returns The decoded {@link ox#Bytes.Bytes}.
|
|
833
|
+
*/
|
|
834
|
+
function toBytes$1(hex, options = {}) {
|
|
835
|
+
return fromHex(hex, options);
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Decodes a {@link ox#Hex.Hex} value into a number.
|
|
839
|
+
*
|
|
840
|
+
* @example
|
|
841
|
+
* ```ts twoslash
|
|
842
|
+
* import { Hex } from 'ox'
|
|
843
|
+
*
|
|
844
|
+
* Hex.toNumber('0x1a4')
|
|
845
|
+
* // @log: 420
|
|
846
|
+
*
|
|
847
|
+
* Hex.toNumber('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
|
|
848
|
+
* // @log: 420
|
|
849
|
+
* ```
|
|
850
|
+
*
|
|
851
|
+
* @param hex - The {@link ox#Hex.Hex} value to decode.
|
|
852
|
+
* @param options - Options.
|
|
853
|
+
* @returns The decoded number.
|
|
854
|
+
*/
|
|
855
|
+
function toNumber(hex, options = {}) {
|
|
856
|
+
const { signed, size } = options;
|
|
857
|
+
if (!signed && !size) return Number(hex);
|
|
858
|
+
return Number(toBigInt(hex, options));
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Decodes a {@link ox#Hex.Hex} value into a string.
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
864
|
+
* ```ts twoslash
|
|
865
|
+
* import { Hex } from 'ox'
|
|
866
|
+
*
|
|
867
|
+
* Hex.toString('0x48656c6c6f20576f726c6421')
|
|
868
|
+
* // @log: 'Hello world!'
|
|
869
|
+
*
|
|
870
|
+
* Hex.toString('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', {
|
|
871
|
+
* size: 32,
|
|
872
|
+
* })
|
|
873
|
+
* // @log: 'Hello world'
|
|
874
|
+
* ```
|
|
875
|
+
*
|
|
876
|
+
* @param hex - The {@link ox#Hex.Hex} value to decode.
|
|
877
|
+
* @param options - Options.
|
|
878
|
+
* @returns The decoded string.
|
|
879
|
+
*/
|
|
880
|
+
function toString$2(hex, options = {}) {
|
|
881
|
+
const { size } = options;
|
|
882
|
+
let bytes = fromHex(hex);
|
|
883
|
+
if (size) {
|
|
884
|
+
assertSize$1(bytes, size);
|
|
885
|
+
bytes = trimRight(bytes);
|
|
886
|
+
}
|
|
887
|
+
return new TextDecoder().decode(bytes);
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Checks if the given value is {@link ox#Hex.Hex}.
|
|
891
|
+
*
|
|
892
|
+
* @example
|
|
893
|
+
* ```ts twoslash
|
|
894
|
+
* import { Bytes, Hex } from 'ox'
|
|
895
|
+
*
|
|
896
|
+
* Hex.validate('0xdeadbeef')
|
|
897
|
+
* // @log: true
|
|
898
|
+
*
|
|
899
|
+
* Hex.validate(Bytes.from([1, 2, 3]))
|
|
900
|
+
* // @log: false
|
|
901
|
+
* ```
|
|
902
|
+
*
|
|
903
|
+
* @param value - The value to check.
|
|
904
|
+
* @param options - Options.
|
|
905
|
+
* @returns `true` if the value is a {@link ox#Hex.Hex}, `false` otherwise.
|
|
906
|
+
*/
|
|
907
|
+
function validate$1(value, options = {}) {
|
|
908
|
+
const { strict = false } = options;
|
|
909
|
+
try {
|
|
910
|
+
assert$1(value, { strict });
|
|
911
|
+
return true;
|
|
912
|
+
} catch {
|
|
913
|
+
return false;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Thrown when the provided integer is out of range, and cannot be represented as a hex value.
|
|
918
|
+
*
|
|
919
|
+
* @example
|
|
920
|
+
* ```ts twoslash
|
|
921
|
+
* import { Hex } from 'ox'
|
|
922
|
+
*
|
|
923
|
+
* Hex.fromNumber(420182738912731283712937129)
|
|
924
|
+
* // @error: Hex.IntegerOutOfRangeError: Number \`4.2018273891273126e+26\` is not in safe unsigned integer range (`0` to `9007199254740991`)
|
|
925
|
+
* ```
|
|
926
|
+
*/
|
|
927
|
+
var IntegerOutOfRangeError = class extends BaseError {
|
|
928
|
+
constructor({ max, min, signed, size, value }) {
|
|
929
|
+
super(`Number \`${value}\` is not in safe${size ? ` ${size * 8}-bit` : ""}${signed ? " signed" : " unsigned"} integer range ${max ? `(\`${min}\` to \`${max}\`)` : `(above \`${min}\`)`}`);
|
|
930
|
+
Object.defineProperty(this, "name", {
|
|
931
|
+
enumerable: true,
|
|
932
|
+
configurable: true,
|
|
933
|
+
writable: true,
|
|
934
|
+
value: "Hex.IntegerOutOfRangeError"
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
/**
|
|
939
|
+
* Thrown when the provided value is not a valid hex type.
|
|
940
|
+
*
|
|
941
|
+
* @example
|
|
942
|
+
* ```ts twoslash
|
|
943
|
+
* import { Hex } from 'ox'
|
|
944
|
+
*
|
|
945
|
+
* Hex.assert(1)
|
|
946
|
+
* // @error: Hex.InvalidHexTypeError: Value `1` of type `number` is an invalid hex type.
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
var InvalidHexTypeError = class extends BaseError {
|
|
950
|
+
constructor(value) {
|
|
951
|
+
super(`Value \`${typeof value === "object" ? stringify(value) : value}\` of type \`${typeof value}\` is an invalid hex type.`, { metaMessages: ["Hex types must be represented as `\"0x${string}\"`."] });
|
|
952
|
+
Object.defineProperty(this, "name", {
|
|
953
|
+
enumerable: true,
|
|
954
|
+
configurable: true,
|
|
955
|
+
writable: true,
|
|
956
|
+
value: "Hex.InvalidHexTypeError"
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
};
|
|
960
|
+
/**
|
|
961
|
+
* Thrown when the provided hex value is invalid.
|
|
962
|
+
*
|
|
963
|
+
* @example
|
|
964
|
+
* ```ts twoslash
|
|
965
|
+
* import { Hex } from 'ox'
|
|
966
|
+
*
|
|
967
|
+
* Hex.assert('0x0123456789abcdefg')
|
|
968
|
+
* // @error: Hex.InvalidHexValueError: Value `0x0123456789abcdefg` is an invalid hex value.
|
|
969
|
+
* // @error: Hex values must start with `"0x"` and contain only hexadecimal characters (0-9, a-f, A-F).
|
|
970
|
+
* ```
|
|
971
|
+
*/
|
|
972
|
+
var InvalidHexValueError = class extends BaseError {
|
|
973
|
+
constructor(value) {
|
|
974
|
+
super(`Value \`${value}\` is an invalid hex value.`, { metaMessages: ["Hex values must start with `\"0x\"` and contain only hexadecimal characters (0-9, a-f, A-F)."] });
|
|
975
|
+
Object.defineProperty(this, "name", {
|
|
976
|
+
enumerable: true,
|
|
977
|
+
configurable: true,
|
|
978
|
+
writable: true,
|
|
979
|
+
value: "Hex.InvalidHexValueError"
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
/**
|
|
984
|
+
* Thrown when the size of the value exceeds the expected max size.
|
|
985
|
+
*
|
|
986
|
+
* @example
|
|
987
|
+
* ```ts twoslash
|
|
988
|
+
* import { Hex } from 'ox'
|
|
989
|
+
*
|
|
990
|
+
* Hex.fromString('Hello World!', { size: 8 })
|
|
991
|
+
* // @error: Hex.SizeOverflowError: Size cannot exceed `8` bytes. Given size: `12` bytes.
|
|
992
|
+
* ```
|
|
993
|
+
*/
|
|
994
|
+
var SizeOverflowError$1 = class extends BaseError {
|
|
995
|
+
constructor({ givenSize, maxSize }) {
|
|
996
|
+
super(`Size cannot exceed \`${maxSize}\` bytes. Given size: \`${givenSize}\` bytes.`);
|
|
997
|
+
Object.defineProperty(this, "name", {
|
|
998
|
+
enumerable: true,
|
|
999
|
+
configurable: true,
|
|
1000
|
+
writable: true,
|
|
1001
|
+
value: "Hex.SizeOverflowError"
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
/**
|
|
1006
|
+
* Thrown when the slice offset exceeds the bounds of the value.
|
|
1007
|
+
*
|
|
1008
|
+
* @example
|
|
1009
|
+
* ```ts twoslash
|
|
1010
|
+
* import { Hex } from 'ox'
|
|
1011
|
+
*
|
|
1012
|
+
* Hex.slice('0x0123456789', 6)
|
|
1013
|
+
* // @error: Hex.SliceOffsetOutOfBoundsError: Slice starting at offset `6` is out-of-bounds (size: `5`).
|
|
1014
|
+
* ```
|
|
1015
|
+
*/
|
|
1016
|
+
var SliceOffsetOutOfBoundsError = class extends BaseError {
|
|
1017
|
+
constructor({ offset, position, size }) {
|
|
1018
|
+
super(`Slice ${position === "start" ? "starting" : "ending"} at offset \`${offset}\` is out-of-bounds (size: \`${size}\`).`);
|
|
1019
|
+
Object.defineProperty(this, "name", {
|
|
1020
|
+
enumerable: true,
|
|
1021
|
+
configurable: true,
|
|
1022
|
+
writable: true,
|
|
1023
|
+
value: "Hex.SliceOffsetOutOfBoundsError"
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
/**
|
|
1028
|
+
* Thrown when the size of the value exceeds the pad size.
|
|
1029
|
+
*
|
|
1030
|
+
* @example
|
|
1031
|
+
* ```ts twoslash
|
|
1032
|
+
* import { Hex } from 'ox'
|
|
1033
|
+
*
|
|
1034
|
+
* Hex.padLeft('0x1a4e12a45a21323123aaa87a897a897a898a6567a578a867a98778a667a85a875a87a6a787a65a675a6a9', 32)
|
|
1035
|
+
* // @error: Hex.SizeExceedsPaddingSizeError: Hex size (`43`) exceeds padding size (`32`).
|
|
1036
|
+
* ```
|
|
1037
|
+
*/
|
|
1038
|
+
var SizeExceedsPaddingSizeError$1 = class extends BaseError {
|
|
1039
|
+
constructor({ size, targetSize, type }) {
|
|
1040
|
+
super(`${type.charAt(0).toUpperCase()}${type.slice(1).toLowerCase()} size (\`${size}\`) exceeds padding size (\`${targetSize}\`).`);
|
|
1041
|
+
Object.defineProperty(this, "name", {
|
|
1042
|
+
enumerable: true,
|
|
1043
|
+
configurable: true,
|
|
1044
|
+
writable: true,
|
|
1045
|
+
value: "Hex.SizeExceedsPaddingSizeError"
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
};
|
|
1049
|
+
//#endregion
|
|
1050
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/Bytes.js
|
|
1051
|
+
const decoder$1 = /* @__PURE__ */ new TextDecoder();
|
|
1052
|
+
const encoder$1 = /* @__PURE__ */ new TextEncoder();
|
|
1053
|
+
/**
|
|
1054
|
+
* Asserts if the given value is {@link ox#Bytes.Bytes}.
|
|
1055
|
+
*
|
|
1056
|
+
* @example
|
|
1057
|
+
* ```ts twoslash
|
|
1058
|
+
* import { Bytes } from 'ox'
|
|
1059
|
+
*
|
|
1060
|
+
* Bytes.assert('abc')
|
|
1061
|
+
* // @error: Bytes.InvalidBytesTypeError:
|
|
1062
|
+
* // @error: Value `"abc"` of type `string` is an invalid Bytes value.
|
|
1063
|
+
* // @error: Bytes values must be of type `Uint8Array`.
|
|
1064
|
+
* ```
|
|
1065
|
+
*
|
|
1066
|
+
* @param value - Value to assert.
|
|
1067
|
+
*/
|
|
1068
|
+
function assert(value) {
|
|
1069
|
+
if (value instanceof Uint8Array) return;
|
|
1070
|
+
if (!value) throw new InvalidBytesTypeError(value);
|
|
1071
|
+
if (typeof value !== "object") throw new InvalidBytesTypeError(value);
|
|
1072
|
+
if (!("BYTES_PER_ELEMENT" in value)) throw new InvalidBytesTypeError(value);
|
|
1073
|
+
if (value.BYTES_PER_ELEMENT !== 1 || value.constructor.name !== "Uint8Array") throw new InvalidBytesTypeError(value);
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Instantiates a {@link ox#Bytes.Bytes} value from a `Uint8Array`, a hex string, or an array of unsigned 8-bit integers.
|
|
1077
|
+
*
|
|
1078
|
+
* :::tip
|
|
1079
|
+
*
|
|
1080
|
+
* To instantiate from a **Boolean**, **String**, or **Number**, use one of the following:
|
|
1081
|
+
*
|
|
1082
|
+
* - `Bytes.fromBoolean`
|
|
1083
|
+
*
|
|
1084
|
+
* - `Bytes.fromString`
|
|
1085
|
+
*
|
|
1086
|
+
* - `Bytes.fromNumber`
|
|
1087
|
+
*
|
|
1088
|
+
* :::
|
|
1089
|
+
*
|
|
1090
|
+
* @example
|
|
1091
|
+
* ```ts twoslash
|
|
1092
|
+
* // @noErrors
|
|
1093
|
+
* import { Bytes } from 'ox'
|
|
1094
|
+
*
|
|
1095
|
+
* const data = Bytes.from([255, 124, 5, 4])
|
|
1096
|
+
* // @log: Uint8Array([255, 124, 5, 4])
|
|
1097
|
+
*
|
|
1098
|
+
* const data = Bytes.from('0xdeadbeef')
|
|
1099
|
+
* // @log: Uint8Array([222, 173, 190, 239])
|
|
1100
|
+
* ```
|
|
1101
|
+
*
|
|
1102
|
+
* @param value - Value to convert.
|
|
1103
|
+
* @returns A {@link ox#Bytes.Bytes} instance.
|
|
1104
|
+
*/
|
|
1105
|
+
function from$1(value) {
|
|
1106
|
+
if (value instanceof Uint8Array) return value;
|
|
1107
|
+
if (typeof value === "string") return fromHex(value);
|
|
1108
|
+
return fromArray(value);
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Converts an array of unsigned 8-bit integers into {@link ox#Bytes.Bytes}.
|
|
1112
|
+
*
|
|
1113
|
+
* @example
|
|
1114
|
+
* ```ts twoslash
|
|
1115
|
+
* import { Bytes } from 'ox'
|
|
1116
|
+
*
|
|
1117
|
+
* const data = Bytes.fromArray([255, 124, 5, 4])
|
|
1118
|
+
* // @log: Uint8Array([255, 124, 5, 4])
|
|
1119
|
+
* ```
|
|
1120
|
+
*
|
|
1121
|
+
* @param value - Value to convert.
|
|
1122
|
+
* @returns A {@link ox#Bytes.Bytes} instance.
|
|
1123
|
+
*/
|
|
1124
|
+
function fromArray(value) {
|
|
1125
|
+
return value instanceof Uint8Array ? value : new Uint8Array(value);
|
|
1126
|
+
}
|
|
1127
|
+
/**
|
|
1128
|
+
* Encodes a {@link ox#Hex.Hex} value into {@link ox#Bytes.Bytes}.
|
|
1129
|
+
*
|
|
1130
|
+
* @example
|
|
1131
|
+
* ```ts twoslash
|
|
1132
|
+
* import { Bytes } from 'ox'
|
|
1133
|
+
*
|
|
1134
|
+
* const data = Bytes.fromHex('0x48656c6c6f20776f726c6421')
|
|
1135
|
+
* // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
|
1136
|
+
* ```
|
|
1137
|
+
*
|
|
1138
|
+
* @example
|
|
1139
|
+
* ```ts twoslash
|
|
1140
|
+
* import { Bytes } from 'ox'
|
|
1141
|
+
*
|
|
1142
|
+
* const data = Bytes.fromHex('0x48656c6c6f20776f726c6421', { size: 32 })
|
|
1143
|
+
* // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
1144
|
+
* ```
|
|
1145
|
+
*
|
|
1146
|
+
* @param value - {@link ox#Hex.Hex} value to encode.
|
|
1147
|
+
* @param options - Encoding options.
|
|
1148
|
+
* @returns Encoded {@link ox#Bytes.Bytes}.
|
|
1149
|
+
*/
|
|
1150
|
+
function fromHex(value, options = {}) {
|
|
1151
|
+
const { size } = options;
|
|
1152
|
+
let hex = value;
|
|
1153
|
+
if (size) {
|
|
1154
|
+
assertSize(value, size);
|
|
1155
|
+
hex = padRight$1(value, size);
|
|
1156
|
+
}
|
|
1157
|
+
let hexString = hex.slice(2);
|
|
1158
|
+
if (hexString.length % 2) hexString = `0${hexString}`;
|
|
1159
|
+
const length = hexString.length / 2;
|
|
1160
|
+
const bytes = new Uint8Array(length);
|
|
1161
|
+
for (let index = 0, j = 0; index < length; index++) {
|
|
1162
|
+
const nibbleLeft = charCodeToBase16(hexString.charCodeAt(j++));
|
|
1163
|
+
const nibbleRight = charCodeToBase16(hexString.charCodeAt(j++));
|
|
1164
|
+
if (nibbleLeft === void 0 || nibbleRight === void 0) throw new BaseError(`Invalid byte sequence ("${hexString[j - 2]}${hexString[j - 1]}" in "${hexString}").`);
|
|
1165
|
+
bytes[index] = nibbleLeft << 4 | nibbleRight;
|
|
1166
|
+
}
|
|
1167
|
+
return bytes;
|
|
1168
|
+
}
|
|
1169
|
+
/**
|
|
1170
|
+
* Encodes a string into {@link ox#Bytes.Bytes}.
|
|
1171
|
+
*
|
|
1172
|
+
* @example
|
|
1173
|
+
* ```ts twoslash
|
|
1174
|
+
* import { Bytes } from 'ox'
|
|
1175
|
+
*
|
|
1176
|
+
* const data = Bytes.fromString('Hello world!')
|
|
1177
|
+
* // @log: Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
|
|
1178
|
+
* ```
|
|
1179
|
+
*
|
|
1180
|
+
* @example
|
|
1181
|
+
* ```ts twoslash
|
|
1182
|
+
* import { Bytes } from 'ox'
|
|
1183
|
+
*
|
|
1184
|
+
* const data = Bytes.fromString('Hello world!', { size: 32 })
|
|
1185
|
+
* // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
|
1186
|
+
* ```
|
|
1187
|
+
*
|
|
1188
|
+
* @param value - String to encode.
|
|
1189
|
+
* @param options - Encoding options.
|
|
1190
|
+
* @returns Encoded {@link ox#Bytes.Bytes}.
|
|
1191
|
+
*/
|
|
1192
|
+
function fromString$1(value, options = {}) {
|
|
1193
|
+
const { size } = options;
|
|
1194
|
+
const bytes = encoder$1.encode(value);
|
|
1195
|
+
if (typeof size === "number") {
|
|
1196
|
+
assertSize$1(bytes, size);
|
|
1197
|
+
return padRight(bytes, size);
|
|
1198
|
+
}
|
|
1199
|
+
return bytes;
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* Pads a {@link ox#Bytes.Bytes} value to the right with zero bytes until it reaches the given `size` (default: 32 bytes).
|
|
1203
|
+
*
|
|
1204
|
+
* @example
|
|
1205
|
+
* ```ts twoslash
|
|
1206
|
+
* import { Bytes } from 'ox'
|
|
1207
|
+
*
|
|
1208
|
+
* Bytes.padRight(Bytes.from([1]), 4)
|
|
1209
|
+
* // @log: Uint8Array([1, 0, 0, 0])
|
|
1210
|
+
* ```
|
|
1211
|
+
*
|
|
1212
|
+
* @param value - {@link ox#Bytes.Bytes} value to pad.
|
|
1213
|
+
* @param size - Size to pad the {@link ox#Bytes.Bytes} value to.
|
|
1214
|
+
* @returns Padded {@link ox#Bytes.Bytes} value.
|
|
1215
|
+
*/
|
|
1216
|
+
function padRight(value, size) {
|
|
1217
|
+
return pad$1(value, {
|
|
1218
|
+
dir: "right",
|
|
1219
|
+
size
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
/**
|
|
1223
|
+
* Generates random {@link ox#Bytes.Bytes} of the specified length.
|
|
1224
|
+
*
|
|
1225
|
+
* @example
|
|
1226
|
+
* ```ts twoslash
|
|
1227
|
+
* import { Bytes } from 'ox'
|
|
1228
|
+
*
|
|
1229
|
+
* const bytes = Bytes.random(32)
|
|
1230
|
+
* // @log: Uint8Array([... x32])
|
|
1231
|
+
* ```
|
|
1232
|
+
*
|
|
1233
|
+
* @param length - Length of the random {@link ox#Bytes.Bytes} to generate.
|
|
1234
|
+
* @returns Random {@link ox#Bytes.Bytes} of the specified length.
|
|
1235
|
+
*/
|
|
1236
|
+
function random(length) {
|
|
1237
|
+
return crypto.getRandomValues(new Uint8Array(length));
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Retrieves the size of a {@link ox#Bytes.Bytes} value.
|
|
1241
|
+
*
|
|
1242
|
+
* @example
|
|
1243
|
+
* ```ts twoslash
|
|
1244
|
+
* import { Bytes } from 'ox'
|
|
1245
|
+
*
|
|
1246
|
+
* Bytes.size(Bytes.from([1, 2, 3, 4]))
|
|
1247
|
+
* // @log: 4
|
|
1248
|
+
* ```
|
|
1249
|
+
*
|
|
1250
|
+
* @param value - {@link ox#Bytes.Bytes} value.
|
|
1251
|
+
* @returns Size of the {@link ox#Bytes.Bytes} value.
|
|
1252
|
+
*/
|
|
1253
|
+
function size(value) {
|
|
1254
|
+
return value.length;
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Decodes a {@link ox#Bytes.Bytes} into a string.
|
|
1258
|
+
*
|
|
1259
|
+
* @example
|
|
1260
|
+
* ```ts twoslash
|
|
1261
|
+
* import { Bytes } from 'ox'
|
|
1262
|
+
*
|
|
1263
|
+
* const data = Bytes.toString(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
|
|
1264
|
+
* // @log: 'Hello world'
|
|
1265
|
+
* ```
|
|
1266
|
+
*
|
|
1267
|
+
* @param bytes - The {@link ox#Bytes.Bytes} to decode.
|
|
1268
|
+
* @param options - Options.
|
|
1269
|
+
* @returns Decoded string.
|
|
1270
|
+
*/
|
|
1271
|
+
function toString$1(bytes, options = {}) {
|
|
1272
|
+
const { size } = options;
|
|
1273
|
+
let bytes_ = bytes;
|
|
1274
|
+
if (typeof size !== "undefined") {
|
|
1275
|
+
assertSize$1(bytes_, size);
|
|
1276
|
+
bytes_ = trimRight(bytes_);
|
|
1277
|
+
}
|
|
1278
|
+
return decoder$1.decode(bytes_);
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* Trims trailing zeros from a {@link ox#Bytes.Bytes} value.
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* ```ts twoslash
|
|
1285
|
+
* import { Bytes } from 'ox'
|
|
1286
|
+
*
|
|
1287
|
+
* Bytes.trimRight(Bytes.from([1, 2, 3, 0, 0, 0, 0]))
|
|
1288
|
+
* // @log: Uint8Array([1, 2, 3])
|
|
1289
|
+
* ```
|
|
1290
|
+
*
|
|
1291
|
+
* @param value - {@link ox#Bytes.Bytes} value.
|
|
1292
|
+
* @returns Trimmed {@link ox#Bytes.Bytes} value.
|
|
1293
|
+
*/
|
|
1294
|
+
function trimRight(value) {
|
|
1295
|
+
return trim(value, { dir: "right" });
|
|
1296
|
+
}
|
|
1297
|
+
/**
|
|
1298
|
+
* Checks if the given value is {@link ox#Bytes.Bytes}.
|
|
1299
|
+
*
|
|
1300
|
+
* @example
|
|
1301
|
+
* ```ts twoslash
|
|
1302
|
+
* import { Bytes } from 'ox'
|
|
1303
|
+
*
|
|
1304
|
+
* Bytes.validate('0x')
|
|
1305
|
+
* // @log: false
|
|
1306
|
+
*
|
|
1307
|
+
* Bytes.validate(Bytes.from([1, 2, 3]))
|
|
1308
|
+
* // @log: true
|
|
1309
|
+
* ```
|
|
1310
|
+
*
|
|
1311
|
+
* @param value - Value to check.
|
|
1312
|
+
* @returns `true` if the value is {@link ox#Bytes.Bytes}, otherwise `false`.
|
|
1313
|
+
*/
|
|
1314
|
+
function validate(value) {
|
|
1315
|
+
try {
|
|
1316
|
+
assert(value);
|
|
1317
|
+
return true;
|
|
1318
|
+
} catch {
|
|
1319
|
+
return false;
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
/**
|
|
1323
|
+
* Thrown when a value cannot be converted to bytes.
|
|
1324
|
+
*
|
|
1325
|
+
* @example
|
|
1326
|
+
* ```ts twoslash
|
|
1327
|
+
* // @noErrors
|
|
1328
|
+
* import { Bytes } from 'ox'
|
|
1329
|
+
*
|
|
1330
|
+
* Bytes.from('foo')
|
|
1331
|
+
* // @error: Bytes.InvalidBytesTypeError: Value `foo` of type `string` is an invalid Bytes value.
|
|
1332
|
+
* ```
|
|
1333
|
+
*/
|
|
1334
|
+
var InvalidBytesTypeError = class extends BaseError {
|
|
1335
|
+
constructor(value) {
|
|
1336
|
+
super(`Value \`${typeof value === "object" ? stringify(value) : value}\` of type \`${typeof value}\` is an invalid Bytes value.`, { metaMessages: ["Bytes values must be of type `Bytes`."] });
|
|
1337
|
+
Object.defineProperty(this, "name", {
|
|
1338
|
+
enumerable: true,
|
|
1339
|
+
configurable: true,
|
|
1340
|
+
writable: true,
|
|
1341
|
+
value: "Bytes.InvalidBytesTypeError"
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
/**
|
|
1346
|
+
* Thrown when a size exceeds the maximum allowed size.
|
|
1347
|
+
*
|
|
1348
|
+
* @example
|
|
1349
|
+
* ```ts twoslash
|
|
1350
|
+
* import { Bytes } from 'ox'
|
|
1351
|
+
*
|
|
1352
|
+
* Bytes.fromString('Hello World!', { size: 8 })
|
|
1353
|
+
* // @error: Bytes.SizeOverflowError: Size cannot exceed `8` bytes. Given size: `12` bytes.
|
|
1354
|
+
* ```
|
|
1355
|
+
*/
|
|
1356
|
+
var SizeOverflowError = class extends BaseError {
|
|
1357
|
+
constructor({ givenSize, maxSize }) {
|
|
1358
|
+
super(`Size cannot exceed \`${maxSize}\` bytes. Given size: \`${givenSize}\` bytes.`);
|
|
1359
|
+
Object.defineProperty(this, "name", {
|
|
1360
|
+
enumerable: true,
|
|
1361
|
+
configurable: true,
|
|
1362
|
+
writable: true,
|
|
1363
|
+
value: "Bytes.SizeOverflowError"
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
};
|
|
1367
|
+
/**
|
|
1368
|
+
* Thrown when a the padding size exceeds the maximum allowed size.
|
|
1369
|
+
*
|
|
1370
|
+
* @example
|
|
1371
|
+
* ```ts twoslash
|
|
1372
|
+
* import { Bytes } from 'ox'
|
|
1373
|
+
*
|
|
1374
|
+
* Bytes.padLeft(Bytes.fromString('Hello World!'), 8)
|
|
1375
|
+
* // @error: [Bytes.SizeExceedsPaddingSizeError: Bytes size (`12`) exceeds padding size (`8`).
|
|
1376
|
+
* ```
|
|
1377
|
+
*/
|
|
1378
|
+
var SizeExceedsPaddingSizeError = class extends BaseError {
|
|
1379
|
+
constructor({ size, targetSize, type }) {
|
|
1380
|
+
super(`${type.charAt(0).toUpperCase()}${type.slice(1).toLowerCase()} size (\`${size}\`) exceeds padding size (\`${targetSize}\`).`);
|
|
1381
|
+
Object.defineProperty(this, "name", {
|
|
1382
|
+
enumerable: true,
|
|
1383
|
+
configurable: true,
|
|
1384
|
+
writable: true,
|
|
1385
|
+
value: "Bytes.SizeExceedsPaddingSizeError"
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
};
|
|
1389
|
+
//#endregion
|
|
1390
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/Hash.js
|
|
1391
|
+
/**
|
|
1392
|
+
* Calculates the [Keccak256](https://en.wikipedia.org/wiki/SHA-3) hash of a {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
|
|
1393
|
+
*
|
|
1394
|
+
* This function is a re-export of `keccak_256` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library.
|
|
1395
|
+
*
|
|
1396
|
+
* @example
|
|
1397
|
+
* ```ts twoslash
|
|
1398
|
+
* import { Hash } from 'ox'
|
|
1399
|
+
*
|
|
1400
|
+
* Hash.keccak256('0xdeadbeef')
|
|
1401
|
+
* // @log: '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1'
|
|
1402
|
+
* ```
|
|
1403
|
+
*
|
|
1404
|
+
* @example
|
|
1405
|
+
* ### Calculate Hash of a String
|
|
1406
|
+
*
|
|
1407
|
+
* ```ts twoslash
|
|
1408
|
+
* import { Hash, Hex } from 'ox'
|
|
1409
|
+
*
|
|
1410
|
+
* Hash.keccak256(Hex.fromString('hello world'))
|
|
1411
|
+
* // @log: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0'
|
|
1412
|
+
* ```
|
|
1413
|
+
*
|
|
1414
|
+
* @example
|
|
1415
|
+
* ### Configure Return Type
|
|
1416
|
+
*
|
|
1417
|
+
* ```ts twoslash
|
|
1418
|
+
* import { Hash } from 'ox'
|
|
1419
|
+
*
|
|
1420
|
+
* Hash.keccak256('0xdeadbeef', { as: 'Bytes' })
|
|
1421
|
+
* // @log: Uint8Array [...]
|
|
1422
|
+
* ```
|
|
1423
|
+
*
|
|
1424
|
+
* @param value - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
|
|
1425
|
+
* @param options - Options.
|
|
1426
|
+
* @returns Keccak256 hash.
|
|
1427
|
+
*/
|
|
1428
|
+
function keccak256(value, options = {}) {
|
|
1429
|
+
const { as = typeof value === "string" ? "Hex" : "Bytes" } = options;
|
|
1430
|
+
const bytes = keccak_256(from$1(value));
|
|
1431
|
+
if (as === "Bytes") return bytes;
|
|
1432
|
+
return fromBytes$1(bytes);
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* Calculates the [HMAC-SHA256](https://en.wikipedia.org/wiki/HMAC) of a {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
|
|
1436
|
+
*
|
|
1437
|
+
* This function is a re-export of `hmac` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library.
|
|
1438
|
+
*
|
|
1439
|
+
* @example
|
|
1440
|
+
* ```ts twoslash
|
|
1441
|
+
* import { Hash, Hex } from 'ox'
|
|
1442
|
+
*
|
|
1443
|
+
* Hash.hmac256(Hex.fromString('key'), '0xdeadbeef')
|
|
1444
|
+
* // @log: '0x...'
|
|
1445
|
+
* ```
|
|
1446
|
+
*
|
|
1447
|
+
* @example
|
|
1448
|
+
* ### Configure Return Type
|
|
1449
|
+
*
|
|
1450
|
+
* ```ts twoslash
|
|
1451
|
+
* import { Hash, Hex } from 'ox'
|
|
1452
|
+
*
|
|
1453
|
+
* Hash.hmac256(Hex.fromString('key'), '0xdeadbeef', { as: 'Bytes' })
|
|
1454
|
+
* // @log: Uint8Array [...]
|
|
1455
|
+
* ```
|
|
1456
|
+
*
|
|
1457
|
+
* @param key - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} key.
|
|
1458
|
+
* @param value - {@link ox#Bytes.Bytes} or {@link ox#Hex.Hex} value.
|
|
1459
|
+
* @param options - Options.
|
|
1460
|
+
* @returns HMAC-SHA256 hash.
|
|
1461
|
+
*/
|
|
1462
|
+
function hmac256(key, value, options = {}) {
|
|
1463
|
+
const { as = typeof value === "string" ? "Hex" : "Bytes" } = options;
|
|
1464
|
+
const bytes = hmac(sha256, from$1(key), from$1(value));
|
|
1465
|
+
if (as === "Bytes") return bytes;
|
|
1466
|
+
return fromBytes$1(bytes);
|
|
1467
|
+
}
|
|
1468
|
+
//#endregion
|
|
1469
|
+
//#region node_modules/.pnpm/ox@0.14.10_typescript@5.9.3_zod@4.3.6/node_modules/ox/_esm/core/Base64.js
|
|
1470
|
+
const encoder = /* @__PURE__ */ new TextEncoder();
|
|
1471
|
+
const decoder = /* @__PURE__ */ new TextDecoder();
|
|
1472
|
+
const integerToCharacter = /* @__PURE__ */ Object.fromEntries(Array.from("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").map((a, i) => [i, a.charCodeAt(0)]));
|
|
1473
|
+
const characterToInteger = {
|
|
1474
|
+
...Object.fromEntries(Array.from("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").map((a, i) => [a.charCodeAt(0), i])),
|
|
1475
|
+
["=".charCodeAt(0)]: 0,
|
|
1476
|
+
["-".charCodeAt(0)]: 62,
|
|
1477
|
+
["_".charCodeAt(0)]: 63
|
|
1478
|
+
};
|
|
1479
|
+
/**
|
|
1480
|
+
* Encodes a {@link ox#Bytes.Bytes} to a Base64-encoded string (with optional padding and/or URL-safe characters).
|
|
1481
|
+
*
|
|
1482
|
+
* @example
|
|
1483
|
+
* ```ts twoslash
|
|
1484
|
+
* import { Base64, Bytes } from 'ox'
|
|
1485
|
+
*
|
|
1486
|
+
* const value = Base64.fromBytes(Bytes.fromString('hello world'))
|
|
1487
|
+
* // @log: 'aGVsbG8gd29ybGQ='
|
|
1488
|
+
* ```
|
|
1489
|
+
*
|
|
1490
|
+
* @example
|
|
1491
|
+
* ### No Padding
|
|
1492
|
+
*
|
|
1493
|
+
* Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option:
|
|
1494
|
+
*
|
|
1495
|
+
* ```ts twoslash
|
|
1496
|
+
* import { Base64, Bytes } from 'ox'
|
|
1497
|
+
*
|
|
1498
|
+
* const value = Base64.fromBytes(Bytes.fromString('hello world'), { pad: false })
|
|
1499
|
+
* // @log: 'aGVsbG8gd29ybGQ'
|
|
1500
|
+
* ```
|
|
1501
|
+
*
|
|
1502
|
+
* ### URL-safe Encoding
|
|
1503
|
+
*
|
|
1504
|
+
* Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option:
|
|
1505
|
+
*
|
|
1506
|
+
* ```ts twoslash
|
|
1507
|
+
* import { Base64, Bytes } from 'ox'
|
|
1508
|
+
*
|
|
1509
|
+
* const value = Base64.fromBytes(Bytes.fromString('hello wod'), { url: true })
|
|
1510
|
+
* // @log: 'aGVsbG8gd29_77-9ZA=='
|
|
1511
|
+
* ```
|
|
1512
|
+
*
|
|
1513
|
+
* @param value - The byte array to encode.
|
|
1514
|
+
* @param options - Encoding options.
|
|
1515
|
+
* @returns The Base64 encoded string.
|
|
1516
|
+
*/
|
|
1517
|
+
function fromBytes(value, options = {}) {
|
|
1518
|
+
const { pad = true, url = false } = options;
|
|
1519
|
+
const encoded = new Uint8Array(Math.ceil(value.length / 3) * 4);
|
|
1520
|
+
for (let i = 0, j = 0; j < value.length; i += 4, j += 3) {
|
|
1521
|
+
const y = (value[j] << 16) + (value[j + 1] << 8) + (value[j + 2] | 0);
|
|
1522
|
+
encoded[i] = integerToCharacter[y >> 18];
|
|
1523
|
+
encoded[i + 1] = integerToCharacter[y >> 12 & 63];
|
|
1524
|
+
encoded[i + 2] = integerToCharacter[y >> 6 & 63];
|
|
1525
|
+
encoded[i + 3] = integerToCharacter[y & 63];
|
|
1526
|
+
}
|
|
1527
|
+
const k = value.length % 3;
|
|
1528
|
+
const end = Math.floor(value.length / 3) * 4 + (k && k + 1);
|
|
1529
|
+
let base64 = decoder.decode(new Uint8Array(encoded.buffer, 0, end));
|
|
1530
|
+
if (pad && k === 1) base64 += "==";
|
|
1531
|
+
if (pad && k === 2) base64 += "=";
|
|
1532
|
+
if (url) base64 = base64.replaceAll("+", "-").replaceAll("/", "_");
|
|
1533
|
+
return base64;
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* Encodes a string to a Base64-encoded string (with optional padding and/or URL-safe characters).
|
|
1537
|
+
*
|
|
1538
|
+
* @example
|
|
1539
|
+
* ```ts twoslash
|
|
1540
|
+
* import { Base64 } from 'ox'
|
|
1541
|
+
*
|
|
1542
|
+
* const value = Base64.fromString('hello world')
|
|
1543
|
+
* // @log: 'aGVsbG8gd29ybGQ='
|
|
1544
|
+
* ```
|
|
1545
|
+
*
|
|
1546
|
+
* @example
|
|
1547
|
+
* ### No Padding
|
|
1548
|
+
*
|
|
1549
|
+
* Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option:
|
|
1550
|
+
*
|
|
1551
|
+
* ```ts twoslash
|
|
1552
|
+
* import { Base64 } from 'ox'
|
|
1553
|
+
*
|
|
1554
|
+
* const value = Base64.fromString('hello world', { pad: false })
|
|
1555
|
+
* // @log: 'aGVsbG8gd29ybGQ'
|
|
1556
|
+
* ```
|
|
1557
|
+
*
|
|
1558
|
+
* ### URL-safe Encoding
|
|
1559
|
+
*
|
|
1560
|
+
* Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option:
|
|
1561
|
+
*
|
|
1562
|
+
* ```ts twoslash
|
|
1563
|
+
* import { Base64 } from 'ox'
|
|
1564
|
+
*
|
|
1565
|
+
* const value = Base64.fromString('hello wod', { url: true })
|
|
1566
|
+
* // @log: 'aGVsbG8gd29_77-9ZA=='
|
|
1567
|
+
* ```
|
|
1568
|
+
*
|
|
1569
|
+
* @param value - The string to encode.
|
|
1570
|
+
* @param options - Encoding options.
|
|
1571
|
+
* @returns The Base64 encoded string.
|
|
1572
|
+
*/
|
|
1573
|
+
function fromString(value, options = {}) {
|
|
1574
|
+
return fromBytes(fromString$1(value), options);
|
|
1575
|
+
}
|
|
1576
|
+
/**
|
|
1577
|
+
* Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to {@link ox#Bytes.Bytes}.
|
|
1578
|
+
*
|
|
1579
|
+
* @example
|
|
1580
|
+
* ```ts twoslash
|
|
1581
|
+
* import { Base64, Bytes } from 'ox'
|
|
1582
|
+
*
|
|
1583
|
+
* const value = Base64.toBytes('aGVsbG8gd29ybGQ=')
|
|
1584
|
+
* // @log: Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
|
|
1585
|
+
* ```
|
|
1586
|
+
*
|
|
1587
|
+
* @param value - The string, hex value, or byte array to encode.
|
|
1588
|
+
* @returns The Base64 decoded {@link ox#Bytes.Bytes}.
|
|
1589
|
+
*/
|
|
1590
|
+
function toBytes(value) {
|
|
1591
|
+
const base64 = value.replace(/=+$/, "");
|
|
1592
|
+
const size = base64.length;
|
|
1593
|
+
const decoded = new Uint8Array(size + 3);
|
|
1594
|
+
encoder.encodeInto(base64 + "===", decoded);
|
|
1595
|
+
for (let i = 0, j = 0; i < base64.length; i += 4, j += 3) {
|
|
1596
|
+
const x = (characterToInteger[decoded[i]] << 18) + (characterToInteger[decoded[i + 1]] << 12) + (characterToInteger[decoded[i + 2]] << 6) + characterToInteger[decoded[i + 3]];
|
|
1597
|
+
decoded[j] = x >> 16;
|
|
1598
|
+
decoded[j + 1] = x >> 8 & 255;
|
|
1599
|
+
decoded[j + 2] = x & 255;
|
|
1600
|
+
}
|
|
1601
|
+
const decodedSize = (size >> 2) * 3 + (size % 4 && size % 4 - 1);
|
|
1602
|
+
return new Uint8Array(decoded.buffer, 0, decodedSize);
|
|
1603
|
+
}
|
|
1604
|
+
/**
|
|
1605
|
+
* Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to a string.
|
|
1606
|
+
*
|
|
1607
|
+
* @example
|
|
1608
|
+
* ```ts twoslash
|
|
1609
|
+
* import { Base64 } from 'ox'
|
|
1610
|
+
*
|
|
1611
|
+
* const value = Base64.toString('aGVsbG8gd29ybGQ=')
|
|
1612
|
+
* // @log: 'hello world'
|
|
1613
|
+
* ```
|
|
1614
|
+
*
|
|
1615
|
+
* @param value - The string, hex value, or byte array to encode.
|
|
1616
|
+
* @returns The Base64 decoded string.
|
|
1617
|
+
*/
|
|
1618
|
+
function toString(value) {
|
|
1619
|
+
return toString$1(toBytes(value));
|
|
1620
|
+
}
|
|
1621
|
+
//#endregion
|
|
1622
|
+
//#region node_modules/.pnpm/mppx@0.5.1_@modelcontextprotocol+sdk@1.28.0_zod@4.3.6__express@5.2.1_hono@4.12.9_openap_d8babfcabcc768186151f117bc0e35e5/node_modules/mppx/dist/PaymentRequest.js
|
|
1623
|
+
/**
|
|
1624
|
+
* Deserializes a base64url string to a request.
|
|
1625
|
+
*
|
|
1626
|
+
* @param encoded - The base64url-encoded string.
|
|
1627
|
+
* @returns The deserialized request.
|
|
1628
|
+
*
|
|
1629
|
+
* @example
|
|
1630
|
+
* ```ts
|
|
1631
|
+
* import { Request } from 'mppx'
|
|
1632
|
+
*
|
|
1633
|
+
* const request = Request.deserialize(serialized)
|
|
1634
|
+
* ```
|
|
1635
|
+
*/
|
|
1636
|
+
function deserialize$2(encoded) {
|
|
1637
|
+
const json = toString(encoded);
|
|
1638
|
+
return JSON.parse(json);
|
|
1639
|
+
}
|
|
1640
|
+
/**
|
|
1641
|
+
* Serializes a request to a base64url string.
|
|
1642
|
+
*
|
|
1643
|
+
* @param request - The request to serialize.
|
|
1644
|
+
* @returns A base64url-encoded string (no padding).
|
|
1645
|
+
*
|
|
1646
|
+
* @example
|
|
1647
|
+
* ```ts
|
|
1648
|
+
* import { Request } from 'mppx'
|
|
1649
|
+
*
|
|
1650
|
+
* const serialized = Request.serialize(request)
|
|
1651
|
+
* // => "eyJhbW91bnQiOiIxMDAwMDAwIiwiY3VycmVuY3kiOiIweC4uLiJ9"
|
|
1652
|
+
* ```
|
|
1653
|
+
*/
|
|
1654
|
+
function serialize$1(request) {
|
|
1655
|
+
return fromString(canonicalize(request), {
|
|
1656
|
+
pad: false,
|
|
1657
|
+
url: true
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
//#endregion
|
|
1661
|
+
//#region node_modules/.pnpm/mppx@0.5.1_@modelcontextprotocol+sdk@1.28.0_zod@4.3.6__express@5.2.1_hono@4.12.9_openap_d8babfcabcc768186151f117bc0e35e5/node_modules/mppx/dist/zod.js
|
|
1662
|
+
/** Numeric string amount (e.g., "1", "1.5", "1000000"). */
|
|
1663
|
+
function amount() {
|
|
1664
|
+
return (/* @__PURE__ */ string()).check(_regex(/^\d+(\.\d+)?$/, "Invalid amount"));
|
|
1665
|
+
}
|
|
1666
|
+
/** ISO 8601 datetime string (e.g., "2025-01-06T12:00:00Z"). */
|
|
1667
|
+
function datetime() {
|
|
1668
|
+
return (/* @__PURE__ */ string()).check(_regex(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/, "Invalid ISO 8601 datetime"));
|
|
1669
|
+
}
|
|
1670
|
+
/** Hex-encoded hash string (0x-prefixed, 64 hex chars). */
|
|
1671
|
+
function hash() {
|
|
1672
|
+
return (/* @__PURE__ */ string()).check(_regex(/^0x[0-9a-fA-F]{64}$/, "Invalid hash"));
|
|
1673
|
+
}
|
|
1674
|
+
/** Hex-encoded signature string (0x-prefixed). */
|
|
1675
|
+
function signature() {
|
|
1676
|
+
return (/* @__PURE__ */ string()).check(_regex(/^0x[0-9a-fA-F]+$/, "Invalid signature"));
|
|
1677
|
+
}
|
|
1678
|
+
//#endregion
|
|
1679
|
+
//#region node_modules/.pnpm/mppx@0.5.1_@modelcontextprotocol+sdk@1.28.0_zod@4.3.6__express@5.2.1_hono@4.12.9_openap_d8babfcabcc768186151f117bc0e35e5/node_modules/mppx/dist/Challenge.js
|
|
1680
|
+
/**
|
|
1681
|
+
* Schema for a payment challenge.
|
|
1682
|
+
*
|
|
1683
|
+
* @example
|
|
1684
|
+
* ```ts
|
|
1685
|
+
* import { Challenge } from 'mppx'
|
|
1686
|
+
*
|
|
1687
|
+
* const challenge = Challenge.Schema.parse(data)
|
|
1688
|
+
* ```
|
|
1689
|
+
*/
|
|
1690
|
+
const Schema = /* @__PURE__ */ object({
|
|
1691
|
+
description: /* @__PURE__ */ optional(/* @__PURE__ */ string()),
|
|
1692
|
+
digest: /* @__PURE__ */ optional((/* @__PURE__ */ string()).check(_regex(/^sha-256=/, "Invalid digest format"))),
|
|
1693
|
+
expires: /* @__PURE__ */ optional(datetime()),
|
|
1694
|
+
id: /* @__PURE__ */ string(),
|
|
1695
|
+
intent: /* @__PURE__ */ string(),
|
|
1696
|
+
method: /* @__PURE__ */ string(),
|
|
1697
|
+
opaque: /* @__PURE__ */ optional(/* @__PURE__ */ record(/* @__PURE__ */ string(), /* @__PURE__ */ string())),
|
|
1698
|
+
realm: /* @__PURE__ */ string(),
|
|
1699
|
+
request: /* @__PURE__ */ record(/* @__PURE__ */ string(), /* @__PURE__ */ unknown())
|
|
1700
|
+
});
|
|
1701
|
+
/**
|
|
1702
|
+
* Creates a challenge from the given parameters.
|
|
1703
|
+
*
|
|
1704
|
+
* If `secretKey` option is provided, the challenge ID is computed as HMAC-SHA256
|
|
1705
|
+
* over the challenge parameters (realm|method|intent|request|expires|digest),
|
|
1706
|
+
* cryptographically binding the ID to its contents.
|
|
1707
|
+
*
|
|
1708
|
+
* @param parameters - Challenge parameters.
|
|
1709
|
+
* @param options - Optional settings including secretKey for HMAC-bound ID.
|
|
1710
|
+
* @returns A challenge.
|
|
1711
|
+
*
|
|
1712
|
+
* @example
|
|
1713
|
+
* ```ts
|
|
1714
|
+
* import { Challenge } from 'mppx'
|
|
1715
|
+
*
|
|
1716
|
+
* // With HMAC-bound ID (recommended for servers)
|
|
1717
|
+
* const challenge = Challenge.from(
|
|
1718
|
+
* {
|
|
1719
|
+
* realm: 'api.example.com',
|
|
1720
|
+
* method: 'tempo',
|
|
1721
|
+
* intent: 'charge',
|
|
1722
|
+
* request: { amount: '1000000', currency: '0x...', recipient: '0x...' },
|
|
1723
|
+
* },
|
|
1724
|
+
* { secretKey: 'my-secret' },
|
|
1725
|
+
* )
|
|
1726
|
+
*
|
|
1727
|
+
* // With explicit ID
|
|
1728
|
+
* const challenge = Challenge.from({
|
|
1729
|
+
* id: 'abc123',
|
|
1730
|
+
* realm: 'api.example.com',
|
|
1731
|
+
* method: 'tempo',
|
|
1732
|
+
* intent: 'charge',
|
|
1733
|
+
* request: { amount: '1000000', currency: '0x...', recipient: '0x...' },
|
|
1734
|
+
* })
|
|
1735
|
+
* ```
|
|
1736
|
+
*/
|
|
1737
|
+
function from(parameters, options) {
|
|
1738
|
+
const { description, digest, meta, method: methodName, intent, realm, request, secretKey } = parameters;
|
|
1739
|
+
const expires = parameters.expires;
|
|
1740
|
+
const id = secretKey ? computeId({
|
|
1741
|
+
...parameters,
|
|
1742
|
+
expires,
|
|
1743
|
+
...meta && { opaque: meta }
|
|
1744
|
+
}, { secretKey }) : parameters.id;
|
|
1745
|
+
return Schema.parse({
|
|
1746
|
+
id,
|
|
1747
|
+
realm,
|
|
1748
|
+
method: methodName,
|
|
1749
|
+
intent,
|
|
1750
|
+
request,
|
|
1751
|
+
...description && { description },
|
|
1752
|
+
...digest && { digest },
|
|
1753
|
+
...expires && { expires },
|
|
1754
|
+
...meta && { opaque: meta }
|
|
1755
|
+
});
|
|
1756
|
+
}
|
|
1757
|
+
/**
|
|
1758
|
+
* Deserializes a WWW-Authenticate header value to a challenge.
|
|
1759
|
+
*
|
|
1760
|
+
* @example
|
|
1761
|
+
* ```ts
|
|
1762
|
+
* import { Challenge } from 'mppx'
|
|
1763
|
+
*
|
|
1764
|
+
* const challenge = Challenge.deserialize(header)
|
|
1765
|
+
*
|
|
1766
|
+
* // With methods for type narrowing
|
|
1767
|
+
* const challenge = Challenge.deserialize(header, { methods })
|
|
1768
|
+
* ```
|
|
1769
|
+
*
|
|
1770
|
+
* @param header - The WWW-Authenticate header value.
|
|
1771
|
+
* @param options - Optional settings to narrow the challenge type.
|
|
1772
|
+
* @returns The deserialized challenge.
|
|
1773
|
+
*/
|
|
1774
|
+
function deserialize$1(value, options) {
|
|
1775
|
+
const params = extractPaymentAuthParams(value);
|
|
1776
|
+
if (!params) throw new Error("Missing Payment scheme.");
|
|
1777
|
+
const { request, opaque, ...rest } = parseAuthParams(params);
|
|
1778
|
+
if (!request) throw new Error("Missing request parameter.");
|
|
1779
|
+
if (rest.method && !/^[a-z][a-z0-9:_-]*$/.test(rest.method)) throw new Error(`Invalid method: "${rest.method}". Must be lowercase per spec.`);
|
|
1780
|
+
return from({
|
|
1781
|
+
...rest,
|
|
1782
|
+
request: deserialize$2(request),
|
|
1783
|
+
...opaque && { meta: deserialize$2(opaque) }
|
|
1784
|
+
}, options);
|
|
1785
|
+
}
|
|
1786
|
+
/** @internal Extracts the `Payment` scheme from a WWW-Authenticate value that may contain multiple schemes. */
|
|
1787
|
+
function extractPaymentAuthParams(header) {
|
|
1788
|
+
const token = "Payment";
|
|
1789
|
+
let inQuotes = false;
|
|
1790
|
+
let escaped = false;
|
|
1791
|
+
for (let i = 0; i < header.length; i++) {
|
|
1792
|
+
const char = header[i];
|
|
1793
|
+
if (inQuotes) {
|
|
1794
|
+
if (escaped) escaped = false;
|
|
1795
|
+
else if (char === "\\") escaped = true;
|
|
1796
|
+
else if (char === "\"") inQuotes = false;
|
|
1797
|
+
continue;
|
|
1798
|
+
}
|
|
1799
|
+
if (char === "\"") {
|
|
1800
|
+
inQuotes = true;
|
|
1801
|
+
continue;
|
|
1802
|
+
}
|
|
1803
|
+
if (!startsWithSchemeToken(header, i, token)) continue;
|
|
1804
|
+
const prefix = header.slice(0, i);
|
|
1805
|
+
if (prefix.trim() && !prefix.trimEnd().endsWith(",")) continue;
|
|
1806
|
+
let paramsStart = i + 7;
|
|
1807
|
+
while (paramsStart < header.length && /\s/.test(header[paramsStart] ?? "")) paramsStart++;
|
|
1808
|
+
return header.slice(paramsStart);
|
|
1809
|
+
}
|
|
1810
|
+
return null;
|
|
1811
|
+
}
|
|
1812
|
+
/** @internal Parses auth-params with support for escaped quoted-string values. */
|
|
1813
|
+
function parseAuthParams(input) {
|
|
1814
|
+
const result = {};
|
|
1815
|
+
let i = 0;
|
|
1816
|
+
while (i < input.length) {
|
|
1817
|
+
while (i < input.length && /[\s,]/.test(input[i] ?? "")) i++;
|
|
1818
|
+
if (i >= input.length) break;
|
|
1819
|
+
const keyStart = i;
|
|
1820
|
+
while (i < input.length && /[A-Za-z0-9_-]/.test(input[i] ?? "")) i++;
|
|
1821
|
+
const key = input.slice(keyStart, i);
|
|
1822
|
+
if (!key) throw new Error("Malformed auth-param.");
|
|
1823
|
+
while (i < input.length && /\s/.test(input[i] ?? "")) i++;
|
|
1824
|
+
if (input[i] !== "=") break;
|
|
1825
|
+
i++;
|
|
1826
|
+
while (i < input.length && /\s/.test(input[i] ?? "")) i++;
|
|
1827
|
+
const [value, nextIndex] = readAuthParamValue(input, i);
|
|
1828
|
+
i = nextIndex;
|
|
1829
|
+
if (key in result) throw new Error(`Duplicate parameter: ${key}`);
|
|
1830
|
+
result[key] = value;
|
|
1831
|
+
}
|
|
1832
|
+
return result;
|
|
1833
|
+
}
|
|
1834
|
+
/** @internal */
|
|
1835
|
+
function readAuthParamValue(input, start) {
|
|
1836
|
+
if (input[start] === "\"") return readQuotedAuthParamValue(input, start + 1);
|
|
1837
|
+
let i = start;
|
|
1838
|
+
while (i < input.length && input[i] !== ",") i++;
|
|
1839
|
+
return [input.slice(start, i).trim(), i];
|
|
1840
|
+
}
|
|
1841
|
+
/** @internal */
|
|
1842
|
+
function readQuotedAuthParamValue(input, start) {
|
|
1843
|
+
let i = start;
|
|
1844
|
+
let value = "";
|
|
1845
|
+
let escaped = false;
|
|
1846
|
+
while (i < input.length) {
|
|
1847
|
+
const char = input[i];
|
|
1848
|
+
i++;
|
|
1849
|
+
if (escaped) {
|
|
1850
|
+
value += char;
|
|
1851
|
+
escaped = false;
|
|
1852
|
+
continue;
|
|
1853
|
+
}
|
|
1854
|
+
if (char === "\\") {
|
|
1855
|
+
escaped = true;
|
|
1856
|
+
continue;
|
|
1857
|
+
}
|
|
1858
|
+
if (char === "\"") return [value, i];
|
|
1859
|
+
value += char;
|
|
1860
|
+
}
|
|
1861
|
+
throw new Error("Unterminated quoted-string.");
|
|
1862
|
+
}
|
|
1863
|
+
/** @internal */
|
|
1864
|
+
function startsWithSchemeToken(value, index, token) {
|
|
1865
|
+
if (!value.slice(index).toLowerCase().startsWith(token.toLowerCase())) return false;
|
|
1866
|
+
const next = value[index + token.length];
|
|
1867
|
+
return Boolean(next && /\s/.test(next));
|
|
1868
|
+
}
|
|
1869
|
+
/**
|
|
1870
|
+
* Extracts the challenge from a Headers object.
|
|
1871
|
+
*
|
|
1872
|
+
* @param headers - The HTTP headers.
|
|
1873
|
+
* @param options - Optional settings to narrow the challenge type.
|
|
1874
|
+
* @returns The deserialized challenge.
|
|
1875
|
+
*
|
|
1876
|
+
* @example
|
|
1877
|
+
* ```ts
|
|
1878
|
+
* import { Challenge } from 'mppx'
|
|
1879
|
+
*
|
|
1880
|
+
* const challenge = Challenge.fromHeaders(response.headers)
|
|
1881
|
+
*
|
|
1882
|
+
* // With methods for type narrowing
|
|
1883
|
+
* const challenge = Challenge.fromHeaders(response.headers, { methods })
|
|
1884
|
+
* ```
|
|
1885
|
+
*/
|
|
1886
|
+
function fromHeaders(headers, options) {
|
|
1887
|
+
const header = headers.get("WWW-Authenticate");
|
|
1888
|
+
if (!header) throw new Error("Missing WWW-Authenticate header.");
|
|
1889
|
+
return deserialize$1(header, options);
|
|
1890
|
+
}
|
|
1891
|
+
/**
|
|
1892
|
+
* Extracts the challenge from a Response's WWW-Authenticate header.
|
|
1893
|
+
*
|
|
1894
|
+
* @param response - The HTTP response (must be 402 status).
|
|
1895
|
+
* @param options - Optional settings to narrow the challenge type.
|
|
1896
|
+
* @returns The deserialized challenge.
|
|
1897
|
+
*
|
|
1898
|
+
* @example
|
|
1899
|
+
* ```ts
|
|
1900
|
+
* import { Challenge } from 'mppx'
|
|
1901
|
+
*
|
|
1902
|
+
* const response = await fetch('/resource')
|
|
1903
|
+
* if (response.status === 402)
|
|
1904
|
+
* const challenge = Challenge.fromResponse(response)
|
|
1905
|
+
*
|
|
1906
|
+
* // With methods for type narrowing
|
|
1907
|
+
* const challenge = Challenge.fromResponse(response, { methods })
|
|
1908
|
+
* ```
|
|
1909
|
+
*/
|
|
1910
|
+
function fromResponse(response, options) {
|
|
1911
|
+
if (response.status !== 402) throw new Error("Response status is not 402.");
|
|
1912
|
+
return fromHeaders(response.headers, options);
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* Extracts all Payment challenges from a Response with potentially merged
|
|
1916
|
+
* `WWW-Authenticate` headers (multiple challenges in a single header value).
|
|
1917
|
+
*
|
|
1918
|
+
* @param response - The HTTP response (must be 402 status).
|
|
1919
|
+
* @param options - Optional settings to narrow the challenge type.
|
|
1920
|
+
* @returns An array of deserialized challenges.
|
|
1921
|
+
*
|
|
1922
|
+
* @example
|
|
1923
|
+
* ```ts
|
|
1924
|
+
* import { Challenge } from 'mppx'
|
|
1925
|
+
*
|
|
1926
|
+
* const response = await fetch('/resource')
|
|
1927
|
+
* if (response.status === 402) {
|
|
1928
|
+
* const challenges = Challenge.fromResponseList(response)
|
|
1929
|
+
* // challenges[0] => tempo/charge, challenges[1] => stripe/charge
|
|
1930
|
+
* }
|
|
1931
|
+
* ```
|
|
1932
|
+
*/
|
|
1933
|
+
function fromResponseList(response, options) {
|
|
1934
|
+
if (response.status !== 402) throw new Error("Response status is not 402.");
|
|
1935
|
+
return fromHeadersList(response.headers, options);
|
|
1936
|
+
}
|
|
1937
|
+
/**
|
|
1938
|
+
* Extracts all Payment challenges from a Headers object with potentially
|
|
1939
|
+
* merged `WWW-Authenticate` values.
|
|
1940
|
+
*
|
|
1941
|
+
* @param headers - The HTTP headers.
|
|
1942
|
+
* @param options - Optional settings to narrow the challenge type.
|
|
1943
|
+
* @returns An array of deserialized challenges.
|
|
1944
|
+
*/
|
|
1945
|
+
function fromHeadersList(headers, options) {
|
|
1946
|
+
const header = headers.get("WWW-Authenticate");
|
|
1947
|
+
if (!header) throw new Error("Missing WWW-Authenticate header.");
|
|
1948
|
+
return deserializeList(header, options);
|
|
1949
|
+
}
|
|
1950
|
+
/**
|
|
1951
|
+
* Deserializes a `WWW-Authenticate` header value that may contain multiple
|
|
1952
|
+
* Payment challenges (comma-separated per RFC 9110 §11.6.1).
|
|
1953
|
+
*
|
|
1954
|
+
* @param value - The raw WWW-Authenticate header value.
|
|
1955
|
+
* @param options - Optional settings to narrow the challenge type.
|
|
1956
|
+
* @returns An array of deserialized challenges.
|
|
1957
|
+
*/
|
|
1958
|
+
function deserializeList(value, options) {
|
|
1959
|
+
const starts = [];
|
|
1960
|
+
for (const match of value.matchAll(/Payment\s+/gi)) starts.push(match.index);
|
|
1961
|
+
if (starts.length === 0) throw new Error("No Payment schemes found.");
|
|
1962
|
+
return starts.map((start, i) => {
|
|
1963
|
+
const end = i + 1 < starts.length ? starts[i + 1] : value.length;
|
|
1964
|
+
return deserialize$1(value.slice(start, end).replace(/,\s*$/, ""), options);
|
|
1965
|
+
});
|
|
1966
|
+
}
|
|
1967
|
+
/** @internal Computes HMAC-SHA256 challenge ID from parameters. */
|
|
1968
|
+
function computeId(challenge, options) {
|
|
1969
|
+
const input = [
|
|
1970
|
+
challenge.realm,
|
|
1971
|
+
challenge.method,
|
|
1972
|
+
challenge.intent,
|
|
1973
|
+
serialize$1(challenge.request),
|
|
1974
|
+
challenge.expires ?? "",
|
|
1975
|
+
challenge.digest ?? "",
|
|
1976
|
+
challenge.opaque ? serialize$1(challenge.opaque) : ""
|
|
1977
|
+
].join("|");
|
|
1978
|
+
return fromBytes(hmac256(fromString$1(options.secretKey), fromString$1(input), { as: "Bytes" }), {
|
|
1979
|
+
url: true,
|
|
1980
|
+
pad: false
|
|
1981
|
+
});
|
|
1982
|
+
}
|
|
1983
|
+
//#endregion
|
|
1984
|
+
//#region node_modules/.pnpm/mppx@0.5.1_@modelcontextprotocol+sdk@1.28.0_zod@4.3.6__express@5.2.1_hono@4.12.9_openap_d8babfcabcc768186151f117bc0e35e5/node_modules/mppx/dist/Credential.js
|
|
1985
|
+
/**
|
|
1986
|
+
* Deserializes an Authorization header value to a credential.
|
|
1987
|
+
*
|
|
1988
|
+
* @param header - The Authorization header value.
|
|
1989
|
+
* @returns The deserialized credential.
|
|
1990
|
+
*
|
|
1991
|
+
* @example
|
|
1992
|
+
* ```ts
|
|
1993
|
+
* import { Credential } from 'mppx'
|
|
1994
|
+
*
|
|
1995
|
+
* const credential = Credential.deserialize(header)
|
|
1996
|
+
* ```
|
|
1997
|
+
*/
|
|
1998
|
+
function deserialize(value) {
|
|
1999
|
+
const prefixMatch = value.match(/^Payment\s+(.+)$/i);
|
|
2000
|
+
if (!prefixMatch?.[1]) throw new Error("Missing Payment scheme.");
|
|
2001
|
+
try {
|
|
2002
|
+
const json = toString(prefixMatch[1]);
|
|
2003
|
+
const parsed = JSON.parse(json);
|
|
2004
|
+
return {
|
|
2005
|
+
challenge: Schema.parse({
|
|
2006
|
+
...parsed.challenge,
|
|
2007
|
+
request: deserialize$2(parsed.challenge.request)
|
|
2008
|
+
}),
|
|
2009
|
+
payload: parsed.payload,
|
|
2010
|
+
...parsed.source && { source: parsed.source }
|
|
2011
|
+
};
|
|
2012
|
+
} catch {
|
|
2013
|
+
throw new Error("Invalid base64url or JSON.");
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
/**
|
|
2017
|
+
* Serializes a credential to the Authorization header format.
|
|
2018
|
+
*
|
|
2019
|
+
* @param credential - The credential to serialize.
|
|
2020
|
+
* @returns A string suitable for the Authorization header value.
|
|
2021
|
+
*
|
|
2022
|
+
* @example
|
|
2023
|
+
* ```ts
|
|
2024
|
+
* import { Credential } from 'mppx'
|
|
2025
|
+
*
|
|
2026
|
+
* const header = Credential.serialize(credential)
|
|
2027
|
+
* // => 'Payment eyJjaGFsbGVuZ2UiOnsi...'
|
|
2028
|
+
* ```
|
|
2029
|
+
*/
|
|
2030
|
+
function serialize(credential) {
|
|
2031
|
+
const wire = {
|
|
2032
|
+
challenge: {
|
|
2033
|
+
...credential.challenge,
|
|
2034
|
+
request: serialize$1(credential.challenge.request)
|
|
2035
|
+
},
|
|
2036
|
+
payload: credential.payload,
|
|
2037
|
+
...credential.source && { source: credential.source }
|
|
2038
|
+
};
|
|
2039
|
+
return `Payment ${fromString(JSON.stringify(wire), {
|
|
2040
|
+
pad: false,
|
|
2041
|
+
url: true
|
|
2042
|
+
})}`;
|
|
2043
|
+
}
|
|
2044
|
+
/**
|
|
2045
|
+
* Extracts the `Payment` scheme from an Authorization header value
|
|
2046
|
+
* that may contain multiple schemes (comma-separated per RFC 9110).
|
|
2047
|
+
*
|
|
2048
|
+
* @param header - The raw Authorization header value.
|
|
2049
|
+
* @returns The `Payment ...` scheme string, or `null` if not found.
|
|
2050
|
+
*/
|
|
2051
|
+
function extractPaymentScheme(header) {
|
|
2052
|
+
return header.split(",").map((s) => s.trim()).find((s) => /^Payment\s+/i.test(s)) ?? null;
|
|
2053
|
+
}
|
|
2054
|
+
//#endregion
|
|
2055
|
+
export { validate$1 as A, object as B, random$1 as C, toBytes$1 as D, toBigInt as E, boolean as F, transform as G, pipe as H, custom as I, union as K, discriminatedUnion as L, BaseError as M, _enum as N, toNumber as O, array as P, literal as R, padRight$1 as S, slice as T, refine as U, optional as V, string as W, fromBoolean as _, fromResponseList as a, fromString$2 as b, signature as c, keccak256 as d, fromString$1 as f, from$2 as g, concat as h, fromResponse as i, stringify as j, toString$2 as k, fromString as l, IntegerOutOfRangeError as m, extractPaymentScheme as n, amount as o, validate as p, serialize as r, hash as s, deserialize as t, toString as u, fromBytes$1 as v, size$1 as w, padLeft as x, fromNumber as y, number as z };
|