@abraca/wiki 2.33.0 → 2.35.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.
@@ -1,12 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import wtf from "wtf_wikipedia";
3
3
  import wtfApiPlugin from "wtf-plugin-api";
4
- import { DocumentManager } from "@abraca/dabra";
5
- import * as ed from "@noble/ed25519";
6
- import { mkdir, readFile, writeFile } from "node:fs/promises";
7
- import { existsSync } from "node:fs";
8
- import { homedir } from "node:os";
9
- import { dirname, join } from "node:path";
4
+ import { DocumentManager, loadOrCreateKeyFile, loginOrRegisterWithKey } from "@abraca/dabra";
10
5
 
11
6
  //#region packages/wiki/src/parser.ts
12
7
  /**
@@ -389,675 +384,41 @@ function rewriteLinks(markdown, titleToDocId) {
389
384
  });
390
385
  }
391
386
 
392
- //#endregion
393
- //#region node_modules/@noble/hashes/utils.js
394
- /**
395
- * Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
396
- * @param a - value to test
397
- * @returns `true` when the value is a Uint8Array-compatible view.
398
- * @example
399
- * Check whether a value is a Uint8Array-compatible view.
400
- * ```ts
401
- * isBytes(new Uint8Array([1, 2, 3]));
402
- * ```
403
- */
404
- function isBytes(a) {
405
- return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
406
- }
407
- /**
408
- * Asserts something is Uint8Array.
409
- * @param value - value to validate
410
- * @param length - optional exact length constraint
411
- * @param title - label included in thrown errors
412
- * @returns The validated byte array.
413
- * @throws On wrong argument types. {@link TypeError}
414
- * @throws On wrong argument ranges or values. {@link RangeError}
415
- * @example
416
- * Validate that a value is a byte array.
417
- * ```ts
418
- * abytes(new Uint8Array([1, 2, 3]));
419
- * ```
420
- */
421
- function abytes(value, length, title = "") {
422
- const bytes = isBytes(value);
423
- const len = value?.length;
424
- const needsLen = length !== void 0;
425
- if (!bytes || needsLen && len !== length) {
426
- const prefix = title && `"${title}" `;
427
- const ofLen = needsLen ? ` of length ${length}` : "";
428
- const got = bytes ? `length=${len}` : `type=${typeof value}`;
429
- const message = prefix + "expected Uint8Array" + ofLen + ", got " + got;
430
- if (!bytes) throw new TypeError(message);
431
- throw new RangeError(message);
432
- }
433
- return value;
434
- }
435
- /**
436
- * Asserts a hash instance has not been destroyed or finished.
437
- * @param instance - hash instance to validate
438
- * @param checkFinished - whether to reject finalized instances
439
- * @throws If the hash instance has already been destroyed or finalized. {@link Error}
440
- * @example
441
- * Validate that a hash instance is still usable.
442
- * ```ts
443
- * import { aexists } from '@noble/hashes/utils.js';
444
- * import { sha256 } from '@noble/hashes/sha2.js';
445
- * const hash = sha256.create();
446
- * aexists(hash);
447
- * ```
448
- */
449
- function aexists(instance, checkFinished = true) {
450
- if (instance.destroyed) throw new Error("Hash instance has been destroyed");
451
- if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
452
- }
453
- /**
454
- * Asserts output is a sufficiently-sized byte array.
455
- * @param out - destination buffer
456
- * @param instance - hash instance providing output length
457
- * Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
458
- * @throws On wrong argument types. {@link TypeError}
459
- * @throws On wrong argument ranges or values. {@link RangeError}
460
- * @example
461
- * Validate a caller-provided digest buffer.
462
- * ```ts
463
- * import { aoutput } from '@noble/hashes/utils.js';
464
- * import { sha256 } from '@noble/hashes/sha2.js';
465
- * const hash = sha256.create();
466
- * aoutput(new Uint8Array(hash.outputLen), hash);
467
- * ```
468
- */
469
- function aoutput(out, instance) {
470
- abytes(out, void 0, "digestInto() output");
471
- const min = instance.outputLen;
472
- if (out.length < min) throw new RangeError("\"digestInto() output\" expected to be of length >=" + min);
473
- }
474
- /**
475
- * Zeroizes typed arrays in place. Warning: JS provides no guarantees.
476
- * @param arrays - arrays to overwrite with zeros
477
- * @example
478
- * Zeroize sensitive buffers in place.
479
- * ```ts
480
- * clean(new Uint8Array([1, 2, 3]));
481
- * ```
482
- */
483
- function clean(...arrays) {
484
- for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
485
- }
486
- /**
487
- * Creates a DataView for byte-level manipulation.
488
- * @param arr - source typed array
489
- * @returns DataView over the same buffer region.
490
- * @example
491
- * Create a DataView over an existing buffer.
492
- * ```ts
493
- * createView(new Uint8Array(4));
494
- * ```
495
- */
496
- function createView(arr) {
497
- return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
498
- }
499
- /** Whether the current platform is little-endian. */
500
- const isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
501
- const hasHexBuiltin = typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function";
502
- /**
503
- * Creates a callable hash function from a stateful class constructor.
504
- * @param hashCons - hash constructor or factory
505
- * @param info - optional metadata such as DER OID
506
- * @returns Frozen callable hash wrapper with `.create()`.
507
- * Wrapper construction eagerly calls `hashCons(undefined)` once to read
508
- * `outputLen` / `blockLen`, so constructor side effects happen at module
509
- * init time.
510
- * @example
511
- * Wrap a stateful hash constructor into a callable helper.
512
- * ```ts
513
- * import { createHasher } from '@noble/hashes/utils.js';
514
- * import { sha256 } from '@noble/hashes/sha2.js';
515
- * const wrapped = createHasher(sha256.create, { oid: sha256.oid });
516
- * wrapped(new Uint8Array([1]));
517
- * ```
518
- */
519
- function createHasher(hashCons, info = {}) {
520
- const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
521
- const tmp = hashCons(void 0);
522
- hashC.outputLen = tmp.outputLen;
523
- hashC.blockLen = tmp.blockLen;
524
- hashC.canXOF = tmp.canXOF;
525
- hashC.create = (opts) => hashCons(opts);
526
- Object.assign(hashC, info);
527
- return Object.freeze(hashC);
528
- }
529
- /**
530
- * Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
531
- * @param suffix - final OID byte for the selected hash.
532
- * The helper accepts any byte even though only the documented NIST hash
533
- * suffixes are meaningful downstream.
534
- * @returns Object containing the DER-encoded OID.
535
- * @example
536
- * Build OID metadata for a NIST hash.
537
- * ```ts
538
- * oidNist(0x01);
539
- * ```
540
- */
541
- const oidNist = (suffix) => ({ oid: Uint8Array.from([
542
- 6,
543
- 9,
544
- 96,
545
- 134,
546
- 72,
547
- 1,
548
- 101,
549
- 3,
550
- 4,
551
- 2,
552
- suffix
553
- ]) });
554
-
555
- //#endregion
556
- //#region node_modules/@noble/hashes/_md.js
557
- /**
558
- * Internal Merkle-Damgard hash utils.
559
- * @module
560
- */
561
- /**
562
- * Merkle-Damgard hash construction base class.
563
- * Could be used to create MD5, RIPEMD, SHA1, SHA2.
564
- * Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
565
- * strings with partial-byte tails.
566
- * @param blockLen - internal block size in bytes
567
- * @param outputLen - digest size in bytes
568
- * @param padOffset - trailing length field size in bytes
569
- * @param isLE - whether length and state words are encoded in little-endian
570
- * @example
571
- * Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
572
- * ```ts
573
- * import { _SHA1 } from '@noble/hashes/legacy.js';
574
- * const hash = new _SHA1();
575
- * hash.update(new Uint8Array([97, 98, 99]));
576
- * hash.digest();
577
- * ```
578
- */
579
- var HashMD = class {
580
- blockLen;
581
- outputLen;
582
- canXOF = false;
583
- padOffset;
584
- isLE;
585
- buffer;
586
- view;
587
- finished = false;
588
- length = 0;
589
- pos = 0;
590
- destroyed = false;
591
- constructor(blockLen, outputLen, padOffset, isLE) {
592
- this.blockLen = blockLen;
593
- this.outputLen = outputLen;
594
- this.padOffset = padOffset;
595
- this.isLE = isLE;
596
- this.buffer = new Uint8Array(blockLen);
597
- this.view = createView(this.buffer);
598
- }
599
- update(data) {
600
- aexists(this);
601
- abytes(data);
602
- const { view, buffer, blockLen } = this;
603
- const len = data.length;
604
- for (let pos = 0; pos < len;) {
605
- const take = Math.min(blockLen - this.pos, len - pos);
606
- if (take === blockLen) {
607
- const dataView = createView(data);
608
- for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
609
- continue;
610
- }
611
- buffer.set(data.subarray(pos, pos + take), this.pos);
612
- this.pos += take;
613
- pos += take;
614
- if (this.pos === blockLen) {
615
- this.process(view, 0);
616
- this.pos = 0;
617
- }
618
- }
619
- this.length += data.length;
620
- this.roundClean();
621
- return this;
622
- }
623
- digestInto(out) {
624
- aexists(this);
625
- aoutput(out, this);
626
- this.finished = true;
627
- const { buffer, view, blockLen, isLE } = this;
628
- let { pos } = this;
629
- buffer[pos++] = 128;
630
- clean(this.buffer.subarray(pos));
631
- if (this.padOffset > blockLen - pos) {
632
- this.process(view, 0);
633
- pos = 0;
634
- }
635
- for (let i = pos; i < blockLen; i++) buffer[i] = 0;
636
- view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
637
- this.process(view, 0);
638
- const oview = createView(out);
639
- const len = this.outputLen;
640
- if (len % 4) throw new Error("_sha2: outputLen must be aligned to 32bit");
641
- const outLen = len / 4;
642
- const state = this.get();
643
- if (outLen > state.length) throw new Error("_sha2: outputLen bigger than state");
644
- for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
645
- }
646
- digest() {
647
- const { buffer, outputLen } = this;
648
- this.digestInto(buffer);
649
- const res = buffer.slice(0, outputLen);
650
- this.destroy();
651
- return res;
652
- }
653
- _cloneInto(to) {
654
- to ||= new this.constructor();
655
- to.set(...this.get());
656
- const { blockLen, buffer, length, finished, destroyed, pos } = this;
657
- to.destroyed = destroyed;
658
- to.finished = finished;
659
- to.length = length;
660
- to.pos = pos;
661
- if (length % blockLen) to.buffer.set(buffer);
662
- return to;
663
- }
664
- clone() {
665
- return this._cloneInto();
666
- }
667
- };
668
- /** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
669
- * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
670
- * eight prime numbers. Exported as a shared table; callers must treat it as read-only because
671
- * constructors copy halves from it by index. */
672
- const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
673
- 1779033703,
674
- 4089235720,
675
- 3144134277,
676
- 2227873595,
677
- 1013904242,
678
- 4271175723,
679
- 2773480762,
680
- 1595750129,
681
- 1359893119,
682
- 2917565137,
683
- 2600822924,
684
- 725511199,
685
- 528734635,
686
- 4215389547,
687
- 1541459225,
688
- 327033209
689
- ]);
690
-
691
- //#endregion
692
- //#region node_modules/@noble/hashes/_u64.js
693
- const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
694
- const _32n = /* @__PURE__ */ BigInt(32);
695
- function fromBig(n, le = false) {
696
- if (le) return {
697
- h: Number(n & U32_MASK64),
698
- l: Number(n >> _32n & U32_MASK64)
699
- };
700
- return {
701
- h: Number(n >> _32n & U32_MASK64) | 0,
702
- l: Number(n & U32_MASK64) | 0
703
- };
704
- }
705
- function split(lst, le = false) {
706
- const len = lst.length;
707
- let Ah = new Uint32Array(len);
708
- let Al = new Uint32Array(len);
709
- for (let i = 0; i < len; i++) {
710
- const { h, l } = fromBig(lst[i], le);
711
- [Ah[i], Al[i]] = [h, l];
712
- }
713
- return [Ah, Al];
714
- }
715
- const shrSH = (h, _l, s) => h >>> s;
716
- const shrSL = (h, l, s) => h << 32 - s | l >>> s;
717
- const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
718
- const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
719
- const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
720
- const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
721
- function add(Ah, Al, Bh, Bl) {
722
- const l = (Al >>> 0) + (Bl >>> 0);
723
- return {
724
- h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
725
- l: l | 0
726
- };
727
- }
728
- const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
729
- const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
730
- const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
731
- const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
732
- const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
733
- const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
734
-
735
- //#endregion
736
- //#region node_modules/@noble/hashes/sha2.js
737
- /**
738
- * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
739
- * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
740
- * Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
741
- * {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
742
- * @module
743
- */
744
- const K512 = split([
745
- "0x428a2f98d728ae22",
746
- "0x7137449123ef65cd",
747
- "0xb5c0fbcfec4d3b2f",
748
- "0xe9b5dba58189dbbc",
749
- "0x3956c25bf348b538",
750
- "0x59f111f1b605d019",
751
- "0x923f82a4af194f9b",
752
- "0xab1c5ed5da6d8118",
753
- "0xd807aa98a3030242",
754
- "0x12835b0145706fbe",
755
- "0x243185be4ee4b28c",
756
- "0x550c7dc3d5ffb4e2",
757
- "0x72be5d74f27b896f",
758
- "0x80deb1fe3b1696b1",
759
- "0x9bdc06a725c71235",
760
- "0xc19bf174cf692694",
761
- "0xe49b69c19ef14ad2",
762
- "0xefbe4786384f25e3",
763
- "0x0fc19dc68b8cd5b5",
764
- "0x240ca1cc77ac9c65",
765
- "0x2de92c6f592b0275",
766
- "0x4a7484aa6ea6e483",
767
- "0x5cb0a9dcbd41fbd4",
768
- "0x76f988da831153b5",
769
- "0x983e5152ee66dfab",
770
- "0xa831c66d2db43210",
771
- "0xb00327c898fb213f",
772
- "0xbf597fc7beef0ee4",
773
- "0xc6e00bf33da88fc2",
774
- "0xd5a79147930aa725",
775
- "0x06ca6351e003826f",
776
- "0x142929670a0e6e70",
777
- "0x27b70a8546d22ffc",
778
- "0x2e1b21385c26c926",
779
- "0x4d2c6dfc5ac42aed",
780
- "0x53380d139d95b3df",
781
- "0x650a73548baf63de",
782
- "0x766a0abb3c77b2a8",
783
- "0x81c2c92e47edaee6",
784
- "0x92722c851482353b",
785
- "0xa2bfe8a14cf10364",
786
- "0xa81a664bbc423001",
787
- "0xc24b8b70d0f89791",
788
- "0xc76c51a30654be30",
789
- "0xd192e819d6ef5218",
790
- "0xd69906245565a910",
791
- "0xf40e35855771202a",
792
- "0x106aa07032bbd1b8",
793
- "0x19a4c116b8d2d0c8",
794
- "0x1e376c085141ab53",
795
- "0x2748774cdf8eeb99",
796
- "0x34b0bcb5e19b48a8",
797
- "0x391c0cb3c5c95a63",
798
- "0x4ed8aa4ae3418acb",
799
- "0x5b9cca4f7763e373",
800
- "0x682e6ff3d6b2b8a3",
801
- "0x748f82ee5defb2fc",
802
- "0x78a5636f43172f60",
803
- "0x84c87814a1f0ab72",
804
- "0x8cc702081a6439ec",
805
- "0x90befffa23631e28",
806
- "0xa4506cebde82bde9",
807
- "0xbef9a3f7b2c67915",
808
- "0xc67178f2e372532b",
809
- "0xca273eceea26619c",
810
- "0xd186b8c721c0c207",
811
- "0xeada7dd6cde0eb1e",
812
- "0xf57d4f7fee6ed178",
813
- "0x06f067aa72176fba",
814
- "0x0a637dc5a2c898a6",
815
- "0x113f9804bef90dae",
816
- "0x1b710b35131c471b",
817
- "0x28db77f523047d84",
818
- "0x32caab7b40c72493",
819
- "0x3c9ebe0a15c9bebc",
820
- "0x431d67c49c100d4c",
821
- "0x4cc5d4becb3e42b6",
822
- "0x597f299cfc657e2a",
823
- "0x5fcb6fab3ad6faec",
824
- "0x6c44198c4a475817"
825
- ].map((n) => BigInt(n)));
826
- const SHA512_Kh = K512[0];
827
- const SHA512_Kl = K512[1];
828
- const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
829
- const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
830
- /** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
831
- var SHA2_64B = class extends HashMD {
832
- constructor(outputLen) {
833
- super(128, outputLen, 16, false);
834
- }
835
- get() {
836
- const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
837
- return [
838
- Ah,
839
- Al,
840
- Bh,
841
- Bl,
842
- Ch,
843
- Cl,
844
- Dh,
845
- Dl,
846
- Eh,
847
- El,
848
- Fh,
849
- Fl,
850
- Gh,
851
- Gl,
852
- Hh,
853
- Hl
854
- ];
855
- }
856
- set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
857
- this.Ah = Ah | 0;
858
- this.Al = Al | 0;
859
- this.Bh = Bh | 0;
860
- this.Bl = Bl | 0;
861
- this.Ch = Ch | 0;
862
- this.Cl = Cl | 0;
863
- this.Dh = Dh | 0;
864
- this.Dl = Dl | 0;
865
- this.Eh = Eh | 0;
866
- this.El = El | 0;
867
- this.Fh = Fh | 0;
868
- this.Fl = Fl | 0;
869
- this.Gh = Gh | 0;
870
- this.Gl = Gl | 0;
871
- this.Hh = Hh | 0;
872
- this.Hl = Hl | 0;
873
- }
874
- process(view, offset) {
875
- for (let i = 0; i < 16; i++, offset += 4) {
876
- SHA512_W_H[i] = view.getUint32(offset);
877
- SHA512_W_L[i] = view.getUint32(offset += 4);
878
- }
879
- for (let i = 16; i < 80; i++) {
880
- const W15h = SHA512_W_H[i - 15] | 0;
881
- const W15l = SHA512_W_L[i - 15] | 0;
882
- const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
883
- const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
884
- const W2h = SHA512_W_H[i - 2] | 0;
885
- const W2l = SHA512_W_L[i - 2] | 0;
886
- const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
887
- const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
888
- const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
889
- SHA512_W_H[i] = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]) | 0;
890
- SHA512_W_L[i] = SUMl | 0;
891
- }
892
- let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
893
- for (let i = 0; i < 80; i++) {
894
- const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
895
- const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
896
- const CHIh = Eh & Fh ^ ~Eh & Gh;
897
- const CHIl = El & Fl ^ ~El & Gl;
898
- const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
899
- const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
900
- const T1l = T1ll | 0;
901
- const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
902
- const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
903
- const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
904
- const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
905
- Hh = Gh | 0;
906
- Hl = Gl | 0;
907
- Gh = Fh | 0;
908
- Gl = Fl | 0;
909
- Fh = Eh | 0;
910
- Fl = El | 0;
911
- ({h: Eh, l: El} = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
912
- Dh = Ch | 0;
913
- Dl = Cl | 0;
914
- Ch = Bh | 0;
915
- Cl = Bl | 0;
916
- Bh = Ah | 0;
917
- Bl = Al | 0;
918
- const All = add3L(T1l, sigma0l, MAJl);
919
- Ah = add3H(All, T1h, sigma0h, MAJh);
920
- Al = All | 0;
921
- }
922
- ({h: Ah, l: Al} = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
923
- ({h: Bh, l: Bl} = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
924
- ({h: Ch, l: Cl} = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
925
- ({h: Dh, l: Dl} = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
926
- ({h: Eh, l: El} = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
927
- ({h: Fh, l: Fl} = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
928
- ({h: Gh, l: Gl} = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
929
- ({h: Hh, l: Hl} = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
930
- this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
931
- }
932
- roundClean() {
933
- clean(SHA512_W_H, SHA512_W_L);
934
- }
935
- destroy() {
936
- this.destroyed = true;
937
- clean(this.buffer);
938
- this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
939
- }
940
- };
941
- /** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
942
- var _SHA512 = class extends SHA2_64B {
943
- Ah = SHA512_IV[0] | 0;
944
- Al = SHA512_IV[1] | 0;
945
- Bh = SHA512_IV[2] | 0;
946
- Bl = SHA512_IV[3] | 0;
947
- Ch = SHA512_IV[4] | 0;
948
- Cl = SHA512_IV[5] | 0;
949
- Dh = SHA512_IV[6] | 0;
950
- Dl = SHA512_IV[7] | 0;
951
- Eh = SHA512_IV[8] | 0;
952
- El = SHA512_IV[9] | 0;
953
- Fh = SHA512_IV[10] | 0;
954
- Fl = SHA512_IV[11] | 0;
955
- Gh = SHA512_IV[12] | 0;
956
- Gl = SHA512_IV[13] | 0;
957
- Hh = SHA512_IV[14] | 0;
958
- Hl = SHA512_IV[15] | 0;
959
- constructor() {
960
- super(64);
961
- }
962
- };
963
- /**
964
- * SHA2-512 hash function from RFC 4634.
965
- * @param msg - message bytes to hash
966
- * @returns Digest bytes.
967
- * @example
968
- * Hash a message with SHA2-512.
969
- * ```ts
970
- * sha512(new Uint8Array([97, 98, 99]));
971
- * ```
972
- */
973
- const sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(), /* @__PURE__ */ oidNist(3));
974
-
975
- //#endregion
976
- //#region packages/wiki/src/crypto.ts
977
- /**
978
- * Ed25519 key generation, persistence, and challenge signing for CLI auth.
979
- * Mirrors @abraca/mcp/src/crypto.ts — standalone to avoid MCP SDK dependency.
980
- */
981
- ed.hashes.sha512 = sha512;
982
- ed.hashes.sha512Async = (m) => Promise.resolve(sha512(m));
983
- const DEFAULT_KEY_PATH = join(homedir(), ".abracadabra", "cli.key");
984
- function toBase64url(bytes) {
985
- return Buffer.from(bytes).toString("base64url");
986
- }
987
- function fromBase64url(b64) {
988
- return new Uint8Array(Buffer.from(b64, "base64url"));
989
- }
990
- /**
991
- * Load an existing Ed25519 keypair from disk, or generate and persist a new one.
992
- * The file stores the raw 32-byte private key seed.
993
- */
994
- async function loadOrCreateKeypair(keyPath) {
995
- const path = keyPath || DEFAULT_KEY_PATH;
996
- if (existsSync(path)) {
997
- const seed = await readFile(path);
998
- if (seed.length !== 32) throw new Error(`Invalid key file at ${path}: expected 32 bytes, got ${seed.length}`);
999
- const privateKey = new Uint8Array(seed);
1000
- return {
1001
- privateKey,
1002
- publicKeyB64: toBase64url(ed.getPublicKey(privateKey))
1003
- };
1004
- }
1005
- const privateKey = ed.utils.randomSecretKey();
1006
- const publicKey = ed.getPublicKey(privateKey);
1007
- const dir = dirname(path);
1008
- if (!existsSync(dir)) await mkdir(dir, {
1009
- recursive: true,
1010
- mode: 448
1011
- });
1012
- await writeFile(path, Buffer.from(privateKey), { mode: 384 });
1013
- console.error(`[abracadabra] Generated new keypair at ${path}`);
1014
- console.error(`[abracadabra] Public key: ${toBase64url(publicKey)}`);
1015
- return {
1016
- privateKey,
1017
- publicKeyB64: toBase64url(publicKey)
1018
- };
1019
- }
1020
- /** Sign a base64url challenge with the private key; returns base64url signature. */
1021
- function signChallenge(challengeB64, privateKey) {
1022
- const challenge = fromBase64url(challengeB64);
1023
- return toBase64url(ed.sign(challenge, privateKey));
1024
- }
1025
-
1026
387
  //#endregion
1027
388
  //#region packages/wiki/src/connect.ts
1028
389
  /**
1029
390
  * Open a DocumentManager session for the wiki command, mirroring the
1030
391
  * auth/register flow that CLIConnection uses but using the modern public API.
1031
392
  *
1032
- * Reuses the CLI's Ed25519 keypair handling (loadOrCreateKeypair, signChallenge)
1033
- * so the wiki command authenticates with the same identity as every other
1034
- * subcommand.
393
+ * Uses the shared `@abraca/dabra` Node identity helpers (loadOrCreateKeyFile,
394
+ * loginOrRegisterWithKey) against `~/.abracadabra/cli.key`, so the wiki command
395
+ * authenticates with the same identity as every other CLI subcommand.
1035
396
  */
1036
397
  async function openSession(config) {
1037
- const keypair = await loadOrCreateKeypair(config.keyFile);
1038
- const sign = (challenge) => Promise.resolve(signChallenge(challenge, keypair.privateKey));
398
+ const keypair = await loadOrCreateKeyFile(config.keyFile, {
399
+ defaultFileName: "cli.key",
400
+ onCreate: ({ path, publicKeyB64 }) => {
401
+ if (!config.quiet) {
402
+ console.error(`[abracadabra] Generated new keypair at ${path}`);
403
+ console.error(`[abracadabra] Public key: ${publicKeyB64}`);
404
+ }
405
+ }
406
+ });
1039
407
  const dm = new DocumentManager({
1040
408
  url: config.url,
1041
409
  name: config.name ?? "Wiki Extractor",
1042
410
  color: config.color,
1043
411
  quiet: config.quiet
1044
412
  });
1045
- try {
1046
- await dm.client.loginWithKey(keypair.publicKeyB64, sign);
1047
- } catch (err) {
1048
- const status = err?.status ?? err?.response?.status;
1049
- if (status === 404 || status === 422) {
413
+ await loginOrRegisterWithKey(dm.client, keypair, {
414
+ username: (config.name ?? "wiki-extractor").replace(/\s+/g, "-").toLowerCase(),
415
+ displayName: config.name ?? "Wiki Extractor",
416
+ deviceName: "CLI Wiki",
417
+ inviteCode: config.inviteCode,
418
+ onRegister: () => {
1050
419
  if (!config.quiet) console.error("[abracadabra] Key not registered, creating new account...");
1051
- await dm.client.registerWithKey({
1052
- publicKey: keypair.publicKeyB64,
1053
- username: (config.name ?? "wiki-extractor").replace(/\s+/g, "-").toLowerCase(),
1054
- displayName: config.name ?? "Wiki Extractor",
1055
- deviceName: "CLI Wiki",
1056
- inviteCode: config.inviteCode
1057
- });
1058
- await dm.client.loginWithKey(keypair.publicKeyB64, sign);
1059
- } else throw err;
1060
- }
420
+ }
421
+ });
1061
422
  await dm.connect();
1062
423
  const rootDocId = dm.rootDocId;
1063
424
  if (!rootDocId) throw new Error("Connected but no rootDocId — server has no spaces.");