@bsv/sdk 1.6.16 → 1.6.18

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 (111) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/auth/Peer.js +16 -25
  3. package/dist/cjs/src/auth/Peer.js.map +1 -1
  4. package/dist/cjs/src/auth/SessionManager.js +2 -4
  5. package/dist/cjs/src/auth/SessionManager.js.map +1 -1
  6. package/dist/cjs/src/auth/certificates/Certificate.js +2 -4
  7. package/dist/cjs/src/auth/certificates/Certificate.js.map +1 -1
  8. package/dist/cjs/src/auth/certificates/MasterCertificate.js +1 -1
  9. package/dist/cjs/src/auth/certificates/MasterCertificate.js.map +1 -1
  10. package/dist/cjs/src/auth/clients/AuthFetch.js +2 -4
  11. package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -1
  12. package/dist/cjs/src/compat/ECIES.js +1 -1
  13. package/dist/cjs/src/compat/ECIES.js.map +1 -1
  14. package/dist/cjs/src/compat/Mnemonic.js +2 -2
  15. package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
  16. package/dist/cjs/src/identity/IdentityClient.js +1 -1
  17. package/dist/cjs/src/identity/IdentityClient.js.map +1 -1
  18. package/dist/cjs/src/kvstore/LocalKVStore.js +1 -2
  19. package/dist/cjs/src/kvstore/LocalKVStore.js.map +1 -1
  20. package/dist/cjs/src/overlay-tools/LookupResolver.js +10 -8
  21. package/dist/cjs/src/overlay-tools/LookupResolver.js.map +1 -1
  22. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js +9 -10
  23. package/dist/cjs/src/overlay-tools/SHIPBroadcaster.js.map +1 -1
  24. package/dist/cjs/src/primitives/AESGCM.js +1 -2
  25. package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
  26. package/dist/cjs/src/primitives/BigNumber.js +2 -3
  27. package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
  28. package/dist/cjs/src/primitives/Curve.js +2 -3
  29. package/dist/cjs/src/primitives/Curve.js.map +1 -1
  30. package/dist/cjs/src/primitives/ECDSA.js +174 -396
  31. package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
  32. package/dist/cjs/src/primitives/JacobianPoint.js +1 -2
  33. package/dist/cjs/src/primitives/JacobianPoint.js.map +1 -1
  34. package/dist/cjs/src/primitives/Point.js +217 -181
  35. package/dist/cjs/src/primitives/Point.js.map +1 -1
  36. package/dist/cjs/src/primitives/Polynomial.js +1 -1
  37. package/dist/cjs/src/primitives/Polynomial.js.map +1 -1
  38. package/dist/cjs/src/primitives/Random.js +1 -2
  39. package/dist/cjs/src/primitives/Random.js.map +1 -1
  40. package/dist/cjs/src/primitives/TransactionSignature.js +5 -7
  41. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  42. package/dist/cjs/src/primitives/utils.js +1 -2
  43. package/dist/cjs/src/primitives/utils.js.map +1 -1
  44. package/dist/cjs/src/registry/RegistryClient.js +2 -4
  45. package/dist/cjs/src/registry/RegistryClient.js.map +1 -1
  46. package/dist/cjs/src/script/Spend.js +1 -2
  47. package/dist/cjs/src/script/Spend.js.map +1 -1
  48. package/dist/cjs/src/script/templates/P2PKH.js +4 -4
  49. package/dist/cjs/src/script/templates/P2PKH.js.map +1 -1
  50. package/dist/cjs/src/script/templates/PushDrop.js +7 -8
  51. package/dist/cjs/src/script/templates/PushDrop.js.map +1 -1
  52. package/dist/cjs/src/script/templates/RPuzzle.js +7 -6
  53. package/dist/cjs/src/script/templates/RPuzzle.js.map +1 -1
  54. package/dist/cjs/src/storage/StorageDownloader.js +1 -1
  55. package/dist/cjs/src/storage/StorageDownloader.js.map +1 -1
  56. package/dist/cjs/src/storage/StorageUploader.js +6 -9
  57. package/dist/cjs/src/storage/StorageUploader.js.map +1 -1
  58. package/dist/cjs/src/transaction/Beef.js +2 -3
  59. package/dist/cjs/src/transaction/Beef.js.map +1 -1
  60. package/dist/cjs/src/transaction/MerklePath.js +9 -12
  61. package/dist/cjs/src/transaction/MerklePath.js.map +1 -1
  62. package/dist/cjs/src/transaction/Transaction.js +15 -22
  63. package/dist/cjs/src/transaction/Transaction.js.map +1 -1
  64. package/dist/cjs/src/transaction/broadcasters/ARC.js +3 -3
  65. package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -1
  66. package/dist/cjs/src/transaction/broadcasters/Teranode.js +2 -3
  67. package/dist/cjs/src/transaction/broadcasters/Teranode.js.map +1 -1
  68. package/dist/cjs/src/transaction/broadcasters/WhatsOnChainBroadcaster.js +2 -3
  69. package/dist/cjs/src/transaction/broadcasters/WhatsOnChainBroadcaster.js.map +1 -1
  70. package/dist/cjs/src/transaction/chaintrackers/BlockHeadersService.js +2 -2
  71. package/dist/cjs/src/transaction/chaintrackers/BlockHeadersService.js.map +1 -1
  72. package/dist/cjs/src/transaction/chaintrackers/WhatsOnChain.js +2 -2
  73. package/dist/cjs/src/transaction/chaintrackers/WhatsOnChain.js.map +1 -1
  74. package/dist/cjs/src/transaction/http/FetchHttpClient.js +1 -2
  75. package/dist/cjs/src/transaction/http/FetchHttpClient.js.map +1 -1
  76. package/dist/cjs/src/wallet/CachedKeyDeriver.js +1 -1
  77. package/dist/cjs/src/wallet/CachedKeyDeriver.js.map +1 -1
  78. package/dist/cjs/src/wallet/KeyDeriver.js +4 -3
  79. package/dist/cjs/src/wallet/KeyDeriver.js.map +1 -1
  80. package/dist/cjs/src/wallet/ProtoWallet.js +21 -25
  81. package/dist/cjs/src/wallet/ProtoWallet.js.map +1 -1
  82. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js +2 -3
  83. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  84. package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js +1 -1
  85. package/dist/cjs/src/wallet/substrates/HTTPWalletWire.js.map +1 -1
  86. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js +12 -19
  87. package/dist/cjs/src/wallet/substrates/WalletWireTransceiver.js.map +1 -1
  88. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  89. package/dist/esm/src/overlay-tools/LookupResolver.js +4 -0
  90. package/dist/esm/src/overlay-tools/LookupResolver.js.map +1 -1
  91. package/dist/esm/src/primitives/ECDSA.js +174 -395
  92. package/dist/esm/src/primitives/ECDSA.js.map +1 -1
  93. package/dist/esm/src/primitives/Point.js +192 -146
  94. package/dist/esm/src/primitives/Point.js.map +1 -1
  95. package/dist/esm/src/wallet/KeyDeriver.js +3 -1
  96. package/dist/esm/src/wallet/KeyDeriver.js.map +1 -1
  97. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  98. package/dist/types/src/overlay-tools/LookupResolver.d.ts.map +1 -1
  99. package/dist/types/src/primitives/ECDSA.d.ts.map +1 -1
  100. package/dist/types/src/primitives/Point.d.ts.map +1 -1
  101. package/dist/types/src/wallet/KeyDeriver.d.ts.map +1 -1
  102. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  103. package/dist/umd/bundle.js +1 -1
  104. package/docs/reference/primitives.md +165 -377
  105. package/package.json +1 -1
  106. package/src/overlay-tools/LookupResolver.ts +5 -0
  107. package/src/primitives/ECDSA.ts +218 -488
  108. package/src/primitives/Point.ts +212 -162
  109. package/src/transaction/__tests/Transaction.test.ts +1 -1
  110. package/src/wallet/KeyDeriver.ts +2 -1
  111. package/src/wallet/__tests/ProtoWallet.test.ts +46 -1
@@ -5337,257 +5337,67 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5337
5337
 
5338
5338
  ```ts
5339
5339
  sign = (msg: BigNumber, key: BigNumber, forceLowS: boolean = false, customK?: BigNumber | ((iter: number) => BigNumber)): Signature => {
5340
- if (typeof BigInt === "function") {
5341
- const zero = BigInt(0);
5342
- const one = BigInt(1);
5343
- const two = BigInt(2);
5344
- const n = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
5345
- const p = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
5346
- const Gx = BigInt("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798");
5347
- const Gy = BigInt("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8");
5348
- const G = { x: Gx, y: Gy };
5349
- const z = BigInt("0x" + msg.toString(16));
5350
- const d = BigInt("0x" + key.toString(16));
5351
- if (d <= zero || d >= n) {
5352
- throw new Error("Invalid private key");
5340
+ const curve = new Curve();
5341
+ msg = truncateToN(msg);
5342
+ const bytes = curve.n.byteLength();
5343
+ const bkey = key.toArray("be", bytes);
5344
+ const nonce = msg.toArray("be", bytes);
5345
+ const drbg = new DRBG(bkey, nonce);
5346
+ const ns1 = curve.n.subn(1);
5347
+ for (let iter = 0;; iter++) {
5348
+ let k = typeof customK === "function"
5349
+ ? customK(iter)
5350
+ : BigNumber.isBN(customK)
5351
+ ? customK
5352
+ : new BigNumber(drbg.generate(bytes), 16);
5353
+ if (k != null) {
5354
+ k = truncateToN(k, true);
5353
5355
  }
5354
- function bigIntToBytes(value: bigint, length: number): Uint8Array {
5355
- const hex = value.toString(16).padStart(length * 2, "0");
5356
- const bytes = new Uint8Array(length);
5357
- for (let i = 0; i < length; i++) {
5358
- bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
5359
- }
5360
- return bytes;
5356
+ else {
5357
+ throw new Error("k is undefined");
5361
5358
  }
5362
- const bytes = 32;
5363
- const bkey = bigIntToBytes(d, bytes);
5364
- const nonce = bigIntToBytes(z, bytes);
5365
- const drbg = new DRBG(Array.from(bkey), Array.from(nonce));
5366
- const ns1 = n - one;
5367
- let iter = 0;
5368
- function truncateToN(k: bigint, n: bigint, truncOnly: boolean = true): bigint {
5369
- const kBitLength = k.toString(2).length;
5370
- const nBitLength = n.toString(2).length;
5371
- const delta = kBitLength - nBitLength;
5372
- if (delta > 0) {
5373
- k = k >> BigInt(delta);
5374
- }
5375
- if (!truncOnly && k >= n) {
5376
- return k - n;
5359
+ if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0) {
5360
+ if (BigNumber.isBN(customK)) {
5361
+ throw new Error("Invalid fixed custom K value (must be more than 1 and less than N-1)");
5377
5362
  }
5378
5363
  else {
5379
- return k;
5364
+ continue;
5380
5365
  }
5381
5366
  }
5382
- function generateK(customK?: BigNumber | ((iter: number) => BigNumber)): bigint {
5383
- if (typeof customK === "function") {
5384
- const kbn = customK(iter);
5385
- const kstr = kbn.toString(16);
5386
- return BigInt("0x" + kstr);
5387
- }
5388
- else if ((customK != null) && BigNumber.isBN(customK)) {
5389
- const kstr = customK.toString(16);
5390
- return BigInt("0x" + kstr);
5367
+ const kp = curve.g.mul(k);
5368
+ if (kp.isInfinity()) {
5369
+ if (BigNumber.isBN(customK)) {
5370
+ throw new Error("Invalid fixed custom K value (must not create a point at infinity when multiplied by the generator point)");
5391
5371
  }
5392
5372
  else {
5393
- const khex = drbg.generate(bytes);
5394
- return BigInt("0x" + khex);
5395
- }
5396
- }
5397
- function mod(a: bigint, m: bigint): bigint {
5398
- return ((a % m) + m) % m;
5399
- }
5400
- function modInv(a: bigint, m: bigint): bigint {
5401
- let lm = one;
5402
- let hm = zero;
5403
- let low = mod(a, m);
5404
- let high = m;
5405
- while (low > one) {
5406
- const r = high / low;
5407
- const nm = hm - lm * r;
5408
- const neww = high - low * r;
5409
- hm = lm;
5410
- lm = nm;
5411
- high = low;
5412
- low = neww;
5373
+ continue;
5413
5374
  }
5414
- return mod(lm, m);
5415
5375
  }
5416
- function pointAdd(P: {
5417
- x: bigint;
5418
- y: bigint;
5419
- } | null, Q: {
5420
- x: bigint;
5421
- y: bigint;
5422
- } | null): {
5423
- x: bigint;
5424
- y: bigint;
5425
- } | null {
5426
- if (P === null)
5427
- return Q;
5428
- if (Q === null)
5429
- return P;
5430
- if (P.x === Q.x && P.y === mod(-Q.y, p)) {
5431
- return null;
5432
- }
5433
- let m: bigint;
5434
- if (P.x === Q.x && P.y === Q.y) {
5435
- if (P.y === zero) {
5436
- return null;
5437
- }
5438
- const numerator = mod(BigInt(3) * P.x * P.x, p);
5439
- const denominator = modInv(two * P.y, p);
5440
- m = mod(numerator * denominator, p);
5376
+ const kpX = kp.getX();
5377
+ const r = kpX.umod(curve.n);
5378
+ if (r.cmpn(0) === 0) {
5379
+ if (BigNumber.isBN(customK)) {
5380
+ throw new Error("Invalid fixed custom K value (when multiplied by G, the resulting x coordinate mod N must not be zero)");
5441
5381
  }
5442
5382
  else {
5443
- const numerator = mod(Q.y - P.y, p);
5444
- const denominator = modInv(Q.x - P.x, p);
5445
- m = mod(numerator * denominator, p);
5446
- }
5447
- const xR = mod(m * m - P.x - Q.x, p);
5448
- const yR = mod(m * (P.x - xR) - P.y, p);
5449
- return { x: xR, y: yR };
5450
- }
5451
- function scalarMul(k: bigint, P: {
5452
- x: bigint;
5453
- y: bigint;
5454
- }): {
5455
- x: bigint;
5456
- y: bigint;
5457
- } {
5458
- let N = P;
5459
- let Q: {
5460
- x: bigint;
5461
- y: bigint;
5462
- } | null = null;
5463
- while (k > BigInt(0)) {
5464
- if (k % BigInt(2) === BigInt(1)) {
5465
- Q = Q === null ? N : (pointAdd(Q, N) ?? Q);
5466
- }
5467
- N = pointAdd(N, N) ?? N;
5468
- k >>= BigInt(1);
5469
- }
5470
- if (Q === null) {
5471
- throw new Error("Scalar multiplication resulted in an invalid point.");
5472
- }
5473
- return Q;
5474
- }
5475
- let validSignature = false;
5476
- while (!validSignature) {
5477
- iter += 1;
5478
- validSignature = true;
5479
- iter += 1;
5480
- let k = generateK(customK);
5481
- k = truncateToN(k, n, true);
5482
- if (k <= one || k >= ns1) {
5483
- if (customK instanceof BigNumber) {
5484
- throw new Error("Invalid fixed custom K value (must be more than 1 and less than N-1)");
5485
- }
5486
- else {
5487
- continue;
5488
- }
5489
- }
5490
- const R = scalarMul(k, G);
5491
- if (R === null) {
5492
- if (customK instanceof BigNumber) {
5493
- throw new Error("Invalid fixed custom K value (must not create a point at infinity when multiplied by the generator point)");
5494
- }
5495
- else {
5496
- continue;
5497
- }
5498
- }
5499
- const r = mod(R.x, n);
5500
- if (r === zero) {
5501
- if (customK instanceof BigNumber) {
5502
- throw new Error("Invalid fixed custom K value (when multiplied by G, the resulting x coordinate mod N must not be zero)");
5503
- }
5504
- else {
5505
- continue;
5506
- }
5383
+ continue;
5507
5384
  }
5508
- const kInv = modInv(k, n);
5509
- const rd = mod(r * d, n);
5510
- let s = mod(kInv * (z + rd), n);
5511
- if (s === zero) {
5512
- if (customK instanceof BigNumber) {
5513
- throw new Error("Invalid fixed custom K value (when used with the key, it cannot create a zero value for S)");
5514
- }
5515
- else {
5516
- continue;
5517
- }
5518
- }
5519
- if (forceLowS && s > n / two) {
5520
- s = n - s;
5521
- }
5522
- const rbn = new BigNumber(r.toString(16), 16);
5523
- const sbn = new BigNumber(s.toString(16), 16);
5524
- return new Signature(rbn, sbn);
5525
5385
  }
5526
- }
5527
- else {
5528
- const curve = new Curve();
5529
- msg = truncateToN(msg);
5530
- const bytes = curve.n.byteLength();
5531
- const bkey = key.toArray("be", bytes);
5532
- const nonce = msg.toArray("be", bytes);
5533
- const drbg = new DRBG(bkey, nonce);
5534
- const ns1 = curve.n.subn(1);
5535
- for (let iter = 0;; iter++) {
5536
- let k = typeof customK === "function"
5537
- ? customK(iter)
5538
- : BigNumber.isBN(customK)
5539
- ? customK
5540
- : new BigNumber(drbg.generate(bytes), 16);
5541
- if (k != null) {
5542
- k = truncateToN(k, true);
5386
+ let s = k.invm(curve.n).mul(r.mul(key).iadd(msg));
5387
+ s = s.umod(curve.n);
5388
+ if (s.cmpn(0) === 0) {
5389
+ if (BigNumber.isBN(customK)) {
5390
+ throw new Error("Invalid fixed custom K value (when used with the key, it cannot create a zero value for S)");
5543
5391
  }
5544
5392
  else {
5545
- throw new Error("k is undefined");
5546
- }
5547
- if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0) {
5548
- if (BigNumber.isBN(customK)) {
5549
- throw new Error("Invalid fixed custom K value (must be more than 1 and less than N-1)");
5550
- }
5551
- else {
5552
- continue;
5553
- }
5554
- }
5555
- const kp = curve.g.mul(k);
5556
- if (kp.isInfinity()) {
5557
- if (BigNumber.isBN(customK)) {
5558
- throw new Error("Invalid fixed custom K value (must not create a point at infinity when multiplied by the generator point)");
5559
- }
5560
- else {
5561
- continue;
5562
- }
5563
- }
5564
- const kpX = kp.getX();
5565
- const r = kpX.umod(curve.n);
5566
- if (r.cmpn(0) === 0) {
5567
- if (BigNumber.isBN(customK)) {
5568
- throw new Error("Invalid fixed custom K value (when multiplied by G, the resulting x coordinate mod N must not be zero)");
5569
- }
5570
- else {
5571
- continue;
5572
- }
5573
- }
5574
- let s = k.invm(curve.n).mul(r.mul(key).iadd(msg));
5575
- s = s.umod(curve.n);
5576
- if (s.cmpn(0) === 0) {
5577
- if (BigNumber.isBN(customK)) {
5578
- throw new Error("Invalid fixed custom K value (when used with the key, it cannot create a zero value for S)");
5579
- }
5580
- else {
5581
- continue;
5582
- }
5583
- }
5584
- if (forceLowS && s.cmp(curve.n.ushrn(1)) > 0) {
5585
- s = curve.n.sub(s);
5393
+ continue;
5586
5394
  }
5587
- return new Signature(r, s);
5588
5395
  }
5396
+ if (forceLowS && s.cmp(curve.n.ushrn(1)) > 0) {
5397
+ s = curve.n.sub(s);
5398
+ }
5399
+ return new Signature(r, s);
5589
5400
  }
5590
- throw new Error("Failed to generate a valid signature");
5591
5401
  }
5592
5402
  ```
5593
5403
 
@@ -5741,167 +5551,145 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5741
5551
 
5742
5552
  ```ts
5743
5553
  verify = (msg: BigNumber, sig: Signature, key: Point): boolean => {
5744
- if (typeof BigInt === "function") {
5745
- const zero = BigInt(0);
5746
- const one = BigInt(1);
5747
- const two = BigInt(2);
5748
- const three = BigInt(3);
5749
- const p = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
5750
- const n = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
5751
- const G = {
5752
- x: BigInt("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"),
5753
- y: BigInt("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")
5754
- };
5755
- const mod = (a: bigint, m: bigint): bigint => ((a % m) + m) % m;
5756
- const modInv = (a: bigint, m: bigint): bigint => {
5757
- let [oldr, r] = [a, m];
5758
- let [olds, s] = [BigInt(1), BigInt(0)];
5759
- while (r !== zero) {
5760
- const q = oldr / r;
5761
- [oldr, r] = [r, oldr - q * r];
5762
- [olds, s] = [s, olds - q * s];
5763
- }
5764
- if (oldr > one)
5765
- return zero;
5766
- return mod(olds, m);
5767
- };
5768
- const modMul = (a: bigint, b: bigint, m: bigint): bigint => mod(a * b, m);
5769
- const modSub = (a: bigint, b: bigint, m: bigint): bigint => mod(a - b, m);
5770
- const four = BigInt(4);
5771
- const eight = BigInt(8);
5772
- interface JacobianPoint {
5773
- X: bigint;
5774
- Y: bigint;
5775
- Z: bigint;
5554
+ const zero = BigInt(0);
5555
+ const one = BigInt(1);
5556
+ const two = BigInt(2);
5557
+ const three = BigInt(3);
5558
+ const p = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
5559
+ const n = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
5560
+ const G = {
5561
+ x: BigInt("0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"),
5562
+ y: BigInt("0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")
5563
+ };
5564
+ const mod = (a: bigint, m: bigint): bigint => ((a % m) + m) % m;
5565
+ const modInv = (a: bigint, m: bigint): bigint => {
5566
+ let [oldr, r] = [a, m];
5567
+ let [olds, s] = [BigInt(1), BigInt(0)];
5568
+ while (r !== zero) {
5569
+ const q = oldr / r;
5570
+ [oldr, r] = [r, oldr - q * r];
5571
+ [olds, s] = [s, olds - q * s];
5776
5572
  }
5777
- const pointDouble = (P: JacobianPoint): JacobianPoint => {
5778
- const { X: X1, Y: Y1, Z: Z1 } = P;
5779
- if (Y1 === zero) {
5780
- return { X: zero, Y: one, Z: zero };
5781
- }
5782
- const Y1sq = modMul(Y1, Y1, p);
5783
- const S = modMul(four, modMul(X1, Y1sq, p), p);
5784
- const M = modMul(three, modMul(X1, X1, p), p);
5785
- const X3 = modSub(modMul(M, M, p), modMul(two, S, p), p);
5786
- const Y3 = modSub(modMul(M, modSub(S, X3, p), p), modMul(eight, modMul(Y1sq, Y1sq, p), p), p);
5787
- const Z3 = modMul(two, modMul(Y1, Z1, p), p);
5788
- return { X: X3, Y: Y3, Z: Z3 };
5789
- };
5790
- const pointAdd = (P: JacobianPoint, Q: JacobianPoint): JacobianPoint => {
5791
- if (P.Z === zero)
5792
- return Q;
5793
- if (Q.Z === zero)
5794
- return P;
5795
- const Z1Z1 = modMul(P.Z, P.Z, p);
5796
- const Z2Z2 = modMul(Q.Z, Q.Z, p);
5797
- const U1 = modMul(P.X, Z2Z2, p);
5798
- const U2 = modMul(Q.X, Z1Z1, p);
5799
- const S1 = modMul(P.Y, modMul(Z2Z2, Q.Z, p), p);
5800
- const S2 = modMul(Q.Y, modMul(Z1Z1, P.Z, p), p);
5801
- const H = modSub(U2, U1, p);
5802
- const r = modSub(S2, S1, p);
5803
- if (H === zero) {
5804
- if (r === zero) {
5805
- return pointDouble(P);
5806
- }
5807
- else {
5808
- return { X: zero, Y: one, Z: zero };
5809
- }
5810
- }
5811
- const HH = modMul(H, H, p);
5812
- const HHH = modMul(H, HH, p);
5813
- const V = modMul(U1, HH, p);
5814
- const X3 = modSub(modSub(modMul(r, r, p), HHH, p), modMul(two, V, p), p);
5815
- const Y3 = modSub(modMul(r, modSub(V, X3, p), p), modMul(S1, HHH, p), p);
5816
- const Z3 = modMul(H, modMul(P.Z, Q.Z, p), p);
5817
- return { X: X3, Y: Y3, Z: Z3 };
5818
- };
5819
- const scalarMultiply = (k: bigint, P: {
5820
- x: bigint;
5821
- y: bigint;
5822
- }): JacobianPoint => {
5823
- const N: JacobianPoint = { X: P.x, Y: P.y, Z: one };
5824
- let Q: JacobianPoint = { X: zero, Y: one, Z: zero };
5825
- const kBin = k.toString(2);
5826
- for (let i = 0; i < kBin.length; i++) {
5827
- Q = pointDouble(Q);
5828
- if (kBin[i] === "1") {
5829
- Q = pointAdd(Q, N);
5830
- }
5831
- }
5573
+ if (oldr > one)
5574
+ return zero;
5575
+ return mod(olds, m);
5576
+ };
5577
+ const modMul = (a: bigint, b: bigint, m: bigint): bigint => mod(a * b, m);
5578
+ const modSub = (a: bigint, b: bigint, m: bigint): bigint => mod(a - b, m);
5579
+ const four = BigInt(4);
5580
+ const eight = BigInt(8);
5581
+ interface JacobianPoint {
5582
+ X: bigint;
5583
+ Y: bigint;
5584
+ Z: bigint;
5585
+ }
5586
+ const pointDouble = (P: JacobianPoint): JacobianPoint => {
5587
+ const { X: X1, Y: Y1, Z: Z1 } = P;
5588
+ if (Y1 === zero) {
5589
+ return { X: zero, Y: one, Z: zero };
5590
+ }
5591
+ const Y1sq = modMul(Y1, Y1, p);
5592
+ const S = modMul(four, modMul(X1, Y1sq, p), p);
5593
+ const M = modMul(three, modMul(X1, X1, p), p);
5594
+ const X3 = modSub(modMul(M, M, p), modMul(two, S, p), p);
5595
+ const Y3 = modSub(modMul(M, modSub(S, X3, p), p), modMul(eight, modMul(Y1sq, Y1sq, p), p), p);
5596
+ const Z3 = modMul(two, modMul(Y1, Z1, p), p);
5597
+ return { X: X3, Y: Y3, Z: Z3 };
5598
+ };
5599
+ const pointAdd = (P: JacobianPoint, Q: JacobianPoint): JacobianPoint => {
5600
+ if (P.Z === zero)
5832
5601
  return Q;
5833
- };
5834
- const verifyECDSA = (hash: bigint, publicKey: {
5835
- x: bigint;
5836
- y: bigint;
5837
- }, signature: {
5838
- r: bigint;
5839
- s: bigint;
5840
- }): boolean => {
5841
- const { r, s } = signature;
5842
- const z = hash;
5843
- if (r <= zero || r >= n || s <= zero || s >= n) {
5844
- return false;
5845
- }
5846
- const w = modInv(s, n);
5847
- if (w === zero) {
5848
- return false;
5602
+ if (Q.Z === zero)
5603
+ return P;
5604
+ const Z1Z1 = modMul(P.Z, P.Z, p);
5605
+ const Z2Z2 = modMul(Q.Z, Q.Z, p);
5606
+ const U1 = modMul(P.X, Z2Z2, p);
5607
+ const U2 = modMul(Q.X, Z1Z1, p);
5608
+ const S1 = modMul(P.Y, modMul(Z2Z2, Q.Z, p), p);
5609
+ const S2 = modMul(Q.Y, modMul(Z1Z1, P.Z, p), p);
5610
+ const H = modSub(U2, U1, p);
5611
+ const r = modSub(S2, S1, p);
5612
+ if (H === zero) {
5613
+ if (r === zero) {
5614
+ return pointDouble(P);
5849
5615
  }
5850
- const u1 = modMul(z, w, n);
5851
- const u2 = modMul(r, w, n);
5852
- const RG = scalarMultiply(u1, G);
5853
- const RQ = scalarMultiply(u2, publicKey);
5854
- const R = pointAdd(RG, RQ);
5855
- if (R.Z === zero) {
5856
- return false;
5616
+ else {
5617
+ return { X: zero, Y: one, Z: zero };
5857
5618
  }
5858
- const ZInv = modInv(R.Z, p);
5859
- if (ZInv === zero) {
5860
- return false;
5619
+ }
5620
+ const HH = modMul(H, H, p);
5621
+ const HHH = modMul(H, HH, p);
5622
+ const V = modMul(U1, HH, p);
5623
+ const X3 = modSub(modSub(modMul(r, r, p), HHH, p), modMul(two, V, p), p);
5624
+ const Y3 = modSub(modMul(r, modSub(V, X3, p), p), modMul(S1, HHH, p), p);
5625
+ const Z3 = modMul(H, modMul(P.Z, Q.Z, p), p);
5626
+ return { X: X3, Y: Y3, Z: Z3 };
5627
+ };
5628
+ const scalarMultiply = (k: bigint, P: {
5629
+ x: bigint;
5630
+ y: bigint;
5631
+ }): JacobianPoint => {
5632
+ const N: JacobianPoint = { X: P.x, Y: P.y, Z: one };
5633
+ let Q: JacobianPoint = { X: zero, Y: one, Z: zero };
5634
+ const kBin = k.toString(2);
5635
+ for (let i = 0; i < kBin.length; i++) {
5636
+ Q = pointDouble(Q);
5637
+ if (kBin[i] === "1") {
5638
+ Q = pointAdd(Q, N);
5861
5639
  }
5862
- const ZInv2 = modMul(ZInv, ZInv, p);
5863
- const x1affine = modMul(R.X, ZInv2, p);
5864
- const v = mod(x1affine, n);
5865
- return v === r;
5866
- };
5867
- const hash = BigInt("0x" + msg.toString(16));
5868
- if ((key.x == null) || (key.y == null)) {
5869
- throw new Error("Invalid public key: missing coordinates.");
5870
5640
  }
5871
- const publicKey = {
5872
- x: BigInt("0x" + key.x.toString(16)),
5873
- y: BigInt("0x" + key.y.toString(16))
5874
- };
5875
- const signature = {
5876
- r: BigInt("0x" + sig.r.toString(16)),
5877
- s: BigInt("0x" + sig.s.toString(16))
5878
- };
5879
- return verifyECDSA(hash, publicKey, signature);
5880
- }
5881
- else {
5882
- const curve = new Curve();
5883
- msg = truncateToN(msg);
5884
- const r = sig.r;
5885
- const s = sig.s;
5886
- if (r.cmpn(1) < 0 || r.cmp(curve.n) >= 0) {
5641
+ return Q;
5642
+ };
5643
+ const verifyECDSA = (hash: bigint, publicKey: {
5644
+ x: bigint;
5645
+ y: bigint;
5646
+ }, signature: {
5647
+ r: bigint;
5648
+ s: bigint;
5649
+ }): boolean => {
5650
+ const { r, s } = signature;
5651
+ const z = hash;
5652
+ if (r <= zero || r >= n || s <= zero || s >= n) {
5653
+ return false;
5654
+ }
5655
+ const w = modInv(s, n);
5656
+ if (w === zero) {
5887
5657
  return false;
5888
5658
  }
5889
- if (s.cmpn(1) < 0 || s.cmp(curve.n) >= 0) {
5659
+ const u1 = modMul(z, w, n);
5660
+ const u2 = modMul(r, w, n);
5661
+ const RG = scalarMultiply(u1, G);
5662
+ const RQ = scalarMultiply(u2, publicKey);
5663
+ const R = pointAdd(RG, RQ);
5664
+ if (R.Z === zero) {
5890
5665
  return false;
5891
5666
  }
5892
- const sinv = s.invm(curve.n);
5893
- const u1 = sinv.mul(msg).umod(curve.n);
5894
- const u2 = sinv.mul(r).umod(curve.n);
5895
- const p = curve.g.jmulAdd(u1, key, u2);
5896
- if (p.isInfinity()) {
5667
+ const ZInv = modInv(R.Z, p);
5668
+ if (ZInv === zero) {
5897
5669
  return false;
5898
5670
  }
5899
- return p.eqXToP(r);
5671
+ const ZInv2 = modMul(ZInv, ZInv, p);
5672
+ const x1affine = modMul(R.X, ZInv2, p);
5673
+ const v = mod(x1affine, n);
5674
+ return v === r;
5675
+ };
5676
+ const hash = BigInt("0x" + msg.toString(16));
5677
+ if ((key.x == null) || (key.y == null)) {
5678
+ throw new Error("Invalid public key: missing coordinates.");
5900
5679
  }
5680
+ const publicKey = {
5681
+ x: BigInt("0x" + key.x.toString(16)),
5682
+ y: BigInt("0x" + key.y.toString(16))
5683
+ };
5684
+ const signature = {
5685
+ r: BigInt("0x" + sig.r.toString(16)),
5686
+ s: BigInt("0x" + sig.s.toString(16))
5687
+ };
5688
+ return verifyECDSA(hash, publicKey, signature);
5901
5689
  }
5902
5690
  ```
5903
5691
 
5904
- See also: [BigNumber](./primitives.md#class-bignumber), [Curve](./primitives.md#class-curve), [JacobianPoint](./primitives.md#class-jacobianpoint), [Point](./primitives.md#class-point), [Signature](./primitives.md#class-signature)
5692
+ See also: [BigNumber](./primitives.md#class-bignumber), [JacobianPoint](./primitives.md#class-jacobianpoint), [Point](./primitives.md#class-point), [Signature](./primitives.md#class-signature)
5905
5693
 
5906
5694
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5907
5695
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.6.16",
3
+ "version": "1.6.18",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -33,6 +33,11 @@ export type LookupAnswer =
33
33
 
34
34
  /** Default SLAP trackers */
35
35
  export const DEFAULT_SLAP_TRACKERS: string[] = [
36
+ // BSVA clusters
37
+ 'https://overlay-us-1.bsvb.tech',
38
+ 'https://overlay-eu-1.bsvb.tech',
39
+ 'https://overlay-ap-1.bsvb.tech',
40
+
36
41
  // Babbage primary overlay service
37
42
  'https://users.bapp.dev'
38
43