@civiwave/mesh-core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1221 -0
- package/dist/index.d.cts +163 -0
- package/dist/index.d.ts +163 -0
- package/dist/index.js +1180 -0
- package/package.json +42 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1180 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// node_modules/blakejs/util.js
|
|
28
|
+
var require_util = __commonJS({
|
|
29
|
+
"node_modules/blakejs/util.js"(exports, module) {
|
|
30
|
+
"use strict";
|
|
31
|
+
var ERROR_MSG_INPUT = "Input must be an string, Buffer or Uint8Array";
|
|
32
|
+
function normalizeInput(input) {
|
|
33
|
+
let ret;
|
|
34
|
+
if (input instanceof Uint8Array) {
|
|
35
|
+
ret = input;
|
|
36
|
+
} else if (typeof input === "string") {
|
|
37
|
+
const encoder = new TextEncoder();
|
|
38
|
+
ret = encoder.encode(input);
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error(ERROR_MSG_INPUT);
|
|
41
|
+
}
|
|
42
|
+
return ret;
|
|
43
|
+
}
|
|
44
|
+
function toHex2(bytes) {
|
|
45
|
+
return Array.prototype.map.call(bytes, function(n) {
|
|
46
|
+
return (n < 16 ? "0" : "") + n.toString(16);
|
|
47
|
+
}).join("");
|
|
48
|
+
}
|
|
49
|
+
function uint32ToHex(val) {
|
|
50
|
+
return (4294967296 + val).toString(16).substring(1);
|
|
51
|
+
}
|
|
52
|
+
function debugPrint(label, arr, size) {
|
|
53
|
+
let msg = "\n" + label + " = ";
|
|
54
|
+
for (let i = 0; i < arr.length; i += 2) {
|
|
55
|
+
if (size === 32) {
|
|
56
|
+
msg += uint32ToHex(arr[i]).toUpperCase();
|
|
57
|
+
msg += " ";
|
|
58
|
+
msg += uint32ToHex(arr[i + 1]).toUpperCase();
|
|
59
|
+
} else if (size === 64) {
|
|
60
|
+
msg += uint32ToHex(arr[i + 1]).toUpperCase();
|
|
61
|
+
msg += uint32ToHex(arr[i]).toUpperCase();
|
|
62
|
+
} else throw new Error("Invalid size " + size);
|
|
63
|
+
if (i % 6 === 4) {
|
|
64
|
+
msg += "\n" + new Array(label.length + 4).join(" ");
|
|
65
|
+
} else if (i < arr.length - 2) {
|
|
66
|
+
msg += " ";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
console.log(msg);
|
|
70
|
+
}
|
|
71
|
+
function testSpeed(hashFn, N, M) {
|
|
72
|
+
let startMs = (/* @__PURE__ */ new Date()).getTime();
|
|
73
|
+
const input = new Uint8Array(N);
|
|
74
|
+
for (let i = 0; i < N; i++) {
|
|
75
|
+
input[i] = i % 256;
|
|
76
|
+
}
|
|
77
|
+
const genMs = (/* @__PURE__ */ new Date()).getTime();
|
|
78
|
+
console.log("Generated random input in " + (genMs - startMs) + "ms");
|
|
79
|
+
startMs = genMs;
|
|
80
|
+
for (let i = 0; i < M; i++) {
|
|
81
|
+
const hashHex = hashFn(input);
|
|
82
|
+
const hashMs = (/* @__PURE__ */ new Date()).getTime();
|
|
83
|
+
const ms = hashMs - startMs;
|
|
84
|
+
startMs = hashMs;
|
|
85
|
+
console.log("Hashed in " + ms + "ms: " + hashHex.substring(0, 20) + "...");
|
|
86
|
+
console.log(
|
|
87
|
+
Math.round(N / (1 << 20) / (ms / 1e3) * 100) / 100 + " MB PER SECOND"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
module.exports = {
|
|
92
|
+
normalizeInput,
|
|
93
|
+
toHex: toHex2,
|
|
94
|
+
debugPrint,
|
|
95
|
+
testSpeed
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// node_modules/blakejs/blake2b.js
|
|
101
|
+
var require_blake2b = __commonJS({
|
|
102
|
+
"node_modules/blakejs/blake2b.js"(exports, module) {
|
|
103
|
+
"use strict";
|
|
104
|
+
var util = require_util();
|
|
105
|
+
function ADD64AA(v2, a, b) {
|
|
106
|
+
const o0 = v2[a] + v2[b];
|
|
107
|
+
let o1 = v2[a + 1] + v2[b + 1];
|
|
108
|
+
if (o0 >= 4294967296) {
|
|
109
|
+
o1++;
|
|
110
|
+
}
|
|
111
|
+
v2[a] = o0;
|
|
112
|
+
v2[a + 1] = o1;
|
|
113
|
+
}
|
|
114
|
+
function ADD64AC(v2, a, b0, b1) {
|
|
115
|
+
let o0 = v2[a] + b0;
|
|
116
|
+
if (b0 < 0) {
|
|
117
|
+
o0 += 4294967296;
|
|
118
|
+
}
|
|
119
|
+
let o1 = v2[a + 1] + b1;
|
|
120
|
+
if (o0 >= 4294967296) {
|
|
121
|
+
o1++;
|
|
122
|
+
}
|
|
123
|
+
v2[a] = o0;
|
|
124
|
+
v2[a + 1] = o1;
|
|
125
|
+
}
|
|
126
|
+
function B2B_GET32(arr, i) {
|
|
127
|
+
return arr[i] ^ arr[i + 1] << 8 ^ arr[i + 2] << 16 ^ arr[i + 3] << 24;
|
|
128
|
+
}
|
|
129
|
+
function B2B_G(a, b, c, d, ix, iy) {
|
|
130
|
+
const x0 = m[ix];
|
|
131
|
+
const x1 = m[ix + 1];
|
|
132
|
+
const y0 = m[iy];
|
|
133
|
+
const y1 = m[iy + 1];
|
|
134
|
+
ADD64AA(v, a, b);
|
|
135
|
+
ADD64AC(v, a, x0, x1);
|
|
136
|
+
let xor0 = v[d] ^ v[a];
|
|
137
|
+
let xor1 = v[d + 1] ^ v[a + 1];
|
|
138
|
+
v[d] = xor1;
|
|
139
|
+
v[d + 1] = xor0;
|
|
140
|
+
ADD64AA(v, c, d);
|
|
141
|
+
xor0 = v[b] ^ v[c];
|
|
142
|
+
xor1 = v[b + 1] ^ v[c + 1];
|
|
143
|
+
v[b] = xor0 >>> 24 ^ xor1 << 8;
|
|
144
|
+
v[b + 1] = xor1 >>> 24 ^ xor0 << 8;
|
|
145
|
+
ADD64AA(v, a, b);
|
|
146
|
+
ADD64AC(v, a, y0, y1);
|
|
147
|
+
xor0 = v[d] ^ v[a];
|
|
148
|
+
xor1 = v[d + 1] ^ v[a + 1];
|
|
149
|
+
v[d] = xor0 >>> 16 ^ xor1 << 16;
|
|
150
|
+
v[d + 1] = xor1 >>> 16 ^ xor0 << 16;
|
|
151
|
+
ADD64AA(v, c, d);
|
|
152
|
+
xor0 = v[b] ^ v[c];
|
|
153
|
+
xor1 = v[b + 1] ^ v[c + 1];
|
|
154
|
+
v[b] = xor1 >>> 31 ^ xor0 << 1;
|
|
155
|
+
v[b + 1] = xor0 >>> 31 ^ xor1 << 1;
|
|
156
|
+
}
|
|
157
|
+
var BLAKE2B_IV32 = new Uint32Array([
|
|
158
|
+
4089235720,
|
|
159
|
+
1779033703,
|
|
160
|
+
2227873595,
|
|
161
|
+
3144134277,
|
|
162
|
+
4271175723,
|
|
163
|
+
1013904242,
|
|
164
|
+
1595750129,
|
|
165
|
+
2773480762,
|
|
166
|
+
2917565137,
|
|
167
|
+
1359893119,
|
|
168
|
+
725511199,
|
|
169
|
+
2600822924,
|
|
170
|
+
4215389547,
|
|
171
|
+
528734635,
|
|
172
|
+
327033209,
|
|
173
|
+
1541459225
|
|
174
|
+
]);
|
|
175
|
+
var SIGMA8 = [
|
|
176
|
+
0,
|
|
177
|
+
1,
|
|
178
|
+
2,
|
|
179
|
+
3,
|
|
180
|
+
4,
|
|
181
|
+
5,
|
|
182
|
+
6,
|
|
183
|
+
7,
|
|
184
|
+
8,
|
|
185
|
+
9,
|
|
186
|
+
10,
|
|
187
|
+
11,
|
|
188
|
+
12,
|
|
189
|
+
13,
|
|
190
|
+
14,
|
|
191
|
+
15,
|
|
192
|
+
14,
|
|
193
|
+
10,
|
|
194
|
+
4,
|
|
195
|
+
8,
|
|
196
|
+
9,
|
|
197
|
+
15,
|
|
198
|
+
13,
|
|
199
|
+
6,
|
|
200
|
+
1,
|
|
201
|
+
12,
|
|
202
|
+
0,
|
|
203
|
+
2,
|
|
204
|
+
11,
|
|
205
|
+
7,
|
|
206
|
+
5,
|
|
207
|
+
3,
|
|
208
|
+
11,
|
|
209
|
+
8,
|
|
210
|
+
12,
|
|
211
|
+
0,
|
|
212
|
+
5,
|
|
213
|
+
2,
|
|
214
|
+
15,
|
|
215
|
+
13,
|
|
216
|
+
10,
|
|
217
|
+
14,
|
|
218
|
+
3,
|
|
219
|
+
6,
|
|
220
|
+
7,
|
|
221
|
+
1,
|
|
222
|
+
9,
|
|
223
|
+
4,
|
|
224
|
+
7,
|
|
225
|
+
9,
|
|
226
|
+
3,
|
|
227
|
+
1,
|
|
228
|
+
13,
|
|
229
|
+
12,
|
|
230
|
+
11,
|
|
231
|
+
14,
|
|
232
|
+
2,
|
|
233
|
+
6,
|
|
234
|
+
5,
|
|
235
|
+
10,
|
|
236
|
+
4,
|
|
237
|
+
0,
|
|
238
|
+
15,
|
|
239
|
+
8,
|
|
240
|
+
9,
|
|
241
|
+
0,
|
|
242
|
+
5,
|
|
243
|
+
7,
|
|
244
|
+
2,
|
|
245
|
+
4,
|
|
246
|
+
10,
|
|
247
|
+
15,
|
|
248
|
+
14,
|
|
249
|
+
1,
|
|
250
|
+
11,
|
|
251
|
+
12,
|
|
252
|
+
6,
|
|
253
|
+
8,
|
|
254
|
+
3,
|
|
255
|
+
13,
|
|
256
|
+
2,
|
|
257
|
+
12,
|
|
258
|
+
6,
|
|
259
|
+
10,
|
|
260
|
+
0,
|
|
261
|
+
11,
|
|
262
|
+
8,
|
|
263
|
+
3,
|
|
264
|
+
4,
|
|
265
|
+
13,
|
|
266
|
+
7,
|
|
267
|
+
5,
|
|
268
|
+
15,
|
|
269
|
+
14,
|
|
270
|
+
1,
|
|
271
|
+
9,
|
|
272
|
+
12,
|
|
273
|
+
5,
|
|
274
|
+
1,
|
|
275
|
+
15,
|
|
276
|
+
14,
|
|
277
|
+
13,
|
|
278
|
+
4,
|
|
279
|
+
10,
|
|
280
|
+
0,
|
|
281
|
+
7,
|
|
282
|
+
6,
|
|
283
|
+
3,
|
|
284
|
+
9,
|
|
285
|
+
2,
|
|
286
|
+
8,
|
|
287
|
+
11,
|
|
288
|
+
13,
|
|
289
|
+
11,
|
|
290
|
+
7,
|
|
291
|
+
14,
|
|
292
|
+
12,
|
|
293
|
+
1,
|
|
294
|
+
3,
|
|
295
|
+
9,
|
|
296
|
+
5,
|
|
297
|
+
0,
|
|
298
|
+
15,
|
|
299
|
+
4,
|
|
300
|
+
8,
|
|
301
|
+
6,
|
|
302
|
+
2,
|
|
303
|
+
10,
|
|
304
|
+
6,
|
|
305
|
+
15,
|
|
306
|
+
14,
|
|
307
|
+
9,
|
|
308
|
+
11,
|
|
309
|
+
3,
|
|
310
|
+
0,
|
|
311
|
+
8,
|
|
312
|
+
12,
|
|
313
|
+
2,
|
|
314
|
+
13,
|
|
315
|
+
7,
|
|
316
|
+
1,
|
|
317
|
+
4,
|
|
318
|
+
10,
|
|
319
|
+
5,
|
|
320
|
+
10,
|
|
321
|
+
2,
|
|
322
|
+
8,
|
|
323
|
+
4,
|
|
324
|
+
7,
|
|
325
|
+
6,
|
|
326
|
+
1,
|
|
327
|
+
5,
|
|
328
|
+
15,
|
|
329
|
+
11,
|
|
330
|
+
9,
|
|
331
|
+
14,
|
|
332
|
+
3,
|
|
333
|
+
12,
|
|
334
|
+
13,
|
|
335
|
+
0,
|
|
336
|
+
0,
|
|
337
|
+
1,
|
|
338
|
+
2,
|
|
339
|
+
3,
|
|
340
|
+
4,
|
|
341
|
+
5,
|
|
342
|
+
6,
|
|
343
|
+
7,
|
|
344
|
+
8,
|
|
345
|
+
9,
|
|
346
|
+
10,
|
|
347
|
+
11,
|
|
348
|
+
12,
|
|
349
|
+
13,
|
|
350
|
+
14,
|
|
351
|
+
15,
|
|
352
|
+
14,
|
|
353
|
+
10,
|
|
354
|
+
4,
|
|
355
|
+
8,
|
|
356
|
+
9,
|
|
357
|
+
15,
|
|
358
|
+
13,
|
|
359
|
+
6,
|
|
360
|
+
1,
|
|
361
|
+
12,
|
|
362
|
+
0,
|
|
363
|
+
2,
|
|
364
|
+
11,
|
|
365
|
+
7,
|
|
366
|
+
5,
|
|
367
|
+
3
|
|
368
|
+
];
|
|
369
|
+
var SIGMA82 = new Uint8Array(
|
|
370
|
+
SIGMA8.map(function(x) {
|
|
371
|
+
return x * 2;
|
|
372
|
+
})
|
|
373
|
+
);
|
|
374
|
+
var v = new Uint32Array(32);
|
|
375
|
+
var m = new Uint32Array(32);
|
|
376
|
+
function blake2bCompress(ctx, last) {
|
|
377
|
+
let i = 0;
|
|
378
|
+
for (i = 0; i < 16; i++) {
|
|
379
|
+
v[i] = ctx.h[i];
|
|
380
|
+
v[i + 16] = BLAKE2B_IV32[i];
|
|
381
|
+
}
|
|
382
|
+
v[24] = v[24] ^ ctx.t;
|
|
383
|
+
v[25] = v[25] ^ ctx.t / 4294967296;
|
|
384
|
+
if (last) {
|
|
385
|
+
v[28] = ~v[28];
|
|
386
|
+
v[29] = ~v[29];
|
|
387
|
+
}
|
|
388
|
+
for (i = 0; i < 32; i++) {
|
|
389
|
+
m[i] = B2B_GET32(ctx.b, 4 * i);
|
|
390
|
+
}
|
|
391
|
+
for (i = 0; i < 12; i++) {
|
|
392
|
+
B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]);
|
|
393
|
+
B2B_G(2, 10, 18, 26, SIGMA82[i * 16 + 2], SIGMA82[i * 16 + 3]);
|
|
394
|
+
B2B_G(4, 12, 20, 28, SIGMA82[i * 16 + 4], SIGMA82[i * 16 + 5]);
|
|
395
|
+
B2B_G(6, 14, 22, 30, SIGMA82[i * 16 + 6], SIGMA82[i * 16 + 7]);
|
|
396
|
+
B2B_G(0, 10, 20, 30, SIGMA82[i * 16 + 8], SIGMA82[i * 16 + 9]);
|
|
397
|
+
B2B_G(2, 12, 22, 24, SIGMA82[i * 16 + 10], SIGMA82[i * 16 + 11]);
|
|
398
|
+
B2B_G(4, 14, 16, 26, SIGMA82[i * 16 + 12], SIGMA82[i * 16 + 13]);
|
|
399
|
+
B2B_G(6, 8, 18, 28, SIGMA82[i * 16 + 14], SIGMA82[i * 16 + 15]);
|
|
400
|
+
}
|
|
401
|
+
for (i = 0; i < 16; i++) {
|
|
402
|
+
ctx.h[i] = ctx.h[i] ^ v[i] ^ v[i + 16];
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
var parameterBlock = new Uint8Array([
|
|
406
|
+
0,
|
|
407
|
+
0,
|
|
408
|
+
0,
|
|
409
|
+
0,
|
|
410
|
+
// 0: outlen, keylen, fanout, depth
|
|
411
|
+
0,
|
|
412
|
+
0,
|
|
413
|
+
0,
|
|
414
|
+
0,
|
|
415
|
+
// 4: leaf length, sequential mode
|
|
416
|
+
0,
|
|
417
|
+
0,
|
|
418
|
+
0,
|
|
419
|
+
0,
|
|
420
|
+
// 8: node offset
|
|
421
|
+
0,
|
|
422
|
+
0,
|
|
423
|
+
0,
|
|
424
|
+
0,
|
|
425
|
+
// 12: node offset
|
|
426
|
+
0,
|
|
427
|
+
0,
|
|
428
|
+
0,
|
|
429
|
+
0,
|
|
430
|
+
// 16: node depth, inner length, rfu
|
|
431
|
+
0,
|
|
432
|
+
0,
|
|
433
|
+
0,
|
|
434
|
+
0,
|
|
435
|
+
// 20: rfu
|
|
436
|
+
0,
|
|
437
|
+
0,
|
|
438
|
+
0,
|
|
439
|
+
0,
|
|
440
|
+
// 24: rfu
|
|
441
|
+
0,
|
|
442
|
+
0,
|
|
443
|
+
0,
|
|
444
|
+
0,
|
|
445
|
+
// 28: rfu
|
|
446
|
+
0,
|
|
447
|
+
0,
|
|
448
|
+
0,
|
|
449
|
+
0,
|
|
450
|
+
// 32: salt
|
|
451
|
+
0,
|
|
452
|
+
0,
|
|
453
|
+
0,
|
|
454
|
+
0,
|
|
455
|
+
// 36: salt
|
|
456
|
+
0,
|
|
457
|
+
0,
|
|
458
|
+
0,
|
|
459
|
+
0,
|
|
460
|
+
// 40: salt
|
|
461
|
+
0,
|
|
462
|
+
0,
|
|
463
|
+
0,
|
|
464
|
+
0,
|
|
465
|
+
// 44: salt
|
|
466
|
+
0,
|
|
467
|
+
0,
|
|
468
|
+
0,
|
|
469
|
+
0,
|
|
470
|
+
// 48: personal
|
|
471
|
+
0,
|
|
472
|
+
0,
|
|
473
|
+
0,
|
|
474
|
+
0,
|
|
475
|
+
// 52: personal
|
|
476
|
+
0,
|
|
477
|
+
0,
|
|
478
|
+
0,
|
|
479
|
+
0,
|
|
480
|
+
// 56: personal
|
|
481
|
+
0,
|
|
482
|
+
0,
|
|
483
|
+
0,
|
|
484
|
+
0
|
|
485
|
+
// 60: personal
|
|
486
|
+
]);
|
|
487
|
+
function blake2bInit(outlen, key, salt, personal) {
|
|
488
|
+
if (outlen === 0 || outlen > 64) {
|
|
489
|
+
throw new Error("Illegal output length, expected 0 < length <= 64");
|
|
490
|
+
}
|
|
491
|
+
if (key && key.length > 64) {
|
|
492
|
+
throw new Error("Illegal key, expected Uint8Array with 0 < length <= 64");
|
|
493
|
+
}
|
|
494
|
+
if (salt && salt.length !== 16) {
|
|
495
|
+
throw new Error("Illegal salt, expected Uint8Array with length is 16");
|
|
496
|
+
}
|
|
497
|
+
if (personal && personal.length !== 16) {
|
|
498
|
+
throw new Error("Illegal personal, expected Uint8Array with length is 16");
|
|
499
|
+
}
|
|
500
|
+
const ctx = {
|
|
501
|
+
b: new Uint8Array(128),
|
|
502
|
+
h: new Uint32Array(16),
|
|
503
|
+
t: 0,
|
|
504
|
+
// input count
|
|
505
|
+
c: 0,
|
|
506
|
+
// pointer within buffer
|
|
507
|
+
outlen
|
|
508
|
+
// output length in bytes
|
|
509
|
+
};
|
|
510
|
+
parameterBlock.fill(0);
|
|
511
|
+
parameterBlock[0] = outlen;
|
|
512
|
+
if (key) parameterBlock[1] = key.length;
|
|
513
|
+
parameterBlock[2] = 1;
|
|
514
|
+
parameterBlock[3] = 1;
|
|
515
|
+
if (salt) parameterBlock.set(salt, 32);
|
|
516
|
+
if (personal) parameterBlock.set(personal, 48);
|
|
517
|
+
for (let i = 0; i < 16; i++) {
|
|
518
|
+
ctx.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameterBlock, i * 4);
|
|
519
|
+
}
|
|
520
|
+
if (key) {
|
|
521
|
+
blake2bUpdate(ctx, key);
|
|
522
|
+
ctx.c = 128;
|
|
523
|
+
}
|
|
524
|
+
return ctx;
|
|
525
|
+
}
|
|
526
|
+
function blake2bUpdate(ctx, input) {
|
|
527
|
+
for (let i = 0; i < input.length; i++) {
|
|
528
|
+
if (ctx.c === 128) {
|
|
529
|
+
ctx.t += ctx.c;
|
|
530
|
+
blake2bCompress(ctx, false);
|
|
531
|
+
ctx.c = 0;
|
|
532
|
+
}
|
|
533
|
+
ctx.b[ctx.c++] = input[i];
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
function blake2bFinal(ctx) {
|
|
537
|
+
ctx.t += ctx.c;
|
|
538
|
+
while (ctx.c < 128) {
|
|
539
|
+
ctx.b[ctx.c++] = 0;
|
|
540
|
+
}
|
|
541
|
+
blake2bCompress(ctx, true);
|
|
542
|
+
const out = new Uint8Array(ctx.outlen);
|
|
543
|
+
for (let i = 0; i < ctx.outlen; i++) {
|
|
544
|
+
out[i] = ctx.h[i >> 2] >> 8 * (i & 3);
|
|
545
|
+
}
|
|
546
|
+
return out;
|
|
547
|
+
}
|
|
548
|
+
function blake2b2(input, key, outlen, salt, personal) {
|
|
549
|
+
outlen = outlen || 64;
|
|
550
|
+
input = util.normalizeInput(input);
|
|
551
|
+
if (salt) {
|
|
552
|
+
salt = util.normalizeInput(salt);
|
|
553
|
+
}
|
|
554
|
+
if (personal) {
|
|
555
|
+
personal = util.normalizeInput(personal);
|
|
556
|
+
}
|
|
557
|
+
const ctx = blake2bInit(outlen, key, salt, personal);
|
|
558
|
+
blake2bUpdate(ctx, input);
|
|
559
|
+
return blake2bFinal(ctx);
|
|
560
|
+
}
|
|
561
|
+
function blake2bHex(input, key, outlen, salt, personal) {
|
|
562
|
+
const output = blake2b2(input, key, outlen, salt, personal);
|
|
563
|
+
return util.toHex(output);
|
|
564
|
+
}
|
|
565
|
+
module.exports = {
|
|
566
|
+
blake2b: blake2b2,
|
|
567
|
+
blake2bHex,
|
|
568
|
+
blake2bInit,
|
|
569
|
+
blake2bUpdate,
|
|
570
|
+
blake2bFinal
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
// node_modules/blakejs/blake2s.js
|
|
576
|
+
var require_blake2s = __commonJS({
|
|
577
|
+
"node_modules/blakejs/blake2s.js"(exports, module) {
|
|
578
|
+
"use strict";
|
|
579
|
+
var util = require_util();
|
|
580
|
+
function B2S_GET32(v2, i) {
|
|
581
|
+
return v2[i] ^ v2[i + 1] << 8 ^ v2[i + 2] << 16 ^ v2[i + 3] << 24;
|
|
582
|
+
}
|
|
583
|
+
function B2S_G(a, b, c, d, x, y) {
|
|
584
|
+
v[a] = v[a] + v[b] + x;
|
|
585
|
+
v[d] = ROTR32(v[d] ^ v[a], 16);
|
|
586
|
+
v[c] = v[c] + v[d];
|
|
587
|
+
v[b] = ROTR32(v[b] ^ v[c], 12);
|
|
588
|
+
v[a] = v[a] + v[b] + y;
|
|
589
|
+
v[d] = ROTR32(v[d] ^ v[a], 8);
|
|
590
|
+
v[c] = v[c] + v[d];
|
|
591
|
+
v[b] = ROTR32(v[b] ^ v[c], 7);
|
|
592
|
+
}
|
|
593
|
+
function ROTR32(x, y) {
|
|
594
|
+
return x >>> y ^ x << 32 - y;
|
|
595
|
+
}
|
|
596
|
+
var BLAKE2S_IV = new Uint32Array([
|
|
597
|
+
1779033703,
|
|
598
|
+
3144134277,
|
|
599
|
+
1013904242,
|
|
600
|
+
2773480762,
|
|
601
|
+
1359893119,
|
|
602
|
+
2600822924,
|
|
603
|
+
528734635,
|
|
604
|
+
1541459225
|
|
605
|
+
]);
|
|
606
|
+
var SIGMA = new Uint8Array([
|
|
607
|
+
0,
|
|
608
|
+
1,
|
|
609
|
+
2,
|
|
610
|
+
3,
|
|
611
|
+
4,
|
|
612
|
+
5,
|
|
613
|
+
6,
|
|
614
|
+
7,
|
|
615
|
+
8,
|
|
616
|
+
9,
|
|
617
|
+
10,
|
|
618
|
+
11,
|
|
619
|
+
12,
|
|
620
|
+
13,
|
|
621
|
+
14,
|
|
622
|
+
15,
|
|
623
|
+
14,
|
|
624
|
+
10,
|
|
625
|
+
4,
|
|
626
|
+
8,
|
|
627
|
+
9,
|
|
628
|
+
15,
|
|
629
|
+
13,
|
|
630
|
+
6,
|
|
631
|
+
1,
|
|
632
|
+
12,
|
|
633
|
+
0,
|
|
634
|
+
2,
|
|
635
|
+
11,
|
|
636
|
+
7,
|
|
637
|
+
5,
|
|
638
|
+
3,
|
|
639
|
+
11,
|
|
640
|
+
8,
|
|
641
|
+
12,
|
|
642
|
+
0,
|
|
643
|
+
5,
|
|
644
|
+
2,
|
|
645
|
+
15,
|
|
646
|
+
13,
|
|
647
|
+
10,
|
|
648
|
+
14,
|
|
649
|
+
3,
|
|
650
|
+
6,
|
|
651
|
+
7,
|
|
652
|
+
1,
|
|
653
|
+
9,
|
|
654
|
+
4,
|
|
655
|
+
7,
|
|
656
|
+
9,
|
|
657
|
+
3,
|
|
658
|
+
1,
|
|
659
|
+
13,
|
|
660
|
+
12,
|
|
661
|
+
11,
|
|
662
|
+
14,
|
|
663
|
+
2,
|
|
664
|
+
6,
|
|
665
|
+
5,
|
|
666
|
+
10,
|
|
667
|
+
4,
|
|
668
|
+
0,
|
|
669
|
+
15,
|
|
670
|
+
8,
|
|
671
|
+
9,
|
|
672
|
+
0,
|
|
673
|
+
5,
|
|
674
|
+
7,
|
|
675
|
+
2,
|
|
676
|
+
4,
|
|
677
|
+
10,
|
|
678
|
+
15,
|
|
679
|
+
14,
|
|
680
|
+
1,
|
|
681
|
+
11,
|
|
682
|
+
12,
|
|
683
|
+
6,
|
|
684
|
+
8,
|
|
685
|
+
3,
|
|
686
|
+
13,
|
|
687
|
+
2,
|
|
688
|
+
12,
|
|
689
|
+
6,
|
|
690
|
+
10,
|
|
691
|
+
0,
|
|
692
|
+
11,
|
|
693
|
+
8,
|
|
694
|
+
3,
|
|
695
|
+
4,
|
|
696
|
+
13,
|
|
697
|
+
7,
|
|
698
|
+
5,
|
|
699
|
+
15,
|
|
700
|
+
14,
|
|
701
|
+
1,
|
|
702
|
+
9,
|
|
703
|
+
12,
|
|
704
|
+
5,
|
|
705
|
+
1,
|
|
706
|
+
15,
|
|
707
|
+
14,
|
|
708
|
+
13,
|
|
709
|
+
4,
|
|
710
|
+
10,
|
|
711
|
+
0,
|
|
712
|
+
7,
|
|
713
|
+
6,
|
|
714
|
+
3,
|
|
715
|
+
9,
|
|
716
|
+
2,
|
|
717
|
+
8,
|
|
718
|
+
11,
|
|
719
|
+
13,
|
|
720
|
+
11,
|
|
721
|
+
7,
|
|
722
|
+
14,
|
|
723
|
+
12,
|
|
724
|
+
1,
|
|
725
|
+
3,
|
|
726
|
+
9,
|
|
727
|
+
5,
|
|
728
|
+
0,
|
|
729
|
+
15,
|
|
730
|
+
4,
|
|
731
|
+
8,
|
|
732
|
+
6,
|
|
733
|
+
2,
|
|
734
|
+
10,
|
|
735
|
+
6,
|
|
736
|
+
15,
|
|
737
|
+
14,
|
|
738
|
+
9,
|
|
739
|
+
11,
|
|
740
|
+
3,
|
|
741
|
+
0,
|
|
742
|
+
8,
|
|
743
|
+
12,
|
|
744
|
+
2,
|
|
745
|
+
13,
|
|
746
|
+
7,
|
|
747
|
+
1,
|
|
748
|
+
4,
|
|
749
|
+
10,
|
|
750
|
+
5,
|
|
751
|
+
10,
|
|
752
|
+
2,
|
|
753
|
+
8,
|
|
754
|
+
4,
|
|
755
|
+
7,
|
|
756
|
+
6,
|
|
757
|
+
1,
|
|
758
|
+
5,
|
|
759
|
+
15,
|
|
760
|
+
11,
|
|
761
|
+
9,
|
|
762
|
+
14,
|
|
763
|
+
3,
|
|
764
|
+
12,
|
|
765
|
+
13,
|
|
766
|
+
0
|
|
767
|
+
]);
|
|
768
|
+
var v = new Uint32Array(16);
|
|
769
|
+
var m = new Uint32Array(16);
|
|
770
|
+
function blake2sCompress(ctx, last) {
|
|
771
|
+
let i = 0;
|
|
772
|
+
for (i = 0; i < 8; i++) {
|
|
773
|
+
v[i] = ctx.h[i];
|
|
774
|
+
v[i + 8] = BLAKE2S_IV[i];
|
|
775
|
+
}
|
|
776
|
+
v[12] ^= ctx.t;
|
|
777
|
+
v[13] ^= ctx.t / 4294967296;
|
|
778
|
+
if (last) {
|
|
779
|
+
v[14] = ~v[14];
|
|
780
|
+
}
|
|
781
|
+
for (i = 0; i < 16; i++) {
|
|
782
|
+
m[i] = B2S_GET32(ctx.b, 4 * i);
|
|
783
|
+
}
|
|
784
|
+
for (i = 0; i < 10; i++) {
|
|
785
|
+
B2S_G(0, 4, 8, 12, m[SIGMA[i * 16 + 0]], m[SIGMA[i * 16 + 1]]);
|
|
786
|
+
B2S_G(1, 5, 9, 13, m[SIGMA[i * 16 + 2]], m[SIGMA[i * 16 + 3]]);
|
|
787
|
+
B2S_G(2, 6, 10, 14, m[SIGMA[i * 16 + 4]], m[SIGMA[i * 16 + 5]]);
|
|
788
|
+
B2S_G(3, 7, 11, 15, m[SIGMA[i * 16 + 6]], m[SIGMA[i * 16 + 7]]);
|
|
789
|
+
B2S_G(0, 5, 10, 15, m[SIGMA[i * 16 + 8]], m[SIGMA[i * 16 + 9]]);
|
|
790
|
+
B2S_G(1, 6, 11, 12, m[SIGMA[i * 16 + 10]], m[SIGMA[i * 16 + 11]]);
|
|
791
|
+
B2S_G(2, 7, 8, 13, m[SIGMA[i * 16 + 12]], m[SIGMA[i * 16 + 13]]);
|
|
792
|
+
B2S_G(3, 4, 9, 14, m[SIGMA[i * 16 + 14]], m[SIGMA[i * 16 + 15]]);
|
|
793
|
+
}
|
|
794
|
+
for (i = 0; i < 8; i++) {
|
|
795
|
+
ctx.h[i] ^= v[i] ^ v[i + 8];
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function blake2sInit(outlen, key) {
|
|
799
|
+
if (!(outlen > 0 && outlen <= 32)) {
|
|
800
|
+
throw new Error("Incorrect output length, should be in [1, 32]");
|
|
801
|
+
}
|
|
802
|
+
const keylen = key ? key.length : 0;
|
|
803
|
+
if (key && !(keylen > 0 && keylen <= 32)) {
|
|
804
|
+
throw new Error("Incorrect key length, should be in [1, 32]");
|
|
805
|
+
}
|
|
806
|
+
const ctx = {
|
|
807
|
+
h: new Uint32Array(BLAKE2S_IV),
|
|
808
|
+
// hash state
|
|
809
|
+
b: new Uint8Array(64),
|
|
810
|
+
// input block
|
|
811
|
+
c: 0,
|
|
812
|
+
// pointer within block
|
|
813
|
+
t: 0,
|
|
814
|
+
// input count
|
|
815
|
+
outlen
|
|
816
|
+
// output length in bytes
|
|
817
|
+
};
|
|
818
|
+
ctx.h[0] ^= 16842752 ^ keylen << 8 ^ outlen;
|
|
819
|
+
if (keylen > 0) {
|
|
820
|
+
blake2sUpdate(ctx, key);
|
|
821
|
+
ctx.c = 64;
|
|
822
|
+
}
|
|
823
|
+
return ctx;
|
|
824
|
+
}
|
|
825
|
+
function blake2sUpdate(ctx, input) {
|
|
826
|
+
for (let i = 0; i < input.length; i++) {
|
|
827
|
+
if (ctx.c === 64) {
|
|
828
|
+
ctx.t += ctx.c;
|
|
829
|
+
blake2sCompress(ctx, false);
|
|
830
|
+
ctx.c = 0;
|
|
831
|
+
}
|
|
832
|
+
ctx.b[ctx.c++] = input[i];
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
function blake2sFinal(ctx) {
|
|
836
|
+
ctx.t += ctx.c;
|
|
837
|
+
while (ctx.c < 64) {
|
|
838
|
+
ctx.b[ctx.c++] = 0;
|
|
839
|
+
}
|
|
840
|
+
blake2sCompress(ctx, true);
|
|
841
|
+
const out = new Uint8Array(ctx.outlen);
|
|
842
|
+
for (let i = 0; i < ctx.outlen; i++) {
|
|
843
|
+
out[i] = ctx.h[i >> 2] >> 8 * (i & 3) & 255;
|
|
844
|
+
}
|
|
845
|
+
return out;
|
|
846
|
+
}
|
|
847
|
+
function blake2s(input, key, outlen) {
|
|
848
|
+
outlen = outlen || 32;
|
|
849
|
+
input = util.normalizeInput(input);
|
|
850
|
+
const ctx = blake2sInit(outlen, key);
|
|
851
|
+
blake2sUpdate(ctx, input);
|
|
852
|
+
return blake2sFinal(ctx);
|
|
853
|
+
}
|
|
854
|
+
function blake2sHex(input, key, outlen) {
|
|
855
|
+
const output = blake2s(input, key, outlen);
|
|
856
|
+
return util.toHex(output);
|
|
857
|
+
}
|
|
858
|
+
module.exports = {
|
|
859
|
+
blake2s,
|
|
860
|
+
blake2sHex,
|
|
861
|
+
blake2sInit,
|
|
862
|
+
blake2sUpdate,
|
|
863
|
+
blake2sFinal
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
// node_modules/blakejs/index.js
|
|
869
|
+
var require_blakejs = __commonJS({
|
|
870
|
+
"node_modules/blakejs/index.js"(exports, module) {
|
|
871
|
+
"use strict";
|
|
872
|
+
var b2b = require_blake2b();
|
|
873
|
+
var b2s = require_blake2s();
|
|
874
|
+
module.exports = {
|
|
875
|
+
blake2b: b2b.blake2b,
|
|
876
|
+
blake2bHex: b2b.blake2bHex,
|
|
877
|
+
blake2bInit: b2b.blake2bInit,
|
|
878
|
+
blake2bUpdate: b2b.blake2bUpdate,
|
|
879
|
+
blake2bFinal: b2b.blake2bFinal,
|
|
880
|
+
blake2s: b2s.blake2s,
|
|
881
|
+
blake2sHex: b2s.blake2sHex,
|
|
882
|
+
blake2sInit: b2s.blake2sInit,
|
|
883
|
+
blake2sUpdate: b2s.blake2sUpdate,
|
|
884
|
+
blake2sFinal: b2s.blake2sFinal
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
});
|
|
888
|
+
|
|
889
|
+
// src/wallet.ts
|
|
890
|
+
import * as ed from "@noble/ed25519";
|
|
891
|
+
var WALLET_STORAGE_KEY = "@mesh/wallet";
|
|
892
|
+
var SS58_PREFIX = 42;
|
|
893
|
+
function toHex(bytes) {
|
|
894
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
895
|
+
}
|
|
896
|
+
function fromHex(hex) {
|
|
897
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
898
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
899
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
900
|
+
}
|
|
901
|
+
return bytes;
|
|
902
|
+
}
|
|
903
|
+
function publicKeyToSS58(pubkey, prefix = SS58_PREFIX) {
|
|
904
|
+
const { blake2b: blake2b2 } = require_blakejs();
|
|
905
|
+
const prefixByte = new Uint8Array([prefix]);
|
|
906
|
+
const payload = new Uint8Array(1 + pubkey.length);
|
|
907
|
+
payload.set(prefixByte, 0);
|
|
908
|
+
payload.set(pubkey, 1);
|
|
909
|
+
const ss58Pre = new TextEncoder().encode("SS58PRE");
|
|
910
|
+
const input = new Uint8Array(ss58Pre.length + payload.length);
|
|
911
|
+
input.set(ss58Pre, 0);
|
|
912
|
+
input.set(payload, ss58Pre.length);
|
|
913
|
+
const hash = blake2b2(input, void 0, 64);
|
|
914
|
+
const checksum = hash.slice(0, 2);
|
|
915
|
+
const full = new Uint8Array(payload.length + 2);
|
|
916
|
+
full.set(payload, 0);
|
|
917
|
+
full.set(checksum, payload.length);
|
|
918
|
+
return base58Encode(full);
|
|
919
|
+
}
|
|
920
|
+
function base58Encode(bytes) {
|
|
921
|
+
const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
922
|
+
let leadingZeros = 0;
|
|
923
|
+
for (const b of bytes) {
|
|
924
|
+
if (b !== 0) break;
|
|
925
|
+
leadingZeros++;
|
|
926
|
+
}
|
|
927
|
+
let num = BigInt(0);
|
|
928
|
+
for (const b of bytes) {
|
|
929
|
+
num = num * BigInt(256) + BigInt(b);
|
|
930
|
+
}
|
|
931
|
+
let result = "";
|
|
932
|
+
while (num > BigInt(0)) {
|
|
933
|
+
const rem = Number(num % BigInt(58));
|
|
934
|
+
num = num / BigInt(58);
|
|
935
|
+
result = ALPHABET[rem] + result;
|
|
936
|
+
}
|
|
937
|
+
return "1".repeat(leadingZeros) + result;
|
|
938
|
+
}
|
|
939
|
+
async function generateKeypair() {
|
|
940
|
+
const secretKey = ed.utils.randomPrivateKey();
|
|
941
|
+
const publicKey = await ed.getPublicKeyAsync(secretKey);
|
|
942
|
+
return { publicKey, secretKey };
|
|
943
|
+
}
|
|
944
|
+
async function sign(message, secretKey) {
|
|
945
|
+
const signature = await ed.signAsync(message, secretKey);
|
|
946
|
+
return signature;
|
|
947
|
+
}
|
|
948
|
+
async function verify(signature, message, publicKey) {
|
|
949
|
+
return ed.verifyAsync(signature, message, publicKey);
|
|
950
|
+
}
|
|
951
|
+
async function saveWallet(wallet, storage) {
|
|
952
|
+
const json = {
|
|
953
|
+
publicKeyHex: toHex(wallet.publicKey),
|
|
954
|
+
secretKeyHex: toHex(wallet.secretKey),
|
|
955
|
+
ss58Address: wallet.ss58Address,
|
|
956
|
+
createdAt: wallet.createdAt
|
|
957
|
+
};
|
|
958
|
+
await storage.setItem(WALLET_STORAGE_KEY, JSON.stringify(json));
|
|
959
|
+
}
|
|
960
|
+
async function loadWallet(storage) {
|
|
961
|
+
const raw = await storage.getItem(WALLET_STORAGE_KEY);
|
|
962
|
+
if (!raw) return null;
|
|
963
|
+
const json = JSON.parse(raw);
|
|
964
|
+
return {
|
|
965
|
+
publicKey: fromHex(json.publicKeyHex),
|
|
966
|
+
secretKey: fromHex(json.secretKeyHex),
|
|
967
|
+
ss58Address: json.ss58Address,
|
|
968
|
+
createdAt: json.createdAt
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
async function deleteWallet(storage) {
|
|
972
|
+
await storage.removeItem(WALLET_STORAGE_KEY);
|
|
973
|
+
}
|
|
974
|
+
function createWallet(publicKey, secretKey) {
|
|
975
|
+
return {
|
|
976
|
+
publicKey,
|
|
977
|
+
secretKey,
|
|
978
|
+
ss58Address: publicKeyToSS58(publicKey),
|
|
979
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
// src/did.ts
|
|
984
|
+
var import_blakejs = __toESM(require_blakejs(), 1);
|
|
985
|
+
|
|
986
|
+
// src/chain.ts
|
|
987
|
+
var DEFAULT_KEEPER_ENDPOINT = "http://localhost:8422";
|
|
988
|
+
var keeperEndpoint = DEFAULT_KEEPER_ENDPOINT;
|
|
989
|
+
function setKeeperEndpoint(url) {
|
|
990
|
+
keeperEndpoint = url.replace(/\/+$/, "");
|
|
991
|
+
}
|
|
992
|
+
function getKeeperEndpoint() {
|
|
993
|
+
return keeperEndpoint;
|
|
994
|
+
}
|
|
995
|
+
function balanceQueryUrl(base, account) {
|
|
996
|
+
return `${base}/v1/chain/query/balance?account=${account}`;
|
|
997
|
+
}
|
|
998
|
+
function extrinsicUrl(base) {
|
|
999
|
+
return `${base}/v1/chain/extrinsic`;
|
|
1000
|
+
}
|
|
1001
|
+
function metadataUrl(base, account) {
|
|
1002
|
+
return `${base}/v1/chain/metadata?account=${account}`;
|
|
1003
|
+
}
|
|
1004
|
+
function stakingUrl(base, op) {
|
|
1005
|
+
return `${base}/v1/staking/${op}`;
|
|
1006
|
+
}
|
|
1007
|
+
async function queryBalance(ss58Address) {
|
|
1008
|
+
try {
|
|
1009
|
+
const url = balanceQueryUrl(keeperEndpoint, ss58Address);
|
|
1010
|
+
const response = await fetch(url);
|
|
1011
|
+
if (!response.ok) {
|
|
1012
|
+
return { success: false, error: `HTTP ${response.status}: ${response.statusText}` };
|
|
1013
|
+
}
|
|
1014
|
+
const data = await response.json();
|
|
1015
|
+
return { success: true, data };
|
|
1016
|
+
} catch (err) {
|
|
1017
|
+
return { success: false, error: "Connection failed" };
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
async function submitExtrinsic(hexPayload) {
|
|
1021
|
+
try {
|
|
1022
|
+
const url = extrinsicUrl(keeperEndpoint);
|
|
1023
|
+
const response = await fetch(url, {
|
|
1024
|
+
method: "POST",
|
|
1025
|
+
headers: { "Content-Type": "application/json" },
|
|
1026
|
+
body: JSON.stringify({ extrinsic: hexPayload })
|
|
1027
|
+
});
|
|
1028
|
+
if (!response.ok) {
|
|
1029
|
+
return { success: false, error: `HTTP ${response.status}: ${response.statusText}` };
|
|
1030
|
+
}
|
|
1031
|
+
const data = await response.json();
|
|
1032
|
+
return { success: true, data };
|
|
1033
|
+
} catch (err) {
|
|
1034
|
+
return { success: false, error: "Connection failed" };
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
async function getChainMetadata(ss58Address) {
|
|
1038
|
+
try {
|
|
1039
|
+
const url = metadataUrl(keeperEndpoint, ss58Address);
|
|
1040
|
+
const response = await fetch(url);
|
|
1041
|
+
if (!response.ok) {
|
|
1042
|
+
return { success: false, error: `HTTP ${response.status}: ${response.statusText}` };
|
|
1043
|
+
}
|
|
1044
|
+
const data = await response.json();
|
|
1045
|
+
return { success: true, data };
|
|
1046
|
+
} catch (err) {
|
|
1047
|
+
return { success: false, error: "Connection failed" };
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
async function submitPalletExtrinsic(request) {
|
|
1051
|
+
try {
|
|
1052
|
+
const url = extrinsicUrl(keeperEndpoint);
|
|
1053
|
+
const response = await fetch(url, {
|
|
1054
|
+
method: "POST",
|
|
1055
|
+
headers: { "Content-Type": "application/json" },
|
|
1056
|
+
body: JSON.stringify(request)
|
|
1057
|
+
});
|
|
1058
|
+
if (!response.ok) {
|
|
1059
|
+
return { success: false, error: `HTTP ${response.status}: ${response.statusText}` };
|
|
1060
|
+
}
|
|
1061
|
+
const data = await response.json();
|
|
1062
|
+
return { success: true, data };
|
|
1063
|
+
} catch (err) {
|
|
1064
|
+
return { success: false, error: "Connection failed" };
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
async function addStake(hotkey, netuid, amount) {
|
|
1068
|
+
try {
|
|
1069
|
+
const url = stakingUrl(keeperEndpoint, "add-stake");
|
|
1070
|
+
const response = await fetch(url, {
|
|
1071
|
+
method: "POST",
|
|
1072
|
+
headers: { "Content-Type": "application/json" },
|
|
1073
|
+
body: JSON.stringify({ hotkey, netuid, amount })
|
|
1074
|
+
});
|
|
1075
|
+
if (!response.ok) {
|
|
1076
|
+
return { success: false, error: `HTTP ${response.status}: ${response.statusText}` };
|
|
1077
|
+
}
|
|
1078
|
+
const data = await response.json();
|
|
1079
|
+
return { success: true, data };
|
|
1080
|
+
} catch (err) {
|
|
1081
|
+
return { success: false, error: "Connection failed" };
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
async function removeStake(hotkey, netuid, amount) {
|
|
1085
|
+
try {
|
|
1086
|
+
const url = stakingUrl(keeperEndpoint, "remove-stake");
|
|
1087
|
+
const response = await fetch(url, {
|
|
1088
|
+
method: "POST",
|
|
1089
|
+
headers: { "Content-Type": "application/json" },
|
|
1090
|
+
body: JSON.stringify({ hotkey, netuid, amount })
|
|
1091
|
+
});
|
|
1092
|
+
if (!response.ok) {
|
|
1093
|
+
return { success: false, error: `HTTP ${response.status}: ${response.statusText}` };
|
|
1094
|
+
}
|
|
1095
|
+
const data = await response.json();
|
|
1096
|
+
return { success: true, data };
|
|
1097
|
+
} catch (err) {
|
|
1098
|
+
return { success: false, error: "Connection failed" };
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
async function transfer(destination, amount) {
|
|
1102
|
+
try {
|
|
1103
|
+
const url = stakingUrl(keeperEndpoint, "transfer");
|
|
1104
|
+
const response = await fetch(url, {
|
|
1105
|
+
method: "POST",
|
|
1106
|
+
headers: { "Content-Type": "application/json" },
|
|
1107
|
+
body: JSON.stringify({ destination, amount })
|
|
1108
|
+
});
|
|
1109
|
+
if (!response.ok) {
|
|
1110
|
+
return { success: false, error: `HTTP ${response.status}: ${response.statusText}` };
|
|
1111
|
+
}
|
|
1112
|
+
const data = await response.json();
|
|
1113
|
+
return { success: true, data };
|
|
1114
|
+
} catch (err) {
|
|
1115
|
+
return { success: false, error: "Connection failed" };
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// src/did.ts
|
|
1120
|
+
function generateDIDHash(publicKey) {
|
|
1121
|
+
return (0, import_blakejs.blake2b)(publicKey, void 0, 32);
|
|
1122
|
+
}
|
|
1123
|
+
function buildDIDDocument(ss58Address, publicKey) {
|
|
1124
|
+
const publicKeyHex = toHex(publicKey);
|
|
1125
|
+
return {
|
|
1126
|
+
"@context": ["https://www.w3.org/ns/did/v1"],
|
|
1127
|
+
id: `did:civiwave:${ss58Address}`,
|
|
1128
|
+
authentication: [
|
|
1129
|
+
{
|
|
1130
|
+
type: "Ed25519VerificationKey2020",
|
|
1131
|
+
publicKeyMultibase: publicKeyHex
|
|
1132
|
+
}
|
|
1133
|
+
]
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
async function registerDID(wallet) {
|
|
1137
|
+
const publicKeyHex = toHex(wallet.publicKey);
|
|
1138
|
+
const didDocument = buildDIDDocument(wallet.ss58Address, wallet.publicKey);
|
|
1139
|
+
return submitPalletExtrinsic({
|
|
1140
|
+
pallet: "IdentityHub",
|
|
1141
|
+
call: "register_did",
|
|
1142
|
+
params: {
|
|
1143
|
+
public_key: publicKeyHex,
|
|
1144
|
+
did_document: didDocument
|
|
1145
|
+
}
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
function didFromSS58(ss58Address) {
|
|
1149
|
+
return `did:civiwave:${ss58Address}`;
|
|
1150
|
+
}
|
|
1151
|
+
export {
|
|
1152
|
+
SS58_PREFIX,
|
|
1153
|
+
addStake,
|
|
1154
|
+
balanceQueryUrl,
|
|
1155
|
+
buildDIDDocument,
|
|
1156
|
+
createWallet,
|
|
1157
|
+
deleteWallet,
|
|
1158
|
+
didFromSS58,
|
|
1159
|
+
extrinsicUrl,
|
|
1160
|
+
fromHex,
|
|
1161
|
+
generateDIDHash,
|
|
1162
|
+
generateKeypair,
|
|
1163
|
+
getChainMetadata,
|
|
1164
|
+
getKeeperEndpoint,
|
|
1165
|
+
loadWallet,
|
|
1166
|
+
metadataUrl,
|
|
1167
|
+
publicKeyToSS58,
|
|
1168
|
+
queryBalance,
|
|
1169
|
+
registerDID,
|
|
1170
|
+
removeStake,
|
|
1171
|
+
saveWallet,
|
|
1172
|
+
setKeeperEndpoint,
|
|
1173
|
+
sign,
|
|
1174
|
+
stakingUrl,
|
|
1175
|
+
submitExtrinsic,
|
|
1176
|
+
submitPalletExtrinsic,
|
|
1177
|
+
toHex,
|
|
1178
|
+
transfer,
|
|
1179
|
+
verify
|
|
1180
|
+
};
|