@bsv/sdk 1.2.1 → 1.2.2

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/docs/compat.md CHANGED
@@ -16,6 +16,148 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
16
16
 
17
17
  ---
18
18
 
19
+ ### Class: ECIES
20
+
21
+ ```ts
22
+ export default class ECIES {
23
+ public static ivkEkM(privKey: PrivateKey, pubKey: PublicKey): {
24
+ iv: number[];
25
+ kE: number[];
26
+ kM: number[];
27
+ }
28
+ public static electrumEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, noKey = false): number[]
29
+ public static electrumDecrypt(encBuf: number[], toPrivateKey: PrivateKey, fromPublicKey?: PublicKey): number[]
30
+ public static bitcoreEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, ivBuf?: number[]): number[]
31
+ public static bitcoreDecrypt(encBuf: number[], toPrivateKey: PrivateKey): number[]
32
+ }
33
+ ```
34
+
35
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey)
36
+
37
+ <details>
38
+
39
+ <summary>Class ECIES Details</summary>
40
+
41
+ #### Method bitcoreDecrypt
42
+
43
+ Decrypts a message encrypted using the Bitcore variant of ECIES.
44
+
45
+ ```ts
46
+ public static bitcoreDecrypt(encBuf: number[], toPrivateKey: PrivateKey): number[]
47
+ ```
48
+ See also: [PrivateKey](#class-privatekey)
49
+
50
+ Returns
51
+
52
+ The decrypted message as a number array.
53
+
54
+ Argument Details
55
+
56
+ + **encBuf**
57
+ + The encrypted message buffer.
58
+ + **toPrivateKey**
59
+ + The private key of the recipient.
60
+
61
+ #### Method bitcoreEncrypt
62
+
63
+ Encrypts a given message using the Bitcore variant of ECIES.
64
+
65
+ ```ts
66
+ public static bitcoreEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, ivBuf?: number[]): number[]
67
+ ```
68
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey)
69
+
70
+ Returns
71
+
72
+ The encrypted message as a number array.
73
+
74
+ Argument Details
75
+
76
+ + **messageBuf**
77
+ + The message to be encrypted, in number array format.
78
+ + **toPublicKey**
79
+ + The public key of the recipient.
80
+ + **fromPrivateKey**
81
+ + The private key of the sender. If not provided, a random private key is used.
82
+ + **ivBuf**
83
+ + The initialization vector for encryption. If not provided, a random IV is used.
84
+
85
+ #### Method electrumDecrypt
86
+
87
+ Decrypts a message encrypted using the Electrum ECIES method.
88
+
89
+ ```ts
90
+ public static electrumDecrypt(encBuf: number[], toPrivateKey: PrivateKey, fromPublicKey?: PublicKey): number[]
91
+ ```
92
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey)
93
+
94
+ Returns
95
+
96
+ The decrypted message as a number array.
97
+
98
+ Argument Details
99
+
100
+ + **encBuf**
101
+ + The encrypted message buffer.
102
+ + **toPrivateKey**
103
+ + The private key of the recipient.
104
+ + **fromPublicKey**
105
+ + The public key of the sender. If not provided, it is extracted from the message.
106
+
107
+ #### Method electrumEncrypt
108
+
109
+ Encrypts a given message using the Electrum ECIES method.
110
+
111
+ ```ts
112
+ public static electrumEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, noKey = false): number[]
113
+ ```
114
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey)
115
+
116
+ Returns
117
+
118
+ The encrypted message as a number array.
119
+
120
+ Argument Details
121
+
122
+ + **messageBuf**
123
+ + The message to be encrypted, in number array format.
124
+ + **toPublicKey**
125
+ + The public key of the recipient.
126
+ + **fromPrivateKey**
127
+ + The private key of the sender. If not provided, a random private key is used.
128
+ + **noKey**
129
+ + If true, does not include the sender's public key in the encrypted message.
130
+
131
+ #### Method ivkEkM
132
+
133
+ Generates the initialization vector (iv), encryption key (kE), and MAC key (kM)
134
+ using the sender's private key and receiver's public key.
135
+
136
+ ```ts
137
+ public static ivkEkM(privKey: PrivateKey, pubKey: PublicKey): {
138
+ iv: number[];
139
+ kE: number[];
140
+ kM: number[];
141
+ }
142
+ ```
143
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey)
144
+
145
+ Returns
146
+
147
+ An object containing the iv, kE, and kM as number arrays.
148
+
149
+ Argument Details
150
+
151
+ + **privKey**
152
+ + The sender's private key.
153
+ + **pubKey**
154
+ + The receiver's public key.
155
+
156
+ </details>
157
+
158
+ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
159
+
160
+ ---
19
161
  ### Class: HD
20
162
 
21
163
  ```ts
@@ -49,6 +191,8 @@ export default class HD {
49
191
  }
50
192
  ```
51
193
 
194
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey)
195
+
52
196
  <details>
53
197
 
54
198
  <summary>Class HD Details</summary>
@@ -61,6 +205,7 @@ Initializes an HD wallet with optional parameters for version bytes, depth, pare
61
205
  ```ts
62
206
  constructor(versionBytesNum?: number, depth?: number, parentFingerPrint?: number[], childIndex?: number, chainCode?: number[], privKey?: PrivateKey, pubKey?: PublicKey)
63
207
  ```
208
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey)
64
209
 
65
210
  Argument Details
66
211
 
@@ -87,6 +232,7 @@ The path specifies the hierarchy of the child key to be derived.
87
232
  ```ts
88
233
  public derive(path: string): HD
89
234
  ```
235
+ See also: [HD](#class-hd)
90
236
 
91
237
  Returns
92
238
 
@@ -105,6 +251,7 @@ This method generates either a private or public child key depending on the curr
105
251
  ```ts
106
252
  public deriveChild(i: number): HD
107
253
  ```
254
+ See also: [HD](#class-hd)
108
255
 
109
256
  Returns
110
257
 
@@ -123,6 +270,7 @@ Parses a binary buffer to set up the wallet's properties.
123
270
  ```ts
124
271
  public static fromBinary(buf: number[]): HD
125
272
  ```
273
+ See also: [HD](#class-hd)
126
274
 
127
275
  Returns
128
276
 
@@ -172,6 +320,7 @@ This method creates a root HD wallet with randomly generated private and public
172
320
  ```ts
173
321
  public static fromRandom(): HD
174
322
  ```
323
+ See also: [HD](#class-hd)
175
324
 
176
325
  Returns
177
326
 
@@ -185,6 +334,7 @@ This method generates keys and other properties from a given seed, conforming to
185
334
  ```ts
186
335
  public static fromSeed(bytes: number[]): HD
187
336
  ```
337
+ See also: [HD](#class-hd)
188
338
 
189
339
  Returns
190
340
 
@@ -221,6 +371,7 @@ This method decodes a provided string to set up the HD wallet's properties.
221
371
  ```ts
222
372
  public static fromString(str: string): HD
223
373
  ```
374
+ See also: [HD](#class-hd)
224
375
 
225
376
  Returns
226
377
 
@@ -283,6 +434,7 @@ This method strips away the private key information, leaving only the public par
283
434
  ```ts
284
435
  public toPublic(): HD
285
436
  ```
437
+ See also: [HD](#class-hd)
286
438
 
287
439
  Returns
288
440
 
@@ -337,6 +489,8 @@ export default class Mnemonic {
337
489
  }
338
490
  ```
339
491
 
492
+ See also: [wordList](#variable-wordlist)
493
+
340
494
  <details>
341
495
 
342
496
  <summary>Class Mnemonic Details</summary>
@@ -348,6 +502,7 @@ Constructs a Mnemonic object.
348
502
  ```ts
349
503
  constructor(mnemonic?: string, seed?: number[], wordlist = wordList)
350
504
  ```
505
+ See also: [wordList](#variable-wordlist)
351
506
 
352
507
  Argument Details
353
508
 
@@ -445,6 +600,7 @@ Static method to create a Mnemonic instance from a given entropy.
445
600
  ```ts
446
601
  public static fromEntropy(buf: number[]): Mnemonic
447
602
  ```
603
+ See also: [Mnemonic](#class-mnemonic)
448
604
 
449
605
  Returns
450
606
 
@@ -483,6 +639,7 @@ Static method to generate a Mnemonic instance with a random mnemonic.
483
639
  ```ts
484
640
  public static fromRandom(bits?: number): Mnemonic
485
641
  ```
642
+ See also: [Mnemonic](#class-mnemonic)
486
643
 
487
644
  Returns
488
645
 
@@ -517,6 +674,7 @@ Static method to create a Mnemonic instance from a mnemonic string.
517
674
  ```ts
518
675
  public static fromString(str: string): Mnemonic
519
676
  ```
677
+ See also: [Mnemonic](#class-mnemonic)
520
678
 
521
679
  Returns
522
680
 
@@ -640,141 +798,6 @@ The mnemonic phrase as a string.
640
798
 
641
799
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
642
800
 
643
- ---
644
- ### Class: ECIES
645
-
646
- ```ts
647
- export default class ECIES {
648
- public static ivkEkM(privKey: PrivateKey, pubKey: PublicKey): {
649
- iv: number[];
650
- kE: number[];
651
- kM: number[];
652
- }
653
- public static electrumEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, noKey = false): number[]
654
- public static electrumDecrypt(encBuf: number[], toPrivateKey: PrivateKey, fromPublicKey?: PublicKey): number[]
655
- public static bitcoreEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, ivBuf?: number[]): number[]
656
- public static bitcoreDecrypt(encBuf: number[], toPrivateKey: PrivateKey): number[]
657
- }
658
- ```
659
-
660
- <details>
661
-
662
- <summary>Class ECIES Details</summary>
663
-
664
- #### Method bitcoreDecrypt
665
-
666
- Decrypts a message encrypted using the Bitcore variant of ECIES.
667
-
668
- ```ts
669
- public static bitcoreDecrypt(encBuf: number[], toPrivateKey: PrivateKey): number[]
670
- ```
671
-
672
- Returns
673
-
674
- The decrypted message as a number array.
675
-
676
- Argument Details
677
-
678
- + **encBuf**
679
- + The encrypted message buffer.
680
- + **toPrivateKey**
681
- + The private key of the recipient.
682
-
683
- #### Method bitcoreEncrypt
684
-
685
- Encrypts a given message using the Bitcore variant of ECIES.
686
-
687
- ```ts
688
- public static bitcoreEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, ivBuf?: number[]): number[]
689
- ```
690
-
691
- Returns
692
-
693
- The encrypted message as a number array.
694
-
695
- Argument Details
696
-
697
- + **messageBuf**
698
- + The message to be encrypted, in number array format.
699
- + **toPublicKey**
700
- + The public key of the recipient.
701
- + **fromPrivateKey**
702
- + The private key of the sender. If not provided, a random private key is used.
703
- + **ivBuf**
704
- + The initialization vector for encryption. If not provided, a random IV is used.
705
-
706
- #### Method electrumDecrypt
707
-
708
- Decrypts a message encrypted using the Electrum ECIES method.
709
-
710
- ```ts
711
- public static electrumDecrypt(encBuf: number[], toPrivateKey: PrivateKey, fromPublicKey?: PublicKey): number[]
712
- ```
713
-
714
- Returns
715
-
716
- The decrypted message as a number array.
717
-
718
- Argument Details
719
-
720
- + **encBuf**
721
- + The encrypted message buffer.
722
- + **toPrivateKey**
723
- + The private key of the recipient.
724
- + **fromPublicKey**
725
- + The public key of the sender. If not provided, it is extracted from the message.
726
-
727
- #### Method electrumEncrypt
728
-
729
- Encrypts a given message using the Electrum ECIES method.
730
-
731
- ```ts
732
- public static electrumEncrypt(messageBuf: number[], toPublicKey: PublicKey, fromPrivateKey?: PrivateKey, noKey = false): number[]
733
- ```
734
-
735
- Returns
736
-
737
- The encrypted message as a number array.
738
-
739
- Argument Details
740
-
741
- + **messageBuf**
742
- + The message to be encrypted, in number array format.
743
- + **toPublicKey**
744
- + The public key of the recipient.
745
- + **fromPrivateKey**
746
- + The private key of the sender. If not provided, a random private key is used.
747
- + **noKey**
748
- + If true, does not include the sender's public key in the encrypted message.
749
-
750
- #### Method ivkEkM
751
-
752
- Generates the initialization vector (iv), encryption key (kE), and MAC key (kM)
753
- using the sender's private key and receiver's public key.
754
-
755
- ```ts
756
- public static ivkEkM(privKey: PrivateKey, pubKey: PublicKey): {
757
- iv: number[];
758
- kE: number[];
759
- kM: number[];
760
- }
761
- ```
762
-
763
- Returns
764
-
765
- An object containing the iv, kE, and kM as number arrays.
766
-
767
- Argument Details
768
-
769
- + **privKey**
770
- + The sender's private key.
771
- + **pubKey**
772
- + The receiver's public key.
773
-
774
- </details>
775
-
776
- Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
777
-
778
801
  ---
779
802
  ## Functions
780
803
 
@@ -800,6 +823,8 @@ export default function fromUtxo(utxo: jsonUtxo, unlockingScriptTemplate: {
800
823
  }): TransactionInput
801
824
  ```
802
825
 
826
+ See also: [Transaction](#class-transaction), [TransactionInput](#interface-transactioninput), [UnlockingScript](#class-unlockingscript), [sign](#variable-sign)
827
+
803
828
  <details>
804
829
 
805
830
  <summary>Function fromUtxo Details</summary>
@@ -846,6 +871,8 @@ magicHash = (messageBuf: number[]): number[] => {
846
871
  }
847
872
  ```
848
873
 
874
+ See also: [Writer](#class-writer), [hash256](#variable-hash256), [toArray](#variable-toarray)
875
+
849
876
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
850
877
 
851
878
  ---
@@ -864,6 +891,8 @@ sign = (message: number[], privateKey: PrivateKey, mode: "raw" | "base64" = "bas
864
891
  }
865
892
  ```
866
893
 
894
+ See also: [BigNumber](#class-bignumber), [PrivateKey](#class-privatekey), [Signature](#class-signature), [magicHash](#variable-magichash)
895
+
867
896
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
868
897
 
869
898
  ---
@@ -876,6 +905,8 @@ verify = (message: number[], sig: Signature, pubKey: PublicKey): boolean => {
876
905
  }
877
906
  ```
878
907
 
908
+ See also: [BigNumber](#class-bignumber), [PublicKey](#class-publickey), [Signature](#class-signature), [magicHash](#variable-magichash)
909
+
879
910
  Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
880
911
 
881
912
  ---
package/docs/messages.md CHANGED
@@ -19,6 +19,66 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Variables](#v
19
19
 
20
20
  ---
21
21
 
22
+ ### Variable: decrypt
23
+
24
+ ```ts
25
+ decrypt = (message: number[], recipient: PrivateKey): number[] => {
26
+ const reader = new Reader(message);
27
+ const messageVersion = toHex(reader.read(4));
28
+ if (messageVersion !== VERSION) {
29
+ throw new Error(`Message version mismatch: Expected ${VERSION}, received ${messageVersion}`);
30
+ }
31
+ const sender = PublicKey.fromString(toHex(reader.read(33)));
32
+ const expectedRecipientDER = toHex(reader.read(33));
33
+ const actualRecipientDER = recipient.toPublicKey().encode(true, "hex") as string;
34
+ if (expectedRecipientDER !== actualRecipientDER) {
35
+ throw new Error(`The encrypted message expects a recipient public key of ${expectedRecipientDER}, but the provided key is ${actualRecipientDER}`);
36
+ }
37
+ const keyID = toBase64(reader.read(32));
38
+ const encrypted = reader.read(reader.bin.length - reader.pos);
39
+ const invoiceNumber = `2-message encryption-${keyID}`;
40
+ const signingPriv = sender.deriveChild(recipient, invoiceNumber);
41
+ const recipientPub = recipient.deriveChild(sender, invoiceNumber);
42
+ const sharedSecret = signingPriv.deriveSharedSecret(recipientPub);
43
+ const symmetricKey = new SymmetricKey(sharedSecret.encode(true).slice(1));
44
+ return symmetricKey.decrypt(encrypted) as number[];
45
+ }
46
+ ```
47
+
48
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey), [Reader](#class-reader), [SymmetricKey](#class-symmetrickey), [encode](#variable-encode), [toBase64](#function-tobase64), [toHex](#variable-tohex)
49
+
50
+ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Variables](#variables)
51
+
52
+ ---
53
+ ### Variable: encrypt
54
+
55
+ ```ts
56
+ encrypt = (message: number[], sender: PrivateKey, recipient: PublicKey): number[] => {
57
+ const keyID = Random(32);
58
+ const keyIDBase64 = toBase64(keyID);
59
+ const invoiceNumber = `2-message encryption-${keyIDBase64}`;
60
+ const signingPriv = sender.deriveChild(recipient, invoiceNumber);
61
+ const recipientPub = recipient.deriveChild(sender, invoiceNumber);
62
+ const sharedSecret = signingPriv.deriveSharedSecret(recipientPub);
63
+ const symmetricKey = new SymmetricKey(sharedSecret.encode(true).slice(1));
64
+ const encrypted = symmetricKey.encrypt(message) as number[];
65
+ const senderPublicKey = sender.toPublicKey().encode(true);
66
+ const version = toArray(VERSION, "hex");
67
+ return [
68
+ ...version,
69
+ ...senderPublicKey,
70
+ ...recipient.encode(true),
71
+ ...keyID,
72
+ ...encrypted
73
+ ];
74
+ }
75
+ ```
76
+
77
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey), [SymmetricKey](#class-symmetrickey), [encode](#variable-encode), [toArray](#variable-toarray), [toBase64](#function-tobase64)
78
+
79
+ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Variables](#variables)
80
+
81
+ ---
22
82
  ### Variable: sign
23
83
 
24
84
  ```ts
@@ -47,6 +107,8 @@ sign = (message: number[], signer: PrivateKey, verifier?: PublicKey): number[] =
47
107
  }
48
108
  ```
49
109
 
110
+ See also: [Curve](#class-curve), [PrivateKey](#class-privatekey), [PublicKey](#class-publickey), [encode](#variable-encode), [toArray](#variable-toarray), [toBase64](#function-tobase64)
111
+
50
112
  Links: [API](#api), [Classes](#classes), [Functions](#functions), [Variables](#variables)
51
113
 
52
114
  ---
@@ -85,61 +147,7 @@ verify = (message: number[], sig: number[], recipient?: PrivateKey): boolean =>
85
147
  }
86
148
  ```
87
149
 
88
- Links: [API](#api), [Classes](#classes), [Functions](#functions), [Variables](#variables)
89
-
90
- ---
91
- ### Variable: encrypt
92
-
93
- ```ts
94
- encrypt = (message: number[], sender: PrivateKey, recipient: PublicKey): number[] => {
95
- const keyID = Random(32);
96
- const keyIDBase64 = toBase64(keyID);
97
- const invoiceNumber = `2-message encryption-${keyIDBase64}`;
98
- const signingPriv = sender.deriveChild(recipient, invoiceNumber);
99
- const recipientPub = recipient.deriveChild(sender, invoiceNumber);
100
- const sharedSecret = signingPriv.deriveSharedSecret(recipientPub);
101
- const symmetricKey = new SymmetricKey(sharedSecret.encode(true).slice(1));
102
- const encrypted = symmetricKey.encrypt(message) as number[];
103
- const senderPublicKey = sender.toPublicKey().encode(true);
104
- const version = toArray(VERSION, "hex");
105
- return [
106
- ...version,
107
- ...senderPublicKey,
108
- ...recipient.encode(true),
109
- ...keyID,
110
- ...encrypted
111
- ];
112
- }
113
- ```
114
-
115
- Links: [API](#api), [Classes](#classes), [Functions](#functions), [Variables](#variables)
116
-
117
- ---
118
- ### Variable: decrypt
119
-
120
- ```ts
121
- decrypt = (message: number[], recipient: PrivateKey): number[] => {
122
- const reader = new Reader(message);
123
- const messageVersion = toHex(reader.read(4));
124
- if (messageVersion !== VERSION) {
125
- throw new Error(`Message version mismatch: Expected ${VERSION}, received ${messageVersion}`);
126
- }
127
- const sender = PublicKey.fromString(toHex(reader.read(33)));
128
- const expectedRecipientDER = toHex(reader.read(33));
129
- const actualRecipientDER = recipient.toPublicKey().encode(true, "hex") as string;
130
- if (expectedRecipientDER !== actualRecipientDER) {
131
- throw new Error(`The encrypted message expects a recipient public key of ${expectedRecipientDER}, but the provided key is ${actualRecipientDER}`);
132
- }
133
- const keyID = toBase64(reader.read(32));
134
- const encrypted = reader.read(reader.bin.length - reader.pos);
135
- const invoiceNumber = `2-message encryption-${keyID}`;
136
- const signingPriv = sender.deriveChild(recipient, invoiceNumber);
137
- const recipientPub = recipient.deriveChild(sender, invoiceNumber);
138
- const sharedSecret = signingPriv.deriveSharedSecret(recipientPub);
139
- const symmetricKey = new SymmetricKey(sharedSecret.encode(true).slice(1));
140
- return symmetricKey.decrypt(encrypted) as number[];
141
- }
142
- ```
150
+ See also: [PrivateKey](#class-privatekey), [PublicKey](#class-publickey), [Reader](#class-reader), [Signature](#class-signature), [encode](#variable-encode), [toBase64](#function-tobase64), [toHex](#variable-tohex)
143
151
 
144
152
  Links: [API](#api), [Classes](#classes), [Functions](#functions), [Variables](#variables)
145
153