@bsv/sdk 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -33,7 +33,7 @@ export class AuthFetch {
33
33
  peers: Record<string, AuthPeer> = {}
34
34
 
35
35
  /**
36
- * Constructs a new SimplifiedFetch instance.
36
+ * Constructs a new AuthFetch instance.
37
37
  * @param wallet - The wallet instance for signing and authentication.
38
38
  * @param requestedCertificates - Optional set of certificates to request from peers.
39
39
  */
@@ -3,6 +3,9 @@ import { Utils } from '../../../mod.js'
3
3
 
4
4
  const SUCCESS_STATUS_CODES = [200, 402]
5
5
 
6
+ // Only bind window.fetch in the browser
7
+ const defaultFetch = typeof window !== 'undefined' ? fetch.bind(window) : fetch;
8
+
6
9
  /**
7
10
  * Implements an HTTP-specific transport for handling Peer mutual authentication messages.
8
11
  * This class integrates with fetch to send and receive authenticated messages between peers.
@@ -17,7 +20,7 @@ export class SimplifiedFetchTransport implements Transport {
17
20
  * @param baseUrl - The base URL for all HTTP requests made by this transport.
18
21
  * @param fetchClient - A fetch implementation to use for HTTP requests (default: global fetch).
19
22
  */
20
- constructor(baseUrl: string, fetchClient = fetch) {
23
+ constructor(baseUrl: string, fetchClient = defaultFetch) {
21
24
  this.fetchClient = fetchClient
22
25
  this.baseUrl = baseUrl
23
26
  }
@@ -41,7 +41,7 @@ export default class Point extends BasePoint {
41
41
  * const derPoint = [ 2, 18, 123, 108, 125, 83, 1, 251, 164, 214, 16, 119, 200, 216, 210, 193, 251, 193, 129, 67, 97, 146, 210, 216, 77, 254, 18, 6, 150, 190, 99, 198, 128 ];
42
42
  * const point = Point.fromDER(derPoint);
43
43
  */
44
- static fromDER(bytes: number[]): Point {
44
+ static fromDER (bytes: number[]): Point {
45
45
  const len = 32
46
46
  // uncompressed, hybrid-odd, hybrid-even
47
47
  if ((bytes[0] === 0x04 || bytes[0] === 0x06 || bytes[0] === 0x07) &&
@@ -86,12 +86,12 @@ export default class Point extends BasePoint {
86
86
  * const pointStr = 'abcdef';
87
87
  * const point = Point.fromString(pointStr);
88
88
  */
89
- static fromString(str: string): Point {
89
+ static fromString (str: string): Point {
90
90
  const bytes = toArray(str, 'hex')
91
91
  return Point.fromDER(bytes)
92
92
  }
93
93
 
94
- static redSqrtOptimized(y2: BigNumber): BigNumber {
94
+ static redSqrtOptimized (y2: BigNumber): BigNumber {
95
95
  const red = Point.red
96
96
  const p = red.m // The modulus
97
97
  const exponent = p.addn(1).iushrn(2) // (p + 1) / 4
@@ -113,12 +113,12 @@ export default class Point extends BasePoint {
113
113
  * const xCoordinate = new BigNumber('10');
114
114
  * const point = Point.fromX(xCoordinate, true);
115
115
  */
116
- static fromX(x: BigNumber | number | number[] | string, odd: boolean): Point {
116
+ static fromX (x: BigNumber | number | number[] | string, odd: boolean): Point {
117
117
  if (typeof BigInt === 'function') {
118
- function mod(a: bigint, n: bigint): bigint {
118
+ function mod (a: bigint, n: bigint): bigint {
119
119
  return ((a % n) + n) % n
120
120
  }
121
- function modPow(base: bigint, exponent: bigint, modulus: bigint): bigint {
121
+ function modPow (base: bigint, exponent: bigint, modulus: bigint): bigint {
122
122
  let result = BigInt(1)
123
123
  base = mod(base, modulus)
124
124
  while (exponent > BigInt(0)) {
@@ -130,7 +130,7 @@ export default class Point extends BasePoint {
130
130
  }
131
131
  return result
132
132
  }
133
- function sqrtMod(a: bigint, p: bigint): bigint | null {
133
+ function sqrtMod (a: bigint, p: bigint): bigint | null {
134
134
  const exponent = (p + BigInt(1)) >> BigInt(2) // Precomputed exponent
135
135
  const sqrtCandidate = modPow(a, exponent, p)
136
136
  if (mod(sqrtCandidate * sqrtCandidate, p) === mod(a, p)) {
@@ -232,7 +232,7 @@ export default class Point extends BasePoint {
232
232
  * const serializedPoint = '{"x":52,"y":15}';
233
233
  * const point = Point.fromJSON(serializedPoint, true);
234
234
  */
235
- static fromJSON(
235
+ static fromJSON (
236
236
  obj: string | any[], isRed: boolean
237
237
  ): Point {
238
238
  if (typeof obj === 'string') {
@@ -252,15 +252,15 @@ export default class Point extends BasePoint {
252
252
  beta: null,
253
253
  doubles: typeof pre.doubles === 'object' && pre.doubles !== null
254
254
  ? {
255
- step: pre.doubles.step,
256
- points: [res].concat(pre.doubles.points.map(obj2point))
257
- }
255
+ step: pre.doubles.step,
256
+ points: [res].concat(pre.doubles.points.map(obj2point))
257
+ }
258
258
  : undefined,
259
259
  naf: typeof pre.naf === 'object' && pre.naf !== null
260
260
  ? {
261
- wnd: pre.naf.wnd,
262
- points: [res].concat(pre.naf.points.map(obj2point))
263
- }
261
+ wnd: pre.naf.wnd,
262
+ points: [res].concat(pre.naf.points.map(obj2point))
263
+ }
264
264
  : undefined
265
265
  }
266
266
  return res
@@ -276,7 +276,7 @@ export default class Point extends BasePoint {
276
276
  * new Point('abc123', 'def456');
277
277
  * new Point(null, null); // Generates Infinity point.
278
278
  */
279
- constructor(
279
+ constructor (
280
280
  x: BigNumber | number | number[] | string | null,
281
281
  y: BigNumber | number | number[] | string | null,
282
282
  isRed: boolean = true
@@ -318,7 +318,7 @@ export default class Point extends BasePoint {
318
318
  * const aPoint = new Point(x, y);
319
319
  * const isValid = aPoint.validate();
320
320
  */
321
- validate(): boolean {
321
+ validate (): boolean {
322
322
  return this.curve.validate(this)
323
323
  }
324
324
 
@@ -337,7 +337,7 @@ export default class Point extends BasePoint {
337
337
  * const encodedPointArray = aPoint.encode();
338
338
  * const encodedPointHex = aPoint.encode(true, 'hex');
339
339
  */
340
- encode(compact: boolean = true, enc?: 'hex'): number[] | string {
340
+ encode (compact: boolean = true, enc?: 'hex'): number[] | string {
341
341
  const len = this.curve.p.byteLength()
342
342
  const x = this.getX().toArray('be', len)
343
343
  let res: number[]
@@ -364,7 +364,7 @@ export default class Point extends BasePoint {
364
364
  * const aPoint = new Point(x, y);
365
365
  * const stringPoint = aPoint.toString();
366
366
  */
367
- toString(): string {
367
+ toString (): string {
368
368
  return this.encode(true, 'hex') as string
369
369
  }
370
370
 
@@ -378,24 +378,24 @@ export default class Point extends BasePoint {
378
378
  * const aPoint = new Point(x, y);
379
379
  * const jsonPoint = aPoint.toJSON();
380
380
  */
381
- toJSON(): [BigNumber | null, BigNumber | null, { doubles: { step: any, points: any[] } | undefined, naf: { wnd: any, points: any[] } | undefined }?] {
381
+ toJSON (): [BigNumber | null, BigNumber | null, { doubles: { step: any, points: any[] } | undefined, naf: { wnd: any, points: any[] } | undefined }?] {
382
382
  if (this.precomputed == null) { return [this.x, this.y] }
383
383
 
384
384
  return [this.x, this.y, typeof this.precomputed === 'object' && this.precomputed !== null
385
385
  ? {
386
- doubles: (this.precomputed.doubles != null)
387
- ? {
388
- step: this.precomputed.doubles.step,
389
- points: this.precomputed.doubles.points.slice(1)
390
- }
391
- : undefined,
392
- naf: (this.precomputed.naf != null)
393
- ? {
394
- wnd: this.precomputed.naf.wnd,
395
- points: this.precomputed.naf.points.slice(1)
396
- }
397
- : undefined
398
- }
386
+ doubles: (this.precomputed.doubles != null)
387
+ ? {
388
+ step: this.precomputed.doubles.step,
389
+ points: this.precomputed.doubles.points.slice(1)
390
+ }
391
+ : undefined,
392
+ naf: (this.precomputed.naf != null)
393
+ ? {
394
+ wnd: this.precomputed.naf.wnd,
395
+ points: this.precomputed.naf.points.slice(1)
396
+ }
397
+ : undefined
398
+ }
399
399
  : undefined]
400
400
  }
401
401
 
@@ -409,7 +409,7 @@ export default class Point extends BasePoint {
409
409
  * const aPoint = new Point(x, y);
410
410
  * console.log(aPoint.inspect());
411
411
  */
412
- inspect(): string {
412
+ inspect (): string {
413
413
  if (this.isInfinity()) {
414
414
  return '<EC Point Infinity>'
415
415
  }
@@ -426,7 +426,7 @@ export default class Point extends BasePoint {
426
426
  * const p = new Point(null, null);
427
427
  * console.log(p.isInfinity()); // outputs: true
428
428
  */
429
- isInfinity(): boolean {
429
+ isInfinity (): boolean {
430
430
  return this.inf
431
431
  }
432
432
 
@@ -442,7 +442,7 @@ export default class Point extends BasePoint {
442
442
  * const p2 = new Point(2, 3);
443
443
  * const result = p1.add(p2);
444
444
  */
445
- add(p: Point): Point {
445
+ add (p: Point): Point {
446
446
  // O + P = P
447
447
  if (this.inf) { return p }
448
448
 
@@ -474,7 +474,7 @@ export default class Point extends BasePoint {
474
474
  * const P = new Point('123', '456');
475
475
  * const result = P.dbl();
476
476
  * */
477
- dbl(): Point {
477
+ dbl (): Point {
478
478
  if (this.inf) { return this }
479
479
 
480
480
  // 2P = O
@@ -501,7 +501,7 @@ export default class Point extends BasePoint {
501
501
  * const P = new Point('123', '456');
502
502
  * const x = P.getX();
503
503
  */
504
- getX(): BigNumber {
504
+ getX (): BigNumber {
505
505
  return this.x.fromRed()
506
506
  }
507
507
 
@@ -512,7 +512,7 @@ export default class Point extends BasePoint {
512
512
  * const P = new Point('123', '456');
513
513
  * const x = P.getX();
514
514
  */
515
- getY(): BigNumber {
515
+ getY (): BigNumber {
516
516
  return this.y.fromRed()
517
517
  }
518
518
 
@@ -527,7 +527,7 @@ export default class Point extends BasePoint {
527
527
  * const p = new Point(1, 2);
528
528
  * const result = p.mul(2); // this doubles the Point
529
529
  */
530
- mul(k: BigNumber | number | number[] | string): Point {
530
+ mul (k: BigNumber | number | number[] | string): Point {
531
531
  if (!BigNumber.isBN(k)) {
532
532
  k = new BigNumber(k as number, 16)
533
533
  }
@@ -556,7 +556,7 @@ export default class Point extends BasePoint {
556
556
  * const p2 = new Point(2, 3);
557
557
  * const result = p1.mulAdd(2, p2, 3);
558
558
  */
559
- mulAdd(k1: BigNumber, p2: Point, k2: BigNumber): Point {
559
+ mulAdd (k1: BigNumber, p2: Point, k2: BigNumber): Point {
560
560
  const points = [this, p2]
561
561
  const coeffs = [k1, k2]
562
562
  return this._endoWnafMulAdd(points, coeffs) as Point
@@ -577,7 +577,7 @@ export default class Point extends BasePoint {
577
577
  * const p2 = new Point(2, 3);
578
578
  * const result = p1.jmulAdd(2, p2, 3);
579
579
  */
580
- jmulAdd(k1: BigNumber, p2: Point, k2: BigNumber): JPoint {
580
+ jmulAdd (k1: BigNumber, p2: Point, k2: BigNumber): JPoint {
581
581
  const points = [this, p2]
582
582
  const coeffs = [k1, k2]
583
583
  return this._endoWnafMulAdd(points, coeffs, true) as JPoint
@@ -596,7 +596,7 @@ export default class Point extends BasePoint {
596
596
  * const p2 = new Point(5, 20);
597
597
  * const areEqual = p1.eq(p2); // returns true
598
598
  */
599
- eq(p: Point): boolean {
599
+ eq (p: Point): boolean {
600
600
  return this === p || (
601
601
  (this.inf === p.inf) &&
602
602
  (this.inf || (this.x.cmp(p.x) === 0 && this.y.cmp(p.y) === 0)))
@@ -611,7 +611,7 @@ export default class Point extends BasePoint {
611
611
  * const P = new Point('123', '456');
612
612
  * const result = P.neg();
613
613
  */
614
- neg(_precompute?: boolean): Point {
614
+ neg (_precompute?: boolean): Point {
615
615
  if (this.inf) { return this }
616
616
 
617
617
  const res = new Point(this.x, this.y.redNeg())
@@ -647,7 +647,7 @@ export default class Point extends BasePoint {
647
647
  * const p = new Point(5, 20);
648
648
  * const doubledPoint = p.dblp(10); // returns the point after "doubled" 10 times
649
649
  */
650
- dblp(k: number): Point {
650
+ dblp (k: number): Point {
651
651
  /* eslint-disable @typescript-eslint/no-this-alias */
652
652
  let r: Point = this
653
653
  for (let i = 0; i < k; i++) { r = r.dbl() }
@@ -665,7 +665,7 @@ export default class Point extends BasePoint {
665
665
  * const point = new Point(xCoordinate, yCoordinate);
666
666
  * const jacobianPoint = point.toJ();
667
667
  */
668
- toJ(): JPoint {
668
+ toJ (): JPoint {
669
669
  if (this.inf) {
670
670
  return new JPoint(null, null, null)
671
671
  }
@@ -673,7 +673,7 @@ export default class Point extends BasePoint {
673
673
  return res
674
674
  }
675
675
 
676
- private _getBeta(): undefined | Point {
676
+ private _getBeta (): undefined | Point {
677
677
  if (typeof this.curve.endo !== 'object') { return }
678
678
 
679
679
  const pre = this.precomputed
@@ -692,22 +692,22 @@ export default class Point extends BasePoint {
692
692
  beta: null,
693
693
  naf: (pre.naf != null)
694
694
  ? {
695
- wnd: pre.naf.wnd,
696
- points: pre.naf.points.map(endoMul)
697
- }
695
+ wnd: pre.naf.wnd,
696
+ points: pre.naf.points.map(endoMul)
697
+ }
698
698
  : undefined,
699
699
  doubles: (pre.doubles != null)
700
700
  ? {
701
- step: pre.doubles.step,
702
- points: pre.doubles.points.map(endoMul)
703
- }
701
+ step: pre.doubles.step,
702
+ points: pre.doubles.points.map(endoMul)
703
+ }
704
704
  : undefined
705
705
  }
706
706
  }
707
707
  return beta
708
708
  }
709
709
 
710
- private _fixedNafMul(k: BigNumber): Point {
710
+ private _fixedNafMul (k: BigNumber): Point {
711
711
  if (typeof this.precomputed !== 'object' || this.precomputed === null) {
712
712
  throw new Error('_fixedNafMul requires precomputed values for the point')
713
713
  }
@@ -743,7 +743,7 @@ export default class Point extends BasePoint {
743
743
  return a.toP()
744
744
  }
745
745
 
746
- private _wnafMulAdd(
746
+ private _wnafMulAdd (
747
747
  defW: number,
748
748
  points: Point[],
749
749
  coeffs: BigNumber[],
@@ -869,7 +869,7 @@ export default class Point extends BasePoint {
869
869
  }
870
870
  }
871
871
 
872
- private _endoWnafMulAdd(points: Point[], coeffs, jacobianResult?: boolean): BasePoint {
872
+ private _endoWnafMulAdd (points: Point[], coeffs, jacobianResult?: boolean): BasePoint {
873
873
  const npoints = this.curve._endoWnafT1
874
874
  const ncoeffs = this.curve._endoWnafT2
875
875
  let i
@@ -902,7 +902,7 @@ export default class Point extends BasePoint {
902
902
  return res
903
903
  }
904
904
 
905
- private _hasDoubles(k: BigNumber): boolean {
905
+ private _hasDoubles (k: BigNumber): boolean {
906
906
  if (this.precomputed == null) { return false }
907
907
 
908
908
  const doubles = this.precomputed.doubles
@@ -911,7 +911,7 @@ export default class Point extends BasePoint {
911
911
  return doubles.points.length >= Math.ceil((k.bitLength() + 1) / doubles.step)
912
912
  };
913
913
 
914
- private _getDoubles(
914
+ private _getDoubles (
915
915
  step?: number,
916
916
  power?: number
917
917
  ): { step: number, points: any[] } {
@@ -936,7 +936,7 @@ export default class Point extends BasePoint {
936
936
  }
937
937
  };
938
938
 
939
- private _getNAFPoints(wnd: number): { wnd: number, points: any[] } {
939
+ private _getNAFPoints (wnd: number): { wnd: number, points: any[] } {
940
940
  if (
941
941
  typeof this.precomputed === 'object' && this.precomputed !== null &&
942
942
  typeof this.precomputed.naf === 'object' && this.precomputed.naf !== null