locuschain-lib 1.1.2 → 1.1.4

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/lclib-cjs.js CHANGED
@@ -32,677 +32,731 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
32
32
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
33
33
  };
34
34
 
35
- /* eslint-disable */
36
-
37
-
38
- function toUint8Array (s) {
39
- if (typeof atob === 'function') return new Uint8Array(atob(s).split('').map(charCodeAt))
40
- // return (require('buf' + 'fer').Buffer).from(s, 'base64')
41
- }
42
-
43
- function charCodeAt (c) {
44
- return c.charCodeAt(0)
45
- }
46
-
47
- const wasmJson = require('./lclib.json');
48
- var wasm = toUint8Array(wasmJson.Base64);
49
-
50
- (() => {
51
- const enosys = () => {
52
- const err = new Error('not implemented');
53
- err.code = 'ENOSYS';
54
- return err;
55
- };
56
-
57
- if (!globalThis.fs) {
58
- let outputBuf = '';
59
- globalThis.fs = {
60
- constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
61
- writeSync(fd, buf) {
62
- outputBuf += decoder.decode(buf);
63
- const nl = outputBuf.lastIndexOf('\n');
64
- if (nl != -1) {
65
- console.log(outputBuf.substring(0, nl));
66
- outputBuf = outputBuf.substring(nl + 1);
67
- }
68
- return buf.length;
69
- },
70
- write(fd, buf, offset, length, position, callback) {
71
- if (offset !== 0 || length !== buf.length || position !== null) {
72
- callback(enosys());
73
- return;
74
- }
75
- const n = this.writeSync(fd, buf);
76
- callback(null, n);
77
- },
78
- chmod(path, mode, callback) {
79
- callback(enosys());
80
- },
81
- chown(path, uid, gid, callback) {
82
- callback(enosys());
83
- },
84
- close(fd, callback) {
85
- callback(enosys());
86
- },
87
- fchmod(fd, mode, callback) {
88
- callback(enosys());
89
- },
90
- fchown(fd, uid, gid, callback) {
91
- callback(enosys());
92
- },
93
- fstat(fd, callback) {
94
- callback(enosys());
95
- },
96
- fsync(fd, callback) {
97
- callback(null);
98
- },
99
- ftruncate(fd, length, callback) {
100
- callback(enosys());
101
- },
102
- lchown(path, uid, gid, callback) {
103
- callback(enosys());
104
- },
105
- link(path, link, callback) {
106
- callback(enosys());
107
- },
108
- lstat(path, callback) {
109
- callback(enosys());
110
- },
111
- mkdir(path, perm, callback) {
112
- callback(enosys());
113
- },
114
- open(path, flags, mode, callback) {
115
- callback(enosys());
116
- },
117
- read(fd, buffer, offset, length, position, callback) {
118
- callback(enosys());
119
- },
120
- readdir(path, callback) {
121
- callback(enosys());
122
- },
123
- readlink(path, callback) {
124
- callback(enosys());
125
- },
126
- rename(from, to, callback) {
127
- callback(enosys());
128
- },
129
- rmdir(path, callback) {
130
- callback(enosys());
131
- },
132
- stat(path, callback) {
133
- callback(enosys());
134
- },
135
- symlink(path, link, callback) {
136
- callback(enosys());
137
- },
138
- truncate(path, length, callback) {
139
- callback(enosys());
140
- },
141
- unlink(path, callback) {
142
- callback(enosys());
143
- },
144
- utimes(path, atime, mtime, callback) {
145
- callback(enosys());
146
- }
147
- };
148
- }
149
-
150
- // if (!globalThis.process) {
151
- // globalThis.process = {
152
- // getuid() {
153
- // return -1;
154
- // },
155
- // getgid() {
156
- // return -1;
157
- // },
158
- // geteuid() {
159
- // return -1;
160
- // },
161
- // getegid() {
162
- // return -1;
163
- // },
164
- // getgroups() {
165
- // throw enosys();
166
- // },
167
- // pid: -1,
168
- // ppid: -1,
169
- // umask() {
170
- // throw enosys();
171
- // },
172
- // cwd() {
173
- // throw enosys();
174
- // },
175
- // chdir() {
176
- // throw enosys();
177
- // }
178
- // };
179
- // }
180
-
181
- if (!globalThis.crypto) {
182
- throw new Error('globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)');
183
- }
184
-
185
- if (!globalThis.performance) {
186
- throw new Error('globalThis.performance is not available, polyfill required (performance.now only)');
187
- }
188
-
189
- if (!globalThis.TextEncoder) {
190
- throw new Error('globalThis.TextEncoder is not available, polyfill required');
191
- }
192
-
193
- if (!globalThis.TextDecoder) {
194
- throw new Error('globalThis.TextDecoder is not available, polyfill required');
195
- }
196
-
197
- const encoder = new TextEncoder('utf-8');
198
- const decoder = new TextDecoder('utf-8');
199
-
200
- globalThis.Go = class {
201
- constructor() {
202
- this.argv = ['js'];
203
- this.env = {};
204
- this.exit = code => {
205
- if (code !== 0) {
206
- console.warn('exit code:', code);
207
- }
208
- };
209
- this._exitPromise = new Promise(resolve => {
210
- this._resolveExitPromise = resolve;
211
- });
212
- this._pendingEvent = null;
213
- this._scheduledTimeouts = new Map();
214
- this._nextCallbackTimeoutID = 1;
215
-
216
- const setInt64 = (addr, v) => {
217
- this.mem.setUint32(addr + 0, v, true);
218
- this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
219
- };
220
-
221
- const getInt64 = addr => {
222
- const low = this.mem.getUint32(addr + 0, true);
223
- const high = this.mem.getInt32(addr + 4, true);
224
- return low + high * 4294967296;
225
- };
226
-
227
- const loadValue = addr => {
228
- const f = this.mem.getFloat64(addr, true);
229
- if (f === 0) {
230
- return undefined;
231
- }
232
- if (!isNaN(f)) {
233
- return f;
234
- }
235
-
236
- const id = this.mem.getUint32(addr, true);
237
- return this._values[id];
238
- };
239
-
240
- const storeValue = (addr, v) => {
241
- const nanHead = 0x7ff80000;
242
-
243
- if (typeof v === 'number' && v !== 0) {
244
- if (isNaN(v)) {
245
- this.mem.setUint32(addr + 4, nanHead, true);
246
- this.mem.setUint32(addr, 0, true);
247
- return;
248
- }
249
- this.mem.setFloat64(addr, v, true);
250
- return;
251
- }
252
-
253
- if (v === undefined) {
254
- this.mem.setFloat64(addr, 0, true);
255
- return;
256
- }
257
-
258
- let id = this._ids.get(v);
259
- if (id === undefined) {
260
- id = this._idPool.pop();
261
- if (id === undefined) {
262
- id = this._values.length;
263
- }
264
- this._values[id] = v;
265
- this._goRefCounts[id] = 0;
266
- this._ids.set(v, id);
267
- }
268
- this._goRefCounts[id]++;
269
- let typeFlag = 0;
270
- switch (typeof v) {
271
- case 'object':
272
- if (v !== null) {
273
- typeFlag = 1;
274
- }
275
- break;
276
- case 'string':
277
- typeFlag = 2;
278
- break;
279
- case 'symbol':
280
- typeFlag = 3;
281
- break;
282
- case 'function':
283
- typeFlag = 4;
284
- break;
285
- }
286
- this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
287
- this.mem.setUint32(addr, id, true);
288
- };
289
-
290
- const loadSlice = addr => {
291
- const array = getInt64(addr + 0);
292
- const len = getInt64(addr + 8);
293
- return new Uint8Array(this._inst.exports.mem.buffer, array, len);
294
- };
295
-
296
- const loadSliceOfValues = addr => {
297
- const array = getInt64(addr + 0);
298
- const len = getInt64(addr + 8);
299
- const a = new Array(len);
300
- for (let i = 0; i < len; i++) {
301
- a[i] = loadValue(array + i * 8);
302
- }
303
- return a;
304
- };
305
-
306
- const loadString = addr => {
307
- const saddr = getInt64(addr + 0);
308
- const len = getInt64(addr + 8);
309
- return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
310
- };
311
-
312
- const timeOrigin = Date.now() - performance.now();
313
- this.importObject = {
314
- _gotest: {
315
- add: (a, b) => a + b
316
- },
317
- gojs: {
318
- // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
319
- // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
320
- // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
321
- // This changes the SP, thus we have to update the SP used by the imported function.
322
-
323
- // func wasmExit(code int32)
324
- 'runtime.wasmExit': sp => {
325
- sp >>>= 0;
326
- const code = this.mem.getInt32(sp + 8, true);
327
- this.exited = true;
328
- delete this._inst;
329
- delete this._values;
330
- delete this._goRefCounts;
331
- delete this._ids;
332
- delete this._idPool;
333
- this.exit(code);
334
- },
335
-
336
- // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
337
- 'runtime.wasmWrite': sp => {
338
- sp >>>= 0;
339
- const fd = getInt64(sp + 8);
340
- const p = getInt64(sp + 16);
341
- const n = this.mem.getInt32(sp + 24, true);
342
- fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
343
- },
344
-
345
- // func resetMemoryDataView()
346
- 'runtime.resetMemoryDataView': sp => {
347
- this.mem = new DataView(this._inst.exports.mem.buffer);
348
- },
349
-
350
- // func nanotime1() int64
351
- 'runtime.nanotime1': sp => {
352
- sp >>>= 0;
353
- setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
354
- },
355
-
356
- // func walltime() (sec int64, nsec int32)
357
- 'runtime.walltime': sp => {
358
- sp >>>= 0;
359
- const msec = new Date().getTime();
360
- setInt64(sp + 8, msec / 1000);
361
- this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
362
- },
363
-
364
- // func scheduleTimeoutEvent(delay int64) int32
365
- 'runtime.scheduleTimeoutEvent': sp => {
366
- sp >>>= 0;
367
- const id = this._nextCallbackTimeoutID;
368
- this._nextCallbackTimeoutID++;
369
- this._scheduledTimeouts.set(
370
- id,
371
- setTimeout(() => {
372
- this._resume();
373
- while (this._scheduledTimeouts.has(id)) {
374
- // for some reason Go failed to register the timeout event, log and try again
375
- // (temporary workaround for https://github.com/golang/go/issues/28975)
376
- console.warn('scheduleTimeoutEvent: missed timeout event');
377
- this._resume();
378
- }
379
- }, getInt64(sp + 8))
380
- );
381
- this.mem.setInt32(sp + 16, id, true);
382
- },
383
-
384
- // func clearTimeoutEvent(id int32)
385
- 'runtime.clearTimeoutEvent': sp => {
386
- sp >>>= 0;
387
- const id = this.mem.getInt32(sp + 8, true);
388
- clearTimeout(this._scheduledTimeouts.get(id));
389
- this._scheduledTimeouts.delete(id);
390
- },
391
-
392
- // func getRandomData(r []byte)
393
- 'runtime.getRandomData': sp => {
394
- sp >>>= 0;
395
- crypto.getRandomValues(loadSlice(sp + 8));
396
- },
397
-
398
- // func finalizeRef(v ref)
399
- 'syscall/js.finalizeRef': sp => {
400
- sp >>>= 0;
401
- const id = this.mem.getUint32(sp + 8, true);
402
- this._goRefCounts[id]--;
403
- if (this._goRefCounts[id] === 0) {
404
- const v = this._values[id];
405
- this._values[id] = null;
406
- this._ids.delete(v);
407
- this._idPool.push(id);
408
- }
409
- },
410
-
411
- // func stringVal(value string) ref
412
- 'syscall/js.stringVal': sp => {
413
- sp >>>= 0;
414
- storeValue(sp + 24, loadString(sp + 8));
415
- },
416
-
417
- // func valueGet(v ref, p string) ref
418
- 'syscall/js.valueGet': sp => {
419
- sp >>>= 0;
420
- const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
421
- sp = this._inst.exports.getsp() >>> 0; // see comment above
422
- storeValue(sp + 32, result);
423
- },
424
-
425
- // func valueSet(v ref, p string, x ref)
426
- 'syscall/js.valueSet': sp => {
427
- sp >>>= 0;
428
- Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
429
- },
430
-
431
- // func valueDelete(v ref, p string)
432
- 'syscall/js.valueDelete': sp => {
433
- sp >>>= 0;
434
- Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
435
- },
436
-
437
- // func valueIndex(v ref, i int) ref
438
- 'syscall/js.valueIndex': sp => {
439
- sp >>>= 0;
440
- storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
441
- },
442
-
443
- // valueSetIndex(v ref, i int, x ref)
444
- 'syscall/js.valueSetIndex': sp => {
445
- sp >>>= 0;
446
- Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
447
- },
448
-
449
- // func valueCall(v ref, m string, args []ref) (ref, bool)
450
- 'syscall/js.valueCall': sp => {
451
- sp >>>= 0;
452
- try {
453
- const v = loadValue(sp + 8);
454
- const m = Reflect.get(v, loadString(sp + 16));
455
- const args = loadSliceOfValues(sp + 32);
456
- const result = Reflect.apply(m, v, args);
457
- sp = this._inst.exports.getsp() >>> 0; // see comment above
458
- storeValue(sp + 56, result);
459
- this.mem.setUint8(sp + 64, 1);
460
- } catch (err) {
461
- sp = this._inst.exports.getsp() >>> 0; // see comment above
462
- storeValue(sp + 56, err);
463
- this.mem.setUint8(sp + 64, 0);
464
- }
465
- },
466
-
467
- // func valueInvoke(v ref, args []ref) (ref, bool)
468
- 'syscall/js.valueInvoke': sp => {
469
- sp >>>= 0;
470
- try {
471
- const v = loadValue(sp + 8);
472
- const args = loadSliceOfValues(sp + 16);
473
- const result = Reflect.apply(v, undefined, args);
474
- sp = this._inst.exports.getsp() >>> 0; // see comment above
475
- storeValue(sp + 40, result);
476
- this.mem.setUint8(sp + 48, 1);
477
- } catch (err) {
478
- sp = this._inst.exports.getsp() >>> 0; // see comment above
479
- storeValue(sp + 40, err);
480
- this.mem.setUint8(sp + 48, 0);
481
- }
482
- },
483
-
484
- // func valueNew(v ref, args []ref) (ref, bool)
485
- 'syscall/js.valueNew': sp => {
486
- sp >>>= 0;
487
- try {
488
- const v = loadValue(sp + 8);
489
- const args = loadSliceOfValues(sp + 16);
490
- const result = Reflect.construct(v, args);
491
- sp = this._inst.exports.getsp() >>> 0; // see comment above
492
- storeValue(sp + 40, result);
493
- this.mem.setUint8(sp + 48, 1);
494
- } catch (err) {
495
- sp = this._inst.exports.getsp() >>> 0; // see comment above
496
- storeValue(sp + 40, err);
497
- this.mem.setUint8(sp + 48, 0);
498
- }
499
- },
500
-
501
- // func valueLength(v ref) int
502
- 'syscall/js.valueLength': sp => {
503
- sp >>>= 0;
504
- setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
505
- },
506
-
507
- // valuePrepareString(v ref) (ref, int)
508
- 'syscall/js.valuePrepareString': sp => {
509
- sp >>>= 0;
510
- const str = encoder.encode(String(loadValue(sp + 8)));
511
- storeValue(sp + 16, str);
512
- setInt64(sp + 24, str.length);
513
- },
514
-
515
- // valueLoadString(v ref, b []byte)
516
- 'syscall/js.valueLoadString': sp => {
517
- sp >>>= 0;
518
- const str = loadValue(sp + 8);
519
- loadSlice(sp + 16).set(str);
520
- },
521
-
522
- // func valueInstanceOf(v ref, t ref) bool
523
- 'syscall/js.valueInstanceOf': sp => {
524
- sp >>>= 0;
525
- this.mem.setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0);
526
- },
527
-
528
- // func copyBytesToGo(dst []byte, src ref) (int, bool)
529
- 'syscall/js.copyBytesToGo': sp => {
530
- sp >>>= 0;
531
- const dst = loadSlice(sp + 8);
532
- const src = loadValue(sp + 32);
533
- if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
534
- this.mem.setUint8(sp + 48, 0);
535
- return;
536
- }
537
- const toCopy = src.subarray(0, dst.length);
538
- dst.set(toCopy);
539
- setInt64(sp + 40, toCopy.length);
540
- this.mem.setUint8(sp + 48, 1);
541
- },
542
-
543
- // func copyBytesToJS(dst ref, src []byte) (int, bool)
544
- 'syscall/js.copyBytesToJS': sp => {
545
- sp >>>= 0;
546
- const dst = loadValue(sp + 8);
547
- const src = loadSlice(sp + 16);
548
- if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
549
- this.mem.setUint8(sp + 48, 0);
550
- return;
551
- }
552
- const toCopy = src.subarray(0, dst.length);
553
- dst.set(toCopy);
554
- setInt64(sp + 40, toCopy.length);
555
- this.mem.setUint8(sp + 48, 1);
556
- },
557
-
558
- debug: value => {
559
- console.log(value);
560
- }
561
- }
562
- };
563
- }
564
-
565
- async run(instance) {
566
- if (!(instance instanceof WebAssembly.Instance)) {
567
- throw new Error('Go.run: WebAssembly.Instance expected');
568
- }
569
- this._inst = instance;
570
- this.mem = new DataView(this._inst.exports.mem.buffer);
571
- this._values = [
572
- // JS values that Go currently has references to, indexed by reference id
573
- NaN,
574
- 0,
575
- null,
576
- true,
577
- false,
578
- globalThis,
579
- this
580
- ];
581
- this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
582
- this._ids = new Map([
583
- // mapping from JS values to reference ids
584
- [0, 1],
585
- [null, 2],
586
- [true, 3],
587
- [false, 4],
588
- [globalThis, 5],
589
- [this, 6]
590
- ]);
591
- this._idPool = []; // unused ids that have been garbage collected
592
- this.exited = false; // whether the Go program has exited
593
-
594
- // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
595
- let offset = 4096;
596
-
597
- const strPtr = str => {
598
- const ptr = offset;
599
- const bytes = encoder.encode(str + '\0');
600
- new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
601
- offset += bytes.length;
602
- if (offset % 8 !== 0) {
603
- offset += 8 - (offset % 8);
604
- }
605
- return ptr;
606
- };
607
-
608
- const argc = this.argv.length;
609
-
610
- const argvPtrs = [];
611
- this.argv.forEach(arg => {
612
- argvPtrs.push(strPtr(arg));
613
- });
614
- argvPtrs.push(0);
615
-
616
- const keys = Object.keys(this.env).sort();
617
- keys.forEach(key => {
618
- argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
619
- });
620
- argvPtrs.push(0);
621
-
622
- const argv = offset;
623
- argvPtrs.forEach(ptr => {
624
- this.mem.setUint32(offset, ptr, true);
625
- this.mem.setUint32(offset + 4, 0, true);
626
- offset += 8;
627
- });
628
-
629
- // The linker guarantees global data starts from at least wasmMinDataAddr.
630
- // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
631
- const wasmMinDataAddr = 4096 + 8192;
632
- if (offset >= wasmMinDataAddr) {
633
- throw new Error('total length of command line and environment variables exceeds limit');
634
- }
635
-
636
- this._inst.exports.run(argc, argv);
637
- if (this.exited) {
638
- this._resolveExitPromise();
639
- }
640
- await this._exitPromise;
641
- }
642
-
643
- _resume() {
644
- if (this.exited) {
645
- throw new Error('Go program has already exited');
646
- }
647
- this._inst.exports.resume();
648
- if (this.exited) {
649
- this._resolveExitPromise();
650
- }
651
- }
652
-
653
- _makeFuncWrapper(id) {
654
- const go = this;
655
- return function () {
656
- const event = { id: id, this: this, args: arguments };
657
- go._pendingEvent = event;
658
- go._resume();
659
- return event.result;
660
- };
661
- }
662
- };
663
- })();
664
-
665
- ((function () {
666
- try {
667
- if (typeof WebAssembly === 'object' && typeof WebAssembly.instantiate === 'function') {
668
- const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
669
- if (module instanceof WebAssembly.Module) {
670
- return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
671
- }
672
- }
673
- } catch (e) {}
674
- return false;
675
- }))();
676
-
677
- const go = new Go();
678
-
679
- function loadLocusLibrary() {
680
- return new Promise((resolve, reject) => {
681
- WebAssembly.instantiate(wasm, go.importObject)
682
- .then(result => {
683
- go.run(result.instance);
684
- resolve();
685
- })
686
- .catch(err => {
687
- console.error('err:', err);
688
- reject();
689
- });
690
- });
35
+ /* eslint-disable */
36
+
37
+
38
+ function toUint8Array(s) {
39
+ if (typeof atob === "function")
40
+ return new Uint8Array(atob(s).split("").map(charCodeAt));
41
+ // return (require('buf' + 'fer').Buffer).from(s, 'base64')
42
+ }
43
+
44
+ function charCodeAt(c) {
45
+ return c.charCodeAt(0);
46
+ }
47
+
48
+ const wasmJson = require("./lclib.json");
49
+ var wasm = toUint8Array(wasmJson.Base64);
50
+
51
+ (() => {
52
+ const enosys = () => {
53
+ const err = new Error("not implemented");
54
+ err.code = "ENOSYS";
55
+ return err;
56
+ };
57
+
58
+ if (!globalThis.fs) {
59
+ let outputBuf = "";
60
+ globalThis.fs = {
61
+ constants: {
62
+ O_WRONLY: -1,
63
+ O_RDWR: -1,
64
+ O_CREAT: -1,
65
+ O_TRUNC: -1,
66
+ O_APPEND: -1,
67
+ O_EXCL: -1,
68
+ }, // unused
69
+ writeSync(fd, buf) {
70
+ outputBuf += decoder.decode(buf);
71
+ const nl = outputBuf.lastIndexOf("\n");
72
+ if (nl != -1) {
73
+ console.log(outputBuf.substring(0, nl));
74
+ outputBuf = outputBuf.substring(nl + 1);
75
+ }
76
+ return buf.length;
77
+ },
78
+ write(fd, buf, offset, length, position, callback) {
79
+ if (offset !== 0 || length !== buf.length || position !== null) {
80
+ callback(enosys());
81
+ return;
82
+ }
83
+ const n = this.writeSync(fd, buf);
84
+ callback(null, n);
85
+ },
86
+ chmod(path, mode, callback) {
87
+ callback(enosys());
88
+ },
89
+ chown(path, uid, gid, callback) {
90
+ callback(enosys());
91
+ },
92
+ close(fd, callback) {
93
+ callback(enosys());
94
+ },
95
+ fchmod(fd, mode, callback) {
96
+ callback(enosys());
97
+ },
98
+ fchown(fd, uid, gid, callback) {
99
+ callback(enosys());
100
+ },
101
+ fstat(fd, callback) {
102
+ callback(enosys());
103
+ },
104
+ fsync(fd, callback) {
105
+ callback(null);
106
+ },
107
+ ftruncate(fd, length, callback) {
108
+ callback(enosys());
109
+ },
110
+ lchown(path, uid, gid, callback) {
111
+ callback(enosys());
112
+ },
113
+ link(path, link, callback) {
114
+ callback(enosys());
115
+ },
116
+ lstat(path, callback) {
117
+ callback(enosys());
118
+ },
119
+ mkdir(path, perm, callback) {
120
+ callback(enosys());
121
+ },
122
+ open(path, flags, mode, callback) {
123
+ callback(enosys());
124
+ },
125
+ read(fd, buffer, offset, length, position, callback) {
126
+ callback(enosys());
127
+ },
128
+ readdir(path, callback) {
129
+ callback(enosys());
130
+ },
131
+ readlink(path, callback) {
132
+ callback(enosys());
133
+ },
134
+ rename(from, to, callback) {
135
+ callback(enosys());
136
+ },
137
+ rmdir(path, callback) {
138
+ callback(enosys());
139
+ },
140
+ stat(path, callback) {
141
+ callback(enosys());
142
+ },
143
+ symlink(path, link, callback) {
144
+ callback(enosys());
145
+ },
146
+ truncate(path, length, callback) {
147
+ callback(enosys());
148
+ },
149
+ unlink(path, callback) {
150
+ callback(enosys());
151
+ },
152
+ utimes(path, atime, mtime, callback) {
153
+ callback(enosys());
154
+ },
155
+ };
156
+ }
157
+
158
+ // if (!globalThis.process) {
159
+ // globalThis.process = {
160
+ // getuid() {
161
+ // return -1;
162
+ // },
163
+ // getgid() {
164
+ // return -1;
165
+ // },
166
+ // geteuid() {
167
+ // return -1;
168
+ // },
169
+ // getegid() {
170
+ // return -1;
171
+ // },
172
+ // getgroups() {
173
+ // throw enosys();
174
+ // },
175
+ // pid: -1,
176
+ // ppid: -1,
177
+ // umask() {
178
+ // throw enosys();
179
+ // },
180
+ // cwd() {
181
+ // throw enosys();
182
+ // },
183
+ // chdir() {
184
+ // throw enosys();
185
+ // }
186
+ // };
187
+ // }
188
+
189
+ if (!globalThis.crypto) {
190
+ throw new Error(
191
+ "globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"
192
+ );
193
+ }
194
+
195
+ if (!globalThis.performance) {
196
+ throw new Error(
197
+ "globalThis.performance is not available, polyfill required (performance.now only)"
198
+ );
199
+ }
200
+
201
+ if (!globalThis.TextEncoder) {
202
+ throw new Error(
203
+ "globalThis.TextEncoder is not available, polyfill required"
204
+ );
205
+ }
206
+
207
+ if (!globalThis.TextDecoder) {
208
+ throw new Error(
209
+ "globalThis.TextDecoder is not available, polyfill required"
210
+ );
211
+ }
212
+
213
+ const encoder = new TextEncoder("utf-8");
214
+ const decoder = new TextDecoder("utf-8");
215
+
216
+ globalThis.Go = class {
217
+ constructor() {
218
+ this.argv = ["js"];
219
+ this.env = {};
220
+ this.exit = (code) => {
221
+ if (code !== 0) {
222
+ console.warn("exit code:", code);
223
+ }
224
+ };
225
+ this._exitPromise = new Promise((resolve) => {
226
+ this._resolveExitPromise = resolve;
227
+ });
228
+ this._pendingEvent = null;
229
+ this._scheduledTimeouts = new Map();
230
+ this._nextCallbackTimeoutID = 1;
231
+
232
+ const setInt64 = (addr, v) => {
233
+ this.mem.setUint32(addr + 0, v, true);
234
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
235
+ };
236
+
237
+ const getInt64 = (addr) => {
238
+ const low = this.mem.getUint32(addr + 0, true);
239
+ const high = this.mem.getInt32(addr + 4, true);
240
+ return low + high * 4294967296;
241
+ };
242
+
243
+ const loadValue = (addr) => {
244
+ const f = this.mem.getFloat64(addr, true);
245
+ if (f === 0) {
246
+ return undefined;
247
+ }
248
+ if (!isNaN(f)) {
249
+ return f;
250
+ }
251
+
252
+ const id = this.mem.getUint32(addr, true);
253
+ return this._values[id];
254
+ };
255
+
256
+ const storeValue = (addr, v) => {
257
+ const nanHead = 0x7ff80000;
258
+
259
+ if (typeof v === "number" && v !== 0) {
260
+ if (isNaN(v)) {
261
+ this.mem.setUint32(addr + 4, nanHead, true);
262
+ this.mem.setUint32(addr, 0, true);
263
+ return;
264
+ }
265
+ this.mem.setFloat64(addr, v, true);
266
+ return;
267
+ }
268
+
269
+ if (v === undefined) {
270
+ this.mem.setFloat64(addr, 0, true);
271
+ return;
272
+ }
273
+
274
+ let id = this._ids.get(v);
275
+ if (id === undefined) {
276
+ id = this._idPool.pop();
277
+ if (id === undefined) {
278
+ id = this._values.length;
279
+ }
280
+ this._values[id] = v;
281
+ this._goRefCounts[id] = 0;
282
+ this._ids.set(v, id);
283
+ }
284
+ this._goRefCounts[id]++;
285
+ let typeFlag = 0;
286
+ switch (typeof v) {
287
+ case "object":
288
+ if (v !== null) {
289
+ typeFlag = 1;
290
+ }
291
+ break;
292
+ case "string":
293
+ typeFlag = 2;
294
+ break;
295
+ case "symbol":
296
+ typeFlag = 3;
297
+ break;
298
+ case "function":
299
+ typeFlag = 4;
300
+ break;
301
+ }
302
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
303
+ this.mem.setUint32(addr, id, true);
304
+ };
305
+
306
+ const loadSlice = (addr) => {
307
+ const array = getInt64(addr + 0);
308
+ const len = getInt64(addr + 8);
309
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
310
+ };
311
+
312
+ const loadSliceOfValues = (addr) => {
313
+ const array = getInt64(addr + 0);
314
+ const len = getInt64(addr + 8);
315
+ const a = new Array(len);
316
+ for (let i = 0; i < len; i++) {
317
+ a[i] = loadValue(array + i * 8);
318
+ }
319
+ return a;
320
+ };
321
+
322
+ const loadString = (addr) => {
323
+ const saddr = getInt64(addr + 0);
324
+ const len = getInt64(addr + 8);
325
+ return decoder.decode(
326
+ new DataView(this._inst.exports.mem.buffer, saddr, len)
327
+ );
328
+ };
329
+
330
+ const timeOrigin = Date.now() - performance.now();
331
+ this.importObject = {
332
+ _gotest: {
333
+ add: (a, b) => a + b,
334
+ },
335
+ gojs: {
336
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
337
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
338
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
339
+ // This changes the SP, thus we have to update the SP used by the imported function.
340
+
341
+ // func wasmExit(code int32)
342
+ "runtime.wasmExit": (sp) => {
343
+ sp >>>= 0;
344
+ const code = this.mem.getInt32(sp + 8, true);
345
+ this.exited = true;
346
+ delete this._inst;
347
+ delete this._values;
348
+ delete this._goRefCounts;
349
+ delete this._ids;
350
+ delete this._idPool;
351
+ this.exit(code);
352
+ },
353
+
354
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
355
+ "runtime.wasmWrite": (sp) => {
356
+ sp >>>= 0;
357
+ const fd = getInt64(sp + 8);
358
+ const p = getInt64(sp + 16);
359
+ const n = this.mem.getInt32(sp + 24, true);
360
+ fs.writeSync(
361
+ fd,
362
+ new Uint8Array(this._inst.exports.mem.buffer, p, n)
363
+ );
364
+ },
365
+
366
+ // func resetMemoryDataView()
367
+ "runtime.resetMemoryDataView": (sp) => {
368
+ this.mem = new DataView(this._inst.exports.mem.buffer);
369
+ },
370
+
371
+ // func nanotime1() int64
372
+ "runtime.nanotime1": (sp) => {
373
+ sp >>>= 0;
374
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
375
+ },
376
+
377
+ // func walltime() (sec int64, nsec int32)
378
+ "runtime.walltime": (sp) => {
379
+ sp >>>= 0;
380
+ const msec = new Date().getTime();
381
+ setInt64(sp + 8, msec / 1000);
382
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
383
+ },
384
+
385
+ // func scheduleTimeoutEvent(delay int64) int32
386
+ "runtime.scheduleTimeoutEvent": (sp) => {
387
+ sp >>>= 0;
388
+ const id = this._nextCallbackTimeoutID;
389
+ this._nextCallbackTimeoutID++;
390
+ this._scheduledTimeouts.set(
391
+ id,
392
+ setTimeout(() => {
393
+ this._resume();
394
+ while (this._scheduledTimeouts.has(id)) {
395
+ // for some reason Go failed to register the timeout event, log and try again
396
+ // (temporary workaround for https://github.com/golang/go/issues/28975)
397
+ console.warn("scheduleTimeoutEvent: missed timeout event");
398
+ this._resume();
399
+ }
400
+ }, getInt64(sp + 8))
401
+ );
402
+ this.mem.setInt32(sp + 16, id, true);
403
+ },
404
+
405
+ // func clearTimeoutEvent(id int32)
406
+ "runtime.clearTimeoutEvent": (sp) => {
407
+ sp >>>= 0;
408
+ const id = this.mem.getInt32(sp + 8, true);
409
+ clearTimeout(this._scheduledTimeouts.get(id));
410
+ this._scheduledTimeouts.delete(id);
411
+ },
412
+
413
+ // func getRandomData(r []byte)
414
+ "runtime.getRandomData": (sp) => {
415
+ sp >>>= 0;
416
+ crypto.getRandomValues(loadSlice(sp + 8));
417
+ },
418
+
419
+ // func finalizeRef(v ref)
420
+ "syscall/js.finalizeRef": (sp) => {
421
+ sp >>>= 0;
422
+ const id = this.mem.getUint32(sp + 8, true);
423
+ this._goRefCounts[id]--;
424
+ if (this._goRefCounts[id] === 0) {
425
+ const v = this._values[id];
426
+ this._values[id] = null;
427
+ this._ids.delete(v);
428
+ this._idPool.push(id);
429
+ }
430
+ },
431
+
432
+ // func stringVal(value string) ref
433
+ "syscall/js.stringVal": (sp) => {
434
+ sp >>>= 0;
435
+ storeValue(sp + 24, loadString(sp + 8));
436
+ },
437
+
438
+ // func valueGet(v ref, p string) ref
439
+ "syscall/js.valueGet": (sp) => {
440
+ sp >>>= 0;
441
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
442
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
443
+ storeValue(sp + 32, result);
444
+ },
445
+
446
+ // func valueSet(v ref, p string, x ref)
447
+ "syscall/js.valueSet": (sp) => {
448
+ sp >>>= 0;
449
+ Reflect.set(
450
+ loadValue(sp + 8),
451
+ loadString(sp + 16),
452
+ loadValue(sp + 32)
453
+ );
454
+ },
455
+
456
+ // func valueDelete(v ref, p string)
457
+ "syscall/js.valueDelete": (sp) => {
458
+ sp >>>= 0;
459
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
460
+ },
461
+
462
+ // func valueIndex(v ref, i int) ref
463
+ "syscall/js.valueIndex": (sp) => {
464
+ sp >>>= 0;
465
+ storeValue(
466
+ sp + 24,
467
+ Reflect.get(loadValue(sp + 8), getInt64(sp + 16))
468
+ );
469
+ },
470
+
471
+ // valueSetIndex(v ref, i int, x ref)
472
+ "syscall/js.valueSetIndex": (sp) => {
473
+ sp >>>= 0;
474
+ Reflect.set(
475
+ loadValue(sp + 8),
476
+ getInt64(sp + 16),
477
+ loadValue(sp + 24)
478
+ );
479
+ },
480
+
481
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
482
+ "syscall/js.valueCall": (sp) => {
483
+ sp >>>= 0;
484
+ try {
485
+ const v = loadValue(sp + 8);
486
+ const m = Reflect.get(v, loadString(sp + 16));
487
+ const args = loadSliceOfValues(sp + 32);
488
+ const result = Reflect.apply(m, v, args);
489
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
490
+ storeValue(sp + 56, result);
491
+ this.mem.setUint8(sp + 64, 1);
492
+ } catch (err) {
493
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
494
+ storeValue(sp + 56, err);
495
+ this.mem.setUint8(sp + 64, 0);
496
+ }
497
+ },
498
+
499
+ // func valueInvoke(v ref, args []ref) (ref, bool)
500
+ "syscall/js.valueInvoke": (sp) => {
501
+ sp >>>= 0;
502
+ try {
503
+ const v = loadValue(sp + 8);
504
+ const args = loadSliceOfValues(sp + 16);
505
+ const result = Reflect.apply(v, undefined, args);
506
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
507
+ storeValue(sp + 40, result);
508
+ this.mem.setUint8(sp + 48, 1);
509
+ } catch (err) {
510
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
511
+ storeValue(sp + 40, err);
512
+ this.mem.setUint8(sp + 48, 0);
513
+ }
514
+ },
515
+
516
+ // func valueNew(v ref, args []ref) (ref, bool)
517
+ "syscall/js.valueNew": (sp) => {
518
+ sp >>>= 0;
519
+ try {
520
+ const v = loadValue(sp + 8);
521
+ const args = loadSliceOfValues(sp + 16);
522
+ const result = Reflect.construct(v, args);
523
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
524
+ storeValue(sp + 40, result);
525
+ this.mem.setUint8(sp + 48, 1);
526
+ } catch (err) {
527
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
528
+ storeValue(sp + 40, err);
529
+ this.mem.setUint8(sp + 48, 0);
530
+ }
531
+ },
532
+
533
+ // func valueLength(v ref) int
534
+ "syscall/js.valueLength": (sp) => {
535
+ sp >>>= 0;
536
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
537
+ },
538
+
539
+ // valuePrepareString(v ref) (ref, int)
540
+ "syscall/js.valuePrepareString": (sp) => {
541
+ sp >>>= 0;
542
+ const str = encoder.encode(String(loadValue(sp + 8)));
543
+ storeValue(sp + 16, str);
544
+ setInt64(sp + 24, str.length);
545
+ },
546
+
547
+ // valueLoadString(v ref, b []byte)
548
+ "syscall/js.valueLoadString": (sp) => {
549
+ sp >>>= 0;
550
+ const str = loadValue(sp + 8);
551
+ loadSlice(sp + 16).set(str);
552
+ },
553
+
554
+ // func valueInstanceOf(v ref, t ref) bool
555
+ "syscall/js.valueInstanceOf": (sp) => {
556
+ sp >>>= 0;
557
+ this.mem.setUint8(
558
+ sp + 24,
559
+ loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0
560
+ );
561
+ },
562
+
563
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
564
+ "syscall/js.copyBytesToGo": (sp) => {
565
+ sp >>>= 0;
566
+ const dst = loadSlice(sp + 8);
567
+ const src = loadValue(sp + 32);
568
+ if (
569
+ !(src instanceof Uint8Array || src instanceof Uint8ClampedArray)
570
+ ) {
571
+ this.mem.setUint8(sp + 48, 0);
572
+ return;
573
+ }
574
+ const toCopy = src.subarray(0, dst.length);
575
+ dst.set(toCopy);
576
+ setInt64(sp + 40, toCopy.length);
577
+ this.mem.setUint8(sp + 48, 1);
578
+ },
579
+
580
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
581
+ "syscall/js.copyBytesToJS": (sp) => {
582
+ sp >>>= 0;
583
+ const dst = loadValue(sp + 8);
584
+ const src = loadSlice(sp + 16);
585
+ if (
586
+ !(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)
587
+ ) {
588
+ this.mem.setUint8(sp + 48, 0);
589
+ return;
590
+ }
591
+ const toCopy = src.subarray(0, dst.length);
592
+ dst.set(toCopy);
593
+ setInt64(sp + 40, toCopy.length);
594
+ this.mem.setUint8(sp + 48, 1);
595
+ },
596
+
597
+ debug: (value) => {
598
+ console.log(value);
599
+ },
600
+ },
601
+ };
602
+ }
603
+
604
+ async run(instance) {
605
+ if (!(instance instanceof WebAssembly.Instance)) {
606
+ throw new Error("Go.run: WebAssembly.Instance expected");
607
+ }
608
+ this._inst = instance;
609
+ this.mem = new DataView(this._inst.exports.mem.buffer);
610
+ this._values = [
611
+ // JS values that Go currently has references to, indexed by reference id
612
+ NaN,
613
+ 0,
614
+ null,
615
+ true,
616
+ false,
617
+ globalThis,
618
+ this,
619
+ ];
620
+ this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
621
+ this._ids = new Map([
622
+ // mapping from JS values to reference ids
623
+ [0, 1],
624
+ [null, 2],
625
+ [true, 3],
626
+ [false, 4],
627
+ [globalThis, 5],
628
+ [this, 6],
629
+ ]);
630
+ this._idPool = []; // unused ids that have been garbage collected
631
+ this.exited = false; // whether the Go program has exited
632
+
633
+ // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
634
+ let offset = 4096;
635
+
636
+ const strPtr = (str) => {
637
+ const ptr = offset;
638
+ const bytes = encoder.encode(str + "\0");
639
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
640
+ offset += bytes.length;
641
+ if (offset % 8 !== 0) {
642
+ offset += 8 - (offset % 8);
643
+ }
644
+ return ptr;
645
+ };
646
+
647
+ const argc = this.argv.length;
648
+
649
+ const argvPtrs = [];
650
+ this.argv.forEach((arg) => {
651
+ argvPtrs.push(strPtr(arg));
652
+ });
653
+ argvPtrs.push(0);
654
+
655
+ const keys = Object.keys(this.env).sort();
656
+ keys.forEach((key) => {
657
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
658
+ });
659
+ argvPtrs.push(0);
660
+
661
+ const argv = offset;
662
+ argvPtrs.forEach((ptr) => {
663
+ this.mem.setUint32(offset, ptr, true);
664
+ this.mem.setUint32(offset + 4, 0, true);
665
+ offset += 8;
666
+ });
667
+
668
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
669
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
670
+ const wasmMinDataAddr = 4096 + 8192;
671
+ if (offset >= wasmMinDataAddr) {
672
+ throw new Error(
673
+ "total length of command line and environment variables exceeds limit"
674
+ );
675
+ }
676
+
677
+ this._inst.exports.run(argc, argv);
678
+ if (this.exited) {
679
+ this._resolveExitPromise();
680
+ }
681
+ await this._exitPromise;
682
+ }
683
+
684
+ _resume() {
685
+ if (this.exited) {
686
+ throw new Error("Go program has already exited");
687
+ }
688
+ this._inst.exports.resume();
689
+ if (this.exited) {
690
+ this._resolveExitPromise();
691
+ }
692
+ }
693
+
694
+ _makeFuncWrapper(id) {
695
+ const go = this;
696
+ return function () {
697
+ const event = { id: id, this: this, args: arguments };
698
+ go._pendingEvent = event;
699
+ go._resume();
700
+ return event.result;
701
+ };
702
+ }
703
+ };
704
+ })();
705
+
706
+ ((function () {
707
+ try {
708
+ if (
709
+ typeof WebAssembly === "object" &&
710
+ typeof WebAssembly.instantiate === "function"
711
+ ) {
712
+ const module = new WebAssembly.Module(
713
+ Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
714
+ );
715
+ if (module instanceof WebAssembly.Module) {
716
+ return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
717
+ }
718
+ }
719
+ } catch (e) {}
720
+ return false;
721
+ }))();
722
+
723
+ const go = new Go();
724
+ let locuschainLibLoaded = false;
725
+ function loadLocusLibrary() {
726
+ return new Promise((resolve, reject) => {
727
+ WebAssembly.instantiate(wasm, go.importObject)
728
+ .then((result) => {
729
+ go.run(result.instance);
730
+ locuschainLibLoaded = true;
731
+ resolve();
732
+ })
733
+ .catch((err) => {
734
+ console.error("err:", err);
735
+ reject();
736
+ });
737
+ });
691
738
  }
692
739
 
693
- loadLocusLibrary().then((res) => {
694
- });
740
+ loadLocusLibrary().then((res) => { });
695
741
  class LocusLib {
696
742
  static callWasm(wasmCmd, wasmParams) {
697
743
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
744
+ if (!locuschainLibLoaded) {
745
+ for (let i = 0; i < 1000; i++) {
746
+ yield LocusLib.sleep(100);
747
+ if (locuschainLibLoaded) {
748
+ break;
749
+ }
750
+ }
751
+ }
698
752
  if (Array.isArray(wasmParams)) {
699
753
  wasmParams = wasmParams[0];
700
754
  }
701
755
  // @ts-ignore
702
- if (typeof globalThis['CallLclib'] !== "function")
756
+ if (typeof globalThis["CallLclib"] !== "function")
703
757
  return;
704
758
  // @ts-ignore
705
- const resultString = yield globalThis['CallLclib'](wasmCmd, JSON.stringify(wasmParams));
759
+ const resultString = yield globalThis["CallLclib"](wasmCmd, JSON.stringify(wasmParams));
706
760
  const result = JSON.parse(resultString);
707
761
  if (result.Error && result.Error.Code != 0) {
708
762
  const error = Object.assign(Object.assign({}, result.Error), { message: result.Error.Message });
@@ -715,124 +769,146 @@ class LocusLib {
715
769
  }));
716
770
  }
717
771
  //=================================================================================
772
+ static GetLibInfo() {
773
+ return wasmJson;
774
+ }
718
775
  static GetLibraryVersions() {
719
- return LocusLib.callWasm('GetLibraryVersions', {});
776
+ return LocusLib.callWasm("GetLibraryVersions", {});
720
777
  }
721
778
  static CreateAccountAndKeystore(passwordMaster, passwordNormal, mkeyAlgoNormal, nkeyAlgoNormal) {
722
- return LocusLib.callWasm('CreateAccountAndKeystore', {
779
+ return LocusLib.callWasm("CreateAccountAndKeystore", {
723
780
  passwordMaster,
724
781
  passwordNormal,
725
782
  mkeyAlgoNormal,
726
- nkeyAlgoNormal
783
+ nkeyAlgoNormal,
727
784
  });
728
785
  }
729
786
  static CreateNormalKey(addrStr, keyAlgo) {
730
- return LocusLib.callWasm('CreateNormalKey', { addrStr, keyAlgo });
787
+ return LocusLib.callWasm("CreateNormalKey", { addrStr, keyAlgo });
731
788
  }
732
789
  static CreateMasterKeystore(AddrStr, Password, MskStr, MpkStr) {
733
- return LocusLib.callWasm('CreateMasterKeystore', { AddrStr, Password, MskStr, MpkStr });
790
+ return LocusLib.callWasm("CreateMasterKeystore", {
791
+ AddrStr,
792
+ Password,
793
+ MskStr,
794
+ MpkStr,
795
+ });
734
796
  }
735
- static CreateNormalKeystore(AddrStr, Password, NskStr, NpkStr, MpkStr = '', KeySign = '') {
736
- return LocusLib.callWasm('CreateNormalKeystore', { AddrStr, Password, NskStr, NpkStr, MpkStr, KeySign });
797
+ static CreateNormalKeystore(AddrStr, Password, NskStr, NpkStr, MpkStr = "", KeySign = "") {
798
+ return LocusLib.callWasm("CreateNormalKeystore", {
799
+ AddrStr,
800
+ Password,
801
+ NskStr,
802
+ NpkStr,
803
+ MpkStr,
804
+ KeySign,
805
+ });
737
806
  }
738
807
  static LoadMasterKeystore(PassStr, KsJson) {
739
- return LocusLib.callWasm('LoadMasterKeystore', { PassStr, KsJson });
808
+ return LocusLib.callWasm("LoadMasterKeystore", { PassStr, KsJson });
740
809
  }
741
810
  static LoadNormalKeystore(PassStr, KsJson) {
742
- return LocusLib.callWasm('LoadNormalKeystore', { PassStr, KsJson });
811
+ return LocusLib.callWasm("LoadNormalKeystore", { PassStr, KsJson });
743
812
  }
744
813
  static ConvertToData(value, valueType) {
745
- return LocusLib.callWasm('ConvertToData', { value, valueType });
814
+ return LocusLib.callWasm("ConvertToData", { value, valueType });
746
815
  }
747
816
  static ConvertDataTo(dataString, retType) {
748
- return LocusLib.callWasm('ConvertDataTo', { dataString, retType });
817
+ return LocusLib.callWasm("ConvertDataTo", { dataString, retType });
749
818
  }
750
819
  static ConvertStringToData(str) {
751
- return LocusLib.callWasm('ConvertStringToData', str);
820
+ return LocusLib.callWasm("ConvertStringToData", str);
752
821
  }
753
822
  static ConvertDataToString(dataString) {
754
- return LocusLib.callWasm('ConvertDataToString', dataString);
823
+ return LocusLib.callWasm("ConvertDataToString", dataString);
755
824
  }
756
825
  static ConvertAddressToHex(addr) {
757
- return LocusLib.callWasm('ConvertAddressToHex', addr);
826
+ return LocusLib.callWasm("ConvertAddressToHex", addr);
758
827
  }
759
828
  static ConvertHexToAddress(hexString) {
760
- return LocusLib.callWasm('ConvertHexToAddress', hexString);
829
+ return LocusLib.callWasm("ConvertHexToAddress", hexString);
761
830
  }
762
831
  static ConvertAddressToData(addr) {
763
- return LocusLib.callWasm('ConvertAddressToData', addr);
832
+ return LocusLib.callWasm("ConvertAddressToData", addr);
764
833
  }
765
834
  static ConvertDataToAddress(dataString) {
766
- return LocusLib.callWasm('ConvertDataToAddress', dataString);
835
+ return LocusLib.callWasm("ConvertDataToAddress", dataString);
767
836
  }
768
837
  static ConvertBase32ToHex(encoded) {
769
- return LocusLib.callWasm('ConvertBase32ToHex', encoded);
838
+ return LocusLib.callWasm("ConvertBase32ToHex", encoded);
770
839
  }
771
840
  static ConvertHexToBase32(hexString) {
772
- return LocusLib.callWasm('ConvertHexToBase32', hexString);
841
+ return LocusLib.callWasm("ConvertHexToBase32", hexString);
773
842
  }
774
843
  static ConvertHexToData(encoded) {
775
- return LocusLib.callWasm('ConvertHexToData', encoded);
844
+ return LocusLib.callWasm("ConvertHexToData", encoded);
776
845
  }
777
846
  static ConvertDataToHex(dataString) {
778
- return LocusLib.callWasm('ConvertDataToHex', dataString);
847
+ return LocusLib.callWasm("ConvertDataToHex", dataString);
779
848
  }
780
849
  static ConvertBase32ToData(encoded) {
781
- return LocusLib.callWasm('ConvertBase32ToData', encoded);
850
+ return LocusLib.callWasm("ConvertBase32ToData", encoded);
782
851
  }
783
852
  static ConvertDataToBase32(dataString) {
784
- return LocusLib.callWasm('ConvertDataToBase32', dataString);
853
+ return LocusLib.callWasm("ConvertDataToBase32", dataString);
785
854
  }
786
855
  static SignByMasterKey(Msk, Message) {
787
- return LocusLib.callWasm('SignByMasterKey', { Msk, Message });
856
+ return LocusLib.callWasm("SignByMasterKey", { Msk, Message });
788
857
  }
789
858
  static Sign(Sk, Message) {
790
- return LocusLib.callWasm('Sign', { Sk, Message });
859
+ return LocusLib.callWasm("Sign", { Sk, Message });
791
860
  }
792
861
  static Verify(Pk, Message, Sig) {
793
- return LocusLib.callWasm('Verify', { Pk, Message, Sig });
862
+ return LocusLib.callWasm("Verify", { Pk, Message, Sig });
794
863
  }
795
864
  static VerifyTx(jsonTx) {
796
- return LocusLib.callWasm('VerifyTx', jsonTx);
865
+ return LocusLib.callWasm("VerifyTx", jsonTx);
797
866
  }
798
867
  static CompileCoreScript(Code) {
799
- return LocusLib.callWasm('CompileCoreScript', { Code });
868
+ return LocusLib.callWasm("CompileCoreScript", { Code });
800
869
  }
801
870
  static DisCompileCoreScript(Code) {
802
- return LocusLib.callWasm('DisCompileCoreScript', { Code });
871
+ return LocusLib.callWasm("DisCompileCoreScript", { Code });
803
872
  }
804
873
  static TestCoreScript(ScriptProvide, ScriptAccept, TxDataProvide, TxDataAccept) {
805
- return LocusLib.callWasm('TestCoreScript', {
874
+ return LocusLib.callWasm("TestCoreScript", {
806
875
  ScriptProvide,
807
876
  ScriptAccept,
808
877
  TxDataProvide,
809
- TxDataAccept
878
+ TxDataAccept,
810
879
  });
811
880
  }
812
881
  static GetDefFromCoreScript(Code) {
813
- return LocusLib.callWasm('GetDefFromCoreScript', { Code });
882
+ return LocusLib.callWasm("GetDefFromCoreScript", { Code });
814
883
  }
815
884
  static EncodeTxNumber(Number, Type) {
816
- return LocusLib.callWasm('EncodeTxNumber', { Number, Type });
885
+ return LocusLib.callWasm("EncodeTxNumber", { Number, Type });
817
886
  }
818
887
  static EncodeTxCurrency(Currency) {
819
- return LocusLib.callWasm('EncodeTxCurrency', { Currency });
888
+ return LocusLib.callWasm("EncodeTxCurrency", { Currency });
820
889
  }
821
890
  static Hash(Data, Type) {
822
- return LocusLib.callWasm('Hash', { Data, Type });
891
+ return LocusLib.callWasm("Hash", { Data, Type });
823
892
  }
824
893
  static VerifyMerkleProof(Proof, Hash, GoalHash) {
825
- return LocusLib.callWasm('VerifyMerkleProof', { Proof, Hash, GoalHash });
894
+ return LocusLib.callWasm("VerifyMerkleProof", { Proof, Hash, GoalHash });
826
895
  }
827
896
  static CalculateTxLinkHash(Tx) {
828
- return LocusLib.callWasm('CalculateTxLinkHash', { Tx });
897
+ return LocusLib.callWasm("CalculateTxLinkHash", { Tx });
829
898
  }
830
899
  static DecodeTxs(Txs) {
831
- return LocusLib.callWasm('DecodeTxs', { Txs });
900
+ return LocusLib.callWasm("DecodeTxs", { Txs });
832
901
  }
833
902
  static GzipAndEncode(str) {
834
- return LocusLib.callWasm('GzipAndEncode', str);
903
+ return LocusLib.callWasm("GzipAndEncode", str);
904
+ }
905
+ static GenerateMnemonic(entropyBit = 256) {
906
+ return LocusLib.callWasm("GenerateMnemonic", { entropyBit });
907
+ }
908
+ static DeriveKeysFromMnemonic(mnemonic, path) {
909
+ return LocusLib.callWasm("DeriveKeysFromMnemonic", { mnemonic, path });
835
910
  }
836
911
  }
912
+ LocusLib.sleep = (ms) => new Promise((resolve) => setTimeout(() => resolve(true), ms));
837
913
 
838
914
  exports.LocusLib = LocusLib;