@bsv/sdk 1.8.13 → 1.9.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.
Files changed (83) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/auth/Peer.js +35 -16
  3. package/dist/cjs/src/auth/Peer.js.map +1 -1
  4. package/dist/cjs/src/primitives/BigNumber.js +43 -31
  5. package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
  6. package/dist/cjs/src/primitives/Hash.js +11 -5
  7. package/dist/cjs/src/primitives/Hash.js.map +1 -1
  8. package/dist/cjs/src/primitives/SymmetricKey.js +15 -6
  9. package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -1
  10. package/dist/cjs/src/primitives/TransactionSignature.js +60 -18
  11. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  12. package/dist/cjs/src/primitives/utils.js +74 -28
  13. package/dist/cjs/src/primitives/utils.js.map +1 -1
  14. package/dist/cjs/src/script/Script.js +217 -108
  15. package/dist/cjs/src/script/Script.js.map +1 -1
  16. package/dist/cjs/src/script/Spend.js +5 -2
  17. package/dist/cjs/src/script/Spend.js.map +1 -1
  18. package/dist/cjs/src/transaction/Beef.js +62 -7
  19. package/dist/cjs/src/transaction/Beef.js.map +1 -1
  20. package/dist/cjs/src/transaction/BeefTx.js +1 -1
  21. package/dist/cjs/src/transaction/BeefTx.js.map +1 -1
  22. package/dist/cjs/src/transaction/Transaction.js +67 -35
  23. package/dist/cjs/src/transaction/Transaction.js.map +1 -1
  24. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  25. package/dist/esm/src/auth/Peer.js +36 -16
  26. package/dist/esm/src/auth/Peer.js.map +1 -1
  27. package/dist/esm/src/primitives/BigNumber.js +43 -31
  28. package/dist/esm/src/primitives/BigNumber.js.map +1 -1
  29. package/dist/esm/src/primitives/Hash.js +11 -5
  30. package/dist/esm/src/primitives/Hash.js.map +1 -1
  31. package/dist/esm/src/primitives/SymmetricKey.js +15 -6
  32. package/dist/esm/src/primitives/SymmetricKey.js.map +1 -1
  33. package/dist/esm/src/primitives/TransactionSignature.js +60 -18
  34. package/dist/esm/src/primitives/TransactionSignature.js.map +1 -1
  35. package/dist/esm/src/primitives/utils.js +74 -28
  36. package/dist/esm/src/primitives/utils.js.map +1 -1
  37. package/dist/esm/src/script/Script.js +222 -110
  38. package/dist/esm/src/script/Script.js.map +1 -1
  39. package/dist/esm/src/script/Spend.js +6 -2
  40. package/dist/esm/src/script/Spend.js.map +1 -1
  41. package/dist/esm/src/transaction/Beef.js +64 -7
  42. package/dist/esm/src/transaction/Beef.js.map +1 -1
  43. package/dist/esm/src/transaction/BeefTx.js +1 -1
  44. package/dist/esm/src/transaction/BeefTx.js.map +1 -1
  45. package/dist/esm/src/transaction/Transaction.js +69 -35
  46. package/dist/esm/src/transaction/Transaction.js.map +1 -1
  47. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  48. package/dist/types/src/auth/Peer.d.ts +4 -0
  49. package/dist/types/src/auth/Peer.d.ts.map +1 -1
  50. package/dist/types/src/primitives/BigNumber.d.ts.map +1 -1
  51. package/dist/types/src/primitives/Hash.d.ts +10 -10
  52. package/dist/types/src/primitives/Hash.d.ts.map +1 -1
  53. package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -1
  54. package/dist/types/src/primitives/TransactionSignature.d.ts +34 -13
  55. package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -1
  56. package/dist/types/src/primitives/utils.d.ts +6 -8
  57. package/dist/types/src/primitives/utils.d.ts.map +1 -1
  58. package/dist/types/src/script/Script.d.ts +18 -9
  59. package/dist/types/src/script/Script.d.ts.map +1 -1
  60. package/dist/types/src/script/Spend.d.ts +1 -0
  61. package/dist/types/src/script/Spend.d.ts.map +1 -1
  62. package/dist/types/src/transaction/Beef.d.ts +9 -0
  63. package/dist/types/src/transaction/Beef.d.ts.map +1 -1
  64. package/dist/types/src/transaction/Transaction.d.ts +7 -0
  65. package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
  66. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  67. package/dist/umd/bundle.js +3 -3
  68. package/dist/umd/bundle.js.map +1 -1
  69. package/docs/reference/primitives.md +87 -37
  70. package/docs/reference/script.md +11 -7
  71. package/docs/reference/transaction.md +2 -0
  72. package/package.json +1 -1
  73. package/src/auth/Peer.ts +44 -18
  74. package/src/primitives/BigNumber.ts +44 -23
  75. package/src/primitives/Hash.ts +41 -17
  76. package/src/primitives/SymmetricKey.ts +15 -6
  77. package/src/primitives/TransactionSignature.ts +77 -31
  78. package/src/primitives/utils.ts +80 -30
  79. package/src/script/Script.ts +238 -104
  80. package/src/script/Spend.ts +7 -3
  81. package/src/transaction/Beef.ts +74 -7
  82. package/src/transaction/BeefTx.ts +1 -1
  83. package/src/transaction/Transaction.ts +77 -34
@@ -4,6 +4,15 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
4
4
 
5
5
  ## Interfaces
6
6
 
7
+ | |
8
+ | --- |
9
+ | [JacobianPointBI](#interface-jacobianpointbi) |
10
+ | [SignatureHashCache](#interface-signaturehashcache) |
11
+
12
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
13
+
14
+ ---
15
+
7
16
  ### Interface: JacobianPointBI
8
17
 
9
18
  ```ts
@@ -16,6 +25,20 @@ export interface JacobianPointBI {
16
25
 
17
26
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
18
27
 
28
+ ---
29
+ ### Interface: SignatureHashCache
30
+
31
+ ```ts
32
+ export interface SignatureHashCache {
33
+ hashPrevouts?: number[];
34
+ hashSequence?: number[];
35
+ hashOutputsAll?: number[];
36
+ hashOutputsSingle?: Map<number, number[]>;
37
+ }
38
+ ```
39
+
40
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
41
+
19
42
  ---
20
43
  ## Classes
21
44
 
@@ -3898,7 +3921,7 @@ const sha256 = new SHA256();
3898
3921
  ```ts
3899
3922
  export class SHA256 {
3900
3923
  constructor()
3901
- update(msg: number[] | string, enc?: "hex" | "utf8"): this
3924
+ update(msg: Uint8Array | number[] | string, enc?: "hex" | "utf8"): this
3902
3925
  digest(): number[]
3903
3926
  digestHex(): string
3904
3927
  }
@@ -3919,8 +3942,8 @@ This class also uses the SHA-256 cryptographic hash algorithm that produces a 25
3919
3942
  export class SHA256HMAC {
3920
3943
  blockSize = 64;
3921
3944
  outSize = 32;
3922
- constructor(key: number[] | string)
3923
- update(msg: number[] | string, enc?: "hex"): SHA256HMAC
3945
+ constructor(key: Uint8Array | number[] | string)
3946
+ update(msg: Uint8Array | number[] | string, enc?: "hex"): SHA256HMAC
3924
3947
  digest(): number[]
3925
3948
  digestHex(): string
3926
3949
  }
@@ -3935,7 +3958,7 @@ If the key size is larger than the blockSize, it is digested using SHA-256.
3935
3958
  If the key size is less than the blockSize, it is padded with zeroes.
3936
3959
 
3937
3960
  ```ts
3938
- constructor(key: number[] | string)
3961
+ constructor(key: Uint8Array | number[] | string)
3939
3962
  ```
3940
3963
 
3941
3964
  Argument Details
@@ -4006,7 +4029,7 @@ let hashedMessage = myHMAC.digestHex();
4006
4029
  Updates the `SHA256HMAC` object with part of the message to be hashed.
4007
4030
 
4008
4031
  ```ts
4009
- update(msg: number[] | string, enc?: "hex"): SHA256HMAC
4032
+ update(msg: Uint8Array | number[] | string, enc?: "hex"): SHA256HMAC
4010
4033
  ```
4011
4034
  See also: [SHA256HMAC](./primitives.md#class-sha256hmac)
4012
4035
 
@@ -4067,8 +4090,8 @@ This class also uses the SHA-512 cryptographic hash algorithm that produces a 51
4067
4090
  export class SHA512HMAC {
4068
4091
  blockSize = 128;
4069
4092
  outSize = 32;
4070
- constructor(key: number[] | string)
4071
- update(msg: number[] | string, enc?: "hex" | "utf8"): SHA512HMAC
4093
+ constructor(key: Uint8Array | number[] | string)
4094
+ update(msg: Uint8Array | number[] | string, enc?: "hex" | "utf8"): SHA512HMAC
4072
4095
  digest(): number[]
4073
4096
  digestHex(): string
4074
4097
  }
@@ -4083,7 +4106,7 @@ If the key size is larger than the blockSize, it is digested using SHA-512.
4083
4106
  If the key size is less than the blockSize, it is padded with zeroes.
4084
4107
 
4085
4108
  ```ts
4086
- constructor(key: number[] | string)
4109
+ constructor(key: Uint8Array | number[] | string)
4087
4110
  ```
4088
4111
 
4089
4112
  Argument Details
@@ -4154,7 +4177,7 @@ let hashedMessage = myHMAC.digestHex();
4154
4177
  Updates the `SHA512HMAC` object with part of the message to be hashed.
4155
4178
 
4156
4179
  ```ts
4157
- update(msg: number[] | string, enc?: "hex" | "utf8"): SHA512HMAC
4180
+ update(msg: Uint8Array | number[] | string, enc?: "hex" | "utf8"): SHA512HMAC
4158
4181
  ```
4159
4182
  See also: [SHA512HMAC](./primitives.md#class-sha512hmac)
4160
4183
 
@@ -4682,19 +4705,8 @@ export default class TransactionSignature extends Signature {
4682
4705
  public static readonly SIGHASH_FORKID = 64;
4683
4706
  public static readonly SIGHASH_ANYONECANPAY = 128;
4684
4707
  scope: number;
4685
- static format(params: {
4686
- sourceTXID: string;
4687
- sourceOutputIndex: number;
4688
- sourceSatoshis: number;
4689
- transactionVersion: number;
4690
- otherInputs: TransactionInput[];
4691
- outputs: TransactionOutput[];
4692
- inputIndex: number;
4693
- subscript: Script;
4694
- inputSequence: number;
4695
- lockTime: number;
4696
- scope: number;
4697
- }): number[]
4708
+ static format(params: TransactionSignatureFormatParams): number[]
4709
+ static formatBytes(params: TransactionSignatureFormatParams): Uint8Array
4698
4710
  static fromChecksigFormat(buf: number[]): TransactionSignature
4699
4711
  constructor(r: BigNumber, s: BigNumber, scope: number)
4700
4712
  public hasLowS(): boolean
@@ -4702,7 +4714,41 @@ export default class TransactionSignature extends Signature {
4702
4714
  }
4703
4715
  ```
4704
4716
 
4705
- See also: [BigNumber](./primitives.md#class-bignumber), [Script](./script.md#class-script), [Signature](./primitives.md#class-signature), [TransactionInput](./transaction.md#interface-transactioninput), [TransactionOutput](./transaction.md#interface-transactionoutput)
4717
+ See also: [BigNumber](./primitives.md#class-bignumber), [Signature](./primitives.md#class-signature)
4718
+
4719
+ #### Method format
4720
+
4721
+ Formats the SIGHASH preimage for the targeted input, optionally using a cache to skip recomputing shared hash prefixes.
4722
+
4723
+ ```ts
4724
+ static format(params: TransactionSignatureFormatParams): number[]
4725
+ ```
4726
+
4727
+ Argument Details
4728
+
4729
+ + **params**
4730
+ + Context for the signing input plus transaction metadata.
4731
+ + **params.cache**
4732
+ + Optional cache storing previously computed `hashPrevouts`, `hashSequence`, or `hashOutputs*` values; it will be populated if present.
4733
+
4734
+ #### Method formatBytes
4735
+
4736
+ Formats the same SIGHASH preimage bytes as `format`, supporting the optional cache for hash reuse.
4737
+
4738
+ ```ts
4739
+ static formatBytes(params: TransactionSignatureFormatParams): Uint8Array
4740
+ ```
4741
+
4742
+ Returns
4743
+
4744
+ Bytes for signing.
4745
+
4746
+ Argument Details
4747
+
4748
+ + **params**
4749
+ + Context for the signing operation.
4750
+ + **params.cache**
4751
+ + Optional `SignatureHashCache` that may already contain hashed prefixes and is populated during formatting.
4706
4752
 
4707
4753
  #### Method hasLowS
4708
4754
 
@@ -4721,11 +4767,12 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
4721
4767
 
4722
4768
  ```ts
4723
4769
  export class Writer {
4724
- public bufs: number[][];
4725
- constructor(bufs?: number[][])
4770
+ public bufs: WriterChunk[];
4771
+ constructor(bufs?: WriterChunk[])
4726
4772
  getLength(): number
4773
+ toUint8Array(): Uint8Array
4727
4774
  toArray(): number[]
4728
- write(buf: number[]): this
4775
+ write(buf: WriterChunk): this
4729
4776
  writeReverse(buf: number[]): this
4730
4777
  writeUInt8(n: number): this
4731
4778
  writeInt8(n: number): this
@@ -5309,7 +5356,7 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5309
5356
  ### Variable: hash160
5310
5357
 
5311
5358
  ```ts
5312
- hash160 = (msg: number[] | string, enc?: "hex" | "utf8"): number[] => {
5359
+ hash160 = (msg: Uint8Array | number[] | string, enc?: "hex" | "utf8"): number[] => {
5313
5360
  const first = new SHA256().update(msg, enc).digest();
5314
5361
  return new RIPEMD160().update(first).digest();
5315
5362
  }
@@ -5323,7 +5370,7 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5323
5370
  ### Variable: hash256
5324
5371
 
5325
5372
  ```ts
5326
- hash256 = (msg: number[] | string, enc?: "hex" | "utf8"): number[] => {
5373
+ hash256 = (msg: Uint8Array | number[] | string, enc?: "hex" | "utf8"): number[] => {
5327
5374
  const first = new SHA256().update(msg, enc).digest();
5328
5375
  return new SHA256().update(first).digest();
5329
5376
  }
@@ -5655,7 +5702,7 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5655
5702
  ### Variable: sha256
5656
5703
 
5657
5704
  ```ts
5658
- sha256 = (msg: number[] | string, enc?: "hex" | "utf8"): number[] => {
5705
+ sha256 = (msg: Uint8Array | number[] | string, enc?: "hex" | "utf8"): number[] => {
5659
5706
  return new SHA256().update(msg, enc).digest();
5660
5707
  }
5661
5708
  ```
@@ -5668,7 +5715,7 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5668
5715
  ### Variable: sha256hmac
5669
5716
 
5670
5717
  ```ts
5671
- sha256hmac = (key: number[] | string, msg: number[] | string, enc?: "hex"): number[] => {
5718
+ sha256hmac = (key: Uint8Array | number[] | string, msg: Uint8Array | number[] | string, enc?: "hex"): number[] => {
5672
5719
  return new SHA256HMAC(key).update(msg, enc).digest();
5673
5720
  }
5674
5721
  ```
@@ -5694,7 +5741,7 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5694
5741
  ### Variable: sha512hmac
5695
5742
 
5696
5743
  ```ts
5697
- sha512hmac = (key: number[] | string, msg: number[] | string, enc?: "hex"): number[] => {
5744
+ sha512hmac = (key: Uint8Array | number[] | string, msg: Uint8Array | number[] | string, enc?: "hex"): number[] => {
5698
5745
  return new SHA512HMAC(key).update(msg, enc).digest();
5699
5746
  }
5700
5747
  ```
@@ -5851,16 +5898,19 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
5851
5898
 
5852
5899
  ```ts
5853
5900
  toHex = (msg: number[]): string => {
5854
- let res = "";
5855
- for (const num of msg) {
5856
- res += zero2(num.toString(16));
5901
+ if (CAN_USE_BUFFER) {
5902
+ return BufferCtor.from(msg).toString("hex");
5857
5903
  }
5858
- return res;
5904
+ if (msg.length === 0)
5905
+ return "";
5906
+ const out = new Array(msg.length);
5907
+ for (let i = 0; i < msg.length; i++) {
5908
+ out[i] = HEX_BYTE_STRINGS[msg[i] & 255];
5909
+ }
5910
+ return out.join("");
5859
5911
  }
5860
5912
  ```
5861
5913
 
5862
- See also: [zero2](./primitives.md#variable-zero2)
5863
-
5864
5914
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
5865
5915
 
5866
5916
  ---
@@ -418,20 +418,18 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
418
418
  ---
419
419
  ### Class: Script
420
420
 
421
- The Script class represents a script in a Bitcoin SV transaction,
422
- encapsulating the functionality to construct, parse, and serialize
423
- scripts used in both locking (output) and unlocking (input) scripts.
424
-
425
421
  ```ts
426
422
  export default class Script {
427
- chunks: ScriptChunk[];
428
423
  static fromASM(asm: string): Script
429
424
  static fromHex(hex: string): Script
430
425
  static fromBinary(bin: number[]): Script
431
- constructor(chunks: ScriptChunk[] = [])
426
+ constructor(chunks: ScriptChunk[] = [], rawBytesCache?: Uint8Array, hexCache?: string, parsed: boolean = true)
427
+ get chunks(): ScriptChunk[]
428
+ set chunks(value: ScriptChunk[])
432
429
  toASM(): string
433
430
  toHex(): string
434
431
  toBinary(): number[]
432
+ toUint8Array(): Uint8Array
435
433
  writeScript(script: Script): Script
436
434
  writeOpCode(op: number): Script
437
435
  setChunkOpCode(i: number, op: number): Script
@@ -451,7 +449,7 @@ See also: [BigNumber](./primitives.md#class-bignumber), [ScriptChunk](./script.m
451
449
  #### Constructor
452
450
 
453
451
  ```ts
454
- constructor(chunks: ScriptChunk[] = [])
452
+ constructor(chunks: ScriptChunk[] = [], rawBytesCache?: Uint8Array, hexCache?: string, parsed: boolean = true)
455
453
  ```
456
454
  See also: [ScriptChunk](./script.md#interface-scriptchunk)
457
455
 
@@ -459,6 +457,12 @@ Argument Details
459
457
 
460
458
  + **chunks**
461
459
  + =[] - An array of script chunks to directly initialize the script.
460
+ + **rawBytesCache**
461
+ + Optional serialized bytes that can be reused instead of reserializing `chunks`.
462
+ + **hexCache**
463
+ + Optional lowercase hex string that matches the serialized bytes, used to satisfy `toHex` quickly.
464
+ + **parsed**
465
+ + When false the script defers parsing `rawBytesCache` until `chunks` is accessed; defaults to true.
462
466
 
463
467
  #### Method findAndDelete
464
468
 
@@ -589,6 +589,7 @@ export class Beef {
589
589
  }
590
590
  toWriter(writer: Writer): void
591
591
  toBinary(): number[]
592
+ toUint8Array(): Uint8Array
592
593
  toBinaryAtomic(txid: string): number[]
593
594
  toHex(): string
594
595
  static fromReader(br: Reader): Beef
@@ -1594,6 +1595,7 @@ export default class Transaction {
1594
1595
  async sign(): Promise<void>
1595
1596
  async broadcast(broadcaster: Broadcaster = defaultBroadcaster()): Promise<BroadcastResponse | BroadcastFailure>
1596
1597
  toBinary(): number[]
1598
+ toUint8Array(): Uint8Array
1597
1599
  toEF(): number[]
1598
1600
  toHexEF(): string
1599
1601
  toHex(): string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.8.13",
3
+ "version": "1.9.0",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Software Development Kit",
6
6
  "main": "dist/cjs/mod.js",
package/src/auth/Peer.ts CHANGED
@@ -17,6 +17,8 @@ import * as Utils from '../primitives/utils.js'
17
17
  import { OriginatorDomainNameStringUnder250Bytes, WalletInterface } from '../wallet/Wallet.interfaces.js'
18
18
 
19
19
  const AUTH_VERSION = '0.1'
20
+ const BufferCtor =
21
+ typeof globalThis !== 'undefined' ? (globalThis as any).Buffer : undefined
20
22
 
21
23
  /**
22
24
  * Represents a peer capable of performing mutual authentication.
@@ -63,6 +65,7 @@ export class Peer {
63
65
  private lastInteractedWithPeer: string | undefined
64
66
 
65
67
  private readonly originator?: OriginatorDomainNameStringUnder250Bytes
68
+ private identityPublicKey?: string
66
69
 
67
70
  /**
68
71
  * Creates a new Peer instance
@@ -136,8 +139,7 @@ export class Peer {
136
139
  const generalMessage: AuthMessage = {
137
140
  version: AUTH_VERSION,
138
141
  messageType: 'general',
139
- identityKey: (await this.wallet.getPublicKey({ identityKey: true }, this.originator))
140
- .publicKey,
142
+ identityKey: await this.getIdentityPublicKey(),
141
143
  nonce: requestNonce,
142
144
  yourNonce: peerSession.peerNonce,
143
145
  payload: message,
@@ -186,7 +188,7 @@ export class Peer {
186
188
  // Prepare the message
187
189
  const requestNonce = Utils.toBase64(Random(32))
188
190
  const { signature } = await this.wallet.createSignature({
189
- data: Utils.toArray(JSON.stringify(certificatesToRequest), 'utf8'),
191
+ data: Peer.utf8ToBytes(JSON.stringify(certificatesToRequest)),
190
192
  protocolID: [2, 'auth message signature'],
191
193
  keyID: `${requestNonce} ${peerSession.peerNonce ?? ''}`,
192
194
  counterparty: peerSession.peerIdentityKey
@@ -195,8 +197,7 @@ export class Peer {
195
197
  const certRequestMessage: AuthMessage = {
196
198
  version: AUTH_VERSION,
197
199
  messageType: 'certificateRequest',
198
- identityKey: (await this.wallet.getPublicKey({ identityKey: true }, this.originator))
199
- .publicKey,
200
+ identityKey: await this.getIdentityPublicKey(),
200
201
  nonce: requestNonce,
201
202
  initialNonce: peerSession.sessionNonce,
202
203
  yourNonce: peerSession.peerNonce,
@@ -352,8 +353,7 @@ export class Peer {
352
353
  const initialRequest: AuthMessage = {
353
354
  version: AUTH_VERSION,
354
355
  messageType: 'initialRequest',
355
- identityKey: (await this.wallet.getPublicKey({ identityKey: true }, this.originator))
356
- .publicKey,
356
+ identityKey: await this.getIdentityPublicKey(),
357
357
  initialNonce: sessionNonce,
358
358
  requestedCertificates: this.certificatesToRequest
359
359
  }
@@ -525,7 +525,7 @@ export class Peer {
525
525
 
526
526
  // Create signature
527
527
  const { signature } = await this.wallet.createSignature({
528
- data: Utils.toArray(message.initialNonce + sessionNonce, 'base64'),
528
+ data: Peer.base64ToBytes(message.initialNonce + sessionNonce),
529
529
  protocolID: [2, 'auth message signature'],
530
530
  keyID: `${message.initialNonce} ${sessionNonce}`,
531
531
  counterparty: message.identityKey
@@ -534,8 +534,7 @@ export class Peer {
534
534
  const initialResponseMessage: AuthMessage = {
535
535
  version: AUTH_VERSION,
536
536
  messageType: 'initialResponse',
537
- identityKey: (await this.wallet.getPublicKey({ identityKey: true }, this.originator))
538
- .publicKey,
537
+ identityKey: await this.getIdentityPublicKey(),
539
538
  initialNonce: sessionNonce,
540
539
  yourNonce: message.initialNonce,
541
540
  certificates: certificatesToInclude,
@@ -574,9 +573,8 @@ export class Peer {
574
573
  }
575
574
 
576
575
  // Validate message signature
577
- const dataToVerify = Utils.toArray(
578
- (peerSession.sessionNonce ?? '') + (message.initialNonce ?? ''),
579
- 'base64'
576
+ const dataToVerify = Peer.base64ToBytes(
577
+ (peerSession.sessionNonce ?? '') + (message.initialNonce ?? '')
580
578
  )
581
579
  const { valid } = await this.wallet.verifySignature({
582
580
  data: dataToVerify,
@@ -668,7 +666,7 @@ export class Peer {
668
666
  }
669
667
 
670
668
  const { valid } = await this.wallet.verifySignature({
671
- data: Utils.toArray(JSON.stringify(message.requestedCertificates), 'utf8'),
669
+ data: Peer.utf8ToBytes(JSON.stringify(message.requestedCertificates)),
672
670
  signature: message.signature as number[],
673
671
  protocolID: [2, 'auth message signature'],
674
672
  keyID: `${message.nonce ?? ''} ${peerSession.sessionNonce ?? ''}`,
@@ -721,7 +719,7 @@ export class Peer {
721
719
  const peerSession = await this.getAuthenticatedSession(verifierIdentityKey)
722
720
  const requestNonce = Utils.toBase64(Random(32))
723
721
  const { signature } = await this.wallet.createSignature({
724
- data: Utils.toArray(JSON.stringify(certificates), 'utf8'),
722
+ data: Peer.utf8ToBytes(JSON.stringify(certificates)),
725
723
  protocolID: [2, 'auth message signature'],
726
724
  keyID: `${requestNonce} ${peerSession.peerNonce ?? ''}`,
727
725
  counterparty: peerSession.peerIdentityKey
@@ -730,8 +728,7 @@ export class Peer {
730
728
  const certificateResponse: AuthMessage = {
731
729
  version: AUTH_VERSION,
732
730
  messageType: 'certificateResponse',
733
- identityKey: (await this.wallet.getPublicKey({ identityKey: true }, this.originator))
734
- .publicKey,
731
+ identityKey: await this.getIdentityPublicKey(),
735
732
  nonce: requestNonce,
736
733
  initialNonce: peerSession.sessionNonce,
737
734
  yourNonce: peerSession.peerNonce,
@@ -772,7 +769,7 @@ export class Peer {
772
769
 
773
770
  // Validate message signature
774
771
  const { valid } = await this.wallet.verifySignature({
775
- data: Utils.toArray(JSON.stringify(message.certificates), 'utf8'),
772
+ data: Peer.utf8ToBytes(JSON.stringify(message.certificates)),
776
773
  signature: message.signature as number[],
777
774
  protocolID: [2, 'auth message signature'],
778
775
  keyID: `${message.nonce ?? ''} ${peerSession.sessionNonce ?? ''}`,
@@ -846,4 +843,33 @@ export class Peer {
846
843
  cb(message.identityKey, message.payload ?? [])
847
844
  })
848
845
  }
846
+
847
+ private async getIdentityPublicKey (): Promise<string> {
848
+ if (this.identityPublicKey != null) {
849
+ return this.identityPublicKey
850
+ }
851
+ const { publicKey } = await this.wallet.getPublicKey(
852
+ { identityKey: true },
853
+ this.originator
854
+ )
855
+ this.identityPublicKey = publicKey
856
+ return publicKey
857
+ }
858
+
859
+ private static utf8ToBytes (data: string): number[] {
860
+ if (BufferCtor != null) {
861
+ return Array.from(BufferCtor.from(data, 'utf8'))
862
+ }
863
+ if (typeof TextEncoder !== 'undefined') {
864
+ return Array.from(new TextEncoder().encode(data))
865
+ }
866
+ return Utils.toArray(data, 'utf8')
867
+ }
868
+
869
+ private static base64ToBytes (data: string): number[] {
870
+ if (BufferCtor != null) {
871
+ return Array.from(BufferCtor.from(data, 'base64'))
872
+ }
873
+ return Utils.toArray(data, 'base64')
874
+ }
849
875
  }
@@ -1,6 +1,19 @@
1
1
  // @ts-nocheck
2
2
  import ReductionContext from './ReductionContext.js'
3
3
 
4
+ const BufferCtor =
5
+ typeof globalThis !== 'undefined' ? (globalThis as any).Buffer : undefined
6
+ const CAN_USE_BUFFER =
7
+ BufferCtor != null && typeof BufferCtor.from === 'function'
8
+ const HEX_CHAR_TO_VALUE = new Int8Array(256).fill(-1)
9
+ for (let i = 0; i < 10; i++) {
10
+ HEX_CHAR_TO_VALUE[48 + i] = i // '0'-'9'
11
+ }
12
+ for (let i = 0; i < 6; i++) {
13
+ HEX_CHAR_TO_VALUE[65 + i] = 10 + i // 'A'-'F'
14
+ HEX_CHAR_TO_VALUE[97 + i] = 10 + i // 'a'-'f'
15
+ }
16
+
4
17
  /**
5
18
  * JavaScript numbers are only precise up to 53 bits. Since Bitcoin relies on
6
19
  * 256-bit cryptography, this BigNumber class enables operations on larger
@@ -1059,31 +1072,30 @@ export default class BigNumber {
1059
1072
  static fromSm (bytes: number[], endian: 'big' | 'little' = 'big'): BigNumber {
1060
1073
  if (bytes.length === 0) return new BigNumber(0n)
1061
1074
 
1075
+ const beBytes = bytes.slice()
1076
+ if (endian === 'little') {
1077
+ beBytes.reverse()
1078
+ }
1062
1079
  let sign: 0 | 1 = 0
1063
- let hex = ''
1080
+ if (beBytes.length > 0 && (beBytes[0] & 0x80) !== 0) {
1081
+ sign = 1
1082
+ beBytes[0] &= 0x7f
1083
+ }
1064
1084
 
1065
- if (endian === 'little') {
1066
- const last = bytes.length - 1
1067
- let firstByte = bytes[last]
1068
- if ((firstByte & 0x80) !== 0) { sign = 1; firstByte &= 0x7f }
1069
- hex += (firstByte < 16 ? '0' : '') + firstByte.toString(16)
1070
- for (let i = last - 1; i >= 0; i--) {
1071
- const b = bytes[i]
1072
- hex += (b < 16 ? '0' : '') + b.toString(16)
1073
- }
1085
+ let magnitude = 0n
1086
+ if (CAN_USE_BUFFER) {
1087
+ const hex = BufferCtor.from(beBytes).toString('hex') as string
1088
+ magnitude = hex.length === 0 ? 0n : BigInt('0x' + hex)
1074
1089
  } else {
1075
- let firstByte = bytes[0]
1076
- if ((firstByte & 0x80) !== 0) { sign = 1; firstByte &= 0x7f }
1077
- hex += (firstByte < 16 ? '0' : '') + firstByte.toString(16)
1078
- for (let i = 1; i < bytes.length; i++) {
1079
- const b = bytes[i]
1080
- hex += (b < 16 ? '0' : '') + b.toString(16)
1090
+ let hex = ''
1091
+ for (const byte of beBytes) {
1092
+ hex += byte < 16 ? '0' + byte.toString(16) : byte.toString(16)
1081
1093
  }
1094
+ magnitude = hex.length === 0 ? 0n : BigInt('0x' + hex)
1082
1095
  }
1083
1096
 
1084
- const mag = hex === '' ? 0n : BigInt('0x' + hex)
1085
1097
  const r = new BigNumber(0n)
1086
- r._initializeState(mag, sign)
1098
+ r._initializeState(magnitude, sign)
1087
1099
  return r
1088
1100
  }
1089
1101
 
@@ -1105,17 +1117,26 @@ export default class BigNumber {
1105
1117
  const byteLen = hex.length / 2
1106
1118
  const bytes = new Array(byteLen)
1107
1119
  for (let i = 0, j = 0; i < hex.length; i += 2) {
1108
- bytes[j++] = parseInt(hex.slice(i, i + 2), 16)
1120
+ const high = HEX_CHAR_TO_VALUE[hex.charCodeAt(i)]
1121
+ const low = HEX_CHAR_TO_VALUE[hex.charCodeAt(i + 1)]
1122
+ bytes[j++] = ((high & 0xf) << 4) | (low & 0xf)
1109
1123
  }
1110
1124
 
1125
+ let result: number[]
1111
1126
  if (this._sign === 1) {
1112
- if ((bytes[0] & 0x80) !== 0) bytes.unshift(0x80)
1113
- else bytes[0] |= 0x80
1127
+ if ((bytes[0] & 0x80) !== 0) {
1128
+ result = [0x80, ...bytes]
1129
+ } else {
1130
+ result = bytes.slice()
1131
+ result[0] |= 0x80
1132
+ }
1114
1133
  } else if ((bytes[0] & 0x80) !== 0) {
1115
- bytes.unshift(0x00)
1134
+ result = [0x00, ...bytes]
1135
+ } else {
1136
+ result = bytes.slice()
1116
1137
  }
1117
1138
 
1118
- return endian === 'little' ? bytes.reverse() : bytes
1139
+ return endian === 'little' ? result.reverse() : result
1119
1140
  }
1120
1141
 
1121
1142
  /**