@bsv/sdk 2.1.1 → 2.1.3

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.
Files changed (79) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/auth/Peer.js +21 -18
  3. package/dist/cjs/src/auth/Peer.js.map +1 -1
  4. package/dist/cjs/src/auth/SessionManager.js.map +1 -1
  5. package/dist/cjs/src/auth/clients/AuthFetch.js +4 -1
  6. package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -1
  7. package/dist/cjs/src/identity/ContactsManager.js +44 -6
  8. package/dist/cjs/src/identity/ContactsManager.js.map +1 -1
  9. package/dist/cjs/src/identity/IdentityClient.js +106 -37
  10. package/dist/cjs/src/identity/IdentityClient.js.map +1 -1
  11. package/dist/cjs/src/overlay-tools/LookupResolver.js +180 -82
  12. package/dist/cjs/src/overlay-tools/LookupResolver.js.map +1 -1
  13. package/dist/cjs/src/primitives/Hash.js +173 -50
  14. package/dist/cjs/src/primitives/Hash.js.map +1 -1
  15. package/dist/cjs/src/primitives/SymmetricKey.js +123 -1
  16. package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -1
  17. package/dist/cjs/src/transaction/MerklePath.js +1 -1
  18. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  19. package/dist/esm/src/auth/Peer.js +28 -18
  20. package/dist/esm/src/auth/Peer.js.map +1 -1
  21. package/dist/esm/src/auth/SessionManager.js.map +1 -1
  22. package/dist/esm/src/auth/clients/AuthFetch.js +4 -1
  23. package/dist/esm/src/auth/clients/AuthFetch.js.map +1 -1
  24. package/dist/esm/src/identity/ContactsManager.js +44 -6
  25. package/dist/esm/src/identity/ContactsManager.js.map +1 -1
  26. package/dist/esm/src/identity/IdentityClient.js +106 -37
  27. package/dist/esm/src/identity/IdentityClient.js.map +1 -1
  28. package/dist/esm/src/overlay-tools/LookupResolver.js +180 -82
  29. package/dist/esm/src/overlay-tools/LookupResolver.js.map +1 -1
  30. package/dist/esm/src/primitives/Hash.js +177 -50
  31. package/dist/esm/src/primitives/Hash.js.map +1 -1
  32. package/dist/esm/src/primitives/SymmetricKey.js +123 -1
  33. package/dist/esm/src/primitives/SymmetricKey.js.map +1 -1
  34. package/dist/esm/src/transaction/MerklePath.js +1 -1
  35. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  36. package/dist/types/src/auth/Peer.d.ts +3 -3
  37. package/dist/types/src/auth/Peer.d.ts.map +1 -1
  38. package/dist/types/src/auth/SessionManager.d.ts +21 -0
  39. package/dist/types/src/auth/SessionManager.d.ts.map +1 -1
  40. package/dist/types/src/auth/clients/AuthFetch.d.ts +2 -2
  41. package/dist/types/src/auth/clients/AuthFetch.d.ts.map +1 -1
  42. package/dist/types/src/identity/ContactsManager.d.ts +13 -2
  43. package/dist/types/src/identity/ContactsManager.d.ts.map +1 -1
  44. package/dist/types/src/identity/IdentityClient.d.ts +50 -24
  45. package/dist/types/src/identity/IdentityClient.d.ts.map +1 -1
  46. package/dist/types/src/overlay-tools/LookupResolver.d.ts +15 -1
  47. package/dist/types/src/overlay-tools/LookupResolver.d.ts.map +1 -1
  48. package/dist/types/src/primitives/Hash.d.ts +21 -16
  49. package/dist/types/src/primitives/Hash.d.ts.map +1 -1
  50. package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -1
  51. package/dist/types/src/wallet/Wallet.interfaces.d.ts +16 -1
  52. package/dist/types/src/wallet/Wallet.interfaces.d.ts.map +1 -1
  53. package/dist/types/src/wallet/WalletClient.d.ts +1 -1
  54. package/dist/types/src/wallet/WalletClient.d.ts.map +1 -1
  55. package/dist/types/src/wallet/substrates/window.CWI.d.ts +1 -1
  56. package/dist/types/src/wallet/substrates/window.CWI.d.ts.map +1 -1
  57. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  58. package/dist/umd/bundle.js +4 -4
  59. package/package.json +1 -1
  60. package/src/auth/Peer.ts +30 -20
  61. package/src/auth/SessionManager.ts +22 -0
  62. package/src/auth/__tests/Peer.test.ts +47 -1
  63. package/src/auth/clients/AuthFetch.ts +6 -3
  64. package/src/identity/ContactsManager.ts +47 -6
  65. package/src/identity/IdentityClient.ts +137 -53
  66. package/src/identity/__tests/IdentityClient.additional.test.ts +150 -1
  67. package/src/identity/__tests/IdentityClient.test.ts +4 -4
  68. package/src/overlay-tools/LookupResolver.ts +191 -77
  69. package/src/overlay-tools/__tests/LookupResolver.additional.test.ts +90 -0
  70. package/src/primitives/Hash.ts +232 -96
  71. package/src/primitives/SymmetricKey.ts +145 -1
  72. package/src/primitives/__tests/Hash.additional.test.ts +65 -0
  73. package/src/primitives/__tests/Hash.test.ts +6 -1
  74. package/src/script/__tests/Spend.test.ts +45 -4
  75. package/src/transaction/MerklePath.ts +1 -1
  76. package/src/transaction/__tests/Transaction.test.ts +17 -0
  77. package/src/wallet/Wallet.interfaces.ts +16 -1
  78. package/src/wallet/WalletClient.ts +1 -1
  79. package/src/wallet/substrates/window.CWI.ts +1 -1
@@ -308,11 +308,115 @@ function zero8(word) {
308
308
  return word;
309
309
  }
310
310
  }
311
+ const BufferCtor = typeof globalThis === 'undefined' ? undefined : globalThis.Buffer;
312
+ const CAN_USE_BUFFER = BufferCtor != null && typeof BufferCtor.from === 'function';
313
+ const HEX_DIGITS = '0123456789abcdef';
314
+ const HEX_BYTE_STRINGS = new Array(256);
315
+ for (let i = 0; i < HEX_BYTE_STRINGS.length; i++) {
316
+ HEX_BYTE_STRINGS[i] = HEX_DIGITS[(i >> 4) & 0xf] + HEX_DIGITS[i & 0xf];
317
+ }
311
318
  function bytesToHex(data) {
312
- let res = '';
313
- for (const b of data)
314
- res += (b.toString(16).padStart(2, '0'));
315
- return res;
319
+ if (CAN_USE_BUFFER) {
320
+ return BufferCtor.from(data).toString('hex');
321
+ }
322
+ const out = new Array(data.length);
323
+ for (let i = 0; i < data.length; i++)
324
+ out[i] = HEX_BYTE_STRINGS[data[i]];
325
+ return out.join('');
326
+ }
327
+ const NODE_CRYPTO = (() => {
328
+ const processLike = typeof globalThis === 'undefined' ? undefined : globalThis.process;
329
+ const getBuiltinModule = processLike?.getBuiltinModule;
330
+ if (typeof getBuiltinModule === 'function') {
331
+ try {
332
+ const crypto = getBuiltinModule.call(processLike, 'node:crypto');
333
+ if (crypto != null)
334
+ return crypto;
335
+ }
336
+ catch {
337
+ // continue to CommonJS fallback
338
+ }
339
+ }
340
+ try {
341
+ if (typeof require === 'function') {
342
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
343
+ return require('node:crypto');
344
+ }
345
+ }
346
+ catch {
347
+ // node:crypto is unavailable in this runtime
348
+ }
349
+ return undefined;
350
+ })();
351
+ function toHashBytes(msg, enc) {
352
+ if (msg instanceof Uint8Array) {
353
+ return msg;
354
+ }
355
+ if (Array.isArray(msg)) {
356
+ return new Uint8Array(msg);
357
+ }
358
+ return Uint8Array.from(toArray(msg, enc));
359
+ }
360
+ function toHashKeyBytes(key) {
361
+ return typeof key === 'string' ? toHashBytes(key, 'hex') : toHashBytes(key);
362
+ }
363
+ function updateNativeOrFallback(native, fallback, data) {
364
+ if (native != null) {
365
+ native.update(data);
366
+ }
367
+ else if (fallback != null) {
368
+ fallback.update(data);
369
+ }
370
+ }
371
+ function digestNativeOrFallback(native, fallback) {
372
+ if (native != null)
373
+ return Array.from(native.digest());
374
+ if (fallback != null)
375
+ return Array.from(fallback.digest());
376
+ return [];
377
+ }
378
+ function digestHexNativeOrFallback(native, fallback) {
379
+ if (native != null)
380
+ return native.digest('hex');
381
+ if (fallback != null)
382
+ return bytesToHex(fallback.digest());
383
+ return '';
384
+ }
385
+ function createNodeHash(algorithm) {
386
+ const createHash = NODE_CRYPTO?.createHash;
387
+ if (typeof createHash !== 'function')
388
+ return undefined;
389
+ try {
390
+ return createHash(algorithm);
391
+ }
392
+ catch {
393
+ return undefined;
394
+ }
395
+ }
396
+ function createNodeHmac(algorithm, keyBytes) {
397
+ const createHmac = NODE_CRYPTO?.createHmac;
398
+ if (typeof createHmac !== 'function')
399
+ return undefined;
400
+ try {
401
+ return createHmac(algorithm, keyBytes);
402
+ }
403
+ catch {
404
+ return undefined;
405
+ }
406
+ }
407
+ function digestWithNodeHash(algorithm, msg, enc) {
408
+ const hash = createNodeHash(algorithm);
409
+ if (hash == null)
410
+ return undefined;
411
+ hash.update(toHashBytes(msg, enc));
412
+ return hash.digest();
413
+ }
414
+ function digestWithNodeHmac(algorithm, key, msg, enc) {
415
+ const hmac = createNodeHmac(algorithm, toHashKeyBytes(key));
416
+ if (hmac == null)
417
+ return undefined;
418
+ hmac.update(toHashBytes(msg, enc));
419
+ return hmac.digest();
316
420
  }
317
421
  function join32(msg, start, end, endian) {
318
422
  const len = end - start;
@@ -561,19 +665,22 @@ export class RIPEMD160 extends BaseHash {
561
665
  */
562
666
  export class SHA256 {
563
667
  h;
668
+ native;
564
669
  constructor() {
565
- this.h = new FastSHA256();
670
+ this.native = createNodeHash('sha256');
671
+ if (this.native == null) {
672
+ this.h = new FastSHA256();
673
+ }
566
674
  }
567
675
  update(msg, enc) {
568
- const data = msg instanceof Uint8Array ? msg : Uint8Array.from(toArray(msg, enc));
569
- this.h.update(data);
676
+ updateNativeOrFallback(this.native, this.h, toHashBytes(msg, enc));
570
677
  return this;
571
678
  }
572
679
  digest() {
573
- return Array.from(this.h.digest());
680
+ return digestNativeOrFallback(this.native, this.h);
574
681
  }
575
682
  digestHex() {
576
- return bytesToHex(this.h.digest());
683
+ return digestHexNativeOrFallback(this.native, this.h);
577
684
  }
578
685
  }
579
686
  /**
@@ -666,19 +773,22 @@ export class SHA1 extends BaseHash {
666
773
  */
667
774
  export class SHA512 {
668
775
  h;
776
+ native;
669
777
  constructor() {
670
- this.h = new FastSHA512();
778
+ this.native = createNodeHash('sha512');
779
+ if (this.native == null) {
780
+ this.h = new FastSHA512();
781
+ }
671
782
  }
672
783
  update(msg, enc) {
673
- const data = Uint8Array.from(toArray(msg, enc));
674
- this.h.update(data);
784
+ updateNativeOrFallback(this.native, this.h, toHashBytes(msg, enc));
675
785
  return this;
676
786
  }
677
787
  digest() {
678
- return Array.from(this.h.digest());
788
+ return digestNativeOrFallback(this.native, this.h);
679
789
  }
680
790
  digestHex() {
681
- return bytesToHex(this.h.digest());
791
+ return digestHexNativeOrFallback(this.native, this.h);
682
792
  }
683
793
  }
684
794
  /**
@@ -695,6 +805,7 @@ export class SHA512 {
695
805
  */
696
806
  export class SHA256HMAC {
697
807
  h;
808
+ native;
698
809
  blockSize = 64;
699
810
  outSize = 32;
700
811
  /**
@@ -711,10 +822,11 @@ export class SHA256HMAC {
711
822
  * const myHMAC = new SHA256HMAC('deadbeef');
712
823
  */
713
824
  constructor(key) {
714
- const k = key instanceof Uint8Array
715
- ? key
716
- : Uint8Array.from(toArray(key, typeof key === 'string' ? 'hex' : undefined));
717
- this.h = new HMAC(sha256Fast, k);
825
+ const k = toHashKeyBytes(key);
826
+ this.native = createNodeHmac('sha256', k);
827
+ if (this.native == null) {
828
+ this.h = new HMAC(sha256Fast, k);
829
+ }
718
830
  }
719
831
  /**
720
832
  * Updates the `SHA256HMAC` object with part of the message to be hashed.
@@ -728,8 +840,7 @@ export class SHA256HMAC {
728
840
  * myHMAC.update('deadbeef', 'hex');
729
841
  */
730
842
  update(msg, enc) {
731
- const data = msg instanceof Uint8Array ? msg : Uint8Array.from(toArray(msg, enc));
732
- this.h.update(data);
843
+ updateNativeOrFallback(this.native, this.h, toHashBytes(msg, enc));
733
844
  return this;
734
845
  }
735
846
  /**
@@ -742,7 +853,7 @@ export class SHA256HMAC {
742
853
  * let hashedMessage = myHMAC.digest();
743
854
  */
744
855
  digest() {
745
- return Array.from(this.h.digest());
856
+ return digestNativeOrFallback(this.native, this.h);
746
857
  }
747
858
  /**
748
859
  * Finalizes the HMAC computation and returns the resultant hash as a hex string.
@@ -754,7 +865,7 @@ export class SHA256HMAC {
754
865
  * let hashedMessage = myHMAC.digestHex();
755
866
  */
756
867
  digestHex() {
757
- return bytesToHex(this.h.digest());
868
+ return digestHexNativeOrFallback(this.native, this.h);
758
869
  }
759
870
  }
760
871
  export class SHA1HMAC {
@@ -809,6 +920,7 @@ export class SHA1HMAC {
809
920
  */
810
921
  export class SHA512HMAC {
811
922
  h;
923
+ native;
812
924
  blockSize = 128;
813
925
  outSize = 32;
814
926
  /**
@@ -825,10 +937,11 @@ export class SHA512HMAC {
825
937
  * const myHMAC = new SHA512HMAC('deadbeef');
826
938
  */
827
939
  constructor(key) {
828
- const k = key instanceof Uint8Array
829
- ? key
830
- : Uint8Array.from(toArray(key, typeof key === 'string' ? 'hex' : undefined));
831
- this.h = new HMAC(sha512Fast, k);
940
+ const k = toHashKeyBytes(key);
941
+ this.native = createNodeHmac('sha512', k);
942
+ if (this.native == null) {
943
+ this.h = new HMAC(sha512Fast, k);
944
+ }
832
945
  }
833
946
  /**
834
947
  * Updates the `SHA512HMAC` object with part of the message to be hashed.
@@ -842,8 +955,7 @@ export class SHA512HMAC {
842
955
  * myHMAC.update('deadbeef', 'hex');
843
956
  */
844
957
  update(msg, enc) {
845
- const data = msg instanceof Uint8Array ? msg : Uint8Array.from(toArray(msg, enc));
846
- this.h.update(data);
958
+ updateNativeOrFallback(this.native, this.h, toHashBytes(msg, enc));
847
959
  return this;
848
960
  }
849
961
  /**
@@ -856,7 +968,7 @@ export class SHA512HMAC {
856
968
  * let hashedMessage = myHMAC.digest();
857
969
  */
858
970
  digest() {
859
- return Array.from(this.h.digest());
971
+ return digestNativeOrFallback(this.native, this.h);
860
972
  }
861
973
  /**
862
974
  * Finalizes the HMAC computation and returns the resultant hash as a hex string.
@@ -868,9 +980,24 @@ export class SHA512HMAC {
868
980
  * let hashedMessage = myHMAC.digestHex();
869
981
  */
870
982
  digestHex() {
871
- return bytesToHex(this.h.digest());
983
+ return digestHexNativeOrFallback(this.native, this.h);
872
984
  }
873
985
  }
986
+ function sha256Bytes(msg, enc) {
987
+ const native = digestWithNodeHash('sha256', msg, enc);
988
+ if (native != null)
989
+ return native;
990
+ return new FastSHA256().update(toHashBytes(msg, enc)).digest();
991
+ }
992
+ function sha512Bytes(msg, enc) {
993
+ const native = digestWithNodeHash('sha512', msg, enc);
994
+ if (native != null)
995
+ return native;
996
+ return new FastSHA512().update(toHashBytes(msg, enc)).digest();
997
+ }
998
+ function ripemd160Bytes(msg, enc) {
999
+ return digestWithNodeHash('ripemd160', msg, enc);
1000
+ }
874
1001
  /**
875
1002
  * Computes RIPEMD160 hash of a given message.
876
1003
  * @function ripemd160
@@ -883,6 +1010,9 @@ export class SHA512HMAC {
883
1010
  * const digest = ripemd160('Hello, world!');
884
1011
  */
885
1012
  export const ripemd160 = (msg, enc) => {
1013
+ const native = ripemd160Bytes(msg, enc);
1014
+ if (native != null)
1015
+ return Array.from(native);
886
1016
  return new RIPEMD160().update(msg, enc).digest();
887
1017
  };
888
1018
  /**
@@ -911,7 +1041,7 @@ export const sha1 = (msg, enc) => {
911
1041
  * const digest = sha256('Hello, world!');
912
1042
  */
913
1043
  export const sha256 = (msg, enc) => {
914
- return new SHA256().update(msg, enc).digest();
1044
+ return Array.from(sha256Bytes(msg, enc));
915
1045
  };
916
1046
  /**
917
1047
  * Computes SHA512 hash of a given message.
@@ -925,7 +1055,7 @@ export const sha256 = (msg, enc) => {
925
1055
  * const digest = sha512('Hello, world!');
926
1056
  */
927
1057
  export const sha512 = (msg, enc) => {
928
- return new SHA512().update(msg, enc).digest();
1058
+ return Array.from(sha512Bytes(msg, enc));
929
1059
  };
930
1060
  /**
931
1061
  * Performs a 'double hash' using SHA256. This means the data is hashed twice
@@ -941,8 +1071,7 @@ export const sha512 = (msg, enc) => {
941
1071
  * const doubleHash = hash256('Hello, world!');
942
1072
  */
943
1073
  export const hash256 = (msg, enc) => {
944
- const first = new SHA256().update(msg, enc).digest();
945
- return new SHA256().update(first).digest();
1074
+ return Array.from(sha256Bytes(sha256Bytes(msg, enc)));
946
1075
  };
947
1076
  /**
948
1077
  * Computes SHA256 hash of a given message and then computes a RIPEMD160 hash of the result.
@@ -957,7 +1086,10 @@ export const hash256 = (msg, enc) => {
957
1086
  * const hash = hash160('Hello, world!');
958
1087
  */
959
1088
  export const hash160 = (msg, enc) => {
960
- const first = new SHA256().update(msg, enc).digest();
1089
+ const first = sha256Bytes(msg, enc);
1090
+ const native = ripemd160Bytes(first);
1091
+ if (native != null)
1092
+ return Array.from(native);
961
1093
  return new RIPEMD160().update(first).digest();
962
1094
  };
963
1095
  /**
@@ -973,6 +1105,9 @@ export const hash160 = (msg, enc) => {
973
1105
  * const digest = sha256hmac('deadbeef', 'ffff001d');
974
1106
  */
975
1107
  export const sha256hmac = (key, msg, enc) => {
1108
+ const native = digestWithNodeHmac('sha256', key, msg, enc);
1109
+ if (native != null)
1110
+ return Array.from(native);
976
1111
  return new SHA256HMAC(key).update(msg, enc).digest();
977
1112
  };
978
1113
  /**
@@ -988,6 +1123,9 @@ export const sha256hmac = (key, msg, enc) => {
988
1123
  * const digest = sha512hmac('deadbeef', 'ffff001d');
989
1124
  */
990
1125
  export const sha512hmac = (key, msg, enc) => {
1126
+ const native = digestWithNodeHmac('sha512', key, msg, enc);
1127
+ if (native != null)
1128
+ return Array.from(native);
991
1129
  return new SHA512HMAC(key).update(msg, enc).digest();
992
1130
  };
993
1131
  // BEGIN fast-pbkdf2 helpers
@@ -1694,21 +1832,10 @@ export function pbkdf2(password, salt, iterations, keylen, digest = 'sha512') {
1694
1832
  if (digest !== 'sha512') {
1695
1833
  throw new Error('Only sha512 is supported in this PBKDF2 implementation');
1696
1834
  }
1697
- // Attempt to use the native Node.js implementation if available as it is
1698
- // considerably faster than the pure TypeScript fallback below. If the crypto
1699
- // module isn't present (for example in a browser build) we'll silently fall
1700
- // back to the original implementation.
1701
- try {
1702
- // eslint-disable-next-line @typescript-eslint/no-var-requires
1703
- const nodeCrypto = require('node:crypto');
1704
- if (typeof nodeCrypto.pbkdf2Sync === 'function') {
1705
- const p = Buffer.from(password);
1706
- const s = Buffer.from(salt);
1707
- return [...nodeCrypto.pbkdf2Sync(p, s, iterations, keylen, digest)];
1708
- }
1709
- }
1710
- catch {
1711
- // ignore
1835
+ const pbkdf2Sync = NODE_CRYPTO?.pbkdf2Sync;
1836
+ if (typeof pbkdf2Sync === 'function') {
1837
+ const out = pbkdf2Sync(toHashBytes(password), toHashBytes(salt), iterations, keylen, digest);
1838
+ return Array.from(out);
1712
1839
  }
1713
1840
  const p = Uint8Array.from(password);
1714
1841
  const s = Uint8Array.from(salt);