@hyperauth/vue 0.1.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/dist/chains.d.ts +5 -0
- package/dist/chains.d.ts.map +1 -0
- package/dist/csp.d.ts +20 -0
- package/dist/csp.d.ts.map +1 -0
- package/dist/env.d.ts +7 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/iframe-bridge.d.ts +20 -0
- package/dist/iframe-bridge.d.ts.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4248 -0
- package/dist/index.js.map +59 -0
- package/dist/injection-key.d.ts +4 -0
- package/dist/injection-key.d.ts.map +1 -0
- package/dist/policy.d.ts +36 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/provider.d.ts +29 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/store.d.ts +8 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/types.d.ts +156 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/ui/button.d.ts +48 -0
- package/dist/ui/button.d.ts.map +1 -0
- package/dist/ui/index.d.ts +7 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/ui/modal.d.ts +46 -0
- package/dist/ui/modal.d.ts.map +1 -0
- package/dist/ui/settlement-progress.d.ts +17 -0
- package/dist/ui/settlement-progress.d.ts.map +1 -0
- package/dist/use-account.d.ts +16 -0
- package/dist/use-account.d.ts.map +1 -0
- package/dist/use-hyperauth.d.ts +8 -0
- package/dist/use-hyperauth.d.ts.map +1 -0
- package/dist/use-login.d.ts +20 -0
- package/dist/use-login.d.ts.map +1 -0
- package/dist/use-policy-mint.d.ts +50 -0
- package/dist/use-policy-mint.d.ts.map +1 -0
- package/dist/use-policy-verify.d.ts +27 -0
- package/dist/use-policy-verify.d.ts.map +1 -0
- package/dist/use-registration.d.ts +14 -0
- package/dist/use-registration.d.ts.map +1 -0
- package/dist/use-session.d.ts +17 -0
- package/dist/use-session.d.ts.map +1 -0
- package/dist/use-settlement.d.ts +17 -0
- package/dist/use-settlement.d.ts.map +1 -0
- package/dist/use-signer.d.ts +19 -0
- package/dist/use-signer.d.ts.map +1 -0
- package/dist/use-store.d.ts +13 -0
- package/dist/use-store.d.ts.map +1 -0
- package/dist/use-transaction.d.ts +18 -0
- package/dist/use-transaction.d.ts.map +1 -0
- package/package.json +48 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,4248 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/provider.ts
|
|
10
|
+
import { reactive } from "vue";
|
|
11
|
+
import { createClient } from "@hyperauth/client";
|
|
12
|
+
|
|
13
|
+
// src/injection-key.ts
|
|
14
|
+
var HyperAuthKey = Symbol("hyperauth");
|
|
15
|
+
|
|
16
|
+
// src/store.ts
|
|
17
|
+
var initialState = {
|
|
18
|
+
client: null,
|
|
19
|
+
status: "initializing",
|
|
20
|
+
error: null,
|
|
21
|
+
user: null,
|
|
22
|
+
authStatus: "unauthenticated",
|
|
23
|
+
session: null,
|
|
24
|
+
provider: "unknown",
|
|
25
|
+
policy: null,
|
|
26
|
+
chain: "base",
|
|
27
|
+
pendingTransaction: {
|
|
28
|
+
phase: "idle",
|
|
29
|
+
error: null,
|
|
30
|
+
result: null,
|
|
31
|
+
userOp: null
|
|
32
|
+
},
|
|
33
|
+
settlement: {
|
|
34
|
+
phase: "idle",
|
|
35
|
+
steps: [],
|
|
36
|
+
params: null,
|
|
37
|
+
txHash: null,
|
|
38
|
+
error: null
|
|
39
|
+
},
|
|
40
|
+
accountState: null,
|
|
41
|
+
restartAttempts: 0
|
|
42
|
+
};
|
|
43
|
+
function createStore(seed = {}) {
|
|
44
|
+
let state = { ...initialState, ...seed };
|
|
45
|
+
const listeners = new Set;
|
|
46
|
+
return {
|
|
47
|
+
getState: () => state,
|
|
48
|
+
setState: (patch) => {
|
|
49
|
+
const next = typeof patch === "function" ? patch(state) : patch;
|
|
50
|
+
state = { ...state, ...next };
|
|
51
|
+
listeners.forEach((l) => l());
|
|
52
|
+
},
|
|
53
|
+
subscribe: (listener) => {
|
|
54
|
+
listeners.add(listener);
|
|
55
|
+
return () => {
|
|
56
|
+
listeners.delete(listener);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/env.ts
|
|
63
|
+
var isBrowser = () => typeof window !== "undefined" && typeof navigator !== "undefined";
|
|
64
|
+
var hasWebAuthn = () => isBrowser() && typeof window.PublicKeyCredential !== "undefined";
|
|
65
|
+
function getOrigin() {
|
|
66
|
+
if (!isBrowser())
|
|
67
|
+
return null;
|
|
68
|
+
return window.location.origin;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/is.js
|
|
72
|
+
var objectTypeNames = [
|
|
73
|
+
"Object",
|
|
74
|
+
"RegExp",
|
|
75
|
+
"Date",
|
|
76
|
+
"Error",
|
|
77
|
+
"Map",
|
|
78
|
+
"Set",
|
|
79
|
+
"WeakMap",
|
|
80
|
+
"WeakSet",
|
|
81
|
+
"ArrayBuffer",
|
|
82
|
+
"SharedArrayBuffer",
|
|
83
|
+
"DataView",
|
|
84
|
+
"Promise",
|
|
85
|
+
"URL",
|
|
86
|
+
"HTMLElement",
|
|
87
|
+
"Int8Array",
|
|
88
|
+
"Uint8ClampedArray",
|
|
89
|
+
"Int16Array",
|
|
90
|
+
"Uint16Array",
|
|
91
|
+
"Int32Array",
|
|
92
|
+
"Uint32Array",
|
|
93
|
+
"Float32Array",
|
|
94
|
+
"Float64Array",
|
|
95
|
+
"BigInt64Array",
|
|
96
|
+
"BigUint64Array",
|
|
97
|
+
"Tagged"
|
|
98
|
+
];
|
|
99
|
+
function is(value) {
|
|
100
|
+
if (value === null) {
|
|
101
|
+
return "null";
|
|
102
|
+
}
|
|
103
|
+
if (value === undefined) {
|
|
104
|
+
return "undefined";
|
|
105
|
+
}
|
|
106
|
+
if (value === true || value === false) {
|
|
107
|
+
return "boolean";
|
|
108
|
+
}
|
|
109
|
+
const typeOf = typeof value;
|
|
110
|
+
if (typeOf === "string" || typeOf === "number" || typeOf === "bigint" || typeOf === "symbol") {
|
|
111
|
+
return typeOf;
|
|
112
|
+
}
|
|
113
|
+
if (typeOf === "function") {
|
|
114
|
+
return "Function";
|
|
115
|
+
}
|
|
116
|
+
if (Array.isArray(value)) {
|
|
117
|
+
return "Array";
|
|
118
|
+
}
|
|
119
|
+
if (value instanceof Uint8Array) {
|
|
120
|
+
return "Uint8Array";
|
|
121
|
+
}
|
|
122
|
+
if (value.constructor === Object) {
|
|
123
|
+
return "Object";
|
|
124
|
+
}
|
|
125
|
+
const objectType = getObjectType(value);
|
|
126
|
+
if (objectType) {
|
|
127
|
+
return objectType;
|
|
128
|
+
}
|
|
129
|
+
return "Object";
|
|
130
|
+
}
|
|
131
|
+
function getObjectType(value) {
|
|
132
|
+
const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);
|
|
133
|
+
if (objectTypeNames.includes(objectTypeName)) {
|
|
134
|
+
return objectTypeName;
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/token.js
|
|
140
|
+
class Type {
|
|
141
|
+
constructor(major, name, terminal) {
|
|
142
|
+
this.major = major;
|
|
143
|
+
this.majorEncoded = major << 5;
|
|
144
|
+
this.name = name;
|
|
145
|
+
this.terminal = terminal;
|
|
146
|
+
}
|
|
147
|
+
toString() {
|
|
148
|
+
return `Type[${this.major}].${this.name}`;
|
|
149
|
+
}
|
|
150
|
+
compare(typ) {
|
|
151
|
+
return this.major < typ.major ? -1 : this.major > typ.major ? 1 : 0;
|
|
152
|
+
}
|
|
153
|
+
static equals(a, b) {
|
|
154
|
+
return a === b || a.major === b.major && a.name === b.name;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
Type.uint = new Type(0, "uint", true);
|
|
158
|
+
Type.negint = new Type(1, "negint", true);
|
|
159
|
+
Type.bytes = new Type(2, "bytes", true);
|
|
160
|
+
Type.string = new Type(3, "string", true);
|
|
161
|
+
Type.array = new Type(4, "array", false);
|
|
162
|
+
Type.map = new Type(5, "map", false);
|
|
163
|
+
Type.tag = new Type(6, "tag", false);
|
|
164
|
+
Type.float = new Type(7, "float", true);
|
|
165
|
+
Type.false = new Type(7, "false", true);
|
|
166
|
+
Type.true = new Type(7, "true", true);
|
|
167
|
+
Type.null = new Type(7, "null", true);
|
|
168
|
+
Type.undefined = new Type(7, "undefined", true);
|
|
169
|
+
Type.break = new Type(7, "break", true);
|
|
170
|
+
|
|
171
|
+
class Token {
|
|
172
|
+
constructor(type, value, encodedLength) {
|
|
173
|
+
this.type = type;
|
|
174
|
+
this.value = value;
|
|
175
|
+
this.encodedLength = encodedLength;
|
|
176
|
+
this.encodedBytes = undefined;
|
|
177
|
+
this.byteValue = undefined;
|
|
178
|
+
}
|
|
179
|
+
toString() {
|
|
180
|
+
return `Token[${this.type}].${this.value}`;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/byte-utils.js
|
|
185
|
+
var useBuffer = globalThis.process && !globalThis.process.browser && globalThis.Buffer && typeof globalThis.Buffer.isBuffer === "function";
|
|
186
|
+
var textEncoder = new TextEncoder;
|
|
187
|
+
function isBuffer(buf) {
|
|
188
|
+
return useBuffer && globalThis.Buffer.isBuffer(buf);
|
|
189
|
+
}
|
|
190
|
+
function asU8A(buf) {
|
|
191
|
+
if (!(buf instanceof Uint8Array)) {
|
|
192
|
+
return Uint8Array.from(buf);
|
|
193
|
+
}
|
|
194
|
+
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf;
|
|
195
|
+
}
|
|
196
|
+
var FROM_STRING_THRESHOLD_BUFFER = 24;
|
|
197
|
+
var FROM_STRING_THRESHOLD_TEXTENCODER = 200;
|
|
198
|
+
var fromString = useBuffer ? (string) => {
|
|
199
|
+
return string.length >= FROM_STRING_THRESHOLD_BUFFER ? globalThis.Buffer.from(string) : utf8ToBytes(string);
|
|
200
|
+
} : (string) => {
|
|
201
|
+
return string.length >= FROM_STRING_THRESHOLD_TEXTENCODER ? textEncoder.encode(string) : utf8ToBytes(string);
|
|
202
|
+
};
|
|
203
|
+
var fromArray = (arr) => {
|
|
204
|
+
return Uint8Array.from(arr);
|
|
205
|
+
};
|
|
206
|
+
var slice = useBuffer ? (bytes, start, end) => {
|
|
207
|
+
if (isBuffer(bytes)) {
|
|
208
|
+
return new Uint8Array(bytes.subarray(start, end));
|
|
209
|
+
}
|
|
210
|
+
return bytes.slice(start, end);
|
|
211
|
+
} : (bytes, start, end) => {
|
|
212
|
+
return bytes.slice(start, end);
|
|
213
|
+
};
|
|
214
|
+
var concat = useBuffer ? (chunks, length) => {
|
|
215
|
+
chunks = chunks.map((c) => c instanceof Uint8Array ? c : globalThis.Buffer.from(c));
|
|
216
|
+
return asU8A(globalThis.Buffer.concat(chunks, length));
|
|
217
|
+
} : (chunks, length) => {
|
|
218
|
+
const out = new Uint8Array(length);
|
|
219
|
+
let off = 0;
|
|
220
|
+
for (let b of chunks) {
|
|
221
|
+
if (off + b.length > out.length) {
|
|
222
|
+
b = b.subarray(0, out.length - off);
|
|
223
|
+
}
|
|
224
|
+
out.set(b, off);
|
|
225
|
+
off += b.length;
|
|
226
|
+
}
|
|
227
|
+
return out;
|
|
228
|
+
};
|
|
229
|
+
var alloc = useBuffer ? (size) => {
|
|
230
|
+
return globalThis.Buffer.allocUnsafe(size);
|
|
231
|
+
} : (size) => {
|
|
232
|
+
return new Uint8Array(size);
|
|
233
|
+
};
|
|
234
|
+
function compare(b1, b2) {
|
|
235
|
+
if (isBuffer(b1) && isBuffer(b2)) {
|
|
236
|
+
return b1.compare(b2);
|
|
237
|
+
}
|
|
238
|
+
for (let i = 0;i < b1.length; i++) {
|
|
239
|
+
if (b1[i] === b2[i]) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
return b1[i] < b2[i] ? -1 : 1;
|
|
243
|
+
}
|
|
244
|
+
return 0;
|
|
245
|
+
}
|
|
246
|
+
function utf8ToBytes(str) {
|
|
247
|
+
const out = [];
|
|
248
|
+
let p = 0;
|
|
249
|
+
for (let i = 0;i < str.length; i++) {
|
|
250
|
+
let c = str.charCodeAt(i);
|
|
251
|
+
if (c < 128) {
|
|
252
|
+
out[p++] = c;
|
|
253
|
+
} else if (c < 2048) {
|
|
254
|
+
out[p++] = c >> 6 | 192;
|
|
255
|
+
out[p++] = c & 63 | 128;
|
|
256
|
+
} else if ((c & 64512) === 55296 && i + 1 < str.length && (str.charCodeAt(i + 1) & 64512) === 56320) {
|
|
257
|
+
c = 65536 + ((c & 1023) << 10) + (str.charCodeAt(++i) & 1023);
|
|
258
|
+
out[p++] = c >> 18 | 240;
|
|
259
|
+
out[p++] = c >> 12 & 63 | 128;
|
|
260
|
+
out[p++] = c >> 6 & 63 | 128;
|
|
261
|
+
out[p++] = c & 63 | 128;
|
|
262
|
+
} else {
|
|
263
|
+
if (c >= 55296 && c <= 57343) {
|
|
264
|
+
c = 65533;
|
|
265
|
+
}
|
|
266
|
+
out[p++] = c >> 12 | 224;
|
|
267
|
+
out[p++] = c >> 6 & 63 | 128;
|
|
268
|
+
out[p++] = c & 63 | 128;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/bl.js
|
|
275
|
+
var defaultChunkSize = 256;
|
|
276
|
+
|
|
277
|
+
class Bl {
|
|
278
|
+
constructor(chunkSize = defaultChunkSize) {
|
|
279
|
+
this.chunkSize = chunkSize;
|
|
280
|
+
this.cursor = 0;
|
|
281
|
+
this.maxCursor = -1;
|
|
282
|
+
this.chunks = [];
|
|
283
|
+
this._initReuseChunk = null;
|
|
284
|
+
}
|
|
285
|
+
reset() {
|
|
286
|
+
this.cursor = 0;
|
|
287
|
+
this.maxCursor = -1;
|
|
288
|
+
if (this.chunks.length) {
|
|
289
|
+
this.chunks = [];
|
|
290
|
+
}
|
|
291
|
+
if (this._initReuseChunk !== null) {
|
|
292
|
+
this.chunks.push(this._initReuseChunk);
|
|
293
|
+
this.maxCursor = this._initReuseChunk.length - 1;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
push(bytes) {
|
|
297
|
+
let topChunk = this.chunks[this.chunks.length - 1];
|
|
298
|
+
const newMax = this.cursor + bytes.length;
|
|
299
|
+
if (newMax <= this.maxCursor + 1) {
|
|
300
|
+
const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
|
|
301
|
+
topChunk.set(bytes, chunkPos);
|
|
302
|
+
} else {
|
|
303
|
+
if (topChunk) {
|
|
304
|
+
const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
|
|
305
|
+
if (chunkPos < topChunk.length) {
|
|
306
|
+
this.chunks[this.chunks.length - 1] = topChunk.subarray(0, chunkPos);
|
|
307
|
+
this.maxCursor = this.cursor - 1;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (bytes.length < 64 && bytes.length < this.chunkSize) {
|
|
311
|
+
topChunk = alloc(this.chunkSize);
|
|
312
|
+
this.chunks.push(topChunk);
|
|
313
|
+
this.maxCursor += topChunk.length;
|
|
314
|
+
if (this._initReuseChunk === null) {
|
|
315
|
+
this._initReuseChunk = topChunk;
|
|
316
|
+
}
|
|
317
|
+
topChunk.set(bytes, 0);
|
|
318
|
+
} else {
|
|
319
|
+
this.chunks.push(bytes);
|
|
320
|
+
this.maxCursor += bytes.length;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
this.cursor += bytes.length;
|
|
324
|
+
}
|
|
325
|
+
toBytes(reset = false) {
|
|
326
|
+
let byts;
|
|
327
|
+
if (this.chunks.length === 1) {
|
|
328
|
+
const chunk = this.chunks[0];
|
|
329
|
+
if (reset && this.cursor > chunk.length / 2) {
|
|
330
|
+
byts = this.cursor === chunk.length ? chunk : chunk.subarray(0, this.cursor);
|
|
331
|
+
this._initReuseChunk = null;
|
|
332
|
+
this.chunks = [];
|
|
333
|
+
} else {
|
|
334
|
+
byts = slice(chunk, 0, this.cursor);
|
|
335
|
+
}
|
|
336
|
+
} else {
|
|
337
|
+
byts = concat(this.chunks, this.cursor);
|
|
338
|
+
}
|
|
339
|
+
if (reset) {
|
|
340
|
+
this.reset();
|
|
341
|
+
}
|
|
342
|
+
return byts;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
class U8Bl {
|
|
347
|
+
constructor(dest) {
|
|
348
|
+
this.dest = dest;
|
|
349
|
+
this.cursor = 0;
|
|
350
|
+
this.chunks = [dest];
|
|
351
|
+
}
|
|
352
|
+
reset() {
|
|
353
|
+
this.cursor = 0;
|
|
354
|
+
}
|
|
355
|
+
push(bytes) {
|
|
356
|
+
if (this.cursor + bytes.length > this.dest.length) {
|
|
357
|
+
throw new Error("write out of bounds, destination buffer is too small");
|
|
358
|
+
}
|
|
359
|
+
this.dest.set(bytes, this.cursor);
|
|
360
|
+
this.cursor += bytes.length;
|
|
361
|
+
}
|
|
362
|
+
toBytes(reset = false) {
|
|
363
|
+
const byts = this.dest.subarray(0, this.cursor);
|
|
364
|
+
if (reset) {
|
|
365
|
+
this.reset();
|
|
366
|
+
}
|
|
367
|
+
return byts;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/common.js
|
|
372
|
+
var decodeErrPrefix = "CBOR decode error:";
|
|
373
|
+
var encodeErrPrefix = "CBOR encode error:";
|
|
374
|
+
var uintMinorPrefixBytes = [];
|
|
375
|
+
uintMinorPrefixBytes[23] = 1;
|
|
376
|
+
uintMinorPrefixBytes[24] = 2;
|
|
377
|
+
uintMinorPrefixBytes[25] = 3;
|
|
378
|
+
uintMinorPrefixBytes[26] = 5;
|
|
379
|
+
uintMinorPrefixBytes[27] = 9;
|
|
380
|
+
function assertEnoughData(data, pos, need) {
|
|
381
|
+
if (data.length - pos < need) {
|
|
382
|
+
throw new Error(`${decodeErrPrefix} not enough data for type`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/0uint.js
|
|
387
|
+
var uintBoundaries = [24, 256, 65536, 4294967296, BigInt("18446744073709551616")];
|
|
388
|
+
function readUint8(data, offset, options) {
|
|
389
|
+
assertEnoughData(data, offset, 1);
|
|
390
|
+
const value = data[offset];
|
|
391
|
+
if (options.strict === true && value < uintBoundaries[0]) {
|
|
392
|
+
throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
393
|
+
}
|
|
394
|
+
return value;
|
|
395
|
+
}
|
|
396
|
+
function readUint16(data, offset, options) {
|
|
397
|
+
assertEnoughData(data, offset, 2);
|
|
398
|
+
const value = data[offset] << 8 | data[offset + 1];
|
|
399
|
+
if (options.strict === true && value < uintBoundaries[1]) {
|
|
400
|
+
throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
401
|
+
}
|
|
402
|
+
return value;
|
|
403
|
+
}
|
|
404
|
+
function readUint32(data, offset, options) {
|
|
405
|
+
assertEnoughData(data, offset, 4);
|
|
406
|
+
const value = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
|
|
407
|
+
if (options.strict === true && value < uintBoundaries[2]) {
|
|
408
|
+
throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
409
|
+
}
|
|
410
|
+
return value;
|
|
411
|
+
}
|
|
412
|
+
function readUint64(data, offset, options) {
|
|
413
|
+
assertEnoughData(data, offset, 8);
|
|
414
|
+
const hi = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
|
|
415
|
+
const lo = data[offset + 4] * 16777216 + (data[offset + 5] << 16) + (data[offset + 6] << 8) + data[offset + 7];
|
|
416
|
+
const value = (BigInt(hi) << BigInt(32)) + BigInt(lo);
|
|
417
|
+
if (options.strict === true && value < uintBoundaries[3]) {
|
|
418
|
+
throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
419
|
+
}
|
|
420
|
+
if (value <= Number.MAX_SAFE_INTEGER) {
|
|
421
|
+
return Number(value);
|
|
422
|
+
}
|
|
423
|
+
if (options.allowBigInt === true) {
|
|
424
|
+
return value;
|
|
425
|
+
}
|
|
426
|
+
throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`);
|
|
427
|
+
}
|
|
428
|
+
function decodeUint8(data, pos, _minor, options) {
|
|
429
|
+
return new Token(Type.uint, readUint8(data, pos + 1, options), 2);
|
|
430
|
+
}
|
|
431
|
+
function decodeUint16(data, pos, _minor, options) {
|
|
432
|
+
return new Token(Type.uint, readUint16(data, pos + 1, options), 3);
|
|
433
|
+
}
|
|
434
|
+
function decodeUint32(data, pos, _minor, options) {
|
|
435
|
+
return new Token(Type.uint, readUint32(data, pos + 1, options), 5);
|
|
436
|
+
}
|
|
437
|
+
function decodeUint64(data, pos, _minor, options) {
|
|
438
|
+
return new Token(Type.uint, readUint64(data, pos + 1, options), 9);
|
|
439
|
+
}
|
|
440
|
+
function encodeUint(writer, token) {
|
|
441
|
+
return encodeUintValue(writer, 0, token.value);
|
|
442
|
+
}
|
|
443
|
+
function encodeUintValue(writer, major, uint) {
|
|
444
|
+
if (uint < uintBoundaries[0]) {
|
|
445
|
+
const nuint = Number(uint);
|
|
446
|
+
writer.push([major | nuint]);
|
|
447
|
+
} else if (uint < uintBoundaries[1]) {
|
|
448
|
+
const nuint = Number(uint);
|
|
449
|
+
writer.push([major | 24, nuint]);
|
|
450
|
+
} else if (uint < uintBoundaries[2]) {
|
|
451
|
+
const nuint = Number(uint);
|
|
452
|
+
writer.push([major | 25, nuint >>> 8, nuint & 255]);
|
|
453
|
+
} else if (uint < uintBoundaries[3]) {
|
|
454
|
+
const nuint = Number(uint);
|
|
455
|
+
writer.push([major | 26, nuint >>> 24 & 255, nuint >>> 16 & 255, nuint >>> 8 & 255, nuint & 255]);
|
|
456
|
+
} else {
|
|
457
|
+
const buint = BigInt(uint);
|
|
458
|
+
if (buint < uintBoundaries[4]) {
|
|
459
|
+
const set = [major | 27, 0, 0, 0, 0, 0, 0, 0];
|
|
460
|
+
let lo = Number(buint & BigInt(4294967295));
|
|
461
|
+
let hi = Number(buint >> BigInt(32) & BigInt(4294967295));
|
|
462
|
+
set[8] = lo & 255;
|
|
463
|
+
lo = lo >> 8;
|
|
464
|
+
set[7] = lo & 255;
|
|
465
|
+
lo = lo >> 8;
|
|
466
|
+
set[6] = lo & 255;
|
|
467
|
+
lo = lo >> 8;
|
|
468
|
+
set[5] = lo & 255;
|
|
469
|
+
set[4] = hi & 255;
|
|
470
|
+
hi = hi >> 8;
|
|
471
|
+
set[3] = hi & 255;
|
|
472
|
+
hi = hi >> 8;
|
|
473
|
+
set[2] = hi & 255;
|
|
474
|
+
hi = hi >> 8;
|
|
475
|
+
set[1] = hi & 255;
|
|
476
|
+
writer.push(set);
|
|
477
|
+
} else {
|
|
478
|
+
throw new Error(`${decodeErrPrefix} encountered BigInt larger than allowable range`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
encodeUint.encodedSize = function encodedSize(token) {
|
|
483
|
+
return encodeUintValue.encodedSize(token.value);
|
|
484
|
+
};
|
|
485
|
+
encodeUintValue.encodedSize = function encodedSize2(uint) {
|
|
486
|
+
if (uint < uintBoundaries[0]) {
|
|
487
|
+
return 1;
|
|
488
|
+
}
|
|
489
|
+
if (uint < uintBoundaries[1]) {
|
|
490
|
+
return 2;
|
|
491
|
+
}
|
|
492
|
+
if (uint < uintBoundaries[2]) {
|
|
493
|
+
return 3;
|
|
494
|
+
}
|
|
495
|
+
if (uint < uintBoundaries[3]) {
|
|
496
|
+
return 5;
|
|
497
|
+
}
|
|
498
|
+
return 9;
|
|
499
|
+
};
|
|
500
|
+
encodeUint.compareTokens = function compareTokens(tok1, tok2) {
|
|
501
|
+
return tok1.value < tok2.value ? -1 : tok1.value > tok2.value ? 1 : 0;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/1negint.js
|
|
505
|
+
function decodeNegint8(data, pos, _minor, options) {
|
|
506
|
+
return new Token(Type.negint, -1 - readUint8(data, pos + 1, options), 2);
|
|
507
|
+
}
|
|
508
|
+
function decodeNegint16(data, pos, _minor, options) {
|
|
509
|
+
return new Token(Type.negint, -1 - readUint16(data, pos + 1, options), 3);
|
|
510
|
+
}
|
|
511
|
+
function decodeNegint32(data, pos, _minor, options) {
|
|
512
|
+
return new Token(Type.negint, -1 - readUint32(data, pos + 1, options), 5);
|
|
513
|
+
}
|
|
514
|
+
var neg1b = BigInt(-1);
|
|
515
|
+
var pos1b = BigInt(1);
|
|
516
|
+
function decodeNegint64(data, pos, _minor, options) {
|
|
517
|
+
const int = readUint64(data, pos + 1, options);
|
|
518
|
+
if (typeof int !== "bigint") {
|
|
519
|
+
const value = -1 - int;
|
|
520
|
+
if (value >= Number.MIN_SAFE_INTEGER) {
|
|
521
|
+
return new Token(Type.negint, value, 9);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
if (options.allowBigInt !== true) {
|
|
525
|
+
throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`);
|
|
526
|
+
}
|
|
527
|
+
return new Token(Type.negint, neg1b - BigInt(int), 9);
|
|
528
|
+
}
|
|
529
|
+
function encodeNegint(writer, token) {
|
|
530
|
+
const negint = token.value;
|
|
531
|
+
const unsigned = typeof negint === "bigint" ? negint * neg1b - pos1b : negint * -1 - 1;
|
|
532
|
+
encodeUintValue(writer, token.type.majorEncoded, unsigned);
|
|
533
|
+
}
|
|
534
|
+
encodeNegint.encodedSize = function encodedSize3(token) {
|
|
535
|
+
const negint = token.value;
|
|
536
|
+
const unsigned = typeof negint === "bigint" ? negint * neg1b - pos1b : negint * -1 - 1;
|
|
537
|
+
if (unsigned < uintBoundaries[0]) {
|
|
538
|
+
return 1;
|
|
539
|
+
}
|
|
540
|
+
if (unsigned < uintBoundaries[1]) {
|
|
541
|
+
return 2;
|
|
542
|
+
}
|
|
543
|
+
if (unsigned < uintBoundaries[2]) {
|
|
544
|
+
return 3;
|
|
545
|
+
}
|
|
546
|
+
if (unsigned < uintBoundaries[3]) {
|
|
547
|
+
return 5;
|
|
548
|
+
}
|
|
549
|
+
return 9;
|
|
550
|
+
};
|
|
551
|
+
encodeNegint.compareTokens = function compareTokens2(tok1, tok2) {
|
|
552
|
+
return tok1.value < tok2.value ? 1 : tok1.value > tok2.value ? -1 : 0;
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/2bytes.js
|
|
556
|
+
function toToken(data, pos, prefix, length) {
|
|
557
|
+
assertEnoughData(data, pos, prefix + length);
|
|
558
|
+
const buf = data.slice(pos + prefix, pos + prefix + length);
|
|
559
|
+
return new Token(Type.bytes, buf, prefix + length);
|
|
560
|
+
}
|
|
561
|
+
function decodeBytesCompact(data, pos, minor, _options) {
|
|
562
|
+
return toToken(data, pos, 1, minor);
|
|
563
|
+
}
|
|
564
|
+
function decodeBytes8(data, pos, _minor, options) {
|
|
565
|
+
return toToken(data, pos, 2, readUint8(data, pos + 1, options));
|
|
566
|
+
}
|
|
567
|
+
function decodeBytes16(data, pos, _minor, options) {
|
|
568
|
+
return toToken(data, pos, 3, readUint16(data, pos + 1, options));
|
|
569
|
+
}
|
|
570
|
+
function decodeBytes32(data, pos, _minor, options) {
|
|
571
|
+
return toToken(data, pos, 5, readUint32(data, pos + 1, options));
|
|
572
|
+
}
|
|
573
|
+
function decodeBytes64(data, pos, _minor, options) {
|
|
574
|
+
const l = readUint64(data, pos + 1, options);
|
|
575
|
+
if (typeof l === "bigint") {
|
|
576
|
+
throw new Error(`${decodeErrPrefix} 64-bit integer bytes lengths not supported`);
|
|
577
|
+
}
|
|
578
|
+
return toToken(data, pos, 9, l);
|
|
579
|
+
}
|
|
580
|
+
function tokenBytes(token) {
|
|
581
|
+
if (token.encodedBytes === undefined) {
|
|
582
|
+
token.encodedBytes = Type.equals(token.type, Type.string) ? fromString(token.value) : token.value;
|
|
583
|
+
}
|
|
584
|
+
return token.encodedBytes;
|
|
585
|
+
}
|
|
586
|
+
function encodeBytes(writer, token) {
|
|
587
|
+
const bytes = tokenBytes(token);
|
|
588
|
+
encodeUintValue(writer, token.type.majorEncoded, bytes.length);
|
|
589
|
+
writer.push(bytes);
|
|
590
|
+
}
|
|
591
|
+
encodeBytes.encodedSize = function encodedSize4(token) {
|
|
592
|
+
const bytes = tokenBytes(token);
|
|
593
|
+
return encodeUintValue.encodedSize(bytes.length) + bytes.length;
|
|
594
|
+
};
|
|
595
|
+
encodeBytes.compareTokens = function compareTokens3(tok1, tok2) {
|
|
596
|
+
return compareBytes(tokenBytes(tok1), tokenBytes(tok2));
|
|
597
|
+
};
|
|
598
|
+
function compareBytes(b1, b2) {
|
|
599
|
+
return b1.length < b2.length ? -1 : b1.length > b2.length ? 1 : compare(b1, b2);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/3string.js
|
|
603
|
+
var textDecoder = new TextDecoder;
|
|
604
|
+
var ASCII_THRESHOLD = 32;
|
|
605
|
+
function toStr(bytes, start, end) {
|
|
606
|
+
const len = end - start;
|
|
607
|
+
if (len < ASCII_THRESHOLD) {
|
|
608
|
+
let str = "";
|
|
609
|
+
for (let i = start;i < end; i++) {
|
|
610
|
+
const c = bytes[i];
|
|
611
|
+
if (c & 128) {
|
|
612
|
+
return textDecoder.decode(bytes.subarray(start, end));
|
|
613
|
+
}
|
|
614
|
+
str += String.fromCharCode(c);
|
|
615
|
+
}
|
|
616
|
+
return str;
|
|
617
|
+
}
|
|
618
|
+
return textDecoder.decode(bytes.subarray(start, end));
|
|
619
|
+
}
|
|
620
|
+
function toToken2(data, pos, prefix, length, options) {
|
|
621
|
+
const totLength = prefix + length;
|
|
622
|
+
assertEnoughData(data, pos, totLength);
|
|
623
|
+
const tok = new Token(Type.string, toStr(data, pos + prefix, pos + totLength), totLength);
|
|
624
|
+
if (options.retainStringBytes === true) {
|
|
625
|
+
tok.byteValue = data.slice(pos + prefix, pos + totLength);
|
|
626
|
+
}
|
|
627
|
+
return tok;
|
|
628
|
+
}
|
|
629
|
+
function decodeStringCompact(data, pos, minor, options) {
|
|
630
|
+
return toToken2(data, pos, 1, minor, options);
|
|
631
|
+
}
|
|
632
|
+
function decodeString8(data, pos, _minor, options) {
|
|
633
|
+
return toToken2(data, pos, 2, readUint8(data, pos + 1, options), options);
|
|
634
|
+
}
|
|
635
|
+
function decodeString16(data, pos, _minor, options) {
|
|
636
|
+
return toToken2(data, pos, 3, readUint16(data, pos + 1, options), options);
|
|
637
|
+
}
|
|
638
|
+
function decodeString32(data, pos, _minor, options) {
|
|
639
|
+
return toToken2(data, pos, 5, readUint32(data, pos + 1, options), options);
|
|
640
|
+
}
|
|
641
|
+
function decodeString64(data, pos, _minor, options) {
|
|
642
|
+
const l = readUint64(data, pos + 1, options);
|
|
643
|
+
if (typeof l === "bigint") {
|
|
644
|
+
throw new Error(`${decodeErrPrefix} 64-bit integer string lengths not supported`);
|
|
645
|
+
}
|
|
646
|
+
return toToken2(data, pos, 9, l, options);
|
|
647
|
+
}
|
|
648
|
+
var encodeString = encodeBytes;
|
|
649
|
+
|
|
650
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/4array.js
|
|
651
|
+
function toToken3(_data, _pos, prefix, length) {
|
|
652
|
+
return new Token(Type.array, length, prefix);
|
|
653
|
+
}
|
|
654
|
+
function decodeArrayCompact(data, pos, minor, _options) {
|
|
655
|
+
return toToken3(data, pos, 1, minor);
|
|
656
|
+
}
|
|
657
|
+
function decodeArray8(data, pos, _minor, options) {
|
|
658
|
+
return toToken3(data, pos, 2, readUint8(data, pos + 1, options));
|
|
659
|
+
}
|
|
660
|
+
function decodeArray16(data, pos, _minor, options) {
|
|
661
|
+
return toToken3(data, pos, 3, readUint16(data, pos + 1, options));
|
|
662
|
+
}
|
|
663
|
+
function decodeArray32(data, pos, _minor, options) {
|
|
664
|
+
return toToken3(data, pos, 5, readUint32(data, pos + 1, options));
|
|
665
|
+
}
|
|
666
|
+
function decodeArray64(data, pos, _minor, options) {
|
|
667
|
+
const l = readUint64(data, pos + 1, options);
|
|
668
|
+
if (typeof l === "bigint") {
|
|
669
|
+
throw new Error(`${decodeErrPrefix} 64-bit integer array lengths not supported`);
|
|
670
|
+
}
|
|
671
|
+
return toToken3(data, pos, 9, l);
|
|
672
|
+
}
|
|
673
|
+
function decodeArrayIndefinite(data, pos, _minor, options) {
|
|
674
|
+
if (options.allowIndefinite === false) {
|
|
675
|
+
throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
676
|
+
}
|
|
677
|
+
return toToken3(data, pos, 1, Infinity);
|
|
678
|
+
}
|
|
679
|
+
function encodeArray(writer, token) {
|
|
680
|
+
encodeUintValue(writer, Type.array.majorEncoded, token.value);
|
|
681
|
+
}
|
|
682
|
+
encodeArray.compareTokens = encodeUint.compareTokens;
|
|
683
|
+
encodeArray.encodedSize = function encodedSize5(token) {
|
|
684
|
+
return encodeUintValue.encodedSize(token.value);
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/5map.js
|
|
688
|
+
function toToken4(_data, _pos, prefix, length) {
|
|
689
|
+
return new Token(Type.map, length, prefix);
|
|
690
|
+
}
|
|
691
|
+
function decodeMapCompact(data, pos, minor, _options) {
|
|
692
|
+
return toToken4(data, pos, 1, minor);
|
|
693
|
+
}
|
|
694
|
+
function decodeMap8(data, pos, _minor, options) {
|
|
695
|
+
return toToken4(data, pos, 2, readUint8(data, pos + 1, options));
|
|
696
|
+
}
|
|
697
|
+
function decodeMap16(data, pos, _minor, options) {
|
|
698
|
+
return toToken4(data, pos, 3, readUint16(data, pos + 1, options));
|
|
699
|
+
}
|
|
700
|
+
function decodeMap32(data, pos, _minor, options) {
|
|
701
|
+
return toToken4(data, pos, 5, readUint32(data, pos + 1, options));
|
|
702
|
+
}
|
|
703
|
+
function decodeMap64(data, pos, _minor, options) {
|
|
704
|
+
const l = readUint64(data, pos + 1, options);
|
|
705
|
+
if (typeof l === "bigint") {
|
|
706
|
+
throw new Error(`${decodeErrPrefix} 64-bit integer map lengths not supported`);
|
|
707
|
+
}
|
|
708
|
+
return toToken4(data, pos, 9, l);
|
|
709
|
+
}
|
|
710
|
+
function decodeMapIndefinite(data, pos, _minor, options) {
|
|
711
|
+
if (options.allowIndefinite === false) {
|
|
712
|
+
throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
713
|
+
}
|
|
714
|
+
return toToken4(data, pos, 1, Infinity);
|
|
715
|
+
}
|
|
716
|
+
function encodeMap(writer, token) {
|
|
717
|
+
encodeUintValue(writer, Type.map.majorEncoded, token.value);
|
|
718
|
+
}
|
|
719
|
+
encodeMap.compareTokens = encodeUint.compareTokens;
|
|
720
|
+
encodeMap.encodedSize = function encodedSize6(token) {
|
|
721
|
+
return encodeUintValue.encodedSize(token.value);
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/6tag.js
|
|
725
|
+
function decodeTagCompact(_data, _pos, minor, _options) {
|
|
726
|
+
return new Token(Type.tag, minor, 1);
|
|
727
|
+
}
|
|
728
|
+
function decodeTag8(data, pos, _minor, options) {
|
|
729
|
+
return new Token(Type.tag, readUint8(data, pos + 1, options), 2);
|
|
730
|
+
}
|
|
731
|
+
function decodeTag16(data, pos, _minor, options) {
|
|
732
|
+
return new Token(Type.tag, readUint16(data, pos + 1, options), 3);
|
|
733
|
+
}
|
|
734
|
+
function decodeTag32(data, pos, _minor, options) {
|
|
735
|
+
return new Token(Type.tag, readUint32(data, pos + 1, options), 5);
|
|
736
|
+
}
|
|
737
|
+
function decodeTag64(data, pos, _minor, options) {
|
|
738
|
+
return new Token(Type.tag, readUint64(data, pos + 1, options), 9);
|
|
739
|
+
}
|
|
740
|
+
function encodeTag(writer, token) {
|
|
741
|
+
encodeUintValue(writer, Type.tag.majorEncoded, token.value);
|
|
742
|
+
}
|
|
743
|
+
encodeTag.compareTokens = encodeUint.compareTokens;
|
|
744
|
+
encodeTag.encodedSize = function encodedSize7(token) {
|
|
745
|
+
return encodeUintValue.encodedSize(token.value);
|
|
746
|
+
};
|
|
747
|
+
|
|
748
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/7float.js
|
|
749
|
+
var MINOR_FALSE = 20;
|
|
750
|
+
var MINOR_TRUE = 21;
|
|
751
|
+
var MINOR_NULL = 22;
|
|
752
|
+
var MINOR_UNDEFINED = 23;
|
|
753
|
+
function decodeUndefined(_data, _pos, _minor, options) {
|
|
754
|
+
if (options.allowUndefined === false) {
|
|
755
|
+
throw new Error(`${decodeErrPrefix} undefined values are not supported`);
|
|
756
|
+
} else if (options.coerceUndefinedToNull === true) {
|
|
757
|
+
return new Token(Type.null, null, 1);
|
|
758
|
+
}
|
|
759
|
+
return new Token(Type.undefined, undefined, 1);
|
|
760
|
+
}
|
|
761
|
+
function decodeBreak(_data, _pos, _minor, options) {
|
|
762
|
+
if (options.allowIndefinite === false) {
|
|
763
|
+
throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
764
|
+
}
|
|
765
|
+
return new Token(Type.break, undefined, 1);
|
|
766
|
+
}
|
|
767
|
+
function createToken(value, bytes, options) {
|
|
768
|
+
if (options) {
|
|
769
|
+
if (options.allowNaN === false && Number.isNaN(value)) {
|
|
770
|
+
throw new Error(`${decodeErrPrefix} NaN values are not supported`);
|
|
771
|
+
}
|
|
772
|
+
if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) {
|
|
773
|
+
throw new Error(`${decodeErrPrefix} Infinity values are not supported`);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return new Token(Type.float, value, bytes);
|
|
777
|
+
}
|
|
778
|
+
function decodeFloat16(data, pos, _minor, options) {
|
|
779
|
+
return createToken(readFloat16(data, pos + 1), 3, options);
|
|
780
|
+
}
|
|
781
|
+
function decodeFloat32(data, pos, _minor, options) {
|
|
782
|
+
return createToken(readFloat32(data, pos + 1), 5, options);
|
|
783
|
+
}
|
|
784
|
+
function decodeFloat64(data, pos, _minor, options) {
|
|
785
|
+
return createToken(readFloat64(data, pos + 1), 9, options);
|
|
786
|
+
}
|
|
787
|
+
function encodeFloat(writer, token, options) {
|
|
788
|
+
const float = token.value;
|
|
789
|
+
if (float === false) {
|
|
790
|
+
writer.push([Type.float.majorEncoded | MINOR_FALSE]);
|
|
791
|
+
} else if (float === true) {
|
|
792
|
+
writer.push([Type.float.majorEncoded | MINOR_TRUE]);
|
|
793
|
+
} else if (float === null) {
|
|
794
|
+
writer.push([Type.float.majorEncoded | MINOR_NULL]);
|
|
795
|
+
} else if (float === undefined) {
|
|
796
|
+
writer.push([Type.float.majorEncoded | MINOR_UNDEFINED]);
|
|
797
|
+
} else {
|
|
798
|
+
let decoded;
|
|
799
|
+
let success = false;
|
|
800
|
+
if (!options || options.float64 !== true) {
|
|
801
|
+
encodeFloat16(float);
|
|
802
|
+
decoded = readFloat16(ui8a, 1);
|
|
803
|
+
if (float === decoded || Number.isNaN(float)) {
|
|
804
|
+
ui8a[0] = 249;
|
|
805
|
+
writer.push(ui8a.slice(0, 3));
|
|
806
|
+
success = true;
|
|
807
|
+
} else {
|
|
808
|
+
encodeFloat32(float);
|
|
809
|
+
decoded = readFloat32(ui8a, 1);
|
|
810
|
+
if (float === decoded) {
|
|
811
|
+
ui8a[0] = 250;
|
|
812
|
+
writer.push(ui8a.slice(0, 5));
|
|
813
|
+
success = true;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
if (!success) {
|
|
818
|
+
encodeFloat64(float);
|
|
819
|
+
decoded = readFloat64(ui8a, 1);
|
|
820
|
+
ui8a[0] = 251;
|
|
821
|
+
writer.push(ui8a.slice(0, 9));
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
encodeFloat.encodedSize = function encodedSize8(token, options) {
|
|
826
|
+
const float = token.value;
|
|
827
|
+
if (float === false || float === true || float === null || float === undefined) {
|
|
828
|
+
return 1;
|
|
829
|
+
}
|
|
830
|
+
if (!options || options.float64 !== true) {
|
|
831
|
+
encodeFloat16(float);
|
|
832
|
+
let decoded = readFloat16(ui8a, 1);
|
|
833
|
+
if (float === decoded || Number.isNaN(float)) {
|
|
834
|
+
return 3;
|
|
835
|
+
}
|
|
836
|
+
encodeFloat32(float);
|
|
837
|
+
decoded = readFloat32(ui8a, 1);
|
|
838
|
+
if (float === decoded) {
|
|
839
|
+
return 5;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
return 9;
|
|
843
|
+
};
|
|
844
|
+
var buffer = new ArrayBuffer(9);
|
|
845
|
+
var dataView = new DataView(buffer, 1);
|
|
846
|
+
var ui8a = new Uint8Array(buffer, 0);
|
|
847
|
+
function encodeFloat16(inp) {
|
|
848
|
+
if (inp === Infinity) {
|
|
849
|
+
dataView.setUint16(0, 31744, false);
|
|
850
|
+
} else if (inp === -Infinity) {
|
|
851
|
+
dataView.setUint16(0, 64512, false);
|
|
852
|
+
} else if (Number.isNaN(inp)) {
|
|
853
|
+
dataView.setUint16(0, 32256, false);
|
|
854
|
+
} else {
|
|
855
|
+
dataView.setFloat32(0, inp);
|
|
856
|
+
const valu32 = dataView.getUint32(0);
|
|
857
|
+
const exponent = (valu32 & 2139095040) >> 23;
|
|
858
|
+
const mantissa = valu32 & 8388607;
|
|
859
|
+
if (exponent === 255) {
|
|
860
|
+
dataView.setUint16(0, 31744, false);
|
|
861
|
+
} else if (exponent === 0) {
|
|
862
|
+
dataView.setUint16(0, (valu32 & 2147483648) >> 16 | mantissa >> 13, false);
|
|
863
|
+
} else {
|
|
864
|
+
const logicalExponent = exponent - 127;
|
|
865
|
+
if (logicalExponent < -24) {
|
|
866
|
+
dataView.setUint16(0, 0);
|
|
867
|
+
} else if (logicalExponent < -14) {
|
|
868
|
+
dataView.setUint16(0, (valu32 & 2147483648) >> 16 | 1 << 24 + logicalExponent, false);
|
|
869
|
+
} else {
|
|
870
|
+
dataView.setUint16(0, (valu32 & 2147483648) >> 16 | logicalExponent + 15 << 10 | mantissa >> 13, false);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
function readFloat16(ui8a2, pos) {
|
|
876
|
+
if (ui8a2.length - pos < 2) {
|
|
877
|
+
throw new Error(`${decodeErrPrefix} not enough data for float16`);
|
|
878
|
+
}
|
|
879
|
+
const half = (ui8a2[pos] << 8) + ui8a2[pos + 1];
|
|
880
|
+
if (half === 31744) {
|
|
881
|
+
return Infinity;
|
|
882
|
+
}
|
|
883
|
+
if (half === 64512) {
|
|
884
|
+
return -Infinity;
|
|
885
|
+
}
|
|
886
|
+
if (half === 32256) {
|
|
887
|
+
return NaN;
|
|
888
|
+
}
|
|
889
|
+
const exp = half >> 10 & 31;
|
|
890
|
+
const mant = half & 1023;
|
|
891
|
+
let val;
|
|
892
|
+
if (exp === 0) {
|
|
893
|
+
val = mant * 2 ** -24;
|
|
894
|
+
} else if (exp !== 31) {
|
|
895
|
+
val = (mant + 1024) * 2 ** (exp - 25);
|
|
896
|
+
} else {
|
|
897
|
+
val = mant === 0 ? Infinity : NaN;
|
|
898
|
+
}
|
|
899
|
+
return half & 32768 ? -val : val;
|
|
900
|
+
}
|
|
901
|
+
function encodeFloat32(inp) {
|
|
902
|
+
dataView.setFloat32(0, inp, false);
|
|
903
|
+
}
|
|
904
|
+
function readFloat32(ui8a2, pos) {
|
|
905
|
+
if (ui8a2.length - pos < 4) {
|
|
906
|
+
throw new Error(`${decodeErrPrefix} not enough data for float32`);
|
|
907
|
+
}
|
|
908
|
+
const offset = (ui8a2.byteOffset || 0) + pos;
|
|
909
|
+
return new DataView(ui8a2.buffer, offset, 4).getFloat32(0, false);
|
|
910
|
+
}
|
|
911
|
+
function encodeFloat64(inp) {
|
|
912
|
+
dataView.setFloat64(0, inp, false);
|
|
913
|
+
}
|
|
914
|
+
function readFloat64(ui8a2, pos) {
|
|
915
|
+
if (ui8a2.length - pos < 8) {
|
|
916
|
+
throw new Error(`${decodeErrPrefix} not enough data for float64`);
|
|
917
|
+
}
|
|
918
|
+
const offset = (ui8a2.byteOffset || 0) + pos;
|
|
919
|
+
return new DataView(ui8a2.buffer, offset, 8).getFloat64(0, false);
|
|
920
|
+
}
|
|
921
|
+
encodeFloat.compareTokens = encodeUint.compareTokens;
|
|
922
|
+
|
|
923
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/jump.js
|
|
924
|
+
function invalidMinor(data, pos, minor) {
|
|
925
|
+
throw new Error(`${decodeErrPrefix} encountered invalid minor (${minor}) for major ${data[pos] >>> 5}`);
|
|
926
|
+
}
|
|
927
|
+
function errorer(msg) {
|
|
928
|
+
return () => {
|
|
929
|
+
throw new Error(`${decodeErrPrefix} ${msg}`);
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
var jump = [];
|
|
933
|
+
for (let i = 0;i <= 23; i++) {
|
|
934
|
+
jump[i] = invalidMinor;
|
|
935
|
+
}
|
|
936
|
+
jump[24] = decodeUint8;
|
|
937
|
+
jump[25] = decodeUint16;
|
|
938
|
+
jump[26] = decodeUint32;
|
|
939
|
+
jump[27] = decodeUint64;
|
|
940
|
+
jump[28] = invalidMinor;
|
|
941
|
+
jump[29] = invalidMinor;
|
|
942
|
+
jump[30] = invalidMinor;
|
|
943
|
+
jump[31] = invalidMinor;
|
|
944
|
+
for (let i = 32;i <= 55; i++) {
|
|
945
|
+
jump[i] = invalidMinor;
|
|
946
|
+
}
|
|
947
|
+
jump[56] = decodeNegint8;
|
|
948
|
+
jump[57] = decodeNegint16;
|
|
949
|
+
jump[58] = decodeNegint32;
|
|
950
|
+
jump[59] = decodeNegint64;
|
|
951
|
+
jump[60] = invalidMinor;
|
|
952
|
+
jump[61] = invalidMinor;
|
|
953
|
+
jump[62] = invalidMinor;
|
|
954
|
+
jump[63] = invalidMinor;
|
|
955
|
+
for (let i = 64;i <= 87; i++) {
|
|
956
|
+
jump[i] = decodeBytesCompact;
|
|
957
|
+
}
|
|
958
|
+
jump[88] = decodeBytes8;
|
|
959
|
+
jump[89] = decodeBytes16;
|
|
960
|
+
jump[90] = decodeBytes32;
|
|
961
|
+
jump[91] = decodeBytes64;
|
|
962
|
+
jump[92] = invalidMinor;
|
|
963
|
+
jump[93] = invalidMinor;
|
|
964
|
+
jump[94] = invalidMinor;
|
|
965
|
+
jump[95] = errorer("indefinite length bytes/strings are not supported");
|
|
966
|
+
for (let i = 96;i <= 119; i++) {
|
|
967
|
+
jump[i] = decodeStringCompact;
|
|
968
|
+
}
|
|
969
|
+
jump[120] = decodeString8;
|
|
970
|
+
jump[121] = decodeString16;
|
|
971
|
+
jump[122] = decodeString32;
|
|
972
|
+
jump[123] = decodeString64;
|
|
973
|
+
jump[124] = invalidMinor;
|
|
974
|
+
jump[125] = invalidMinor;
|
|
975
|
+
jump[126] = invalidMinor;
|
|
976
|
+
jump[127] = errorer("indefinite length bytes/strings are not supported");
|
|
977
|
+
for (let i = 128;i <= 151; i++) {
|
|
978
|
+
jump[i] = decodeArrayCompact;
|
|
979
|
+
}
|
|
980
|
+
jump[152] = decodeArray8;
|
|
981
|
+
jump[153] = decodeArray16;
|
|
982
|
+
jump[154] = decodeArray32;
|
|
983
|
+
jump[155] = decodeArray64;
|
|
984
|
+
jump[156] = invalidMinor;
|
|
985
|
+
jump[157] = invalidMinor;
|
|
986
|
+
jump[158] = invalidMinor;
|
|
987
|
+
jump[159] = decodeArrayIndefinite;
|
|
988
|
+
for (let i = 160;i <= 183; i++) {
|
|
989
|
+
jump[i] = decodeMapCompact;
|
|
990
|
+
}
|
|
991
|
+
jump[184] = decodeMap8;
|
|
992
|
+
jump[185] = decodeMap16;
|
|
993
|
+
jump[186] = decodeMap32;
|
|
994
|
+
jump[187] = decodeMap64;
|
|
995
|
+
jump[188] = invalidMinor;
|
|
996
|
+
jump[189] = invalidMinor;
|
|
997
|
+
jump[190] = invalidMinor;
|
|
998
|
+
jump[191] = decodeMapIndefinite;
|
|
999
|
+
for (let i = 192;i <= 215; i++) {
|
|
1000
|
+
jump[i] = decodeTagCompact;
|
|
1001
|
+
}
|
|
1002
|
+
jump[216] = decodeTag8;
|
|
1003
|
+
jump[217] = decodeTag16;
|
|
1004
|
+
jump[218] = decodeTag32;
|
|
1005
|
+
jump[219] = decodeTag64;
|
|
1006
|
+
jump[220] = invalidMinor;
|
|
1007
|
+
jump[221] = invalidMinor;
|
|
1008
|
+
jump[222] = invalidMinor;
|
|
1009
|
+
jump[223] = invalidMinor;
|
|
1010
|
+
for (let i = 224;i <= 243; i++) {
|
|
1011
|
+
jump[i] = errorer("simple values are not supported");
|
|
1012
|
+
}
|
|
1013
|
+
jump[244] = invalidMinor;
|
|
1014
|
+
jump[245] = invalidMinor;
|
|
1015
|
+
jump[246] = invalidMinor;
|
|
1016
|
+
jump[247] = decodeUndefined;
|
|
1017
|
+
jump[248] = errorer("simple values are not supported");
|
|
1018
|
+
jump[249] = decodeFloat16;
|
|
1019
|
+
jump[250] = decodeFloat32;
|
|
1020
|
+
jump[251] = decodeFloat64;
|
|
1021
|
+
jump[252] = invalidMinor;
|
|
1022
|
+
jump[253] = invalidMinor;
|
|
1023
|
+
jump[254] = invalidMinor;
|
|
1024
|
+
jump[255] = decodeBreak;
|
|
1025
|
+
var quick = [];
|
|
1026
|
+
for (let i = 0;i < 24; i++) {
|
|
1027
|
+
quick[i] = new Token(Type.uint, i, 1);
|
|
1028
|
+
}
|
|
1029
|
+
for (let i = -1;i >= -24; i--) {
|
|
1030
|
+
quick[31 - i] = new Token(Type.negint, i, 1);
|
|
1031
|
+
}
|
|
1032
|
+
quick[64] = new Token(Type.bytes, new Uint8Array(0), 1);
|
|
1033
|
+
quick[96] = new Token(Type.string, "", 1);
|
|
1034
|
+
quick[128] = new Token(Type.array, 0, 1);
|
|
1035
|
+
quick[160] = new Token(Type.map, 0, 1);
|
|
1036
|
+
quick[244] = new Token(Type.false, false, 1);
|
|
1037
|
+
quick[245] = new Token(Type.true, true, 1);
|
|
1038
|
+
quick[246] = new Token(Type.null, null, 1);
|
|
1039
|
+
function quickEncodeToken(token) {
|
|
1040
|
+
switch (token.type) {
|
|
1041
|
+
case Type.false:
|
|
1042
|
+
return fromArray([244]);
|
|
1043
|
+
case Type.true:
|
|
1044
|
+
return fromArray([245]);
|
|
1045
|
+
case Type.null:
|
|
1046
|
+
return fromArray([246]);
|
|
1047
|
+
case Type.bytes:
|
|
1048
|
+
if (!token.value.length) {
|
|
1049
|
+
return fromArray([64]);
|
|
1050
|
+
}
|
|
1051
|
+
return;
|
|
1052
|
+
case Type.string:
|
|
1053
|
+
if (token.value === "") {
|
|
1054
|
+
return fromArray([96]);
|
|
1055
|
+
}
|
|
1056
|
+
return;
|
|
1057
|
+
case Type.array:
|
|
1058
|
+
if (token.value === 0) {
|
|
1059
|
+
return fromArray([128]);
|
|
1060
|
+
}
|
|
1061
|
+
return;
|
|
1062
|
+
case Type.map:
|
|
1063
|
+
if (token.value === 0) {
|
|
1064
|
+
return fromArray([160]);
|
|
1065
|
+
}
|
|
1066
|
+
return;
|
|
1067
|
+
case Type.uint:
|
|
1068
|
+
if (token.value < 24) {
|
|
1069
|
+
return fromArray([Number(token.value)]);
|
|
1070
|
+
}
|
|
1071
|
+
return;
|
|
1072
|
+
case Type.negint:
|
|
1073
|
+
if (token.value >= -24) {
|
|
1074
|
+
return fromArray([31 - Number(token.value)]);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/encode.js
|
|
1080
|
+
var defaultEncodeOptions = {
|
|
1081
|
+
float64: false,
|
|
1082
|
+
mapSorter,
|
|
1083
|
+
quickEncodeToken
|
|
1084
|
+
};
|
|
1085
|
+
var rfc8949EncodeOptions = Object.freeze({
|
|
1086
|
+
float64: true,
|
|
1087
|
+
mapSorter: rfc8949MapSorter,
|
|
1088
|
+
quickEncodeToken
|
|
1089
|
+
});
|
|
1090
|
+
function makeCborEncoders() {
|
|
1091
|
+
const encoders = [];
|
|
1092
|
+
encoders[Type.uint.major] = encodeUint;
|
|
1093
|
+
encoders[Type.negint.major] = encodeNegint;
|
|
1094
|
+
encoders[Type.bytes.major] = encodeBytes;
|
|
1095
|
+
encoders[Type.string.major] = encodeString;
|
|
1096
|
+
encoders[Type.array.major] = encodeArray;
|
|
1097
|
+
encoders[Type.map.major] = encodeMap;
|
|
1098
|
+
encoders[Type.tag.major] = encodeTag;
|
|
1099
|
+
encoders[Type.float.major] = encodeFloat;
|
|
1100
|
+
return encoders;
|
|
1101
|
+
}
|
|
1102
|
+
var cborEncoders = makeCborEncoders();
|
|
1103
|
+
var defaultWriter = new Bl;
|
|
1104
|
+
|
|
1105
|
+
class Ref {
|
|
1106
|
+
constructor(obj, parent) {
|
|
1107
|
+
this.obj = obj;
|
|
1108
|
+
this.parent = parent;
|
|
1109
|
+
}
|
|
1110
|
+
includes(obj) {
|
|
1111
|
+
let p = this;
|
|
1112
|
+
do {
|
|
1113
|
+
if (p.obj === obj) {
|
|
1114
|
+
return true;
|
|
1115
|
+
}
|
|
1116
|
+
} while (p = p.parent);
|
|
1117
|
+
return false;
|
|
1118
|
+
}
|
|
1119
|
+
static createCheck(stack, obj) {
|
|
1120
|
+
if (stack && stack.includes(obj)) {
|
|
1121
|
+
throw new Error(`${encodeErrPrefix} object contains circular references`);
|
|
1122
|
+
}
|
|
1123
|
+
return new Ref(obj, stack);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
var simpleTokens = {
|
|
1127
|
+
null: new Token(Type.null, null),
|
|
1128
|
+
undefined: new Token(Type.undefined, undefined),
|
|
1129
|
+
true: new Token(Type.true, true),
|
|
1130
|
+
false: new Token(Type.false, false),
|
|
1131
|
+
emptyArray: new Token(Type.array, 0),
|
|
1132
|
+
emptyMap: new Token(Type.map, 0)
|
|
1133
|
+
};
|
|
1134
|
+
var typeEncoders = {
|
|
1135
|
+
number(obj, _typ, _options, _refStack) {
|
|
1136
|
+
if (!Number.isInteger(obj) || !Number.isSafeInteger(obj)) {
|
|
1137
|
+
return new Token(Type.float, obj);
|
|
1138
|
+
} else if (obj >= 0) {
|
|
1139
|
+
return new Token(Type.uint, obj);
|
|
1140
|
+
} else {
|
|
1141
|
+
return new Token(Type.negint, obj);
|
|
1142
|
+
}
|
|
1143
|
+
},
|
|
1144
|
+
bigint(obj, _typ, _options, _refStack) {
|
|
1145
|
+
if (obj >= BigInt(0)) {
|
|
1146
|
+
return new Token(Type.uint, obj);
|
|
1147
|
+
} else {
|
|
1148
|
+
return new Token(Type.negint, obj);
|
|
1149
|
+
}
|
|
1150
|
+
},
|
|
1151
|
+
Uint8Array(obj, _typ, _options, _refStack) {
|
|
1152
|
+
return new Token(Type.bytes, obj);
|
|
1153
|
+
},
|
|
1154
|
+
string(obj, _typ, _options, _refStack) {
|
|
1155
|
+
return new Token(Type.string, obj);
|
|
1156
|
+
},
|
|
1157
|
+
boolean(obj, _typ, _options, _refStack) {
|
|
1158
|
+
return obj ? simpleTokens.true : simpleTokens.false;
|
|
1159
|
+
},
|
|
1160
|
+
null(_obj, _typ, _options, _refStack) {
|
|
1161
|
+
return simpleTokens.null;
|
|
1162
|
+
},
|
|
1163
|
+
undefined(_obj, _typ, _options, _refStack) {
|
|
1164
|
+
return simpleTokens.undefined;
|
|
1165
|
+
},
|
|
1166
|
+
ArrayBuffer(obj, _typ, _options, _refStack) {
|
|
1167
|
+
return new Token(Type.bytes, new Uint8Array(obj));
|
|
1168
|
+
},
|
|
1169
|
+
DataView(obj, _typ, _options, _refStack) {
|
|
1170
|
+
return new Token(Type.bytes, new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength));
|
|
1171
|
+
},
|
|
1172
|
+
Array(obj, _typ, options, refStack) {
|
|
1173
|
+
if (!obj.length) {
|
|
1174
|
+
if (options.addBreakTokens === true) {
|
|
1175
|
+
return [simpleTokens.emptyArray, new Token(Type.break)];
|
|
1176
|
+
}
|
|
1177
|
+
return simpleTokens.emptyArray;
|
|
1178
|
+
}
|
|
1179
|
+
refStack = Ref.createCheck(refStack, obj);
|
|
1180
|
+
const entries = [];
|
|
1181
|
+
let i = 0;
|
|
1182
|
+
for (const e of obj) {
|
|
1183
|
+
entries[i++] = objectToTokens(e, options, refStack);
|
|
1184
|
+
}
|
|
1185
|
+
if (options.addBreakTokens) {
|
|
1186
|
+
return [new Token(Type.array, obj.length), entries, new Token(Type.break)];
|
|
1187
|
+
}
|
|
1188
|
+
return [new Token(Type.array, obj.length), entries];
|
|
1189
|
+
},
|
|
1190
|
+
Object(obj, typ, options, refStack) {
|
|
1191
|
+
const isMap = typ !== "Object";
|
|
1192
|
+
const keys = isMap ? obj.keys() : Object.keys(obj);
|
|
1193
|
+
const maxLength = isMap ? obj.size : keys.length;
|
|
1194
|
+
let entries;
|
|
1195
|
+
if (maxLength) {
|
|
1196
|
+
entries = new Array(maxLength);
|
|
1197
|
+
refStack = Ref.createCheck(refStack, obj);
|
|
1198
|
+
const skipUndefined = !isMap && options.ignoreUndefinedProperties;
|
|
1199
|
+
let i = 0;
|
|
1200
|
+
for (const key of keys) {
|
|
1201
|
+
const value = isMap ? obj.get(key) : obj[key];
|
|
1202
|
+
if (skipUndefined && value === undefined) {
|
|
1203
|
+
continue;
|
|
1204
|
+
}
|
|
1205
|
+
entries[i++] = [
|
|
1206
|
+
objectToTokens(key, options, refStack),
|
|
1207
|
+
objectToTokens(value, options, refStack)
|
|
1208
|
+
];
|
|
1209
|
+
}
|
|
1210
|
+
if (i < maxLength) {
|
|
1211
|
+
entries.length = i;
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
if (!entries?.length) {
|
|
1215
|
+
if (options.addBreakTokens === true) {
|
|
1216
|
+
return [simpleTokens.emptyMap, new Token(Type.break)];
|
|
1217
|
+
}
|
|
1218
|
+
return simpleTokens.emptyMap;
|
|
1219
|
+
}
|
|
1220
|
+
sortMapEntries(entries, options);
|
|
1221
|
+
if (options.addBreakTokens) {
|
|
1222
|
+
return [new Token(Type.map, entries.length), entries, new Token(Type.break)];
|
|
1223
|
+
}
|
|
1224
|
+
return [new Token(Type.map, entries.length), entries];
|
|
1225
|
+
},
|
|
1226
|
+
Tagged(obj, _typ, options, refStack) {
|
|
1227
|
+
return [
|
|
1228
|
+
new Token(Type.tag, obj.tag),
|
|
1229
|
+
objectToTokens(obj.value, options, refStack)
|
|
1230
|
+
];
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
typeEncoders.Map = typeEncoders.Object;
|
|
1234
|
+
typeEncoders.Buffer = typeEncoders.Uint8Array;
|
|
1235
|
+
for (const typ of "Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" ")) {
|
|
1236
|
+
typeEncoders[`${typ}Array`] = typeEncoders.DataView;
|
|
1237
|
+
}
|
|
1238
|
+
function objectToTokens(obj, options = {}, refStack) {
|
|
1239
|
+
const typ = is(obj);
|
|
1240
|
+
const customTypeEncoder = options && options.typeEncoders && options.typeEncoders[typ] || typeEncoders[typ];
|
|
1241
|
+
if (typeof customTypeEncoder === "function") {
|
|
1242
|
+
const tokens = customTypeEncoder(obj, typ, options, refStack);
|
|
1243
|
+
if (tokens != null) {
|
|
1244
|
+
return tokens;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
const typeEncoder = typeEncoders[typ];
|
|
1248
|
+
if (!typeEncoder) {
|
|
1249
|
+
throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`);
|
|
1250
|
+
}
|
|
1251
|
+
return typeEncoder(obj, typ, options, refStack);
|
|
1252
|
+
}
|
|
1253
|
+
function sortMapEntries(entries, options) {
|
|
1254
|
+
if (options.mapSorter) {
|
|
1255
|
+
entries.sort(options.mapSorter);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
function mapSorter(e1, e2) {
|
|
1259
|
+
const keyToken1 = Array.isArray(e1[0]) ? e1[0][0] : e1[0];
|
|
1260
|
+
const keyToken2 = Array.isArray(e2[0]) ? e2[0][0] : e2[0];
|
|
1261
|
+
if (keyToken1.type !== keyToken2.type) {
|
|
1262
|
+
return keyToken1.type.compare(keyToken2.type);
|
|
1263
|
+
}
|
|
1264
|
+
const major = keyToken1.type.major;
|
|
1265
|
+
const tcmp = cborEncoders[major].compareTokens(keyToken1, keyToken2);
|
|
1266
|
+
if (tcmp === 0) {
|
|
1267
|
+
console.warn("WARNING: complex key types used, CBOR key sorting guarantees are gone");
|
|
1268
|
+
}
|
|
1269
|
+
return tcmp;
|
|
1270
|
+
}
|
|
1271
|
+
function rfc8949MapSorter(e1, e2) {
|
|
1272
|
+
if (e1[0] instanceof Token && e2[0] instanceof Token) {
|
|
1273
|
+
const t1 = e1[0];
|
|
1274
|
+
const t2 = e2[0];
|
|
1275
|
+
if (!t1._keyBytes) {
|
|
1276
|
+
t1._keyBytes = encodeRfc8949(t1.value);
|
|
1277
|
+
}
|
|
1278
|
+
if (!t2._keyBytes) {
|
|
1279
|
+
t2._keyBytes = encodeRfc8949(t2.value);
|
|
1280
|
+
}
|
|
1281
|
+
return compare(t1._keyBytes, t2._keyBytes);
|
|
1282
|
+
}
|
|
1283
|
+
throw new Error("rfc8949MapSorter: complex key types are not supported yet");
|
|
1284
|
+
}
|
|
1285
|
+
function encodeRfc8949(data) {
|
|
1286
|
+
return encodeCustom(data, cborEncoders, rfc8949EncodeOptions);
|
|
1287
|
+
}
|
|
1288
|
+
function tokensToEncoded(writer, tokens, encoders, options) {
|
|
1289
|
+
if (Array.isArray(tokens)) {
|
|
1290
|
+
for (const token of tokens) {
|
|
1291
|
+
tokensToEncoded(writer, token, encoders, options);
|
|
1292
|
+
}
|
|
1293
|
+
} else {
|
|
1294
|
+
encoders[tokens.type.major](writer, tokens, options);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
var MAJOR_UINT = Type.uint.majorEncoded;
|
|
1298
|
+
var MAJOR_NEGINT = Type.negint.majorEncoded;
|
|
1299
|
+
var MAJOR_BYTES = Type.bytes.majorEncoded;
|
|
1300
|
+
var MAJOR_STRING = Type.string.majorEncoded;
|
|
1301
|
+
var MAJOR_ARRAY = Type.array.majorEncoded;
|
|
1302
|
+
var SIMPLE_FALSE = Type.float.majorEncoded | MINOR_FALSE;
|
|
1303
|
+
var SIMPLE_TRUE = Type.float.majorEncoded | MINOR_TRUE;
|
|
1304
|
+
var SIMPLE_NULL = Type.float.majorEncoded | MINOR_NULL;
|
|
1305
|
+
var SIMPLE_UNDEFINED = Type.float.majorEncoded | MINOR_UNDEFINED;
|
|
1306
|
+
var neg1b2 = BigInt(-1);
|
|
1307
|
+
var pos1b2 = BigInt(1);
|
|
1308
|
+
function canDirectEncode(options) {
|
|
1309
|
+
return options.addBreakTokens !== true;
|
|
1310
|
+
}
|
|
1311
|
+
function directEncode(writer, data, options, refStack) {
|
|
1312
|
+
const typ = is(data);
|
|
1313
|
+
const customEncoder = options.typeEncoders && options.typeEncoders[typ];
|
|
1314
|
+
if (customEncoder) {
|
|
1315
|
+
const tokens = customEncoder(data, typ, options, refStack);
|
|
1316
|
+
if (tokens != null) {
|
|
1317
|
+
tokensToEncoded(writer, tokens, cborEncoders, options);
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
switch (typ) {
|
|
1322
|
+
case "null":
|
|
1323
|
+
writer.push([SIMPLE_NULL]);
|
|
1324
|
+
return;
|
|
1325
|
+
case "undefined":
|
|
1326
|
+
writer.push([SIMPLE_UNDEFINED]);
|
|
1327
|
+
return;
|
|
1328
|
+
case "boolean":
|
|
1329
|
+
writer.push([data ? SIMPLE_TRUE : SIMPLE_FALSE]);
|
|
1330
|
+
return;
|
|
1331
|
+
case "number":
|
|
1332
|
+
if (!Number.isInteger(data) || !Number.isSafeInteger(data)) {
|
|
1333
|
+
encodeFloat(writer, new Token(Type.float, data), options);
|
|
1334
|
+
} else if (data >= 0) {
|
|
1335
|
+
encodeUintValue(writer, MAJOR_UINT, data);
|
|
1336
|
+
} else {
|
|
1337
|
+
encodeUintValue(writer, MAJOR_NEGINT, data * -1 - 1);
|
|
1338
|
+
}
|
|
1339
|
+
return;
|
|
1340
|
+
case "bigint":
|
|
1341
|
+
if (data >= BigInt(0)) {
|
|
1342
|
+
encodeUintValue(writer, MAJOR_UINT, data);
|
|
1343
|
+
} else {
|
|
1344
|
+
encodeUintValue(writer, MAJOR_NEGINT, data * neg1b2 - pos1b2);
|
|
1345
|
+
}
|
|
1346
|
+
return;
|
|
1347
|
+
case "string": {
|
|
1348
|
+
const bytes = fromString(data);
|
|
1349
|
+
encodeUintValue(writer, MAJOR_STRING, bytes.length);
|
|
1350
|
+
writer.push(bytes);
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
case "Uint8Array":
|
|
1354
|
+
encodeUintValue(writer, MAJOR_BYTES, data.length);
|
|
1355
|
+
writer.push(data);
|
|
1356
|
+
return;
|
|
1357
|
+
case "Array":
|
|
1358
|
+
if (!data.length) {
|
|
1359
|
+
writer.push([MAJOR_ARRAY]);
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
refStack = Ref.createCheck(refStack, data);
|
|
1363
|
+
encodeUintValue(writer, MAJOR_ARRAY, data.length);
|
|
1364
|
+
for (const elem of data) {
|
|
1365
|
+
directEncode(writer, elem, options, refStack);
|
|
1366
|
+
}
|
|
1367
|
+
return;
|
|
1368
|
+
case "Object":
|
|
1369
|
+
case "Map":
|
|
1370
|
+
{
|
|
1371
|
+
const tokens = typeEncoders.Object(data, typ, options, refStack);
|
|
1372
|
+
tokensToEncoded(writer, tokens, cborEncoders, options);
|
|
1373
|
+
}
|
|
1374
|
+
return;
|
|
1375
|
+
default: {
|
|
1376
|
+
const typeEncoder = typeEncoders[typ];
|
|
1377
|
+
if (!typeEncoder) {
|
|
1378
|
+
throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`);
|
|
1379
|
+
}
|
|
1380
|
+
const tokens = typeEncoder(data, typ, options, refStack);
|
|
1381
|
+
tokensToEncoded(writer, tokens, cborEncoders, options);
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
function encodeCustom(data, encoders, options, destination) {
|
|
1386
|
+
const hasDest = destination instanceof Uint8Array;
|
|
1387
|
+
let writeTo = hasDest ? new U8Bl(destination) : defaultWriter;
|
|
1388
|
+
const tokens = objectToTokens(data, options);
|
|
1389
|
+
if (!Array.isArray(tokens) && options.quickEncodeToken) {
|
|
1390
|
+
const quickBytes = options.quickEncodeToken(tokens);
|
|
1391
|
+
if (quickBytes) {
|
|
1392
|
+
if (hasDest) {
|
|
1393
|
+
writeTo.push(quickBytes);
|
|
1394
|
+
return writeTo.toBytes();
|
|
1395
|
+
}
|
|
1396
|
+
return quickBytes;
|
|
1397
|
+
}
|
|
1398
|
+
const encoder = encoders[tokens.type.major];
|
|
1399
|
+
if (encoder.encodedSize) {
|
|
1400
|
+
const size = encoder.encodedSize(tokens, options);
|
|
1401
|
+
if (!hasDest) {
|
|
1402
|
+
writeTo = new Bl(size);
|
|
1403
|
+
}
|
|
1404
|
+
encoder(writeTo, tokens, options);
|
|
1405
|
+
if (writeTo.chunks.length !== 1) {
|
|
1406
|
+
throw new Error(`Unexpected error: pre-calculated length for ${tokens} was wrong`);
|
|
1407
|
+
}
|
|
1408
|
+
return hasDest ? writeTo.toBytes() : asU8A(writeTo.chunks[0]);
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
writeTo.reset();
|
|
1412
|
+
tokensToEncoded(writeTo, tokens, encoders, options);
|
|
1413
|
+
return writeTo.toBytes(true);
|
|
1414
|
+
}
|
|
1415
|
+
function encode(data, options) {
|
|
1416
|
+
options = Object.assign({}, defaultEncodeOptions, options);
|
|
1417
|
+
if (canDirectEncode(options)) {
|
|
1418
|
+
defaultWriter.reset();
|
|
1419
|
+
directEncode(defaultWriter, data, options, undefined);
|
|
1420
|
+
return defaultWriter.toBytes(true);
|
|
1421
|
+
}
|
|
1422
|
+
return encodeCustom(data, cborEncoders, options);
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/decode.js
|
|
1426
|
+
var defaultDecodeOptions = {
|
|
1427
|
+
strict: false,
|
|
1428
|
+
allowIndefinite: true,
|
|
1429
|
+
allowUndefined: true,
|
|
1430
|
+
allowBigInt: true
|
|
1431
|
+
};
|
|
1432
|
+
|
|
1433
|
+
class Tokeniser {
|
|
1434
|
+
constructor(data, options = {}) {
|
|
1435
|
+
this._pos = 0;
|
|
1436
|
+
this.data = data;
|
|
1437
|
+
this.options = options;
|
|
1438
|
+
}
|
|
1439
|
+
pos() {
|
|
1440
|
+
return this._pos;
|
|
1441
|
+
}
|
|
1442
|
+
done() {
|
|
1443
|
+
return this._pos >= this.data.length;
|
|
1444
|
+
}
|
|
1445
|
+
next() {
|
|
1446
|
+
const byt = this.data[this._pos];
|
|
1447
|
+
let token = quick[byt];
|
|
1448
|
+
if (token === undefined) {
|
|
1449
|
+
const decoder = jump[byt];
|
|
1450
|
+
if (!decoder) {
|
|
1451
|
+
throw new Error(`${decodeErrPrefix} no decoder for major type ${byt >>> 5} (byte 0x${byt.toString(16).padStart(2, "0")})`);
|
|
1452
|
+
}
|
|
1453
|
+
const minor = byt & 31;
|
|
1454
|
+
token = decoder(this.data, this._pos, minor, this.options);
|
|
1455
|
+
}
|
|
1456
|
+
this._pos += token.encodedLength;
|
|
1457
|
+
return token;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
var DONE = Symbol.for("DONE");
|
|
1461
|
+
var BREAK = Symbol.for("BREAK");
|
|
1462
|
+
function tokenToArray(token, tokeniser, options) {
|
|
1463
|
+
const arr = [];
|
|
1464
|
+
for (let i = 0;i < token.value; i++) {
|
|
1465
|
+
const value = tokensToObject(tokeniser, options);
|
|
1466
|
+
if (value === BREAK) {
|
|
1467
|
+
if (token.value === Infinity) {
|
|
1468
|
+
break;
|
|
1469
|
+
}
|
|
1470
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed array`);
|
|
1471
|
+
}
|
|
1472
|
+
if (value === DONE) {
|
|
1473
|
+
throw new Error(`${decodeErrPrefix} found array but not enough entries (got ${i}, expected ${token.value})`);
|
|
1474
|
+
}
|
|
1475
|
+
arr[i] = value;
|
|
1476
|
+
}
|
|
1477
|
+
return arr;
|
|
1478
|
+
}
|
|
1479
|
+
function tokenToMap(token, tokeniser, options) {
|
|
1480
|
+
const useMaps = options.useMaps === true;
|
|
1481
|
+
const rejectDuplicateMapKeys = options.rejectDuplicateMapKeys === true;
|
|
1482
|
+
const obj = useMaps ? undefined : {};
|
|
1483
|
+
const m = useMaps ? new Map : undefined;
|
|
1484
|
+
for (let i = 0;i < token.value; i++) {
|
|
1485
|
+
const key = tokensToObject(tokeniser, options);
|
|
1486
|
+
if (key === BREAK) {
|
|
1487
|
+
if (token.value === Infinity) {
|
|
1488
|
+
break;
|
|
1489
|
+
}
|
|
1490
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`);
|
|
1491
|
+
}
|
|
1492
|
+
if (key === DONE) {
|
|
1493
|
+
throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`);
|
|
1494
|
+
}
|
|
1495
|
+
if (!useMaps && typeof key !== "string") {
|
|
1496
|
+
throw new Error(`${decodeErrPrefix} non-string keys not supported (got ${typeof key})`);
|
|
1497
|
+
}
|
|
1498
|
+
if (rejectDuplicateMapKeys) {
|
|
1499
|
+
if (useMaps && m.has(key) || !useMaps && Object.hasOwn(obj, key)) {
|
|
1500
|
+
throw new Error(`${decodeErrPrefix} found repeat map key "${key}"`);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
const value = tokensToObject(tokeniser, options);
|
|
1504
|
+
if (value === DONE) {
|
|
1505
|
+
throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`);
|
|
1506
|
+
}
|
|
1507
|
+
if (useMaps) {
|
|
1508
|
+
m.set(key, value);
|
|
1509
|
+
} else {
|
|
1510
|
+
obj[key] = value;
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
return useMaps ? m : obj;
|
|
1514
|
+
}
|
|
1515
|
+
function* tokenToMapEntries(token, tokeniser, options) {
|
|
1516
|
+
for (let i = 0;i < token.value; i++) {
|
|
1517
|
+
const key = tokensToObject(tokeniser, options);
|
|
1518
|
+
if (key === BREAK) {
|
|
1519
|
+
if (token.value === Infinity) {
|
|
1520
|
+
break;
|
|
1521
|
+
}
|
|
1522
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`);
|
|
1523
|
+
}
|
|
1524
|
+
if (key === DONE) {
|
|
1525
|
+
throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`);
|
|
1526
|
+
}
|
|
1527
|
+
const value = tokensToObject(tokeniser, options);
|
|
1528
|
+
if (value === DONE) {
|
|
1529
|
+
throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`);
|
|
1530
|
+
}
|
|
1531
|
+
yield [key, value];
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
function createTagDecodeControl(tokeniser, options) {
|
|
1535
|
+
let called = false;
|
|
1536
|
+
const decode = function() {
|
|
1537
|
+
if (called) {
|
|
1538
|
+
throw new Error(`${decodeErrPrefix} tag decode() may only be called once`);
|
|
1539
|
+
}
|
|
1540
|
+
called = true;
|
|
1541
|
+
const value = tokensToObject(tokeniser, options);
|
|
1542
|
+
if (value === DONE) {
|
|
1543
|
+
throw new Error(`${decodeErrPrefix} tag content missing`);
|
|
1544
|
+
}
|
|
1545
|
+
if (value === BREAK) {
|
|
1546
|
+
throw new Error(`${decodeErrPrefix} got unexpected break in tag content`);
|
|
1547
|
+
}
|
|
1548
|
+
return value;
|
|
1549
|
+
};
|
|
1550
|
+
decode.entries = function() {
|
|
1551
|
+
if (called) {
|
|
1552
|
+
throw new Error(`${decodeErrPrefix} tag decode() may only be called once`);
|
|
1553
|
+
}
|
|
1554
|
+
called = true;
|
|
1555
|
+
const token = tokeniser.next();
|
|
1556
|
+
if (!Type.equals(token.type, Type.map)) {
|
|
1557
|
+
throw new Error(`${decodeErrPrefix} entries() requires map content, got ${token.type.name}`);
|
|
1558
|
+
}
|
|
1559
|
+
const entries = [];
|
|
1560
|
+
for (const entry of tokenToMapEntries(token, tokeniser, options)) {
|
|
1561
|
+
entries.push(entry);
|
|
1562
|
+
}
|
|
1563
|
+
return entries;
|
|
1564
|
+
};
|
|
1565
|
+
Object.defineProperty(decode, "_called", {
|
|
1566
|
+
get() {
|
|
1567
|
+
return called;
|
|
1568
|
+
},
|
|
1569
|
+
enumerable: false
|
|
1570
|
+
});
|
|
1571
|
+
return decode;
|
|
1572
|
+
}
|
|
1573
|
+
function tokensToObject(tokeniser, options) {
|
|
1574
|
+
if (tokeniser.done()) {
|
|
1575
|
+
return DONE;
|
|
1576
|
+
}
|
|
1577
|
+
const token = tokeniser.next();
|
|
1578
|
+
if (Type.equals(token.type, Type.break)) {
|
|
1579
|
+
return BREAK;
|
|
1580
|
+
}
|
|
1581
|
+
if (token.type.terminal) {
|
|
1582
|
+
return token.value;
|
|
1583
|
+
}
|
|
1584
|
+
if (Type.equals(token.type, Type.array)) {
|
|
1585
|
+
return tokenToArray(token, tokeniser, options);
|
|
1586
|
+
}
|
|
1587
|
+
if (Type.equals(token.type, Type.map)) {
|
|
1588
|
+
return tokenToMap(token, tokeniser, options);
|
|
1589
|
+
}
|
|
1590
|
+
if (Type.equals(token.type, Type.tag)) {
|
|
1591
|
+
if (options.tags && typeof options.tags[token.value] === "function") {
|
|
1592
|
+
const decodeControl = createTagDecodeControl(tokeniser, options);
|
|
1593
|
+
const result = options.tags[token.value](decodeControl);
|
|
1594
|
+
if (!decodeControl._called) {
|
|
1595
|
+
throw new Error(`${decodeErrPrefix} tag decoder must call decode() or entries()`);
|
|
1596
|
+
}
|
|
1597
|
+
return result;
|
|
1598
|
+
}
|
|
1599
|
+
throw new Error(`${decodeErrPrefix} tag not supported (${token.value})`);
|
|
1600
|
+
}
|
|
1601
|
+
throw new Error("unsupported");
|
|
1602
|
+
}
|
|
1603
|
+
function decodeFirst(data, options) {
|
|
1604
|
+
if (!(data instanceof Uint8Array)) {
|
|
1605
|
+
throw new Error(`${decodeErrPrefix} data to decode must be a Uint8Array`);
|
|
1606
|
+
}
|
|
1607
|
+
options = Object.assign({}, defaultDecodeOptions, options);
|
|
1608
|
+
const u8aData = asU8A(data);
|
|
1609
|
+
const tokeniser = options.tokenizer || new Tokeniser(u8aData, options);
|
|
1610
|
+
const decoded = tokensToObject(tokeniser, options);
|
|
1611
|
+
if (decoded === DONE) {
|
|
1612
|
+
throw new Error(`${decodeErrPrefix} did not find any content to decode`);
|
|
1613
|
+
}
|
|
1614
|
+
if (decoded === BREAK) {
|
|
1615
|
+
throw new Error(`${decodeErrPrefix} got unexpected break`);
|
|
1616
|
+
}
|
|
1617
|
+
return [decoded, data.subarray(tokeniser.pos())];
|
|
1618
|
+
}
|
|
1619
|
+
function decode(data, options) {
|
|
1620
|
+
const [decoded, remainder] = decodeFirst(data, options);
|
|
1621
|
+
if (remainder.length > 0) {
|
|
1622
|
+
throw new Error(`${decodeErrPrefix} too many terminals, data makes no sense`);
|
|
1623
|
+
}
|
|
1624
|
+
return decoded;
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
// ../../node_modules/.bun/cborg@5.1.1/node_modules/cborg/lib/tagged.js
|
|
1628
|
+
class Tagged {
|
|
1629
|
+
constructor(tag, value) {
|
|
1630
|
+
if (typeof tag !== "number" || !Number.isInteger(tag) || tag < 0) {
|
|
1631
|
+
throw new TypeError("Tagged: tag must be a non-negative integer");
|
|
1632
|
+
}
|
|
1633
|
+
this.tag = tag;
|
|
1634
|
+
this.value = value;
|
|
1635
|
+
}
|
|
1636
|
+
static decoder(tag) {
|
|
1637
|
+
return (decode2) => new Tagged(tag, decode2());
|
|
1638
|
+
}
|
|
1639
|
+
static preserve(...tagNumbers) {
|
|
1640
|
+
const tags = {};
|
|
1641
|
+
for (const tag of tagNumbers) {
|
|
1642
|
+
tags[tag] = Tagged.decoder(tag);
|
|
1643
|
+
}
|
|
1644
|
+
return tags;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
Object.defineProperty(Tagged.prototype, Symbol.toStringTag, {
|
|
1648
|
+
value: "Tagged"
|
|
1649
|
+
});
|
|
1650
|
+
|
|
1651
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/bytes.js
|
|
1652
|
+
var empty = new Uint8Array(0);
|
|
1653
|
+
function equals(aa, bb) {
|
|
1654
|
+
if (aa === bb) {
|
|
1655
|
+
return true;
|
|
1656
|
+
}
|
|
1657
|
+
if (aa.byteLength !== bb.byteLength) {
|
|
1658
|
+
return false;
|
|
1659
|
+
}
|
|
1660
|
+
for (let ii = 0;ii < aa.byteLength; ii++) {
|
|
1661
|
+
if (aa[ii] !== bb[ii]) {
|
|
1662
|
+
return false;
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
return true;
|
|
1666
|
+
}
|
|
1667
|
+
function coerce(o) {
|
|
1668
|
+
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array") {
|
|
1669
|
+
return o;
|
|
1670
|
+
}
|
|
1671
|
+
if (o instanceof ArrayBuffer) {
|
|
1672
|
+
return new Uint8Array(o);
|
|
1673
|
+
}
|
|
1674
|
+
if (ArrayBuffer.isView(o)) {
|
|
1675
|
+
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
|
|
1676
|
+
}
|
|
1677
|
+
throw new Error("Unknown type, must be binary type");
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/vendor/base-x.js
|
|
1681
|
+
function base(ALPHABET, name) {
|
|
1682
|
+
if (ALPHABET.length >= 255) {
|
|
1683
|
+
throw new TypeError("Alphabet too long");
|
|
1684
|
+
}
|
|
1685
|
+
var BASE_MAP = new Uint8Array(256);
|
|
1686
|
+
for (var j = 0;j < BASE_MAP.length; j++) {
|
|
1687
|
+
BASE_MAP[j] = 255;
|
|
1688
|
+
}
|
|
1689
|
+
for (var i = 0;i < ALPHABET.length; i++) {
|
|
1690
|
+
var x = ALPHABET.charAt(i);
|
|
1691
|
+
var xc = x.charCodeAt(0);
|
|
1692
|
+
if (BASE_MAP[xc] !== 255) {
|
|
1693
|
+
throw new TypeError(x + " is ambiguous");
|
|
1694
|
+
}
|
|
1695
|
+
BASE_MAP[xc] = i;
|
|
1696
|
+
}
|
|
1697
|
+
var BASE = ALPHABET.length;
|
|
1698
|
+
var LEADER = ALPHABET.charAt(0);
|
|
1699
|
+
var FACTOR = Math.log(BASE) / Math.log(256);
|
|
1700
|
+
var iFACTOR = Math.log(256) / Math.log(BASE);
|
|
1701
|
+
function encode2(source) {
|
|
1702
|
+
if (source instanceof Uint8Array)
|
|
1703
|
+
;
|
|
1704
|
+
else if (ArrayBuffer.isView(source)) {
|
|
1705
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
1706
|
+
} else if (Array.isArray(source)) {
|
|
1707
|
+
source = Uint8Array.from(source);
|
|
1708
|
+
}
|
|
1709
|
+
if (!(source instanceof Uint8Array)) {
|
|
1710
|
+
throw new TypeError("Expected Uint8Array");
|
|
1711
|
+
}
|
|
1712
|
+
if (source.length === 0) {
|
|
1713
|
+
return "";
|
|
1714
|
+
}
|
|
1715
|
+
var zeroes = 0;
|
|
1716
|
+
var length = 0;
|
|
1717
|
+
var pbegin = 0;
|
|
1718
|
+
var pend = source.length;
|
|
1719
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
1720
|
+
pbegin++;
|
|
1721
|
+
zeroes++;
|
|
1722
|
+
}
|
|
1723
|
+
var size = (pend - pbegin) * iFACTOR + 1 >>> 0;
|
|
1724
|
+
var b58 = new Uint8Array(size);
|
|
1725
|
+
while (pbegin !== pend) {
|
|
1726
|
+
var carry = source[pbegin];
|
|
1727
|
+
var i2 = 0;
|
|
1728
|
+
for (var it1 = size - 1;(carry !== 0 || i2 < length) && it1 !== -1; it1--, i2++) {
|
|
1729
|
+
carry += 256 * b58[it1] >>> 0;
|
|
1730
|
+
b58[it1] = carry % BASE >>> 0;
|
|
1731
|
+
carry = carry / BASE >>> 0;
|
|
1732
|
+
}
|
|
1733
|
+
if (carry !== 0) {
|
|
1734
|
+
throw new Error("Non-zero carry");
|
|
1735
|
+
}
|
|
1736
|
+
length = i2;
|
|
1737
|
+
pbegin++;
|
|
1738
|
+
}
|
|
1739
|
+
var it2 = size - length;
|
|
1740
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
1741
|
+
it2++;
|
|
1742
|
+
}
|
|
1743
|
+
var str = LEADER.repeat(zeroes);
|
|
1744
|
+
for (;it2 < size; ++it2) {
|
|
1745
|
+
str += ALPHABET.charAt(b58[it2]);
|
|
1746
|
+
}
|
|
1747
|
+
return str;
|
|
1748
|
+
}
|
|
1749
|
+
function decodeUnsafe(source) {
|
|
1750
|
+
if (typeof source !== "string") {
|
|
1751
|
+
throw new TypeError("Expected String");
|
|
1752
|
+
}
|
|
1753
|
+
if (source.length === 0) {
|
|
1754
|
+
return new Uint8Array;
|
|
1755
|
+
}
|
|
1756
|
+
var psz = 0;
|
|
1757
|
+
if (source[psz] === " ") {
|
|
1758
|
+
return;
|
|
1759
|
+
}
|
|
1760
|
+
var zeroes = 0;
|
|
1761
|
+
var length = 0;
|
|
1762
|
+
while (source[psz] === LEADER) {
|
|
1763
|
+
zeroes++;
|
|
1764
|
+
psz++;
|
|
1765
|
+
}
|
|
1766
|
+
var size = (source.length - psz) * FACTOR + 1 >>> 0;
|
|
1767
|
+
var b256 = new Uint8Array(size);
|
|
1768
|
+
while (source[psz]) {
|
|
1769
|
+
var carry = BASE_MAP[source.charCodeAt(psz)];
|
|
1770
|
+
if (carry === 255) {
|
|
1771
|
+
return;
|
|
1772
|
+
}
|
|
1773
|
+
var i2 = 0;
|
|
1774
|
+
for (var it3 = size - 1;(carry !== 0 || i2 < length) && it3 !== -1; it3--, i2++) {
|
|
1775
|
+
carry += BASE * b256[it3] >>> 0;
|
|
1776
|
+
b256[it3] = carry % 256 >>> 0;
|
|
1777
|
+
carry = carry / 256 >>> 0;
|
|
1778
|
+
}
|
|
1779
|
+
if (carry !== 0) {
|
|
1780
|
+
throw new Error("Non-zero carry");
|
|
1781
|
+
}
|
|
1782
|
+
length = i2;
|
|
1783
|
+
psz++;
|
|
1784
|
+
}
|
|
1785
|
+
if (source[psz] === " ") {
|
|
1786
|
+
return;
|
|
1787
|
+
}
|
|
1788
|
+
var it4 = size - length;
|
|
1789
|
+
while (it4 !== size && b256[it4] === 0) {
|
|
1790
|
+
it4++;
|
|
1791
|
+
}
|
|
1792
|
+
var vch = new Uint8Array(zeroes + (size - it4));
|
|
1793
|
+
var j2 = zeroes;
|
|
1794
|
+
while (it4 !== size) {
|
|
1795
|
+
vch[j2++] = b256[it4++];
|
|
1796
|
+
}
|
|
1797
|
+
return vch;
|
|
1798
|
+
}
|
|
1799
|
+
function decode2(string) {
|
|
1800
|
+
var buffer2 = decodeUnsafe(string);
|
|
1801
|
+
if (buffer2) {
|
|
1802
|
+
return buffer2;
|
|
1803
|
+
}
|
|
1804
|
+
throw new Error(`Non-${name} character`);
|
|
1805
|
+
}
|
|
1806
|
+
return {
|
|
1807
|
+
encode: encode2,
|
|
1808
|
+
decodeUnsafe,
|
|
1809
|
+
decode: decode2
|
|
1810
|
+
};
|
|
1811
|
+
}
|
|
1812
|
+
var src = base;
|
|
1813
|
+
var _brrp__multiformats_scope_baseX = src;
|
|
1814
|
+
var base_x_default = _brrp__multiformats_scope_baseX;
|
|
1815
|
+
|
|
1816
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/bases/base.js
|
|
1817
|
+
class Encoder {
|
|
1818
|
+
name;
|
|
1819
|
+
prefix;
|
|
1820
|
+
baseEncode;
|
|
1821
|
+
constructor(name, prefix, baseEncode) {
|
|
1822
|
+
this.name = name;
|
|
1823
|
+
this.prefix = prefix;
|
|
1824
|
+
this.baseEncode = baseEncode;
|
|
1825
|
+
}
|
|
1826
|
+
encode(bytes) {
|
|
1827
|
+
if (bytes instanceof Uint8Array) {
|
|
1828
|
+
return `${this.prefix}${this.baseEncode(bytes)}`;
|
|
1829
|
+
} else {
|
|
1830
|
+
throw Error("Unknown type, must be binary type");
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
class Decoder {
|
|
1836
|
+
name;
|
|
1837
|
+
prefix;
|
|
1838
|
+
baseDecode;
|
|
1839
|
+
prefixCodePoint;
|
|
1840
|
+
constructor(name, prefix, baseDecode) {
|
|
1841
|
+
this.name = name;
|
|
1842
|
+
this.prefix = prefix;
|
|
1843
|
+
const prefixCodePoint = prefix.codePointAt(0);
|
|
1844
|
+
if (prefixCodePoint === undefined) {
|
|
1845
|
+
throw new Error("Invalid prefix character");
|
|
1846
|
+
}
|
|
1847
|
+
this.prefixCodePoint = prefixCodePoint;
|
|
1848
|
+
this.baseDecode = baseDecode;
|
|
1849
|
+
}
|
|
1850
|
+
decode(text) {
|
|
1851
|
+
if (typeof text === "string") {
|
|
1852
|
+
if (text.codePointAt(0) !== this.prefixCodePoint) {
|
|
1853
|
+
throw Error(`Unable to decode multibase string ${JSON.stringify(text)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);
|
|
1854
|
+
}
|
|
1855
|
+
return this.baseDecode(text.slice(this.prefix.length));
|
|
1856
|
+
} else {
|
|
1857
|
+
throw Error("Can only multibase decode strings");
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
or(decoder) {
|
|
1861
|
+
return or(this, decoder);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
class ComposedDecoder {
|
|
1866
|
+
decoders;
|
|
1867
|
+
constructor(decoders) {
|
|
1868
|
+
this.decoders = decoders;
|
|
1869
|
+
}
|
|
1870
|
+
or(decoder) {
|
|
1871
|
+
return or(this, decoder);
|
|
1872
|
+
}
|
|
1873
|
+
decode(input) {
|
|
1874
|
+
const prefix = input[0];
|
|
1875
|
+
const decoder = this.decoders[prefix];
|
|
1876
|
+
if (decoder != null) {
|
|
1877
|
+
return decoder.decode(input);
|
|
1878
|
+
} else {
|
|
1879
|
+
throw RangeError(`Unable to decode multibase string ${JSON.stringify(input)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`);
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
function or(left, right) {
|
|
1884
|
+
return new ComposedDecoder({
|
|
1885
|
+
...left.decoders ?? { [left.prefix]: left },
|
|
1886
|
+
...right.decoders ?? { [right.prefix]: right }
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
class Codec {
|
|
1891
|
+
name;
|
|
1892
|
+
prefix;
|
|
1893
|
+
baseEncode;
|
|
1894
|
+
baseDecode;
|
|
1895
|
+
encoder;
|
|
1896
|
+
decoder;
|
|
1897
|
+
constructor(name, prefix, baseEncode, baseDecode) {
|
|
1898
|
+
this.name = name;
|
|
1899
|
+
this.prefix = prefix;
|
|
1900
|
+
this.baseEncode = baseEncode;
|
|
1901
|
+
this.baseDecode = baseDecode;
|
|
1902
|
+
this.encoder = new Encoder(name, prefix, baseEncode);
|
|
1903
|
+
this.decoder = new Decoder(name, prefix, baseDecode);
|
|
1904
|
+
}
|
|
1905
|
+
encode(input) {
|
|
1906
|
+
return this.encoder.encode(input);
|
|
1907
|
+
}
|
|
1908
|
+
decode(input) {
|
|
1909
|
+
return this.decoder.decode(input);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
function from({ name, prefix, encode: encode2, decode: decode2 }) {
|
|
1913
|
+
return new Codec(name, prefix, encode2, decode2);
|
|
1914
|
+
}
|
|
1915
|
+
function baseX({ name, prefix, alphabet }) {
|
|
1916
|
+
const { encode: encode2, decode: decode2 } = base_x_default(alphabet, name);
|
|
1917
|
+
return from({
|
|
1918
|
+
prefix,
|
|
1919
|
+
name,
|
|
1920
|
+
encode: encode2,
|
|
1921
|
+
decode: (text) => coerce(decode2(text))
|
|
1922
|
+
});
|
|
1923
|
+
}
|
|
1924
|
+
function decode2(string, alphabetIdx, bitsPerChar, name) {
|
|
1925
|
+
let end = string.length;
|
|
1926
|
+
while (string[end - 1] === "=") {
|
|
1927
|
+
--end;
|
|
1928
|
+
}
|
|
1929
|
+
const out = new Uint8Array(end * bitsPerChar / 8 | 0);
|
|
1930
|
+
let bits = 0;
|
|
1931
|
+
let buffer2 = 0;
|
|
1932
|
+
let written = 0;
|
|
1933
|
+
for (let i = 0;i < end; ++i) {
|
|
1934
|
+
const value = alphabetIdx[string[i]];
|
|
1935
|
+
if (value === undefined) {
|
|
1936
|
+
throw new SyntaxError(`Non-${name} character`);
|
|
1937
|
+
}
|
|
1938
|
+
buffer2 = buffer2 << bitsPerChar | value;
|
|
1939
|
+
bits += bitsPerChar;
|
|
1940
|
+
if (bits >= 8) {
|
|
1941
|
+
bits -= 8;
|
|
1942
|
+
out[written++] = 255 & buffer2 >> bits;
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
if (bits >= bitsPerChar || (255 & buffer2 << 8 - bits) !== 0) {
|
|
1946
|
+
throw new SyntaxError("Unexpected end of data");
|
|
1947
|
+
}
|
|
1948
|
+
return out;
|
|
1949
|
+
}
|
|
1950
|
+
function encode2(data, alphabet, bitsPerChar) {
|
|
1951
|
+
const pad = alphabet[alphabet.length - 1] === "=";
|
|
1952
|
+
const mask = (1 << bitsPerChar) - 1;
|
|
1953
|
+
let out = "";
|
|
1954
|
+
let bits = 0;
|
|
1955
|
+
let buffer2 = 0;
|
|
1956
|
+
for (let i = 0;i < data.length; ++i) {
|
|
1957
|
+
buffer2 = buffer2 << 8 | data[i];
|
|
1958
|
+
bits += 8;
|
|
1959
|
+
while (bits > bitsPerChar) {
|
|
1960
|
+
bits -= bitsPerChar;
|
|
1961
|
+
out += alphabet[mask & buffer2 >> bits];
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
if (bits !== 0) {
|
|
1965
|
+
out += alphabet[mask & buffer2 << bitsPerChar - bits];
|
|
1966
|
+
}
|
|
1967
|
+
if (pad) {
|
|
1968
|
+
while ((out.length * bitsPerChar & 7) !== 0) {
|
|
1969
|
+
out += "=";
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
return out;
|
|
1973
|
+
}
|
|
1974
|
+
function createAlphabetIdx(alphabet) {
|
|
1975
|
+
const alphabetIdx = {};
|
|
1976
|
+
for (let i = 0;i < alphabet.length; ++i) {
|
|
1977
|
+
alphabetIdx[alphabet[i]] = i;
|
|
1978
|
+
}
|
|
1979
|
+
return alphabetIdx;
|
|
1980
|
+
}
|
|
1981
|
+
function rfc4648({ name, prefix, bitsPerChar, alphabet }) {
|
|
1982
|
+
const alphabetIdx = createAlphabetIdx(alphabet);
|
|
1983
|
+
return from({
|
|
1984
|
+
prefix,
|
|
1985
|
+
name,
|
|
1986
|
+
encode(input) {
|
|
1987
|
+
return encode2(input, alphabet, bitsPerChar);
|
|
1988
|
+
},
|
|
1989
|
+
decode(input) {
|
|
1990
|
+
return decode2(input, alphabetIdx, bitsPerChar, name);
|
|
1991
|
+
}
|
|
1992
|
+
});
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/bases/base32.js
|
|
1996
|
+
var base32 = rfc4648({
|
|
1997
|
+
prefix: "b",
|
|
1998
|
+
name: "base32",
|
|
1999
|
+
alphabet: "abcdefghijklmnopqrstuvwxyz234567",
|
|
2000
|
+
bitsPerChar: 5
|
|
2001
|
+
});
|
|
2002
|
+
var base32upper = rfc4648({
|
|
2003
|
+
prefix: "B",
|
|
2004
|
+
name: "base32upper",
|
|
2005
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
|
|
2006
|
+
bitsPerChar: 5
|
|
2007
|
+
});
|
|
2008
|
+
var base32pad = rfc4648({
|
|
2009
|
+
prefix: "c",
|
|
2010
|
+
name: "base32pad",
|
|
2011
|
+
alphabet: "abcdefghijklmnopqrstuvwxyz234567=",
|
|
2012
|
+
bitsPerChar: 5
|
|
2013
|
+
});
|
|
2014
|
+
var base32padupper = rfc4648({
|
|
2015
|
+
prefix: "C",
|
|
2016
|
+
name: "base32padupper",
|
|
2017
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",
|
|
2018
|
+
bitsPerChar: 5
|
|
2019
|
+
});
|
|
2020
|
+
var base32hex = rfc4648({
|
|
2021
|
+
prefix: "v",
|
|
2022
|
+
name: "base32hex",
|
|
2023
|
+
alphabet: "0123456789abcdefghijklmnopqrstuv",
|
|
2024
|
+
bitsPerChar: 5
|
|
2025
|
+
});
|
|
2026
|
+
var base32hexupper = rfc4648({
|
|
2027
|
+
prefix: "V",
|
|
2028
|
+
name: "base32hexupper",
|
|
2029
|
+
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV",
|
|
2030
|
+
bitsPerChar: 5
|
|
2031
|
+
});
|
|
2032
|
+
var base32hexpad = rfc4648({
|
|
2033
|
+
prefix: "t",
|
|
2034
|
+
name: "base32hexpad",
|
|
2035
|
+
alphabet: "0123456789abcdefghijklmnopqrstuv=",
|
|
2036
|
+
bitsPerChar: 5
|
|
2037
|
+
});
|
|
2038
|
+
var base32hexpadupper = rfc4648({
|
|
2039
|
+
prefix: "T",
|
|
2040
|
+
name: "base32hexpadupper",
|
|
2041
|
+
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV=",
|
|
2042
|
+
bitsPerChar: 5
|
|
2043
|
+
});
|
|
2044
|
+
var base32z = rfc4648({
|
|
2045
|
+
prefix: "h",
|
|
2046
|
+
name: "base32z",
|
|
2047
|
+
alphabet: "ybndrfg8ejkmcpqxot1uwisza345h769",
|
|
2048
|
+
bitsPerChar: 5
|
|
2049
|
+
});
|
|
2050
|
+
|
|
2051
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/bases/base36.js
|
|
2052
|
+
var base36 = baseX({
|
|
2053
|
+
prefix: "k",
|
|
2054
|
+
name: "base36",
|
|
2055
|
+
alphabet: "0123456789abcdefghijklmnopqrstuvwxyz"
|
|
2056
|
+
});
|
|
2057
|
+
var base36upper = baseX({
|
|
2058
|
+
prefix: "K",
|
|
2059
|
+
name: "base36upper",
|
|
2060
|
+
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
2061
|
+
});
|
|
2062
|
+
|
|
2063
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/bases/base58.js
|
|
2064
|
+
var base58btc = baseX({
|
|
2065
|
+
name: "base58btc",
|
|
2066
|
+
prefix: "z",
|
|
2067
|
+
alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
|
2068
|
+
});
|
|
2069
|
+
var base58flickr = baseX({
|
|
2070
|
+
name: "base58flickr",
|
|
2071
|
+
prefix: "Z",
|
|
2072
|
+
alphabet: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
|
|
2073
|
+
});
|
|
2074
|
+
|
|
2075
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/vendor/varint.js
|
|
2076
|
+
var encode_1 = encode3;
|
|
2077
|
+
var MSB = 128;
|
|
2078
|
+
var REST = 127;
|
|
2079
|
+
var MSBALL = ~REST;
|
|
2080
|
+
var INT = Math.pow(2, 31);
|
|
2081
|
+
function encode3(num, out, offset) {
|
|
2082
|
+
out = out || [];
|
|
2083
|
+
offset = offset || 0;
|
|
2084
|
+
var oldOffset = offset;
|
|
2085
|
+
while (num >= INT) {
|
|
2086
|
+
out[offset++] = num & 255 | MSB;
|
|
2087
|
+
num /= 128;
|
|
2088
|
+
}
|
|
2089
|
+
while (num & MSBALL) {
|
|
2090
|
+
out[offset++] = num & 255 | MSB;
|
|
2091
|
+
num >>>= 7;
|
|
2092
|
+
}
|
|
2093
|
+
out[offset] = num | 0;
|
|
2094
|
+
encode3.bytes = offset - oldOffset + 1;
|
|
2095
|
+
return out;
|
|
2096
|
+
}
|
|
2097
|
+
var decode3 = read;
|
|
2098
|
+
var MSB$1 = 128;
|
|
2099
|
+
var REST$1 = 127;
|
|
2100
|
+
function read(buf, offset) {
|
|
2101
|
+
var res = 0, offset = offset || 0, shift = 0, counter = offset, b, l = buf.length;
|
|
2102
|
+
do {
|
|
2103
|
+
if (counter >= l) {
|
|
2104
|
+
read.bytes = 0;
|
|
2105
|
+
throw new RangeError("Could not decode varint");
|
|
2106
|
+
}
|
|
2107
|
+
b = buf[counter++];
|
|
2108
|
+
res += shift < 28 ? (b & REST$1) << shift : (b & REST$1) * Math.pow(2, shift);
|
|
2109
|
+
shift += 7;
|
|
2110
|
+
} while (b >= MSB$1);
|
|
2111
|
+
read.bytes = counter - offset;
|
|
2112
|
+
return res;
|
|
2113
|
+
}
|
|
2114
|
+
var N1 = Math.pow(2, 7);
|
|
2115
|
+
var N2 = Math.pow(2, 14);
|
|
2116
|
+
var N3 = Math.pow(2, 21);
|
|
2117
|
+
var N4 = Math.pow(2, 28);
|
|
2118
|
+
var N5 = Math.pow(2, 35);
|
|
2119
|
+
var N6 = Math.pow(2, 42);
|
|
2120
|
+
var N7 = Math.pow(2, 49);
|
|
2121
|
+
var N8 = Math.pow(2, 56);
|
|
2122
|
+
var N9 = Math.pow(2, 63);
|
|
2123
|
+
var length = function(value) {
|
|
2124
|
+
return value < N1 ? 1 : value < N2 ? 2 : value < N3 ? 3 : value < N4 ? 4 : value < N5 ? 5 : value < N6 ? 6 : value < N7 ? 7 : value < N8 ? 8 : value < N9 ? 9 : 10;
|
|
2125
|
+
};
|
|
2126
|
+
var varint = {
|
|
2127
|
+
encode: encode_1,
|
|
2128
|
+
decode: decode3,
|
|
2129
|
+
encodingLength: length
|
|
2130
|
+
};
|
|
2131
|
+
var _brrp_varint = varint;
|
|
2132
|
+
var varint_default = _brrp_varint;
|
|
2133
|
+
|
|
2134
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/varint.js
|
|
2135
|
+
function decode4(data, offset = 0) {
|
|
2136
|
+
const code = varint_default.decode(data, offset);
|
|
2137
|
+
return [code, varint_default.decode.bytes];
|
|
2138
|
+
}
|
|
2139
|
+
function encodeTo(int, target, offset = 0) {
|
|
2140
|
+
varint_default.encode(int, target, offset);
|
|
2141
|
+
return target;
|
|
2142
|
+
}
|
|
2143
|
+
function encodingLength(int) {
|
|
2144
|
+
return varint_default.encodingLength(int);
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/hashes/digest.js
|
|
2148
|
+
function create(code, digest) {
|
|
2149
|
+
const size = digest.byteLength;
|
|
2150
|
+
const sizeOffset = encodingLength(code);
|
|
2151
|
+
const digestOffset = sizeOffset + encodingLength(size);
|
|
2152
|
+
const bytes = new Uint8Array(digestOffset + size);
|
|
2153
|
+
encodeTo(code, bytes, 0);
|
|
2154
|
+
encodeTo(size, bytes, sizeOffset);
|
|
2155
|
+
bytes.set(digest, digestOffset);
|
|
2156
|
+
return new Digest(code, size, digest, bytes);
|
|
2157
|
+
}
|
|
2158
|
+
function decode5(multihash) {
|
|
2159
|
+
const bytes = coerce(multihash);
|
|
2160
|
+
const [code, sizeOffset] = decode4(bytes);
|
|
2161
|
+
const [size, digestOffset] = decode4(bytes.subarray(sizeOffset));
|
|
2162
|
+
const digest = bytes.subarray(sizeOffset + digestOffset);
|
|
2163
|
+
if (digest.byteLength !== size) {
|
|
2164
|
+
throw new Error("Incorrect length");
|
|
2165
|
+
}
|
|
2166
|
+
return new Digest(code, size, digest, bytes);
|
|
2167
|
+
}
|
|
2168
|
+
function equals2(a, b) {
|
|
2169
|
+
if (a === b) {
|
|
2170
|
+
return true;
|
|
2171
|
+
} else {
|
|
2172
|
+
const data = b;
|
|
2173
|
+
return a.code === data.code && a.size === data.size && data.bytes instanceof Uint8Array && equals(a.bytes, data.bytes);
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
class Digest {
|
|
2178
|
+
code;
|
|
2179
|
+
size;
|
|
2180
|
+
digest;
|
|
2181
|
+
bytes;
|
|
2182
|
+
constructor(code, size, digest, bytes) {
|
|
2183
|
+
this.code = code;
|
|
2184
|
+
this.size = size;
|
|
2185
|
+
this.digest = digest;
|
|
2186
|
+
this.bytes = bytes;
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
// ../../node_modules/.bun/multiformats@13.4.2/node_modules/multiformats/dist/src/cid.js
|
|
2191
|
+
function format(link, base2) {
|
|
2192
|
+
const { bytes, version } = link;
|
|
2193
|
+
switch (version) {
|
|
2194
|
+
case 0:
|
|
2195
|
+
return toStringV0(bytes, baseCache(link), base2 ?? base58btc.encoder);
|
|
2196
|
+
default:
|
|
2197
|
+
return toStringV1(bytes, baseCache(link), base2 ?? base32.encoder);
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
var cache = new WeakMap;
|
|
2201
|
+
function baseCache(cid) {
|
|
2202
|
+
const baseCache2 = cache.get(cid);
|
|
2203
|
+
if (baseCache2 == null) {
|
|
2204
|
+
const baseCache3 = new Map;
|
|
2205
|
+
cache.set(cid, baseCache3);
|
|
2206
|
+
return baseCache3;
|
|
2207
|
+
}
|
|
2208
|
+
return baseCache2;
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
class CID {
|
|
2212
|
+
code;
|
|
2213
|
+
version;
|
|
2214
|
+
multihash;
|
|
2215
|
+
bytes;
|
|
2216
|
+
"/";
|
|
2217
|
+
constructor(version, code, multihash, bytes) {
|
|
2218
|
+
this.code = code;
|
|
2219
|
+
this.version = version;
|
|
2220
|
+
this.multihash = multihash;
|
|
2221
|
+
this.bytes = bytes;
|
|
2222
|
+
this["/"] = bytes;
|
|
2223
|
+
}
|
|
2224
|
+
get asCID() {
|
|
2225
|
+
return this;
|
|
2226
|
+
}
|
|
2227
|
+
get byteOffset() {
|
|
2228
|
+
return this.bytes.byteOffset;
|
|
2229
|
+
}
|
|
2230
|
+
get byteLength() {
|
|
2231
|
+
return this.bytes.byteLength;
|
|
2232
|
+
}
|
|
2233
|
+
toV0() {
|
|
2234
|
+
switch (this.version) {
|
|
2235
|
+
case 0: {
|
|
2236
|
+
return this;
|
|
2237
|
+
}
|
|
2238
|
+
case 1: {
|
|
2239
|
+
const { code, multihash } = this;
|
|
2240
|
+
if (code !== DAG_PB_CODE) {
|
|
2241
|
+
throw new Error("Cannot convert a non dag-pb CID to CIDv0");
|
|
2242
|
+
}
|
|
2243
|
+
if (multihash.code !== SHA_256_CODE) {
|
|
2244
|
+
throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");
|
|
2245
|
+
}
|
|
2246
|
+
return CID.createV0(multihash);
|
|
2247
|
+
}
|
|
2248
|
+
default: {
|
|
2249
|
+
throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`);
|
|
2250
|
+
}
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
toV1() {
|
|
2254
|
+
switch (this.version) {
|
|
2255
|
+
case 0: {
|
|
2256
|
+
const { code, digest } = this.multihash;
|
|
2257
|
+
const multihash = create(code, digest);
|
|
2258
|
+
return CID.createV1(this.code, multihash);
|
|
2259
|
+
}
|
|
2260
|
+
case 1: {
|
|
2261
|
+
return this;
|
|
2262
|
+
}
|
|
2263
|
+
default: {
|
|
2264
|
+
throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`);
|
|
2265
|
+
}
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
equals(other) {
|
|
2269
|
+
return CID.equals(this, other);
|
|
2270
|
+
}
|
|
2271
|
+
static equals(self, other) {
|
|
2272
|
+
const unknown = other;
|
|
2273
|
+
return unknown != null && self.code === unknown.code && self.version === unknown.version && equals2(self.multihash, unknown.multihash);
|
|
2274
|
+
}
|
|
2275
|
+
toString(base2) {
|
|
2276
|
+
return format(this, base2);
|
|
2277
|
+
}
|
|
2278
|
+
toJSON() {
|
|
2279
|
+
return { "/": format(this) };
|
|
2280
|
+
}
|
|
2281
|
+
link() {
|
|
2282
|
+
return this;
|
|
2283
|
+
}
|
|
2284
|
+
[Symbol.toStringTag] = "CID";
|
|
2285
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
2286
|
+
return `CID(${this.toString()})`;
|
|
2287
|
+
}
|
|
2288
|
+
static asCID(input) {
|
|
2289
|
+
if (input == null) {
|
|
2290
|
+
return null;
|
|
2291
|
+
}
|
|
2292
|
+
const value = input;
|
|
2293
|
+
if (value instanceof CID) {
|
|
2294
|
+
return value;
|
|
2295
|
+
} else if (value["/"] != null && value["/"] === value.bytes || value.asCID === value) {
|
|
2296
|
+
const { version, code, multihash, bytes } = value;
|
|
2297
|
+
return new CID(version, code, multihash, bytes ?? encodeCID(version, code, multihash.bytes));
|
|
2298
|
+
} else if (value[cidSymbol] === true) {
|
|
2299
|
+
const { version, multihash, code } = value;
|
|
2300
|
+
const digest = decode5(multihash);
|
|
2301
|
+
return CID.create(version, code, digest);
|
|
2302
|
+
} else {
|
|
2303
|
+
return null;
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
static create(version, code, digest) {
|
|
2307
|
+
if (typeof code !== "number") {
|
|
2308
|
+
throw new Error("String codecs are no longer supported");
|
|
2309
|
+
}
|
|
2310
|
+
if (!(digest.bytes instanceof Uint8Array)) {
|
|
2311
|
+
throw new Error("Invalid digest");
|
|
2312
|
+
}
|
|
2313
|
+
switch (version) {
|
|
2314
|
+
case 0: {
|
|
2315
|
+
if (code !== DAG_PB_CODE) {
|
|
2316
|
+
throw new Error(`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`);
|
|
2317
|
+
} else {
|
|
2318
|
+
return new CID(version, code, digest, digest.bytes);
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
case 1: {
|
|
2322
|
+
const bytes = encodeCID(version, code, digest.bytes);
|
|
2323
|
+
return new CID(version, code, digest, bytes);
|
|
2324
|
+
}
|
|
2325
|
+
default: {
|
|
2326
|
+
throw new Error("Invalid version");
|
|
2327
|
+
}
|
|
2328
|
+
}
|
|
2329
|
+
}
|
|
2330
|
+
static createV0(digest) {
|
|
2331
|
+
return CID.create(0, DAG_PB_CODE, digest);
|
|
2332
|
+
}
|
|
2333
|
+
static createV1(code, digest) {
|
|
2334
|
+
return CID.create(1, code, digest);
|
|
2335
|
+
}
|
|
2336
|
+
static decode(bytes) {
|
|
2337
|
+
const [cid, remainder] = CID.decodeFirst(bytes);
|
|
2338
|
+
if (remainder.length !== 0) {
|
|
2339
|
+
throw new Error("Incorrect length");
|
|
2340
|
+
}
|
|
2341
|
+
return cid;
|
|
2342
|
+
}
|
|
2343
|
+
static decodeFirst(bytes) {
|
|
2344
|
+
const specs = CID.inspectBytes(bytes);
|
|
2345
|
+
const prefixSize = specs.size - specs.multihashSize;
|
|
2346
|
+
const multihashBytes = coerce(bytes.subarray(prefixSize, prefixSize + specs.multihashSize));
|
|
2347
|
+
if (multihashBytes.byteLength !== specs.multihashSize) {
|
|
2348
|
+
throw new Error("Incorrect length");
|
|
2349
|
+
}
|
|
2350
|
+
const digestBytes = multihashBytes.subarray(specs.multihashSize - specs.digestSize);
|
|
2351
|
+
const digest = new Digest(specs.multihashCode, specs.digestSize, digestBytes, multihashBytes);
|
|
2352
|
+
const cid = specs.version === 0 ? CID.createV0(digest) : CID.createV1(specs.codec, digest);
|
|
2353
|
+
return [cid, bytes.subarray(specs.size)];
|
|
2354
|
+
}
|
|
2355
|
+
static inspectBytes(initialBytes) {
|
|
2356
|
+
let offset = 0;
|
|
2357
|
+
const next = () => {
|
|
2358
|
+
const [i, length2] = decode4(initialBytes.subarray(offset));
|
|
2359
|
+
offset += length2;
|
|
2360
|
+
return i;
|
|
2361
|
+
};
|
|
2362
|
+
let version = next();
|
|
2363
|
+
let codec = DAG_PB_CODE;
|
|
2364
|
+
if (version === 18) {
|
|
2365
|
+
version = 0;
|
|
2366
|
+
offset = 0;
|
|
2367
|
+
} else {
|
|
2368
|
+
codec = next();
|
|
2369
|
+
}
|
|
2370
|
+
if (version !== 0 && version !== 1) {
|
|
2371
|
+
throw new RangeError(`Invalid CID version ${version}`);
|
|
2372
|
+
}
|
|
2373
|
+
const prefixSize = offset;
|
|
2374
|
+
const multihashCode = next();
|
|
2375
|
+
const digestSize = next();
|
|
2376
|
+
const size = offset + digestSize;
|
|
2377
|
+
const multihashSize = size - prefixSize;
|
|
2378
|
+
return { version, codec, multihashCode, digestSize, multihashSize, size };
|
|
2379
|
+
}
|
|
2380
|
+
static parse(source, base2) {
|
|
2381
|
+
const [prefix, bytes] = parseCIDtoBytes(source, base2);
|
|
2382
|
+
const cid = CID.decode(bytes);
|
|
2383
|
+
if (cid.version === 0 && source[0] !== "Q") {
|
|
2384
|
+
throw Error("Version 0 CID string must not include multibase prefix");
|
|
2385
|
+
}
|
|
2386
|
+
baseCache(cid).set(prefix, source);
|
|
2387
|
+
return cid;
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
function parseCIDtoBytes(source, base2) {
|
|
2391
|
+
switch (source[0]) {
|
|
2392
|
+
case "Q": {
|
|
2393
|
+
const decoder = base2 ?? base58btc;
|
|
2394
|
+
return [
|
|
2395
|
+
base58btc.prefix,
|
|
2396
|
+
decoder.decode(`${base58btc.prefix}${source}`)
|
|
2397
|
+
];
|
|
2398
|
+
}
|
|
2399
|
+
case base58btc.prefix: {
|
|
2400
|
+
const decoder = base2 ?? base58btc;
|
|
2401
|
+
return [base58btc.prefix, decoder.decode(source)];
|
|
2402
|
+
}
|
|
2403
|
+
case base32.prefix: {
|
|
2404
|
+
const decoder = base2 ?? base32;
|
|
2405
|
+
return [base32.prefix, decoder.decode(source)];
|
|
2406
|
+
}
|
|
2407
|
+
case base36.prefix: {
|
|
2408
|
+
const decoder = base2 ?? base36;
|
|
2409
|
+
return [base36.prefix, decoder.decode(source)];
|
|
2410
|
+
}
|
|
2411
|
+
default: {
|
|
2412
|
+
if (base2 == null) {
|
|
2413
|
+
throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");
|
|
2414
|
+
}
|
|
2415
|
+
return [source[0], base2.decode(source)];
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
function toStringV0(bytes, cache2, base2) {
|
|
2420
|
+
const { prefix } = base2;
|
|
2421
|
+
if (prefix !== base58btc.prefix) {
|
|
2422
|
+
throw Error(`Cannot string encode V0 in ${base2.name} encoding`);
|
|
2423
|
+
}
|
|
2424
|
+
const cid = cache2.get(prefix);
|
|
2425
|
+
if (cid == null) {
|
|
2426
|
+
const cid2 = base2.encode(bytes).slice(1);
|
|
2427
|
+
cache2.set(prefix, cid2);
|
|
2428
|
+
return cid2;
|
|
2429
|
+
} else {
|
|
2430
|
+
return cid;
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
function toStringV1(bytes, cache2, base2) {
|
|
2434
|
+
const { prefix } = base2;
|
|
2435
|
+
const cid = cache2.get(prefix);
|
|
2436
|
+
if (cid == null) {
|
|
2437
|
+
const cid2 = base2.encode(bytes);
|
|
2438
|
+
cache2.set(prefix, cid2);
|
|
2439
|
+
return cid2;
|
|
2440
|
+
} else {
|
|
2441
|
+
return cid;
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
var DAG_PB_CODE = 112;
|
|
2445
|
+
var SHA_256_CODE = 18;
|
|
2446
|
+
function encodeCID(version, code, multihash) {
|
|
2447
|
+
const codeOffset = encodingLength(version);
|
|
2448
|
+
const hashOffset = codeOffset + encodingLength(code);
|
|
2449
|
+
const bytes = new Uint8Array(hashOffset + multihash.byteLength);
|
|
2450
|
+
encodeTo(version, bytes, 0);
|
|
2451
|
+
encodeTo(code, bytes, codeOffset);
|
|
2452
|
+
bytes.set(multihash, hashOffset);
|
|
2453
|
+
return bytes;
|
|
2454
|
+
}
|
|
2455
|
+
var cidSymbol = Symbol.for("@ipld/js-cid/CID");
|
|
2456
|
+
|
|
2457
|
+
// ../../node_modules/.bun/@ipld+dag-cbor@9.2.6/node_modules/@ipld/dag-cbor/src/index.js
|
|
2458
|
+
var CID_CBOR_TAG = 42;
|
|
2459
|
+
function toByteView(buf) {
|
|
2460
|
+
if (buf instanceof ArrayBuffer) {
|
|
2461
|
+
return new Uint8Array(buf, 0, buf.byteLength);
|
|
2462
|
+
}
|
|
2463
|
+
return buf;
|
|
2464
|
+
}
|
|
2465
|
+
function cidEncoder(obj) {
|
|
2466
|
+
if (obj.asCID !== obj && obj["/"] !== obj.bytes) {
|
|
2467
|
+
return null;
|
|
2468
|
+
}
|
|
2469
|
+
const cid = CID.asCID(obj);
|
|
2470
|
+
if (!cid) {
|
|
2471
|
+
return null;
|
|
2472
|
+
}
|
|
2473
|
+
const bytes = new Uint8Array(cid.bytes.byteLength + 1);
|
|
2474
|
+
bytes.set(cid.bytes, 1);
|
|
2475
|
+
return [
|
|
2476
|
+
new Token(Type.tag, CID_CBOR_TAG),
|
|
2477
|
+
new Token(Type.bytes, bytes)
|
|
2478
|
+
];
|
|
2479
|
+
}
|
|
2480
|
+
function undefinedEncoder() {
|
|
2481
|
+
throw new Error("`undefined` is not supported by the IPLD Data Model and cannot be encoded");
|
|
2482
|
+
}
|
|
2483
|
+
function numberEncoder(num) {
|
|
2484
|
+
if (Number.isNaN(num)) {
|
|
2485
|
+
throw new Error("`NaN` is not supported by the IPLD Data Model and cannot be encoded");
|
|
2486
|
+
}
|
|
2487
|
+
if (num === Infinity || num === -Infinity) {
|
|
2488
|
+
throw new Error("`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded");
|
|
2489
|
+
}
|
|
2490
|
+
return null;
|
|
2491
|
+
}
|
|
2492
|
+
function mapEncoder(map) {
|
|
2493
|
+
for (const key of map.keys()) {
|
|
2494
|
+
if (typeof key !== "string" || key.length === 0) {
|
|
2495
|
+
throw new Error("Non-string Map keys are not supported by the IPLD Data Model and cannot be encoded");
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
return null;
|
|
2499
|
+
}
|
|
2500
|
+
var _encodeOptions = {
|
|
2501
|
+
float64: true,
|
|
2502
|
+
typeEncoders: {
|
|
2503
|
+
Map: mapEncoder,
|
|
2504
|
+
Object: cidEncoder,
|
|
2505
|
+
undefined: undefinedEncoder,
|
|
2506
|
+
number: numberEncoder
|
|
2507
|
+
}
|
|
2508
|
+
};
|
|
2509
|
+
var encodeOptions = {
|
|
2510
|
+
..._encodeOptions,
|
|
2511
|
+
typeEncoders: {
|
|
2512
|
+
..._encodeOptions.typeEncoders
|
|
2513
|
+
}
|
|
2514
|
+
};
|
|
2515
|
+
function cidDecoder(decode6) {
|
|
2516
|
+
const bytes = decode6();
|
|
2517
|
+
if (bytes[0] !== 0) {
|
|
2518
|
+
throw new Error("Invalid CID for CBOR tag 42; expected leading 0x00");
|
|
2519
|
+
}
|
|
2520
|
+
return CID.decode(bytes.subarray(1));
|
|
2521
|
+
}
|
|
2522
|
+
var _decodeOptions = {
|
|
2523
|
+
allowIndefinite: false,
|
|
2524
|
+
coerceUndefinedToNull: true,
|
|
2525
|
+
allowNaN: false,
|
|
2526
|
+
allowInfinity: false,
|
|
2527
|
+
allowBigInt: true,
|
|
2528
|
+
strict: true,
|
|
2529
|
+
useMaps: false,
|
|
2530
|
+
rejectDuplicateMapKeys: true,
|
|
2531
|
+
tags: { [CID_CBOR_TAG]: cidDecoder }
|
|
2532
|
+
};
|
|
2533
|
+
var decodeOptions = {
|
|
2534
|
+
..._decodeOptions,
|
|
2535
|
+
tags: { ..._decodeOptions.tags }
|
|
2536
|
+
};
|
|
2537
|
+
var encode4 = (node) => encode(node, _encodeOptions);
|
|
2538
|
+
var decode6 = (data) => decode(toByteView(data), _decodeOptions);
|
|
2539
|
+
|
|
2540
|
+
// src/policy.ts
|
|
2541
|
+
function parsePolicy(token) {
|
|
2542
|
+
const segments = token.split(".");
|
|
2543
|
+
if (segments.length < 2) {
|
|
2544
|
+
throw new Error("Invalid policy token: expected at least 2 segments");
|
|
2545
|
+
}
|
|
2546
|
+
const payload = decodePayload(segments[1]);
|
|
2547
|
+
const allowedOrigins = extractOrigins(payload);
|
|
2548
|
+
const capabilities = extractCapabilities(payload);
|
|
2549
|
+
return { token, allowedOrigins, capabilities };
|
|
2550
|
+
}
|
|
2551
|
+
function assertOriginAllowed(policy, origin) {
|
|
2552
|
+
if (policy.allowedOrigins.length === 0)
|
|
2553
|
+
return;
|
|
2554
|
+
const ok = policy.allowedOrigins.some((allowed) => matchesOrigin(allowed, origin));
|
|
2555
|
+
if (!ok) {
|
|
2556
|
+
throw new Error(`Origin "${origin}" is not authorized by the project policy. ` + `Allowed origins: ${policy.allowedOrigins.join(", ")}`);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
function matchesOrigin(allowed, origin) {
|
|
2560
|
+
if (allowed === "*" || allowed === origin)
|
|
2561
|
+
return true;
|
|
2562
|
+
if (allowed.startsWith("*.")) {
|
|
2563
|
+
const suffix = allowed.slice(1);
|
|
2564
|
+
try {
|
|
2565
|
+
const host = new URL(origin).host;
|
|
2566
|
+
return host.endsWith(suffix.slice(1));
|
|
2567
|
+
} catch {
|
|
2568
|
+
return false;
|
|
2569
|
+
}
|
|
2570
|
+
}
|
|
2571
|
+
return false;
|
|
2572
|
+
}
|
|
2573
|
+
function decodePayload(seg) {
|
|
2574
|
+
try {
|
|
2575
|
+
const padded = seg.padEnd(seg.length + (4 - seg.length % 4) % 4, "=");
|
|
2576
|
+
const normalized = padded.replace(/-/g, "+").replace(/_/g, "/");
|
|
2577
|
+
const decoded = atob(normalized);
|
|
2578
|
+
return JSON.parse(decoded);
|
|
2579
|
+
} catch {
|
|
2580
|
+
return {};
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
function extractOrigins(payload) {
|
|
2584
|
+
const origins = payload.origins ?? payload.aud ?? payload.prf;
|
|
2585
|
+
if (Array.isArray(origins))
|
|
2586
|
+
return origins.filter((o) => typeof o === "string");
|
|
2587
|
+
if (typeof origins === "string")
|
|
2588
|
+
return [origins];
|
|
2589
|
+
return [];
|
|
2590
|
+
}
|
|
2591
|
+
function extractCapabilities(payload) {
|
|
2592
|
+
const att = payload.att;
|
|
2593
|
+
if (!Array.isArray(att))
|
|
2594
|
+
return [];
|
|
2595
|
+
return att.map((entry) => {
|
|
2596
|
+
if (typeof entry === "string")
|
|
2597
|
+
return entry;
|
|
2598
|
+
if (entry && typeof entry === "object" && "cmd" in entry) {
|
|
2599
|
+
return String(entry.cmd);
|
|
2600
|
+
}
|
|
2601
|
+
return null;
|
|
2602
|
+
}).filter((c) => c !== null);
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
class PolicyTokenParseError extends Error {
|
|
2606
|
+
constructor(message) {
|
|
2607
|
+
super(message);
|
|
2608
|
+
this.name = "PolicyTokenParseError";
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
function parsePolicyToken(raw) {
|
|
2612
|
+
const trimmed = raw.trim();
|
|
2613
|
+
const segments = trimmed.split(".");
|
|
2614
|
+
if (segments.length !== 3) {
|
|
2615
|
+
throw new PolicyTokenParseError(`expected JWT-style token with 3 segments, got ${segments.length}`);
|
|
2616
|
+
}
|
|
2617
|
+
const [headerSeg, payloadSeg, envelopeSeg] = segments;
|
|
2618
|
+
const header = parseJsonSegment(headerSeg, "header");
|
|
2619
|
+
if (typeof header.alg !== "string" || typeof header.typ !== "string") {
|
|
2620
|
+
throw new PolicyTokenParseError("header missing alg/typ");
|
|
2621
|
+
}
|
|
2622
|
+
const payload = parseJsonSegment(payloadSeg, "payload");
|
|
2623
|
+
if (typeof payload.iss !== "string") {
|
|
2624
|
+
throw new PolicyTokenParseError("payload missing iss");
|
|
2625
|
+
}
|
|
2626
|
+
const envelopeBytes = base64UrlDecode(envelopeSeg);
|
|
2627
|
+
let envelope;
|
|
2628
|
+
try {
|
|
2629
|
+
envelope = decode6(envelopeBytes);
|
|
2630
|
+
} catch (err) {
|
|
2631
|
+
throw new PolicyTokenParseError(`envelope is not valid DAG-CBOR: ${err.message}`);
|
|
2632
|
+
}
|
|
2633
|
+
if (!Array.isArray(envelope) || envelope.length !== 2) {
|
|
2634
|
+
throw new PolicyTokenParseError("envelope must be a 2-item list");
|
|
2635
|
+
}
|
|
2636
|
+
const [signature, sigPayload] = envelope;
|
|
2637
|
+
if (!(signature instanceof Uint8Array)) {
|
|
2638
|
+
throw new PolicyTokenParseError("envelope[0] must be byte string");
|
|
2639
|
+
}
|
|
2640
|
+
const signedBytes = encode4(sigPayload);
|
|
2641
|
+
const publicKeyHex = pubKeyHexFromDIDKey(payload.iss);
|
|
2642
|
+
return {
|
|
2643
|
+
raw: trimmed,
|
|
2644
|
+
header: { alg: header.alg, typ: header.typ },
|
|
2645
|
+
payload,
|
|
2646
|
+
signature,
|
|
2647
|
+
signedBytes,
|
|
2648
|
+
publicKeyHex
|
|
2649
|
+
};
|
|
2650
|
+
}
|
|
2651
|
+
function getPolicyFacts(payload) {
|
|
2652
|
+
const first = payload.fct?.[0];
|
|
2653
|
+
const facts = first ? first["hyperauth/policy"] : undefined;
|
|
2654
|
+
if (!facts)
|
|
2655
|
+
return null;
|
|
2656
|
+
const origins = Array.isArray(facts.origins) ? facts.origins.filter((o) => typeof o === "string") : [];
|
|
2657
|
+
return {
|
|
2658
|
+
origins,
|
|
2659
|
+
chainId: typeof facts.chain_id === "number" ? facts.chain_id : 0,
|
|
2660
|
+
paymaster: typeof facts.paymaster === "string" ? facts.paymaster : "",
|
|
2661
|
+
maxFeePerUserUsd: typeof facts.max_fee_per_user_usd === "number" ? facts.max_fee_per_user_usd : 0
|
|
2662
|
+
};
|
|
2663
|
+
}
|
|
2664
|
+
function isPolicyExpired(payload, nowSeconds = Date.now() / 1000) {
|
|
2665
|
+
return nowSeconds >= payload.exp;
|
|
2666
|
+
}
|
|
2667
|
+
function isOriginAllowedByFacts(facts, origin) {
|
|
2668
|
+
if (facts.origins.length === 0)
|
|
2669
|
+
return true;
|
|
2670
|
+
return facts.origins.some((allowed) => matchesOrigin(allowed, origin));
|
|
2671
|
+
}
|
|
2672
|
+
function pubKeyHexFromDIDKey(did) {
|
|
2673
|
+
if (!did.startsWith("did:key:z")) {
|
|
2674
|
+
throw new PolicyTokenParseError(`unsupported DID format: ${did}`);
|
|
2675
|
+
}
|
|
2676
|
+
const decoded = base58btcDecode(did.slice("did:key:z".length));
|
|
2677
|
+
if (decoded.length < 3 || decoded[0] !== 231 || decoded[1] !== 1) {
|
|
2678
|
+
throw new PolicyTokenParseError("issuer is not a secp256k1-pub did:key");
|
|
2679
|
+
}
|
|
2680
|
+
const pub = decoded.slice(2);
|
|
2681
|
+
return [...pub].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
2682
|
+
}
|
|
2683
|
+
var BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
2684
|
+
function base58btcDecode(input) {
|
|
2685
|
+
const map = new Map;
|
|
2686
|
+
for (let i = 0;i < BASE58_ALPHABET.length; i++)
|
|
2687
|
+
map.set(BASE58_ALPHABET[i], i);
|
|
2688
|
+
const bytes = [];
|
|
2689
|
+
for (const ch of input) {
|
|
2690
|
+
const value = map.get(ch);
|
|
2691
|
+
if (value === undefined) {
|
|
2692
|
+
throw new PolicyTokenParseError(`invalid base58 character: ${ch}`);
|
|
2693
|
+
}
|
|
2694
|
+
let carry = value;
|
|
2695
|
+
for (let j = 0;j < bytes.length; j++) {
|
|
2696
|
+
carry += (bytes[j] ?? 0) * 58;
|
|
2697
|
+
bytes[j] = carry & 255;
|
|
2698
|
+
carry >>= 8;
|
|
2699
|
+
}
|
|
2700
|
+
while (carry > 0) {
|
|
2701
|
+
bytes.push(carry & 255);
|
|
2702
|
+
carry >>= 8;
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
for (let i = 0;i < input.length && input[i] === "1"; i++)
|
|
2706
|
+
bytes.push(0);
|
|
2707
|
+
return new Uint8Array(bytes.reverse());
|
|
2708
|
+
}
|
|
2709
|
+
function parseJsonSegment(seg, label) {
|
|
2710
|
+
let text;
|
|
2711
|
+
try {
|
|
2712
|
+
text = new TextDecoder().decode(base64UrlDecode(seg));
|
|
2713
|
+
} catch (err) {
|
|
2714
|
+
throw new PolicyTokenParseError(`${label} segment is not base64url: ${err.message}`);
|
|
2715
|
+
}
|
|
2716
|
+
try {
|
|
2717
|
+
return JSON.parse(text);
|
|
2718
|
+
} catch (err) {
|
|
2719
|
+
throw new PolicyTokenParseError(`${label} segment is not valid JSON: ${err.message}`);
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
function base64UrlDecode(input) {
|
|
2723
|
+
const padded = input.padEnd(input.length + (4 - input.length % 4) % 4, "=");
|
|
2724
|
+
const normalized = padded.replace(/-/g, "+").replace(/_/g, "/");
|
|
2725
|
+
const binary = atob(normalized);
|
|
2726
|
+
const out = new Uint8Array(binary.length);
|
|
2727
|
+
for (let i = 0;i < binary.length; i++)
|
|
2728
|
+
out[i] = binary.charCodeAt(i);
|
|
2729
|
+
return out;
|
|
2730
|
+
}
|
|
2731
|
+
|
|
2732
|
+
// src/iframe-bridge.ts
|
|
2733
|
+
var nextId = 1;
|
|
2734
|
+
async function mountIframeBridge(opts) {
|
|
2735
|
+
if (!isBrowser()) {
|
|
2736
|
+
throw new Error("mountIframeBridge can only be called in the browser");
|
|
2737
|
+
}
|
|
2738
|
+
const { origin, path = "/embed", readyTimeoutMs = 1e4 } = opts;
|
|
2739
|
+
const url = new URL(path, origin).toString();
|
|
2740
|
+
const iframe = document.createElement("iframe");
|
|
2741
|
+
iframe.src = url;
|
|
2742
|
+
iframe.style.cssText = "position:absolute;width:0;height:0;border:0;visibility:hidden;pointer-events:none";
|
|
2743
|
+
iframe.setAttribute("aria-hidden", "true");
|
|
2744
|
+
iframe.setAttribute("title", "HyperAuth Enclave Host");
|
|
2745
|
+
document.body.appendChild(iframe);
|
|
2746
|
+
const pending = new Map;
|
|
2747
|
+
let ready = false;
|
|
2748
|
+
const onMessage = (event) => {
|
|
2749
|
+
if (event.origin !== origin)
|
|
2750
|
+
return;
|
|
2751
|
+
const data = event.data;
|
|
2752
|
+
if (!data || typeof data !== "object")
|
|
2753
|
+
return;
|
|
2754
|
+
if (data.type === "hyperauth:ready") {
|
|
2755
|
+
ready = true;
|
|
2756
|
+
return;
|
|
2757
|
+
}
|
|
2758
|
+
if (data.type === "hyperauth:reply" && typeof data.id === "number") {
|
|
2759
|
+
const handler = pending.get(data.id);
|
|
2760
|
+
if (!handler)
|
|
2761
|
+
return;
|
|
2762
|
+
pending.delete(data.id);
|
|
2763
|
+
if (data.error)
|
|
2764
|
+
handler.reject(new Error(data.error));
|
|
2765
|
+
else
|
|
2766
|
+
handler.resolve(data.result);
|
|
2767
|
+
}
|
|
2768
|
+
};
|
|
2769
|
+
window.addEventListener("message", onMessage);
|
|
2770
|
+
await new Promise((resolve, reject) => {
|
|
2771
|
+
const start = Date.now();
|
|
2772
|
+
const tick = () => {
|
|
2773
|
+
if (ready)
|
|
2774
|
+
return resolve();
|
|
2775
|
+
if (Date.now() - start > readyTimeoutMs) {
|
|
2776
|
+
return reject(new Error(`Iframe enclave at ${origin} did not signal ready within ${readyTimeoutMs}ms`));
|
|
2777
|
+
}
|
|
2778
|
+
setTimeout(tick, 50);
|
|
2779
|
+
};
|
|
2780
|
+
tick();
|
|
2781
|
+
});
|
|
2782
|
+
const bridge = {
|
|
2783
|
+
iframe,
|
|
2784
|
+
isReady: () => ready,
|
|
2785
|
+
call(method, payload, timeoutMs = 30000) {
|
|
2786
|
+
const id = nextId++;
|
|
2787
|
+
const target = iframe.contentWindow;
|
|
2788
|
+
if (!target) {
|
|
2789
|
+
return Promise.reject(new Error("Iframe contentWindow is unavailable"));
|
|
2790
|
+
}
|
|
2791
|
+
return new Promise((resolve, reject) => {
|
|
2792
|
+
const timeout = setTimeout(() => {
|
|
2793
|
+
pending.delete(id);
|
|
2794
|
+
reject(new Error(`Iframe call "${method}" timed out after ${timeoutMs}ms`));
|
|
2795
|
+
}, timeoutMs);
|
|
2796
|
+
pending.set(id, {
|
|
2797
|
+
resolve: (v) => {
|
|
2798
|
+
clearTimeout(timeout);
|
|
2799
|
+
resolve(v);
|
|
2800
|
+
},
|
|
2801
|
+
reject: (e) => {
|
|
2802
|
+
clearTimeout(timeout);
|
|
2803
|
+
reject(e);
|
|
2804
|
+
}
|
|
2805
|
+
});
|
|
2806
|
+
target.postMessage({ type: "hyperauth:call", id, method, payload }, origin);
|
|
2807
|
+
});
|
|
2808
|
+
},
|
|
2809
|
+
destroy() {
|
|
2810
|
+
window.removeEventListener("message", onMessage);
|
|
2811
|
+
pending.forEach((h) => h.reject(new Error("Iframe bridge destroyed")));
|
|
2812
|
+
pending.clear();
|
|
2813
|
+
if (iframe.parentNode)
|
|
2814
|
+
iframe.parentNode.removeChild(iframe);
|
|
2815
|
+
bridge.iframe = null;
|
|
2816
|
+
ready = false;
|
|
2817
|
+
}
|
|
2818
|
+
};
|
|
2819
|
+
return bridge;
|
|
2820
|
+
}
|
|
2821
|
+
|
|
2822
|
+
// src/provider.ts
|
|
2823
|
+
var DEFAULT_MAX_RESTARTS = 3;
|
|
2824
|
+
function createHyperAuth(options = {}) {
|
|
2825
|
+
const {
|
|
2826
|
+
projectId,
|
|
2827
|
+
chain = "base",
|
|
2828
|
+
config,
|
|
2829
|
+
enclaveOrigin,
|
|
2830
|
+
maxRestartAttempts = DEFAULT_MAX_RESTARTS,
|
|
2831
|
+
autoRestart = true
|
|
2832
|
+
} = options;
|
|
2833
|
+
return {
|
|
2834
|
+
install(app) {
|
|
2835
|
+
const store = createStore({ chain });
|
|
2836
|
+
const ctx = reactive({
|
|
2837
|
+
client: null,
|
|
2838
|
+
status: "initializing",
|
|
2839
|
+
error: null,
|
|
2840
|
+
isReady: false,
|
|
2841
|
+
user: null,
|
|
2842
|
+
address: null,
|
|
2843
|
+
authStatus: "unauthenticated",
|
|
2844
|
+
session: null,
|
|
2845
|
+
provider: "unknown",
|
|
2846
|
+
policy: null,
|
|
2847
|
+
chain,
|
|
2848
|
+
store
|
|
2849
|
+
});
|
|
2850
|
+
function syncFromStore() {
|
|
2851
|
+
const s = store.getState();
|
|
2852
|
+
ctx.client = s.client;
|
|
2853
|
+
ctx.status = s.status;
|
|
2854
|
+
ctx.error = s.error;
|
|
2855
|
+
ctx.isReady = s.status === "ready" && s.client !== null;
|
|
2856
|
+
ctx.user = s.user;
|
|
2857
|
+
ctx.address = s.user?.smartAccountAddress ? s.user.smartAccountAddress : null;
|
|
2858
|
+
ctx.authStatus = s.authStatus;
|
|
2859
|
+
ctx.session = s.session;
|
|
2860
|
+
ctx.provider = s.provider;
|
|
2861
|
+
ctx.policy = s.policy;
|
|
2862
|
+
ctx.chain = s.chain;
|
|
2863
|
+
}
|
|
2864
|
+
const unsubscribe = store.subscribe(syncFromStore);
|
|
2865
|
+
syncFromStore();
|
|
2866
|
+
app.provide(HyperAuthKey, ctx);
|
|
2867
|
+
let iframe = null;
|
|
2868
|
+
let instance;
|
|
2869
|
+
let disposed = false;
|
|
2870
|
+
async function bootClient(attempt) {
|
|
2871
|
+
try {
|
|
2872
|
+
store.setState({
|
|
2873
|
+
status: attempt === 0 ? "initializing" : "restarting",
|
|
2874
|
+
error: null
|
|
2875
|
+
});
|
|
2876
|
+
if (projectId) {
|
|
2877
|
+
const policy = parsePolicy(projectId);
|
|
2878
|
+
const origin2 = getOrigin();
|
|
2879
|
+
if (origin2)
|
|
2880
|
+
assertOriginAllowed(policy, origin2);
|
|
2881
|
+
store.setState({ policy });
|
|
2882
|
+
}
|
|
2883
|
+
const origin = getOrigin();
|
|
2884
|
+
if (enclaveOrigin && origin && enclaveOrigin !== origin && !iframe) {
|
|
2885
|
+
iframe = await mountIframeBridge({ origin: enclaveOrigin });
|
|
2886
|
+
}
|
|
2887
|
+
instance = await createClient(config);
|
|
2888
|
+
if (disposed) {
|
|
2889
|
+
await instance.close();
|
|
2890
|
+
return;
|
|
2891
|
+
}
|
|
2892
|
+
attachWorkerCrashHandler(() => {
|
|
2893
|
+
if (!autoRestart || disposed)
|
|
2894
|
+
return;
|
|
2895
|
+
const attempts = store.getState().restartAttempts;
|
|
2896
|
+
if (attempts >= maxRestartAttempts) {
|
|
2897
|
+
store.setState({
|
|
2898
|
+
status: "error",
|
|
2899
|
+
error: new Error(`Worker restart attempts exhausted (${attempts})`)
|
|
2900
|
+
});
|
|
2901
|
+
return;
|
|
2902
|
+
}
|
|
2903
|
+
store.setState({ restartAttempts: attempts + 1 });
|
|
2904
|
+
bootClient(attempts + 1);
|
|
2905
|
+
});
|
|
2906
|
+
store.setState({ client: instance, status: "ready" });
|
|
2907
|
+
} catch (err) {
|
|
2908
|
+
if (disposed)
|
|
2909
|
+
return;
|
|
2910
|
+
store.setState({
|
|
2911
|
+
status: "error",
|
|
2912
|
+
error: err instanceof Error ? err : new Error(String(err))
|
|
2913
|
+
});
|
|
2914
|
+
}
|
|
2915
|
+
}
|
|
2916
|
+
if (isBrowser()) {
|
|
2917
|
+
bootClient(0);
|
|
2918
|
+
}
|
|
2919
|
+
app.onUnmount(() => {
|
|
2920
|
+
disposed = true;
|
|
2921
|
+
unsubscribe();
|
|
2922
|
+
instance?.close();
|
|
2923
|
+
iframe?.destroy();
|
|
2924
|
+
iframe = null;
|
|
2925
|
+
});
|
|
2926
|
+
}
|
|
2927
|
+
};
|
|
2928
|
+
}
|
|
2929
|
+
function attachWorkerCrashHandler(onCrash) {
|
|
2930
|
+
if (!isBrowser())
|
|
2931
|
+
return;
|
|
2932
|
+
const handler = (event) => {
|
|
2933
|
+
const err = event;
|
|
2934
|
+
const msg = err?.message ?? "";
|
|
2935
|
+
if (msg.includes("worker") || msg.includes("Worker") || msg.includes("enclave")) {
|
|
2936
|
+
onCrash();
|
|
2937
|
+
}
|
|
2938
|
+
};
|
|
2939
|
+
window.addEventListener("error", handler, { once: true });
|
|
2940
|
+
}
|
|
2941
|
+
// src/use-hyperauth.ts
|
|
2942
|
+
import { inject } from "vue";
|
|
2943
|
+
import { ClientStateError } from "@hyperauth/client";
|
|
2944
|
+
function useHyperAuth() {
|
|
2945
|
+
const ctx = inject(HyperAuthKey);
|
|
2946
|
+
if (!ctx) {
|
|
2947
|
+
throw new ClientStateError("useHyperAuth must be called under a component tree that has installed `createHyperAuth()`");
|
|
2948
|
+
}
|
|
2949
|
+
return ctx;
|
|
2950
|
+
}
|
|
2951
|
+
// src/use-login.ts
|
|
2952
|
+
import { computed } from "vue";
|
|
2953
|
+
import { HyperAuthError, ClientStateError as ClientStateError2 } from "@hyperauth/client";
|
|
2954
|
+
|
|
2955
|
+
// src/use-store.ts
|
|
2956
|
+
import { onScopeDispose, readonly, shallowRef } from "vue";
|
|
2957
|
+
function useStoreSelector(store, selector = (s) => s) {
|
|
2958
|
+
const valueRef = shallowRef(selector(store.getState()));
|
|
2959
|
+
const unsubscribe = store.subscribe(() => {
|
|
2960
|
+
valueRef.value = selector(store.getState());
|
|
2961
|
+
});
|
|
2962
|
+
onScopeDispose(() => {
|
|
2963
|
+
unsubscribe();
|
|
2964
|
+
});
|
|
2965
|
+
return readonly(valueRef);
|
|
2966
|
+
}
|
|
2967
|
+
|
|
2968
|
+
// src/use-login.ts
|
|
2969
|
+
var selectAuthStatus = (s) => s.authStatus;
|
|
2970
|
+
function useLogin() {
|
|
2971
|
+
const ctx = useHyperAuth();
|
|
2972
|
+
const { store } = ctx;
|
|
2973
|
+
const authStatus = useStoreSelector(store, selectAuthStatus);
|
|
2974
|
+
async function login(opts = {}) {
|
|
2975
|
+
const client = ctx.client;
|
|
2976
|
+
if (!client)
|
|
2977
|
+
throw new ClientStateError2("Client not ready");
|
|
2978
|
+
if (!hasWebAuthn())
|
|
2979
|
+
throw new HyperAuthError("WebAuthn unavailable in this environment");
|
|
2980
|
+
store.setState({ authStatus: "authenticating", error: null });
|
|
2981
|
+
try {
|
|
2982
|
+
const sdk = await import("@hyperauth/client");
|
|
2983
|
+
const credentialId = await sdk.authenticatePasskey(opts.rpId);
|
|
2984
|
+
const result = await client.generate(credentialId, opts.alias ? { identifier: opts.alias } : undefined);
|
|
2985
|
+
const publicKeyHex = result.shares.public_key_hex ?? (typeof result.shares.public_key === "string" ? result.shares.public_key : "");
|
|
2986
|
+
const derived = publicKeyHex ? await client.deriveAddress(publicKeyHex, "ethereum") : null;
|
|
2987
|
+
const user = {
|
|
2988
|
+
did: result.did,
|
|
2989
|
+
alias: opts.alias,
|
|
2990
|
+
enclaveId: result.shares.enclave_id,
|
|
2991
|
+
publicKeyHex,
|
|
2992
|
+
pubkeyX: result.shares.pubkey_x ?? "",
|
|
2993
|
+
pubkeyY: result.shares.pubkey_y ?? "",
|
|
2994
|
+
ethAddress: result.address,
|
|
2995
|
+
smartAccountAddress: derived?.address ?? "",
|
|
2996
|
+
credentialId,
|
|
2997
|
+
shares: result.shares
|
|
2998
|
+
};
|
|
2999
|
+
store.setState({ user, authStatus: "authenticated", provider: "webauthn" });
|
|
3000
|
+
return user;
|
|
3001
|
+
} catch (err) {
|
|
3002
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3003
|
+
store.setState({ authStatus: "error", error });
|
|
3004
|
+
throw error;
|
|
3005
|
+
}
|
|
3006
|
+
}
|
|
3007
|
+
async function logout() {
|
|
3008
|
+
const client = ctx.client;
|
|
3009
|
+
if (client) {
|
|
3010
|
+
try {
|
|
3011
|
+
await client.lock();
|
|
3012
|
+
} catch {}
|
|
3013
|
+
}
|
|
3014
|
+
store.setState({
|
|
3015
|
+
user: null,
|
|
3016
|
+
session: null,
|
|
3017
|
+
authStatus: "unauthenticated",
|
|
3018
|
+
provider: "unknown"
|
|
3019
|
+
});
|
|
3020
|
+
}
|
|
3021
|
+
return {
|
|
3022
|
+
login,
|
|
3023
|
+
logout,
|
|
3024
|
+
isAuthenticating: computed(() => authStatus.value === "authenticating")
|
|
3025
|
+
};
|
|
3026
|
+
}
|
|
3027
|
+
// src/use-session.ts
|
|
3028
|
+
import { computed as computed2 } from "vue";
|
|
3029
|
+
import { ClientStateError as ClientStateError3, HyperAuthError as HyperAuthError2 } from "@hyperauth/client";
|
|
3030
|
+
var DEFAULT_TTL = 60 * 60;
|
|
3031
|
+
var DEFAULT_COMMANDS = ["account/sign"];
|
|
3032
|
+
var selectSession = (s) => s.session;
|
|
3033
|
+
function useSession() {
|
|
3034
|
+
const ctx = useHyperAuth();
|
|
3035
|
+
const { store } = ctx;
|
|
3036
|
+
const sessionRef = useStoreSelector(store, selectSession);
|
|
3037
|
+
const session = computed2(() => sessionRef.value);
|
|
3038
|
+
async function create2(opts = {}) {
|
|
3039
|
+
const client = ctx.client;
|
|
3040
|
+
const user = ctx.user;
|
|
3041
|
+
if (!client)
|
|
3042
|
+
throw new ClientStateError3("Client not ready");
|
|
3043
|
+
if (!user?.shares)
|
|
3044
|
+
throw new HyperAuthError2("User must be authenticated before creating a session");
|
|
3045
|
+
const audience = opts.audience ?? `did:web:${(getOrigin() ?? "").replace(/^https?:\/\//, "")}`;
|
|
3046
|
+
const commands = opts.commands ?? DEFAULT_COMMANDS;
|
|
3047
|
+
const ttl = opts.ttlSeconds ?? DEFAULT_TTL;
|
|
3048
|
+
const expiresAt = Math.floor(Date.now() / 1000) + ttl;
|
|
3049
|
+
const ucanResult = await client.mintUcan(user.shares, {
|
|
3050
|
+
audience,
|
|
3051
|
+
commands,
|
|
3052
|
+
expiration: expiresAt,
|
|
3053
|
+
spend_cap_wei: opts.spendCapWei?.toString(),
|
|
3054
|
+
tx_cap: opts.txCap
|
|
3055
|
+
});
|
|
3056
|
+
const token = encodeUcanToken(ucanResult.signed_dag_cbor);
|
|
3057
|
+
const next = {
|
|
3058
|
+
token,
|
|
3059
|
+
audience,
|
|
3060
|
+
commands,
|
|
3061
|
+
expiresAt,
|
|
3062
|
+
spendCapWei: opts.spendCapWei,
|
|
3063
|
+
txCap: opts.txCap,
|
|
3064
|
+
txCount: 0
|
|
3065
|
+
};
|
|
3066
|
+
store.setState({ session: next, provider: "session-key" });
|
|
3067
|
+
return next;
|
|
3068
|
+
}
|
|
3069
|
+
function revoke() {
|
|
3070
|
+
store.setState((s) => ({
|
|
3071
|
+
session: null,
|
|
3072
|
+
provider: s.user ? "webauthn" : "unknown"
|
|
3073
|
+
}));
|
|
3074
|
+
}
|
|
3075
|
+
function recordTx() {
|
|
3076
|
+
store.setState((s) => {
|
|
3077
|
+
if (!s.session)
|
|
3078
|
+
return {};
|
|
3079
|
+
return { session: { ...s.session, txCount: s.session.txCount + 1 } };
|
|
3080
|
+
});
|
|
3081
|
+
}
|
|
3082
|
+
const now = () => Date.now();
|
|
3083
|
+
return {
|
|
3084
|
+
session,
|
|
3085
|
+
isExpired: computed2(() => session.value ? session.value.expiresAt * 1000 <= now() : false),
|
|
3086
|
+
remainingMs: computed2(() => session.value ? Math.max(0, session.value.expiresAt * 1000 - now()) : 0),
|
|
3087
|
+
create: create2,
|
|
3088
|
+
revoke,
|
|
3089
|
+
recordTx
|
|
3090
|
+
};
|
|
3091
|
+
}
|
|
3092
|
+
function encodeUcanToken(b64std) {
|
|
3093
|
+
return b64std.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
3094
|
+
}
|
|
3095
|
+
// src/use-account.ts
|
|
3096
|
+
import { computed as computed3, watch } from "vue";
|
|
3097
|
+
var selectAccount = (s) => ({
|
|
3098
|
+
address: s.user?.smartAccountAddress ?? null,
|
|
3099
|
+
state: s.accountState,
|
|
3100
|
+
user: s.user
|
|
3101
|
+
});
|
|
3102
|
+
function useAccount() {
|
|
3103
|
+
const { store } = useHyperAuth();
|
|
3104
|
+
const snap = useStoreSelector(store, selectAccount);
|
|
3105
|
+
async function refresh() {
|
|
3106
|
+
const addr = snap.value.address;
|
|
3107
|
+
if (!addr)
|
|
3108
|
+
return;
|
|
3109
|
+
const sdk = await import("@hyperauth/client");
|
|
3110
|
+
const accountState = await sdk.getAccountState({ account: addr });
|
|
3111
|
+
store.setState({ accountState });
|
|
3112
|
+
}
|
|
3113
|
+
watch(() => snap.value.address, (addr) => {
|
|
3114
|
+
if (addr && !snap.value.state) {
|
|
3115
|
+
refresh();
|
|
3116
|
+
}
|
|
3117
|
+
}, { immediate: true });
|
|
3118
|
+
return {
|
|
3119
|
+
address: computed3(() => snap.value.address ? snap.value.address : null),
|
|
3120
|
+
ethAddress: computed3(() => snap.value.user?.ethAddress ? snap.value.user.ethAddress : null),
|
|
3121
|
+
state: computed3(() => snap.value.state),
|
|
3122
|
+
isDeployed: computed3(() => snap.value.state?.isActive ?? false),
|
|
3123
|
+
refresh
|
|
3124
|
+
};
|
|
3125
|
+
}
|
|
3126
|
+
// src/use-transaction.ts
|
|
3127
|
+
import { computed as computed4 } from "vue";
|
|
3128
|
+
import { ClientStateError as ClientStateError4, HyperAuthError as HyperAuthError3 } from "@hyperauth/client";
|
|
3129
|
+
|
|
3130
|
+
// src/chains.ts
|
|
3131
|
+
var chainIds = {
|
|
3132
|
+
base: 8453,
|
|
3133
|
+
"base-sepolia": 84532,
|
|
3134
|
+
monad: 41454,
|
|
3135
|
+
ethereum: 1
|
|
3136
|
+
};
|
|
3137
|
+
var explorerUrls = {
|
|
3138
|
+
base: "https://basescan.org",
|
|
3139
|
+
"base-sepolia": "https://sepolia.basescan.org",
|
|
3140
|
+
monad: "https://explorer.monad.xyz",
|
|
3141
|
+
ethereum: "https://etherscan.io"
|
|
3142
|
+
};
|
|
3143
|
+
function txExplorerUrl(chain, hash) {
|
|
3144
|
+
return `${explorerUrls[chain]}/tx/${hash}`;
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
// src/use-transaction.ts
|
|
3148
|
+
var DEFAULT_CALL_GAS = "0x30d40";
|
|
3149
|
+
var DEFAULT_VERIFICATION_GAS = "0xf4240";
|
|
3150
|
+
var DEFAULT_PRE_VERIFICATION_GAS = "0x186a0";
|
|
3151
|
+
var DEFAULT_MAX_FEE_PER_GAS = "0x59682f00";
|
|
3152
|
+
var DEFAULT_MAX_PRIORITY_FEE_PER_GAS = "0x59682f00";
|
|
3153
|
+
var selectTx = (s) => s.pendingTransaction;
|
|
3154
|
+
function useTransaction() {
|
|
3155
|
+
const ctx = useHyperAuth();
|
|
3156
|
+
const { store } = ctx;
|
|
3157
|
+
const tx = useStoreSelector(store, selectTx);
|
|
3158
|
+
async function buildUserOp(intent) {
|
|
3159
|
+
const client = ctx.client;
|
|
3160
|
+
const user = ctx.user;
|
|
3161
|
+
if (!client)
|
|
3162
|
+
throw new ClientStateError4("Client not ready");
|
|
3163
|
+
if (!user?.smartAccountAddress)
|
|
3164
|
+
throw new HyperAuthError3("User has no smart account address");
|
|
3165
|
+
const sdk = await import("@hyperauth/client");
|
|
3166
|
+
const viem = await import("viem");
|
|
3167
|
+
const callData = intent.rawCallData ?? viem.encodeFunctionData({
|
|
3168
|
+
abi: sdk.hyperAuthAccountAbi,
|
|
3169
|
+
functionName: "execute",
|
|
3170
|
+
args: [intent.to, intent.value ?? 0n, intent.data ?? "0x"]
|
|
3171
|
+
});
|
|
3172
|
+
const accountStateResult = await sdk.getAccountState({ account: user.smartAccountAddress });
|
|
3173
|
+
const isActive = accountStateResult.isActive;
|
|
3174
|
+
let factory = null;
|
|
3175
|
+
let factoryData = null;
|
|
3176
|
+
if (!isActive && user.pubkeyX && user.pubkeyY) {
|
|
3177
|
+
factory = sdk.defaultContracts.hyperAuthFactory;
|
|
3178
|
+
factoryData = viem.encodeFunctionData({
|
|
3179
|
+
abi: sdk.hyperAuthFactoryAbi,
|
|
3180
|
+
functionName: "createAccount",
|
|
3181
|
+
args: [BigInt(`0x${user.pubkeyX}`), BigInt(`0x${user.pubkeyY}`), 0n]
|
|
3182
|
+
});
|
|
3183
|
+
}
|
|
3184
|
+
return {
|
|
3185
|
+
sender: user.smartAccountAddress,
|
|
3186
|
+
nonce: accountStateResult.nonce || "0x0",
|
|
3187
|
+
factory,
|
|
3188
|
+
factoryData,
|
|
3189
|
+
callData,
|
|
3190
|
+
callGasLimit: DEFAULT_CALL_GAS,
|
|
3191
|
+
verificationGasLimit: DEFAULT_VERIFICATION_GAS,
|
|
3192
|
+
preVerificationGas: DEFAULT_PRE_VERIFICATION_GAS,
|
|
3193
|
+
maxFeePerGas: DEFAULT_MAX_FEE_PER_GAS,
|
|
3194
|
+
maxPriorityFeePerGas: DEFAULT_MAX_PRIORITY_FEE_PER_GAS,
|
|
3195
|
+
paymaster: null,
|
|
3196
|
+
paymasterVerificationGasLimit: null,
|
|
3197
|
+
paymasterPostOpGasLimit: null,
|
|
3198
|
+
paymasterData: null,
|
|
3199
|
+
signature: "0x"
|
|
3200
|
+
};
|
|
3201
|
+
}
|
|
3202
|
+
async function signAndSend(intent) {
|
|
3203
|
+
const client = ctx.client;
|
|
3204
|
+
const user = ctx.user;
|
|
3205
|
+
const session = ctx.session;
|
|
3206
|
+
const chain = ctx.chain;
|
|
3207
|
+
const policy = ctx.policy;
|
|
3208
|
+
if (!client)
|
|
3209
|
+
throw new ClientStateError4("Client not ready");
|
|
3210
|
+
if (!user?.shares)
|
|
3211
|
+
throw new HyperAuthError3("User must be authenticated");
|
|
3212
|
+
try {
|
|
3213
|
+
store.setState({
|
|
3214
|
+
pendingTransaction: { phase: "building", error: null, result: null, userOp: null }
|
|
3215
|
+
});
|
|
3216
|
+
const sdk = await import("@hyperauth/client");
|
|
3217
|
+
const userOp = await buildUserOp(intent);
|
|
3218
|
+
store.setState({
|
|
3219
|
+
pendingTransaction: { phase: "sponsoring", error: null, result: null, userOp }
|
|
3220
|
+
});
|
|
3221
|
+
const wantsSponsorship = isSponsoredAction(policy, intent);
|
|
3222
|
+
let opToSign = userOp;
|
|
3223
|
+
let sponsored = false;
|
|
3224
|
+
if (wantsSponsorship) {
|
|
3225
|
+
try {
|
|
3226
|
+
opToSign = await sdk.sponsorUserOp(userOp, sdk.defaultContracts.entryPoint);
|
|
3227
|
+
sponsored = true;
|
|
3228
|
+
} catch {}
|
|
3229
|
+
}
|
|
3230
|
+
store.setState({
|
|
3231
|
+
pendingTransaction: { phase: "signing", error: null, result: null, userOp: opToSign }
|
|
3232
|
+
});
|
|
3233
|
+
const chainId = chainIds[chain];
|
|
3234
|
+
const opHash = await sdk.computeUserOpHash(opToSign, sdk.defaultContracts.entryPoint, chainId);
|
|
3235
|
+
let signature;
|
|
3236
|
+
if (session && session.expiresAt * 1000 > Date.now()) {
|
|
3237
|
+
const signed = await client.signUserOp(opToSign, session.token);
|
|
3238
|
+
signature = signed.signature;
|
|
3239
|
+
} else {
|
|
3240
|
+
const signed = await client.sign(user.shares, Array.from(sdk.hexToBytes(opHash)));
|
|
3241
|
+
signature = sdk.bytesToHex(new Uint8Array(signed.signature));
|
|
3242
|
+
}
|
|
3243
|
+
const signedOp = { ...opToSign, signature };
|
|
3244
|
+
store.setState({
|
|
3245
|
+
pendingTransaction: { phase: "submitting", error: null, result: null, userOp: signedOp }
|
|
3246
|
+
});
|
|
3247
|
+
const userOpHash = await sdk.sendUserOp(signedOp, sdk.defaultContracts.entryPoint);
|
|
3248
|
+
store.setState({
|
|
3249
|
+
pendingTransaction: { phase: "confirming", error: null, result: null, userOp: signedOp }
|
|
3250
|
+
});
|
|
3251
|
+
const receipt = await sdk.waitForReceipt(userOpHash);
|
|
3252
|
+
const txHash = receipt.receipt.transactionHash;
|
|
3253
|
+
const blockNumber = parseBlockNumber(receipt.receipt.blockNumber);
|
|
3254
|
+
const result = { userOpHash, txHash, blockNumber, sponsored };
|
|
3255
|
+
store.setState({
|
|
3256
|
+
pendingTransaction: { phase: "confirmed", error: null, result, userOp: signedOp }
|
|
3257
|
+
});
|
|
3258
|
+
if (session) {
|
|
3259
|
+
store.setState((s) => s.session ? { session: { ...s.session, txCount: s.session.txCount + 1 } } : {});
|
|
3260
|
+
}
|
|
3261
|
+
return result;
|
|
3262
|
+
} catch (err) {
|
|
3263
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
3264
|
+
store.setState((s) => ({
|
|
3265
|
+
pendingTransaction: { ...s.pendingTransaction, phase: "error", error: msg }
|
|
3266
|
+
}));
|
|
3267
|
+
throw err;
|
|
3268
|
+
}
|
|
3269
|
+
}
|
|
3270
|
+
function reset() {
|
|
3271
|
+
store.setState({
|
|
3272
|
+
pendingTransaction: { phase: "idle", error: null, result: null, userOp: null }
|
|
3273
|
+
});
|
|
3274
|
+
}
|
|
3275
|
+
return {
|
|
3276
|
+
phase: computed4(() => tx.value.phase),
|
|
3277
|
+
error: computed4(() => tx.value.error),
|
|
3278
|
+
result: computed4(() => tx.value.result),
|
|
3279
|
+
userOp: computed4(() => tx.value.userOp),
|
|
3280
|
+
signAndSend,
|
|
3281
|
+
buildUserOp,
|
|
3282
|
+
reset
|
|
3283
|
+
};
|
|
3284
|
+
}
|
|
3285
|
+
function isSponsoredAction(policy, intent) {
|
|
3286
|
+
if (!policy)
|
|
3287
|
+
return false;
|
|
3288
|
+
if (policy.capabilities.includes("paymaster/sponsor:*"))
|
|
3289
|
+
return true;
|
|
3290
|
+
return policy.capabilities.includes("paymaster/sponsor");
|
|
3291
|
+
}
|
|
3292
|
+
function parseBlockNumber(value) {
|
|
3293
|
+
if (typeof value === "string")
|
|
3294
|
+
return parseInt(value, 16);
|
|
3295
|
+
if (typeof value === "number")
|
|
3296
|
+
return value;
|
|
3297
|
+
return null;
|
|
3298
|
+
}
|
|
3299
|
+
// src/use-settlement.ts
|
|
3300
|
+
import { computed as computed5 } from "vue";
|
|
3301
|
+
import { ClientStateError as ClientStateError5, HyperAuthError as HyperAuthError4 } from "@hyperauth/client";
|
|
3302
|
+
var PHASES = [
|
|
3303
|
+
{ phase: "capturing-fiat", label: "Capturing fiat (Apple Pay)" },
|
|
3304
|
+
{ phase: "auditing", label: "TigerBeetle audit" },
|
|
3305
|
+
{ phase: "minting", label: "CCTP mint" },
|
|
3306
|
+
{ phase: "finalizing", label: "On-chain finality" }
|
|
3307
|
+
];
|
|
3308
|
+
var selectSettlement = (s) => s.settlement;
|
|
3309
|
+
function useSettlement() {
|
|
3310
|
+
const ctx = useHyperAuth();
|
|
3311
|
+
const { store } = ctx;
|
|
3312
|
+
const state = useStoreSelector(store, selectSettlement);
|
|
3313
|
+
async function settle(params) {
|
|
3314
|
+
const client = ctx.client;
|
|
3315
|
+
const user = ctx.user;
|
|
3316
|
+
const session = ctx.session;
|
|
3317
|
+
const chain = ctx.chain;
|
|
3318
|
+
if (!client)
|
|
3319
|
+
throw new ClientStateError5("Client not ready");
|
|
3320
|
+
if (!user?.smartAccountAddress)
|
|
3321
|
+
throw new HyperAuthError4("User has no smart account");
|
|
3322
|
+
const initialSteps = PHASES.map(({ phase, label }) => ({
|
|
3323
|
+
phase,
|
|
3324
|
+
label,
|
|
3325
|
+
status: "pending"
|
|
3326
|
+
}));
|
|
3327
|
+
store.setState({
|
|
3328
|
+
settlement: {
|
|
3329
|
+
phase: "capturing-fiat",
|
|
3330
|
+
steps: initialSteps,
|
|
3331
|
+
params,
|
|
3332
|
+
txHash: null,
|
|
3333
|
+
error: null
|
|
3334
|
+
}
|
|
3335
|
+
});
|
|
3336
|
+
try {
|
|
3337
|
+
const sdk = await import("@hyperauth/client");
|
|
3338
|
+
markActive(store, "capturing-fiat");
|
|
3339
|
+
const recipient = params.recipient ?? user.smartAccountAddress;
|
|
3340
|
+
const ucan = params.sessionToken ?? session?.token;
|
|
3341
|
+
if (!ucan)
|
|
3342
|
+
throw new HyperAuthError4("Settlement requires an active session UCAN");
|
|
3343
|
+
markDone(store, "capturing-fiat");
|
|
3344
|
+
markActive(store, "auditing");
|
|
3345
|
+
const result = await client.submitPayment({
|
|
3346
|
+
to: recipient,
|
|
3347
|
+
amount: String(params.amount),
|
|
3348
|
+
token: params.currency,
|
|
3349
|
+
chainId: chainIdFromName(chain),
|
|
3350
|
+
ucan,
|
|
3351
|
+
label: `Settlement: ${params.amount} ${params.currency}`
|
|
3352
|
+
});
|
|
3353
|
+
markDone(store, "auditing");
|
|
3354
|
+
markActive(store, "minting");
|
|
3355
|
+
markDone(store, "minting");
|
|
3356
|
+
markActive(store, "finalizing");
|
|
3357
|
+
const receipt = await sdk.waitForReceipt(result.userOpHash);
|
|
3358
|
+
const txHash = receipt.receipt.transactionHash;
|
|
3359
|
+
markDone(store, "finalizing");
|
|
3360
|
+
store.setState({
|
|
3361
|
+
settlement: {
|
|
3362
|
+
phase: "complete",
|
|
3363
|
+
steps: store.getState().settlement.steps,
|
|
3364
|
+
params,
|
|
3365
|
+
txHash,
|
|
3366
|
+
error: null
|
|
3367
|
+
}
|
|
3368
|
+
});
|
|
3369
|
+
params.onSuccess?.(txHash);
|
|
3370
|
+
return txHash;
|
|
3371
|
+
} catch (err) {
|
|
3372
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
3373
|
+
store.setState((s) => ({
|
|
3374
|
+
settlement: {
|
|
3375
|
+
...s.settlement,
|
|
3376
|
+
phase: "error",
|
|
3377
|
+
error: error.message,
|
|
3378
|
+
steps: s.settlement.steps.map((step) => step.status === "active" ? { ...step, status: "error", detail: error.message } : step)
|
|
3379
|
+
}
|
|
3380
|
+
}));
|
|
3381
|
+
params.onError?.(error);
|
|
3382
|
+
throw error;
|
|
3383
|
+
}
|
|
3384
|
+
}
|
|
3385
|
+
function reset() {
|
|
3386
|
+
store.setState({
|
|
3387
|
+
settlement: { phase: "idle", steps: [], params: null, txHash: null, error: null }
|
|
3388
|
+
});
|
|
3389
|
+
}
|
|
3390
|
+
return {
|
|
3391
|
+
phase: computed5(() => state.value.phase),
|
|
3392
|
+
steps: computed5(() => state.value.steps),
|
|
3393
|
+
params: computed5(() => state.value.params),
|
|
3394
|
+
txHash: computed5(() => state.value.txHash),
|
|
3395
|
+
error: computed5(() => state.value.error),
|
|
3396
|
+
settle,
|
|
3397
|
+
reset
|
|
3398
|
+
};
|
|
3399
|
+
}
|
|
3400
|
+
function markActive(store, phase) {
|
|
3401
|
+
store.setState((s) => ({
|
|
3402
|
+
settlement: {
|
|
3403
|
+
...s.settlement,
|
|
3404
|
+
phase,
|
|
3405
|
+
steps: s.settlement.steps.map((step) => step.phase === phase ? { ...step, status: "active" } : step)
|
|
3406
|
+
}
|
|
3407
|
+
}));
|
|
3408
|
+
}
|
|
3409
|
+
function markDone(store, phase) {
|
|
3410
|
+
store.setState((s) => ({
|
|
3411
|
+
settlement: {
|
|
3412
|
+
...s.settlement,
|
|
3413
|
+
steps: s.settlement.steps.map((step) => step.phase === phase ? { ...step, status: "done" } : step)
|
|
3414
|
+
}
|
|
3415
|
+
}));
|
|
3416
|
+
}
|
|
3417
|
+
function chainIdFromName(chain) {
|
|
3418
|
+
switch (chain) {
|
|
3419
|
+
case "base":
|
|
3420
|
+
return 8453;
|
|
3421
|
+
case "base-sepolia":
|
|
3422
|
+
return 84532;
|
|
3423
|
+
case "monad":
|
|
3424
|
+
return 41454;
|
|
3425
|
+
case "ethereum":
|
|
3426
|
+
return 1;
|
|
3427
|
+
}
|
|
3428
|
+
}
|
|
3429
|
+
// src/use-registration.ts
|
|
3430
|
+
import { computed as computed6, ref } from "vue";
|
|
3431
|
+
import { HyperAuthError as HyperAuthError5, ClientStateError as ClientStateError6 } from "@hyperauth/client";
|
|
3432
|
+
var DEFAULT_CALL_GAS2 = "0x30d40";
|
|
3433
|
+
var DEFAULT_VERIFICATION_GAS2 = "0xf4240";
|
|
3434
|
+
var DEFAULT_PRE_VERIFICATION_GAS2 = "0x186a0";
|
|
3435
|
+
var DEFAULT_MAX_FEE_PER_GAS2 = "0x59682f00";
|
|
3436
|
+
var DEFAULT_MAX_PRIORITY_FEE_PER_GAS2 = "0x59682f00";
|
|
3437
|
+
var STEP_LABELS = [
|
|
3438
|
+
"Checking availability",
|
|
3439
|
+
"Creating passkey",
|
|
3440
|
+
"Generating identity",
|
|
3441
|
+
"Predicting account",
|
|
3442
|
+
"Checking account state",
|
|
3443
|
+
"Preparing registration",
|
|
3444
|
+
"Covering fees",
|
|
3445
|
+
"Granting permissions",
|
|
3446
|
+
"Signing securely",
|
|
3447
|
+
"Submitting registration",
|
|
3448
|
+
"Waiting for confirmation",
|
|
3449
|
+
"Saving session"
|
|
3450
|
+
];
|
|
3451
|
+
async function importSdk() {
|
|
3452
|
+
return import("@hyperauth/client");
|
|
3453
|
+
}
|
|
3454
|
+
async function importViem() {
|
|
3455
|
+
return import("viem");
|
|
3456
|
+
}
|
|
3457
|
+
function useRegistration() {
|
|
3458
|
+
const ctx = useHyperAuth();
|
|
3459
|
+
const phase = ref("idle");
|
|
3460
|
+
const steps = ref([]);
|
|
3461
|
+
const identity = ref(null);
|
|
3462
|
+
const result = ref(null);
|
|
3463
|
+
const error = ref(null);
|
|
3464
|
+
const isRegistering = ref(false);
|
|
3465
|
+
function updateStep(index, status, detail) {
|
|
3466
|
+
steps.value = steps.value.map((step, i) => i === index ? { ...step, status, detail } : step);
|
|
3467
|
+
}
|
|
3468
|
+
function reset() {
|
|
3469
|
+
phase.value = "idle";
|
|
3470
|
+
steps.value = [];
|
|
3471
|
+
identity.value = null;
|
|
3472
|
+
result.value = null;
|
|
3473
|
+
error.value = null;
|
|
3474
|
+
isRegistering.value = false;
|
|
3475
|
+
}
|
|
3476
|
+
async function register(alias) {
|
|
3477
|
+
const client = ctx.client;
|
|
3478
|
+
if (!client) {
|
|
3479
|
+
throw new ClientStateError6("Client not ready");
|
|
3480
|
+
}
|
|
3481
|
+
if (isRegistering.value) {
|
|
3482
|
+
return;
|
|
3483
|
+
}
|
|
3484
|
+
phase.value = "checking-alias";
|
|
3485
|
+
error.value = null;
|
|
3486
|
+
result.value = null;
|
|
3487
|
+
identity.value = null;
|
|
3488
|
+
isRegistering.value = true;
|
|
3489
|
+
steps.value = STEP_LABELS.map((label) => ({ label, status: "pending" }));
|
|
3490
|
+
async function stepCheckAlias(sdk, rawAlias) {
|
|
3491
|
+
updateStep(0, "active");
|
|
3492
|
+
const normalized = rawAlias.trim().toLowerCase();
|
|
3493
|
+
const aliasHash = await sdk.computeAliasHash(normalized);
|
|
3494
|
+
const aliasResult = await sdk.lookupAlias(aliasHash);
|
|
3495
|
+
if (aliasResult.found) {
|
|
3496
|
+
throw new HyperAuthError5(`"${normalized}" is already taken`);
|
|
3497
|
+
}
|
|
3498
|
+
updateStep(0, "done", "Available");
|
|
3499
|
+
return normalized;
|
|
3500
|
+
}
|
|
3501
|
+
async function stepCreatePasskey(sdk, normalized) {
|
|
3502
|
+
updateStep(1, "active");
|
|
3503
|
+
const created = await sdk.createPasskey(normalized);
|
|
3504
|
+
updateStep(1, "done", "Created");
|
|
3505
|
+
return created;
|
|
3506
|
+
}
|
|
3507
|
+
async function stepGenerateIdentity(credential, normalized) {
|
|
3508
|
+
updateStep(2, "active");
|
|
3509
|
+
const genResult = await client.generate(credential, { identifier: normalized });
|
|
3510
|
+
const { did, address: ethAddress, shares } = genResult;
|
|
3511
|
+
const publicKey = typeof shares.public_key === "string" ? shares.public_key : "";
|
|
3512
|
+
const publicKeyHex = shares.public_key_hex ?? publicKey;
|
|
3513
|
+
const pubkeyX = shares.pubkey_x ?? "";
|
|
3514
|
+
const pubkeyY = shares.pubkey_y ?? "";
|
|
3515
|
+
updateStep(2, "done", "Generated");
|
|
3516
|
+
return {
|
|
3517
|
+
did,
|
|
3518
|
+
enclaveId: shares.enclave_id,
|
|
3519
|
+
publicKey,
|
|
3520
|
+
shares,
|
|
3521
|
+
publicKeyHex,
|
|
3522
|
+
pubkeyX,
|
|
3523
|
+
pubkeyY,
|
|
3524
|
+
ethAddress
|
|
3525
|
+
};
|
|
3526
|
+
}
|
|
3527
|
+
async function stepPredictAccount(publicKeyHex) {
|
|
3528
|
+
updateStep(3, "active");
|
|
3529
|
+
let smartAccountAddress = "";
|
|
3530
|
+
if (publicKeyHex) {
|
|
3531
|
+
const derived = await client.deriveAddress(publicKeyHex, "ethereum");
|
|
3532
|
+
smartAccountAddress = derived.address;
|
|
3533
|
+
}
|
|
3534
|
+
updateStep(3, "done", smartAccountAddress ? "Predicted" : "Skipped");
|
|
3535
|
+
return smartAccountAddress;
|
|
3536
|
+
}
|
|
3537
|
+
async function stepFetchAccountState(sdk, smartAccountAddress) {
|
|
3538
|
+
updateStep(4, "active");
|
|
3539
|
+
let accountNonce = "0x0";
|
|
3540
|
+
let accountIsActive = false;
|
|
3541
|
+
if (smartAccountAddress) {
|
|
3542
|
+
const state = await sdk.getAccountState({ account: smartAccountAddress });
|
|
3543
|
+
accountNonce = state.nonce || "0x0";
|
|
3544
|
+
accountIsActive = state.isActive;
|
|
3545
|
+
}
|
|
3546
|
+
updateStep(4, "done", "Ready");
|
|
3547
|
+
return { accountNonce, accountIsActive };
|
|
3548
|
+
}
|
|
3549
|
+
async function stepPrepareUserOp(sdk, viem, params) {
|
|
3550
|
+
updateStep(5, "active");
|
|
3551
|
+
const {
|
|
3552
|
+
smartAccountAddress,
|
|
3553
|
+
pubkeyX,
|
|
3554
|
+
pubkeyY,
|
|
3555
|
+
did,
|
|
3556
|
+
normalized,
|
|
3557
|
+
accountNonce,
|
|
3558
|
+
accountIsActive
|
|
3559
|
+
} = params;
|
|
3560
|
+
if (!smartAccountAddress) {
|
|
3561
|
+
throw new HyperAuthError5("Unable to derive smart account address");
|
|
3562
|
+
}
|
|
3563
|
+
if (!pubkeyX || !pubkeyY) {
|
|
3564
|
+
throw new HyperAuthError5("Missing public key coordinates for account creation");
|
|
3565
|
+
}
|
|
3566
|
+
const { encodeFunctionData, keccak256, toHex } = viem;
|
|
3567
|
+
const {
|
|
3568
|
+
computeAliasHash,
|
|
3569
|
+
defaultContracts,
|
|
3570
|
+
didRegistryAbi,
|
|
3571
|
+
hyperAuthAccountAbi,
|
|
3572
|
+
hyperAuthFactoryAbi
|
|
3573
|
+
} = sdk;
|
|
3574
|
+
const aliasDigest = await computeAliasHash(normalized);
|
|
3575
|
+
const didHash = keccak256(toHex(did));
|
|
3576
|
+
const metadataCid = "";
|
|
3577
|
+
const registerCall = encodeFunctionData({
|
|
3578
|
+
abi: didRegistryAbi,
|
|
3579
|
+
functionName: "register",
|
|
3580
|
+
args: [didHash, aliasDigest, metadataCid]
|
|
3581
|
+
});
|
|
3582
|
+
const callData = encodeFunctionData({
|
|
3583
|
+
abi: hyperAuthAccountAbi,
|
|
3584
|
+
functionName: "execute",
|
|
3585
|
+
args: [defaultContracts.didRegistry, 0n, registerCall]
|
|
3586
|
+
});
|
|
3587
|
+
const factoryData = encodeFunctionData({
|
|
3588
|
+
abi: hyperAuthFactoryAbi,
|
|
3589
|
+
functionName: "createAccount",
|
|
3590
|
+
args: [BigInt(`0x${pubkeyX}`), BigInt(`0x${pubkeyY}`), 0n]
|
|
3591
|
+
});
|
|
3592
|
+
const userOp = {
|
|
3593
|
+
sender: smartAccountAddress,
|
|
3594
|
+
nonce: accountNonce,
|
|
3595
|
+
factory: accountIsActive ? null : defaultContracts.hyperAuthFactory,
|
|
3596
|
+
factoryData: accountIsActive ? null : factoryData,
|
|
3597
|
+
callData,
|
|
3598
|
+
callGasLimit: DEFAULT_CALL_GAS2,
|
|
3599
|
+
verificationGasLimit: DEFAULT_VERIFICATION_GAS2,
|
|
3600
|
+
preVerificationGas: DEFAULT_PRE_VERIFICATION_GAS2,
|
|
3601
|
+
maxFeePerGas: DEFAULT_MAX_FEE_PER_GAS2,
|
|
3602
|
+
maxPriorityFeePerGas: DEFAULT_MAX_PRIORITY_FEE_PER_GAS2,
|
|
3603
|
+
paymaster: null,
|
|
3604
|
+
paymasterVerificationGasLimit: null,
|
|
3605
|
+
paymasterPostOpGasLimit: null,
|
|
3606
|
+
paymasterData: null,
|
|
3607
|
+
signature: "0x"
|
|
3608
|
+
};
|
|
3609
|
+
updateStep(5, "done", "Prepared");
|
|
3610
|
+
return userOp;
|
|
3611
|
+
}
|
|
3612
|
+
async function stepSponsorUserOp(sdk, userOp, entryPoint) {
|
|
3613
|
+
updateStep(6, "active");
|
|
3614
|
+
const sponsoredOp = await sdk.sponsorUserOp(userOp, entryPoint);
|
|
3615
|
+
updateStep(6, "done", "Fees covered");
|
|
3616
|
+
return sponsoredOp;
|
|
3617
|
+
}
|
|
3618
|
+
function stepAuthorize(did) {
|
|
3619
|
+
updateStep(7, "active");
|
|
3620
|
+
updateStep(7, "done", "Authorized");
|
|
3621
|
+
return did;
|
|
3622
|
+
}
|
|
3623
|
+
async function stepSignUserOp(sdk, params) {
|
|
3624
|
+
updateStep(8, "active");
|
|
3625
|
+
const { shares, sponsoredOp, entryPoint, chainId } = params;
|
|
3626
|
+
if (!shares) {
|
|
3627
|
+
throw new HyperAuthError5("Missing encrypted shares from generate() result");
|
|
3628
|
+
}
|
|
3629
|
+
const hashToSign = await sdk.computeUserOpHash(sponsoredOp, entryPoint, chainId);
|
|
3630
|
+
const signResult = await client.sign(shares, Array.from(sdk.hexToBytes(hashToSign)));
|
|
3631
|
+
const signedOp = {
|
|
3632
|
+
...sponsoredOp,
|
|
3633
|
+
signature: sdk.bytesToHex(new Uint8Array(signResult.signature))
|
|
3634
|
+
};
|
|
3635
|
+
updateStep(8, "done", "Signed");
|
|
3636
|
+
return signedOp;
|
|
3637
|
+
}
|
|
3638
|
+
async function stepSubmitUserOp(sdk, signedOp, entryPoint) {
|
|
3639
|
+
updateStep(9, "active");
|
|
3640
|
+
const userOpHash = await sdk.sendUserOp(signedOp, entryPoint);
|
|
3641
|
+
updateStep(9, "done", "Submitted");
|
|
3642
|
+
return userOpHash;
|
|
3643
|
+
}
|
|
3644
|
+
async function stepWaitForConfirmation(sdk, userOpHash) {
|
|
3645
|
+
updateStep(10, "active");
|
|
3646
|
+
const receipt = await sdk.waitForReceipt(userOpHash);
|
|
3647
|
+
const txHash = receipt.receipt.transactionHash;
|
|
3648
|
+
const blockNum = typeof receipt.receipt.blockNumber === "string" ? parseInt(receipt.receipt.blockNumber, 16) : typeof receipt.receipt.blockNumber === "number" ? receipt.receipt.blockNumber : null;
|
|
3649
|
+
updateStep(10, "done", "Confirmed");
|
|
3650
|
+
return { txHash, blockNum };
|
|
3651
|
+
}
|
|
3652
|
+
async function stepSaveSession(normalized, did) {
|
|
3653
|
+
updateStep(11, "active");
|
|
3654
|
+
try {
|
|
3655
|
+
await fetch("/api/sessions", {
|
|
3656
|
+
method: "POST",
|
|
3657
|
+
headers: { "Content-Type": "application/json" },
|
|
3658
|
+
body: JSON.stringify({ name: normalized, did })
|
|
3659
|
+
});
|
|
3660
|
+
} catch {}
|
|
3661
|
+
updateStep(11, "done", "Saved");
|
|
3662
|
+
}
|
|
3663
|
+
try {
|
|
3664
|
+
const [sdk, viem] = await Promise.all([importSdk(), importViem()]);
|
|
3665
|
+
const normalized = await stepCheckAlias(sdk, alias);
|
|
3666
|
+
phase.value = "creating-passkey";
|
|
3667
|
+
const { credential, credentialId } = await stepCreatePasskey(sdk, normalized);
|
|
3668
|
+
phase.value = "generating-identity";
|
|
3669
|
+
const {
|
|
3670
|
+
did,
|
|
3671
|
+
enclaveId,
|
|
3672
|
+
publicKey,
|
|
3673
|
+
shares,
|
|
3674
|
+
publicKeyHex,
|
|
3675
|
+
pubkeyX,
|
|
3676
|
+
pubkeyY,
|
|
3677
|
+
ethAddress
|
|
3678
|
+
} = await stepGenerateIdentity(credential, normalized);
|
|
3679
|
+
phase.value = "predicting-account";
|
|
3680
|
+
const smartAccountAddress = await stepPredictAccount(publicKeyHex);
|
|
3681
|
+
phase.value = "fetching-account-state";
|
|
3682
|
+
identity.value = {
|
|
3683
|
+
did,
|
|
3684
|
+
enclaveId,
|
|
3685
|
+
publicKey,
|
|
3686
|
+
pubkeyX,
|
|
3687
|
+
pubkeyY,
|
|
3688
|
+
ethAddress,
|
|
3689
|
+
smartAccountAddress,
|
|
3690
|
+
credentialId,
|
|
3691
|
+
publicKeyHex,
|
|
3692
|
+
encryptedShares: shares
|
|
3693
|
+
};
|
|
3694
|
+
const { accountNonce, accountIsActive } = await stepFetchAccountState(sdk, smartAccountAddress);
|
|
3695
|
+
phase.value = "creating-registration";
|
|
3696
|
+
const userOp = await stepPrepareUserOp(sdk, viem, {
|
|
3697
|
+
smartAccountAddress,
|
|
3698
|
+
pubkeyX,
|
|
3699
|
+
pubkeyY,
|
|
3700
|
+
did,
|
|
3701
|
+
normalized,
|
|
3702
|
+
accountNonce,
|
|
3703
|
+
accountIsActive
|
|
3704
|
+
});
|
|
3705
|
+
phase.value = "sponsoring";
|
|
3706
|
+
const entryPoint = sdk.defaultContracts.entryPoint;
|
|
3707
|
+
const sponsoredOp = await stepSponsorUserOp(sdk, userOp, entryPoint);
|
|
3708
|
+
phase.value = "authorizing";
|
|
3709
|
+
stepAuthorize(did);
|
|
3710
|
+
phase.value = "signing";
|
|
3711
|
+
const signedOp = await stepSignUserOp(sdk, {
|
|
3712
|
+
shares,
|
|
3713
|
+
sponsoredOp,
|
|
3714
|
+
entryPoint,
|
|
3715
|
+
chainId: sdk.defaultContracts.chainId
|
|
3716
|
+
});
|
|
3717
|
+
phase.value = "submitting";
|
|
3718
|
+
const userOpHash = await stepSubmitUserOp(sdk, signedOp, entryPoint);
|
|
3719
|
+
phase.value = "confirming";
|
|
3720
|
+
const { txHash, blockNum } = await stepWaitForConfirmation(sdk, userOpHash);
|
|
3721
|
+
phase.value = "storing-session";
|
|
3722
|
+
await stepSaveSession(normalized, did);
|
|
3723
|
+
result.value = {
|
|
3724
|
+
did,
|
|
3725
|
+
txHash,
|
|
3726
|
+
blockNumber: blockNum,
|
|
3727
|
+
smartAccount: smartAccountAddress
|
|
3728
|
+
};
|
|
3729
|
+
phase.value = "complete";
|
|
3730
|
+
} catch (err) {
|
|
3731
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
3732
|
+
error.value = msg;
|
|
3733
|
+
phase.value = "error";
|
|
3734
|
+
steps.value = steps.value.map((step) => step.status === "active" ? { ...step, status: "error", detail: msg } : step);
|
|
3735
|
+
} finally {
|
|
3736
|
+
isRegistering.value = false;
|
|
3737
|
+
}
|
|
3738
|
+
}
|
|
3739
|
+
return {
|
|
3740
|
+
phase,
|
|
3741
|
+
steps,
|
|
3742
|
+
identity,
|
|
3743
|
+
result,
|
|
3744
|
+
error,
|
|
3745
|
+
isRegistering: computed6(() => isRegistering.value),
|
|
3746
|
+
register,
|
|
3747
|
+
reset
|
|
3748
|
+
};
|
|
3749
|
+
}
|
|
3750
|
+
// src/use-signer.ts
|
|
3751
|
+
import { onScopeDispose as onScopeDispose2, ref as ref2, watch as watch2 } from "vue";
|
|
3752
|
+
import {
|
|
3753
|
+
createSignerBridge
|
|
3754
|
+
} from "@hyperauth/client";
|
|
3755
|
+
function useSigner(options) {
|
|
3756
|
+
const signer = ref2(null);
|
|
3757
|
+
const ready = ref2(false);
|
|
3758
|
+
const error = ref2(null);
|
|
3759
|
+
let dispose = null;
|
|
3760
|
+
let cancelled = false;
|
|
3761
|
+
const wasmUrl = options?.wasmUrl;
|
|
3762
|
+
watch2(() => wasmUrl, () => {
|
|
3763
|
+
dispose?.();
|
|
3764
|
+
dispose = null;
|
|
3765
|
+
cancelled = false;
|
|
3766
|
+
signer.value = null;
|
|
3767
|
+
ready.value = false;
|
|
3768
|
+
error.value = null;
|
|
3769
|
+
createSignerBridge(options).then((handle) => {
|
|
3770
|
+
if (cancelled) {
|
|
3771
|
+
handle.dispose();
|
|
3772
|
+
return;
|
|
3773
|
+
}
|
|
3774
|
+
dispose = handle.dispose;
|
|
3775
|
+
signer.value = handle.signer;
|
|
3776
|
+
ready.value = true;
|
|
3777
|
+
}).catch((err) => {
|
|
3778
|
+
if (cancelled)
|
|
3779
|
+
return;
|
|
3780
|
+
error.value = err instanceof Error ? err : new Error(String(err));
|
|
3781
|
+
});
|
|
3782
|
+
}, { immediate: true });
|
|
3783
|
+
onScopeDispose2(() => {
|
|
3784
|
+
cancelled = true;
|
|
3785
|
+
dispose?.();
|
|
3786
|
+
dispose = null;
|
|
3787
|
+
});
|
|
3788
|
+
return { signer, ready, error };
|
|
3789
|
+
}
|
|
3790
|
+
// src/use-policy-mint.ts
|
|
3791
|
+
import { computed as computed7, ref as ref3 } from "vue";
|
|
3792
|
+
var DEFAULT_AUDIENCE = "did:web:hyperauth.org";
|
|
3793
|
+
var DEFAULT_EXPIRY_SECONDS = 60 * 60 * 24 * 365;
|
|
3794
|
+
function usePolicyMint(options = {}) {
|
|
3795
|
+
const { signer, ready, error: signerError } = useSigner({ wasmUrl: options.wasmUrl });
|
|
3796
|
+
const status = ref3("idle");
|
|
3797
|
+
const token = ref3(null);
|
|
3798
|
+
const payload = ref3(null);
|
|
3799
|
+
const error = ref3(null);
|
|
3800
|
+
let demoIdentity = null;
|
|
3801
|
+
async function mint(params) {
|
|
3802
|
+
if (!signer.value) {
|
|
3803
|
+
throw new Error("signer is not ready yet — wait for `ready` before calling mint()");
|
|
3804
|
+
}
|
|
3805
|
+
status.value = "minting";
|
|
3806
|
+
error.value = null;
|
|
3807
|
+
token.value = null;
|
|
3808
|
+
payload.value = null;
|
|
3809
|
+
try {
|
|
3810
|
+
if (!demoIdentity) {
|
|
3811
|
+
demoIdentity = await generateDemoIdentity(signer.value);
|
|
3812
|
+
}
|
|
3813
|
+
const { did, shares } = demoIdentity;
|
|
3814
|
+
const builtPayload = await buildPolicyPayload(did, params, options);
|
|
3815
|
+
payload.value = builtPayload;
|
|
3816
|
+
const result = await signer.value.mintUcan(shares, builtPayload);
|
|
3817
|
+
token.value = formatJwtStyle(builtPayload, result.signed_dag_cbor);
|
|
3818
|
+
status.value = "minted";
|
|
3819
|
+
} catch (err) {
|
|
3820
|
+
const wrapped = err instanceof Error ? err : new Error(String(err));
|
|
3821
|
+
error.value = wrapped;
|
|
3822
|
+
status.value = "error";
|
|
3823
|
+
}
|
|
3824
|
+
}
|
|
3825
|
+
function reset() {
|
|
3826
|
+
status.value = "idle";
|
|
3827
|
+
token.value = null;
|
|
3828
|
+
payload.value = null;
|
|
3829
|
+
error.value = null;
|
|
3830
|
+
}
|
|
3831
|
+
return {
|
|
3832
|
+
mint,
|
|
3833
|
+
reset,
|
|
3834
|
+
status,
|
|
3835
|
+
token,
|
|
3836
|
+
payload,
|
|
3837
|
+
error: computed7(() => error.value ?? signerError.value),
|
|
3838
|
+
ready
|
|
3839
|
+
};
|
|
3840
|
+
}
|
|
3841
|
+
var BASE58_ALPHABET2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
3842
|
+
function base58btcEncode(bytes) {
|
|
3843
|
+
const digits = [0];
|
|
3844
|
+
for (const byte of bytes) {
|
|
3845
|
+
let carry = byte;
|
|
3846
|
+
for (let i = 0;i < digits.length; i++) {
|
|
3847
|
+
carry += (digits[i] ?? 0) << 8;
|
|
3848
|
+
digits[i] = carry % 58;
|
|
3849
|
+
carry = Math.floor(carry / 58);
|
|
3850
|
+
}
|
|
3851
|
+
while (carry > 0) {
|
|
3852
|
+
digits.push(carry % 58);
|
|
3853
|
+
carry = Math.floor(carry / 58);
|
|
3854
|
+
}
|
|
3855
|
+
}
|
|
3856
|
+
let result = "";
|
|
3857
|
+
for (let i = bytes.length - 1;i >= 0 && bytes[i] === 0; i--)
|
|
3858
|
+
result += "1";
|
|
3859
|
+
for (let i = digits.length - 1;i >= 0; i--)
|
|
3860
|
+
result += BASE58_ALPHABET2[digits[i] ?? 0];
|
|
3861
|
+
return result;
|
|
3862
|
+
}
|
|
3863
|
+
function base64UrlEncode(bytes) {
|
|
3864
|
+
const str = typeof bytes === "string" ? bytes : String.fromCharCode(...bytes);
|
|
3865
|
+
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
3866
|
+
}
|
|
3867
|
+
async function generateDemoIdentity(signer) {
|
|
3868
|
+
const result = await signer.generate();
|
|
3869
|
+
const pubBytes = hexToBytes(result.public_key_hex);
|
|
3870
|
+
const prefix = new Uint8Array([231, 1]);
|
|
3871
|
+
const multikey = new Uint8Array(prefix.length + pubBytes.length);
|
|
3872
|
+
multikey.set(prefix);
|
|
3873
|
+
multikey.set(pubBytes, prefix.length);
|
|
3874
|
+
const did = `did:key:z${base58btcEncode(multikey)}`;
|
|
3875
|
+
const shares = {
|
|
3876
|
+
public_key: result.public_key,
|
|
3877
|
+
public_key_hex: result.public_key_hex,
|
|
3878
|
+
val_share: result.val_share,
|
|
3879
|
+
user_share: result.user_share,
|
|
3880
|
+
nonce: result.nonce,
|
|
3881
|
+
curve: result.curve
|
|
3882
|
+
};
|
|
3883
|
+
return { did, shares };
|
|
3884
|
+
}
|
|
3885
|
+
function hexToBytes(hex) {
|
|
3886
|
+
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
3887
|
+
const out = new Uint8Array(clean.length / 2);
|
|
3888
|
+
for (let i = 0;i < out.length; i++) {
|
|
3889
|
+
out[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
3890
|
+
}
|
|
3891
|
+
return out;
|
|
3892
|
+
}
|
|
3893
|
+
async function buildPolicyPreview(params, options = {}) {
|
|
3894
|
+
return buildPolicyPayload("did:key:<pending>", params, options);
|
|
3895
|
+
}
|
|
3896
|
+
async function buildPolicyPayload(did, params, options) {
|
|
3897
|
+
const now = Math.floor(Date.now() / 1000);
|
|
3898
|
+
const exp = now + (options.expirySeconds ?? DEFAULT_EXPIRY_SECONDS);
|
|
3899
|
+
const policyId = await hashPolicyId(params);
|
|
3900
|
+
const nnc = generateNonce();
|
|
3901
|
+
return {
|
|
3902
|
+
iss: did,
|
|
3903
|
+
aud: options.audience ?? DEFAULT_AUDIENCE,
|
|
3904
|
+
exp,
|
|
3905
|
+
cap: [{ with: `hyperauth://policy/${policyId}`, can: "policy/issue" }],
|
|
3906
|
+
fct: [
|
|
3907
|
+
{
|
|
3908
|
+
"hyperauth/policy": {
|
|
3909
|
+
origins: params.origins,
|
|
3910
|
+
chain_id: params.chainId,
|
|
3911
|
+
paymaster: params.paymaster,
|
|
3912
|
+
max_fee_per_user_usd: params.maxFeePerUserUsd
|
|
3913
|
+
}
|
|
3914
|
+
}
|
|
3915
|
+
],
|
|
3916
|
+
prf: [],
|
|
3917
|
+
nnc
|
|
3918
|
+
};
|
|
3919
|
+
}
|
|
3920
|
+
async function hashPolicyId(params) {
|
|
3921
|
+
const text = JSON.stringify({
|
|
3922
|
+
origins: [...params.origins].sort(),
|
|
3923
|
+
chain_id: params.chainId,
|
|
3924
|
+
paymaster: params.paymaster.toLowerCase(),
|
|
3925
|
+
max_fee_per_user_usd: params.maxFeePerUserUsd
|
|
3926
|
+
});
|
|
3927
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
|
|
3928
|
+
return base32EncodeFirstBytes(new Uint8Array(digest), 16);
|
|
3929
|
+
}
|
|
3930
|
+
function base32EncodeFirstBytes(bytes, byteCount) {
|
|
3931
|
+
const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
3932
|
+
let result = "";
|
|
3933
|
+
let bits = 0;
|
|
3934
|
+
let value = 0;
|
|
3935
|
+
const slice2 = bytes.slice(0, byteCount);
|
|
3936
|
+
for (const byte of slice2) {
|
|
3937
|
+
value = value << 8 | byte;
|
|
3938
|
+
bits += 8;
|
|
3939
|
+
while (bits >= 5) {
|
|
3940
|
+
bits -= 5;
|
|
3941
|
+
result += ALPHA[value >> bits & 31];
|
|
3942
|
+
}
|
|
3943
|
+
}
|
|
3944
|
+
if (bits > 0) {
|
|
3945
|
+
result += ALPHA[value << 5 - bits & 31];
|
|
3946
|
+
}
|
|
3947
|
+
return result;
|
|
3948
|
+
}
|
|
3949
|
+
function generateNonce() {
|
|
3950
|
+
const bytes = new Uint8Array(12);
|
|
3951
|
+
crypto.getRandomValues(bytes);
|
|
3952
|
+
return base64UrlEncode(bytes);
|
|
3953
|
+
}
|
|
3954
|
+
function formatJwtStyle(payload, signatureCborB64Std) {
|
|
3955
|
+
const header = base64UrlEncode(JSON.stringify({ alg: "ES256", typ: "UCAN" }));
|
|
3956
|
+
const payloadEnc = base64UrlEncode(JSON.stringify(payload));
|
|
3957
|
+
const sig = base64StdToUrl(signatureCborB64Std);
|
|
3958
|
+
return `${header}.${payloadEnc}.${sig}`;
|
|
3959
|
+
}
|
|
3960
|
+
function base64StdToUrl(b64std) {
|
|
3961
|
+
return b64std.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
3962
|
+
}
|
|
3963
|
+
// src/use-policy-verify.ts
|
|
3964
|
+
import { computed as computed8, ref as ref4 } from "vue";
|
|
3965
|
+
function usePolicyVerify(options = {}) {
|
|
3966
|
+
const { signer, ready, error: signerError } = useSigner({ wasmUrl: options.wasmUrl });
|
|
3967
|
+
const status = ref4("idle");
|
|
3968
|
+
const parsed = ref4(null);
|
|
3969
|
+
const signatureValid = ref4(null);
|
|
3970
|
+
const ownError = ref4(null);
|
|
3971
|
+
async function verify(rawToken) {
|
|
3972
|
+
status.value = "verifying";
|
|
3973
|
+
parsed.value = null;
|
|
3974
|
+
signatureValid.value = null;
|
|
3975
|
+
ownError.value = null;
|
|
3976
|
+
let token;
|
|
3977
|
+
try {
|
|
3978
|
+
token = parsePolicyToken(rawToken);
|
|
3979
|
+
} catch (err) {
|
|
3980
|
+
const wrapped = err instanceof PolicyTokenParseError ? err : err instanceof Error ? err : new Error(String(err));
|
|
3981
|
+
ownError.value = wrapped;
|
|
3982
|
+
status.value = "error";
|
|
3983
|
+
return;
|
|
3984
|
+
}
|
|
3985
|
+
parsed.value = token;
|
|
3986
|
+
if (!signer.value) {
|
|
3987
|
+
ownError.value = new Error("signer is not ready yet — wait for `ready` before calling verify()");
|
|
3988
|
+
status.value = "error";
|
|
3989
|
+
return;
|
|
3990
|
+
}
|
|
3991
|
+
try {
|
|
3992
|
+
const result = await signer.value.verify({
|
|
3993
|
+
public_key_hex: token.publicKeyHex,
|
|
3994
|
+
data: token.signedBytes,
|
|
3995
|
+
signature: token.signature
|
|
3996
|
+
});
|
|
3997
|
+
signatureValid.value = result.valid;
|
|
3998
|
+
status.value = result.valid ? "valid" : "invalid";
|
|
3999
|
+
} catch (err) {
|
|
4000
|
+
const wrapped = err instanceof Error ? err : new Error(String(err));
|
|
4001
|
+
ownError.value = wrapped;
|
|
4002
|
+
status.value = "error";
|
|
4003
|
+
}
|
|
4004
|
+
}
|
|
4005
|
+
function reset() {
|
|
4006
|
+
status.value = "idle";
|
|
4007
|
+
parsed.value = null;
|
|
4008
|
+
signatureValid.value = null;
|
|
4009
|
+
ownError.value = null;
|
|
4010
|
+
}
|
|
4011
|
+
return {
|
|
4012
|
+
verify,
|
|
4013
|
+
reset,
|
|
4014
|
+
status,
|
|
4015
|
+
parsed,
|
|
4016
|
+
signatureValid,
|
|
4017
|
+
error: computed8(() => ownError.value ?? signerError.value),
|
|
4018
|
+
ready
|
|
4019
|
+
};
|
|
4020
|
+
}
|
|
4021
|
+
// src/ui/button.ts
|
|
4022
|
+
import { defineComponent, h } from "vue";
|
|
4023
|
+
var Button = defineComponent({
|
|
4024
|
+
name: "HyperAuthButton",
|
|
4025
|
+
props: {
|
|
4026
|
+
loading: { type: Boolean, default: false },
|
|
4027
|
+
disabled: { type: Boolean, default: false },
|
|
4028
|
+
type: { type: String, default: "button" }
|
|
4029
|
+
},
|
|
4030
|
+
setup(props, { slots, attrs }) {
|
|
4031
|
+
return () => h("button", {
|
|
4032
|
+
...attrs,
|
|
4033
|
+
type: props.type,
|
|
4034
|
+
disabled: props.disabled || props.loading,
|
|
4035
|
+
"data-loading": props.loading ? "true" : undefined,
|
|
4036
|
+
"aria-busy": props.loading || undefined
|
|
4037
|
+
}, props.loading && slots.loading ? slots.loading() : slots.default?.());
|
|
4038
|
+
}
|
|
4039
|
+
});
|
|
4040
|
+
// src/ui/modal.ts
|
|
4041
|
+
import {
|
|
4042
|
+
defineComponent as defineComponent2,
|
|
4043
|
+
h as h2,
|
|
4044
|
+
inject as inject2,
|
|
4045
|
+
onMounted,
|
|
4046
|
+
onUnmounted,
|
|
4047
|
+
provide,
|
|
4048
|
+
ref as ref5,
|
|
4049
|
+
useId
|
|
4050
|
+
} from "vue";
|
|
4051
|
+
var ModalContextKey = Symbol("HyperAuthModal");
|
|
4052
|
+
function useModalContext(consumer) {
|
|
4053
|
+
const ctx = inject2(ModalContextKey);
|
|
4054
|
+
if (!ctx) {
|
|
4055
|
+
throw new Error(`${consumer} must be used inside a <Modal>`);
|
|
4056
|
+
}
|
|
4057
|
+
return ctx;
|
|
4058
|
+
}
|
|
4059
|
+
var Modal = defineComponent2({
|
|
4060
|
+
name: "HyperAuthModal",
|
|
4061
|
+
props: {
|
|
4062
|
+
open: { type: Boolean, required: true },
|
|
4063
|
+
modal: { type: Boolean, default: true }
|
|
4064
|
+
},
|
|
4065
|
+
emits: ["update:open"],
|
|
4066
|
+
setup(props, { slots, emit }) {
|
|
4067
|
+
const titleId = useId();
|
|
4068
|
+
const openRef = ref5(props.open);
|
|
4069
|
+
const ctx = {
|
|
4070
|
+
open: openRef,
|
|
4071
|
+
onOpenChange: (next) => emit("update:open", next),
|
|
4072
|
+
titleId
|
|
4073
|
+
};
|
|
4074
|
+
provide(ModalContextKey, ctx);
|
|
4075
|
+
function onKey(e) {
|
|
4076
|
+
if (e.key === "Escape" && props.modal)
|
|
4077
|
+
ctx.onOpenChange(false);
|
|
4078
|
+
}
|
|
4079
|
+
let prevOverflow = "";
|
|
4080
|
+
onMounted(() => {
|
|
4081
|
+
if (!isBrowser())
|
|
4082
|
+
return;
|
|
4083
|
+
window.addEventListener("keydown", onKey);
|
|
4084
|
+
});
|
|
4085
|
+
onUnmounted(() => {
|
|
4086
|
+
if (!isBrowser())
|
|
4087
|
+
return;
|
|
4088
|
+
window.removeEventListener("keydown", onKey);
|
|
4089
|
+
document.body.style.overflow = prevOverflow;
|
|
4090
|
+
});
|
|
4091
|
+
return () => {
|
|
4092
|
+
openRef.value = props.open;
|
|
4093
|
+
if (isBrowser() && props.modal) {
|
|
4094
|
+
if (props.open) {
|
|
4095
|
+
prevOverflow = document.body.style.overflow;
|
|
4096
|
+
document.body.style.overflow = "hidden";
|
|
4097
|
+
} else {
|
|
4098
|
+
document.body.style.overflow = prevOverflow;
|
|
4099
|
+
}
|
|
4100
|
+
}
|
|
4101
|
+
if (!props.open)
|
|
4102
|
+
return null;
|
|
4103
|
+
return slots.default?.();
|
|
4104
|
+
};
|
|
4105
|
+
}
|
|
4106
|
+
});
|
|
4107
|
+
var ModalBackdrop = defineComponent2({
|
|
4108
|
+
name: "HyperAuthModalBackdrop",
|
|
4109
|
+
setup(_, { attrs }) {
|
|
4110
|
+
const ctx = useModalContext("ModalBackdrop");
|
|
4111
|
+
return () => h2("div", {
|
|
4112
|
+
"data-hyperauth-modal-backdrop": "",
|
|
4113
|
+
...attrs,
|
|
4114
|
+
onClick: (e) => {
|
|
4115
|
+
attrs.onClick?.(e);
|
|
4116
|
+
if (!e.defaultPrevented)
|
|
4117
|
+
ctx.onOpenChange(false);
|
|
4118
|
+
}
|
|
4119
|
+
});
|
|
4120
|
+
}
|
|
4121
|
+
});
|
|
4122
|
+
var ModalContent = defineComponent2({
|
|
4123
|
+
name: "HyperAuthModalContent",
|
|
4124
|
+
setup(_, { slots, attrs }) {
|
|
4125
|
+
const ctx = useModalContext("ModalContent");
|
|
4126
|
+
const rootRef = ref5(null);
|
|
4127
|
+
let prevActive = null;
|
|
4128
|
+
onMounted(() => {
|
|
4129
|
+
if (!isBrowser())
|
|
4130
|
+
return;
|
|
4131
|
+
prevActive = document.activeElement;
|
|
4132
|
+
const focusable = rootRef.value?.querySelector('a[href],button:not([disabled]),textarea,input,select,[tabindex]:not([tabindex="-1"])');
|
|
4133
|
+
focusable?.focus();
|
|
4134
|
+
});
|
|
4135
|
+
onUnmounted(() => {
|
|
4136
|
+
prevActive?.focus?.();
|
|
4137
|
+
});
|
|
4138
|
+
return () => h2("div", {
|
|
4139
|
+
ref: rootRef,
|
|
4140
|
+
role: "dialog",
|
|
4141
|
+
"aria-modal": "true",
|
|
4142
|
+
"aria-labelledby": ctx.titleId,
|
|
4143
|
+
...attrs
|
|
4144
|
+
}, slots.default?.());
|
|
4145
|
+
}
|
|
4146
|
+
});
|
|
4147
|
+
var ModalClose = defineComponent2({
|
|
4148
|
+
name: "HyperAuthModalClose",
|
|
4149
|
+
setup(_, { slots, attrs }) {
|
|
4150
|
+
const ctx = useModalContext("ModalClose");
|
|
4151
|
+
return () => h2("button", {
|
|
4152
|
+
type: "button",
|
|
4153
|
+
...attrs,
|
|
4154
|
+
onClick: (e) => {
|
|
4155
|
+
attrs.onClick?.(e);
|
|
4156
|
+
if (!e.defaultPrevented)
|
|
4157
|
+
ctx.onOpenChange(false);
|
|
4158
|
+
}
|
|
4159
|
+
}, slots.default?.());
|
|
4160
|
+
}
|
|
4161
|
+
});
|
|
4162
|
+
// src/ui/settlement-progress.ts
|
|
4163
|
+
import { defineComponent as defineComponent3, h as h3 } from "vue";
|
|
4164
|
+
var SettlementProgress = defineComponent3({
|
|
4165
|
+
name: "HyperAuthSettlementProgress",
|
|
4166
|
+
setup(_, { slots, attrs }) {
|
|
4167
|
+
const settlement = useSettlement();
|
|
4168
|
+
return () => {
|
|
4169
|
+
const snapshot = {
|
|
4170
|
+
steps: settlement.steps.value,
|
|
4171
|
+
phase: settlement.phase.value,
|
|
4172
|
+
error: settlement.error.value,
|
|
4173
|
+
txHash: settlement.txHash.value
|
|
4174
|
+
};
|
|
4175
|
+
if (slots.default) {
|
|
4176
|
+
return slots.default(snapshot);
|
|
4177
|
+
}
|
|
4178
|
+
return h3("ol", {
|
|
4179
|
+
"data-hyperauth-settlement-progress": "",
|
|
4180
|
+
"data-phase": snapshot.phase,
|
|
4181
|
+
...attrs
|
|
4182
|
+
}, [
|
|
4183
|
+
...snapshot.steps.map((step) => h3("li", {
|
|
4184
|
+
key: step.phase,
|
|
4185
|
+
"data-hyperauth-settlement-step": step.phase,
|
|
4186
|
+
"data-status": step.status
|
|
4187
|
+
}, [
|
|
4188
|
+
h3("span", step.label),
|
|
4189
|
+
step.detail ? h3("span", { "data-detail": "" }, step.detail) : null
|
|
4190
|
+
])),
|
|
4191
|
+
snapshot.error ? h3("li", { "data-hyperauth-settlement-error": "" }, snapshot.error) : null
|
|
4192
|
+
]);
|
|
4193
|
+
};
|
|
4194
|
+
}
|
|
4195
|
+
});
|
|
4196
|
+
// src/csp.ts
|
|
4197
|
+
var cspManifest = {
|
|
4198
|
+
"worker-src": ["'self'", "blob:"],
|
|
4199
|
+
"script-src": ["'self'", "'wasm-unsafe-eval'"],
|
|
4200
|
+
"connect-src": [
|
|
4201
|
+
"'self'",
|
|
4202
|
+
"https://*.hyperauth.io",
|
|
4203
|
+
"https://*.hyperauth.dev",
|
|
4204
|
+
"https://*.pimlico.io",
|
|
4205
|
+
"wss://*.hyperauth.io",
|
|
4206
|
+
"https://api.transak.com",
|
|
4207
|
+
"https://api.sardine.ai"
|
|
4208
|
+
],
|
|
4209
|
+
"frame-src": ["'self'", "https://*.hyperauth.io"],
|
|
4210
|
+
"img-src": ["'self'", "data:", "blob:"]
|
|
4211
|
+
};
|
|
4212
|
+
function cspManifestString() {
|
|
4213
|
+
return Object.entries(cspManifest).map(([directive, values]) => `${directive} ${values.join(" ")}`).join("; ");
|
|
4214
|
+
}
|
|
4215
|
+
export {
|
|
4216
|
+
useTransaction,
|
|
4217
|
+
useSigner,
|
|
4218
|
+
useSettlement,
|
|
4219
|
+
useSession,
|
|
4220
|
+
useRegistration,
|
|
4221
|
+
usePolicyVerify,
|
|
4222
|
+
usePolicyMint,
|
|
4223
|
+
useLogin,
|
|
4224
|
+
useHyperAuth,
|
|
4225
|
+
useAccount,
|
|
4226
|
+
txExplorerUrl,
|
|
4227
|
+
parsePolicyToken,
|
|
4228
|
+
mountIframeBridge,
|
|
4229
|
+
isPolicyExpired,
|
|
4230
|
+
isOriginAllowedByFacts,
|
|
4231
|
+
getPolicyFacts,
|
|
4232
|
+
explorerUrls,
|
|
4233
|
+
cspManifestString,
|
|
4234
|
+
cspManifest,
|
|
4235
|
+
createHyperAuth,
|
|
4236
|
+
chainIds,
|
|
4237
|
+
buildPolicyPreview,
|
|
4238
|
+
SettlementProgress,
|
|
4239
|
+
PolicyTokenParseError,
|
|
4240
|
+
ModalContent,
|
|
4241
|
+
ModalClose,
|
|
4242
|
+
ModalBackdrop,
|
|
4243
|
+
Modal,
|
|
4244
|
+
HyperAuthKey,
|
|
4245
|
+
Button
|
|
4246
|
+
};
|
|
4247
|
+
|
|
4248
|
+
//# debugId=CE8BD49C130B65B564756E2164756E21
|