@bsv/sdk 1.0.9 → 1.0.11

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 (34) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/primitives/BigNumber.js +3 -3
  3. package/dist/cjs/src/primitives/PrivateKey.js +34 -2
  4. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
  5. package/dist/cjs/src/primitives/utils.js +5 -2
  6. package/dist/cjs/src/primitives/utils.js.map +1 -1
  7. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  8. package/dist/esm/src/primitives/BigNumber.js +3 -3
  9. package/dist/esm/src/primitives/PrivateKey.js +34 -2
  10. package/dist/esm/src/primitives/PrivateKey.js.map +1 -1
  11. package/dist/esm/src/primitives/utils.js +5 -2
  12. package/dist/esm/src/primitives/utils.js.map +1 -1
  13. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  14. package/dist/types/src/primitives/BigNumber.d.ts +3 -3
  15. package/dist/types/src/primitives/PrivateKey.d.ts +17 -1
  16. package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -1
  17. package/dist/types/src/primitives/utils.d.ts.map +1 -1
  18. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  19. package/docs/low-level/AES_SYMMETRIC_ENCRYPTION.md +40 -0
  20. package/docs/low-level/ECDH.md +64 -0
  21. package/docs/low-level/NUMBERS_POINTS.md +116 -0
  22. package/docs/low-level/README.md +7 -0
  23. package/docs/low-level/TX_SIG.md +9 -6
  24. package/docs/low-level/TYPE_42.md +53 -0
  25. package/docs/low-level/USING_ECDSA.md +15 -4
  26. package/docs/low-level/USING_HASHES_AND_HMACS.md +79 -0
  27. package/docs/low-level/USING_PRIVATE_PUBLIC_KEYS.md +70 -0
  28. package/docs/low-level/USING_SCRIPTS.md +71 -0
  29. package/package.json +1 -1
  30. package/src/primitives/BigNumber.ts +3 -3
  31. package/src/primitives/PrivateKey.ts +38 -2
  32. package/src/primitives/__tests/bug-31.test.ts +33 -0
  33. package/src/primitives/utils.ts +5 -2
  34. package/docs/low-level/AES_SYMMETRIC_ENCRYPTION +0 -44
@@ -0,0 +1,53 @@
1
+ # Type 42 Key Derivation
2
+
3
+ The BSV SDK supports type-42 key derivation, which is a way for two people who have master keys to derive child keys from one another, given a specific string called an invoice number. They can then use these keys for message signing, message encryption or any other purpose.
4
+
5
+ This guide will cover the process of type-42 derivation, the steps involved, and then demonstrate a simple example of using it for message signing. Finally, we will talk about the "anyone key" and why it can sometimes be useful within type-42 systems.
6
+
7
+ ## The Process
8
+
9
+ The process starts with two users, who each generate a "master key" from which everything else will be based. Then, they share the associated public key with the other party.
10
+
11
+ Next, they make use of [ECDH](./ECDH.md) to arrive at a shared secret between themselves. They agree on the "invoice number" they will be using to communicate. We call it an invoice number because type-42 has historically been used for Bitcoin payments. In reality, this is just the unique identifier for the keys to derive.
12
+
13
+ Now, they compute an [HMAC](./USING_HASHES_AND_HMACS.md) over the invoice number, using the shared secret as the HMAC key. Because the shared secret is private, the HMAC hash function initialized with its value is only usable by the two parties. This means only these two people can hash the invoice number in this unique way.
14
+
15
+ Finally, the output of the HMAC function over the invoice number is used for key derivation. If Alice wants to derive a key from Bob, she will add this HMAC output to Bob's master public key. Conversely, if Bob wants to derive his own private key to match, he can add the same vlue to his original master private key.
16
+
17
+ Every invoice number leads to a unique, privately-derivable key in a "shared key universe" occupied only by Alice and Bob. Alice can derive public keys for Bob, and Bob can derive the corresponding private keys. Conversely, Bob can derive public keys for Alice, and Alice can derive the corresponding private keys. They just need to agree on the right invoice number to use, and know each other's master public keys.
18
+
19
+ Because no one else can compute a shared secret between these two values, no one else can use the special HMAC function over the invoice numbers, and thus no one else can link the master keys of either party to any other party.
20
+
21
+ ## Practical Example
22
+
23
+ Let's use the BSV SDK to create a practical example. Here, Alice and Bob will generate private keys. Then, Alice will sign a message privately for Bob to verify, using a specific invoice number. Finally, Bob will use the invoice number to deriv Alice's child signing public key, so he can verify the message that Alice signed for him:
24
+
25
+ ```ts
26
+ import { PrivateKey, Utils } from '@bsv/sdk'
27
+
28
+ const alice = PrivateKey.fromRandom()
29
+ const alicePub = alice.toPublicKey()
30
+
31
+ const bob = PrivateKey.fromRandom()
32
+ const bobPub = bob.toPublicKey()
33
+
34
+ // Both parties agree on an invoice number to use
35
+ const invoiceNumber = '2-simple signing protocol-1'
36
+
37
+ // Alice derives a child private key for signing
38
+ const aliceSigningChild = alice.deriveChild(bobPub, invoiceNumber)
39
+
40
+ // Alice signs a message for Bob
41
+ const message = Utils.toArray('Hi Bob', 'utf8')
42
+ const signature = aliceSigningChild.sign(message)
43
+
44
+ // Bob derives Alice's correct signing public key from her master public key
45
+ const aliceSigningPub = alicePub.deriveChild(bob, invoiceNumber)
46
+
47
+ // Now, Bob can privately verify Alice's signature
48
+ const verified = aliceSigningPub.verify(message, signature)
49
+ console.log(verified)
50
+ // true
51
+ ```
52
+
53
+ This enables people to securely agree on which keys to use, and derive keys for one another privately.
@@ -1,19 +1,30 @@
1
1
  # Low-level: Making Use of ECDSA with Public and Private Keys
2
2
 
3
- In this tutorial, we will learn how to use ECDSA with asymmetric public and private key pairs. Note, this is a low-level tutorial and should only be used if you would like to add advanced features to your applications.
3
+ In this tutorial, we will learn how to use ECDSA with asymmetric public and private key pairs. Note, this is a low-level tutorial and should only be used if the SDK's higher-level [Message Signing Capabilities](../examples/EXAMPLE_MESSAGE_SIGNING.md) do not meet your needs.
4
4
 
5
5
  ## Getting Started
6
6
 
7
- First, you will want to make sure you have installed the required dependencies.
7
+ First, you'll need to import the `PrivateKey` function. We'll also import `utils` so we can represent our messages in human-readable formats.
8
8
 
9
9
  ```ts
10
10
  import { PrivateKey, Utils } from '@bsv/sdk'
11
11
  ```
12
12
 
13
+ Now, let's generate a random private key and sign a message with it.
14
+
13
15
  ```ts
14
16
  const privateKey = PrivateKey.fromRandom()
15
17
  const message = Utils.toArray('Message to sign!')
18
+
19
+ // The .sign() method creates an ECDSA digital signature for the message from the private key.
16
20
  const signature = privateKey.sign(message)
17
- const valid = privateKey.verify(message, signature)
21
+
22
+ // Anyone with the corresponding public key can now verify the message.
23
+ const publicKey = privateKey.toPublicKey()
24
+
25
+ // The .verify() method is used for message verification
26
+ const valid = publicKey.verify(message, signature)
18
27
  // console.log(valid) --> true
19
- ```
28
+ ```
29
+
30
+ Anyone who knows the public key can verify the message, even if they don't have the private key. You can sign Bitcoin transactions, documents, or any other type of information with ECDSA. Changing the message will invalidate the signature.
@@ -0,0 +1,79 @@
1
+ # Using Hashes and HMACs
2
+
3
+ Hashes allow you to check the integrity of a message, by providing a deterministic one-way function that ransforms the input data. Hashes are widely useful, especially within Bitcoin. The BSV SDK provides first-class support for the hashing functions used within Bitcoin and its scripting language.
4
+
5
+ In this tutorial, we will learn how to use hashes and HMACs (Hash-based Message Authentication Codes) to verify the integrity of information, ensuring it has not been changed.
6
+
7
+ ## Setting Up
8
+
9
+ First, install the `@bsv/sdk` package into your project. We will be using two SDK modules for this exercise:
10
+
11
+ - **Hash** - contains the hash and HMAC functions we will be using.
12
+ - **Utils** - the SDK provides several helpful utility functions such as `toArray` and and `toUTF8` which allow you to encode and transform data as needed.
13
+
14
+ ```ts
15
+ import { Hash, Utils } from '@bsv/sdk'
16
+ ```
17
+
18
+ ## Creating a Hash
19
+
20
+ The following code performs a straightforward SHA256 hash of a message with the resulting hash returned as a hex string.
21
+
22
+ This is a one-way process where the input message is transformed into a fixed-size string (the hash), which acts as a unique representation of the input data. It's primarily used for verifying data integrity.
23
+
24
+ ```ts
25
+ let sha256Hasher = new Hash.SHA256()
26
+ sha256Hasher.update('Message to hash')
27
+ let hashedMessage = sha256Hasher.digestHex()
28
+ // console.log(hashedMessage)
29
+ // -> f1aa45b0f5f6703468f9b9bc2b9874d4fa6b001a170d0f132aa5a26d00d0c7e5
30
+ ```
31
+
32
+ A binary hash can also be produced by using the `digest` function instead of `digestHex`.
33
+
34
+ ```ts
35
+ let hmacMessage = hmacHasher.digest()
36
+ ```
37
+
38
+ Other hashing algorithms are also supported including the following:
39
+ - `Hash.RIPEMD160`
40
+ - `Hash.SHA1`
41
+ - `Hash.SHA512`
42
+
43
+ There are also shorthand helpers, which will construct the hashesr and digest the message automatically. For example, we can use the `hash.sha256` function as follows:
44
+
45
+ ```ts
46
+ const result = Hash.sha256(toArray('hello, world', 'utf8'))
47
+ ```
48
+
49
+ In addition to simple hashes, the library also support HMAC functions.
50
+
51
+ ## Creating an HMAC
52
+
53
+ In comparison to standard hashing, the following code introduces HMAC which combines a secret key with the hashing process to provide both data integrity and authentication.
54
+
55
+ A key is involved in the hashing process, making the output hash specific not just to the message but also to the key. This means the same message hashed with a different key would produce a different result, adding a layer of security.
56
+
57
+ ```ts
58
+ let hmacHasher = new Hash.SHA256HMAC('key')
59
+ hmacHasher.update('Message to hash')
60
+ let hmacMessageHex = hmacHasher.digestHex()
61
+ // console.log
62
+ // -> 41495ec4a050f4059f20c8722b6308efe6e0a90a6a4886b02a31d22180db367c
63
+ ```
64
+
65
+ Just as with hashes, a binary hmac message can also be produced by using the `digest` function instead of `digestHex`.
66
+
67
+ ```ts
68
+ let hmacMessage = hmacHasher.digest()
69
+ ```
70
+
71
+ In addition to SHA256 based HMACs, `Hash.SHA512HMAC` is also supported which uses the SHA-512 cryptographic hash function.
72
+
73
+ Just as with hashes, there are also shorthand helpers available for HMACs:
74
+
75
+ ```ts
76
+ const result = Hash.sha256hmac('key', 'message')
77
+ ```
78
+
79
+ This guide has explored the core concepts and practical applications of hashing and HMACs using the BSV SDK. Whether you're verifying the integrity of data, securing communications, or implementing authentication protocols, these tools will serve you well in your development journey.
@@ -0,0 +1,70 @@
1
+ # Generating, Serializing, and Deserializing Private and Public Keys
2
+
3
+ Private keys protect Bitcoins, enable secure message signing, among other vital roles in the BSV ecosystem. Public keys enable ownership tracking, message attestation, and attribution for signed messages, among other use-cases. In this low-level tutorial, we will learn how to generate, serialize and deserialize public and private keys using the functions provided by the SDK.
4
+
5
+ ## Getting Set Up
6
+
7
+ We'll be making use of two SDK modules in this guide. Make sure you've installed the `@bsv/sdk` package in your project, then import the modules we'll be using:
8
+
9
+ - **PrivateKey** - this class will enable to you create a new PrivateKey from various sources such as random, hex string, and WIP. It can also transform a private key into a public key.
10
+ - **Utils** - the SDK provides several helpful utility functions such as `toArray` and and `toUTF8` which allow you to serialize and deserialize data as needed.
11
+
12
+ ```ts
13
+ import { PrivateKey, Utils } from '@bsv/sdk'
14
+ ```
15
+
16
+ ## Generating and Deserializing Keys
17
+ First, we will learn how to generate new cryptographic keys to be used within your applications. Unless generating a new private key from random, this will most often involve deserializing a key from various types the most common being such as hex, WIF, and binary.
18
+
19
+ Here is an example of how this can be done:
20
+
21
+ ```ts
22
+ const privKeyRandom = PrivateKey.fromRandom()
23
+ const privKeyFromHex = PrivateKey.fromHex('08dcced21ebf831cb1b1d320c5de2dee690ebea9b4930a7f1af9b7bde8f7858a')
24
+ const privKeyFromHex2 = PrivateKey.fromString('08dcced21ebf831cb1b1d320c5de2dee690ebea9b4930a7f1af9b7bde8f7858a', 'hex')
25
+ const privKeyFromWif = PrivateKey.fromWif('L3dyA911FSFwSpgzRFhncUTRPk57aNTHkEhRtXoi4W7fz63bR45W')
26
+ const privKeyFromBinary = new PrivateKey(Utils.toArray('e0f6f9084f02a59cdc0aa9498b28fe8e20d0d4eeeb19af629761099210990894', 'hex'))
27
+ ```
28
+
29
+ We can then transform these to find the corresponding public key as follows:
30
+
31
+ ```ts
32
+ const privKeyRandom = PrivateKey.fromRandom()
33
+ // Get the corresponding public key
34
+ let pubKey = privKeyRandom.toPublicKey()
35
+ ```
36
+
37
+ ## Serializing Keys
38
+
39
+ Sometimes you will want to convert private / public keys into a format that can be more easily transported in your applications such as hex or binary.
40
+
41
+ Let's explore the available functions the SDK provides.
42
+
43
+ ### Private Keys
44
+ Serialize a private key into hex and binary:
45
+ ```ts
46
+ // Starting private key
47
+ const privKey = PrivateKey.fromRandom()
48
+
49
+ // Serialized formats
50
+ const privKeyHex = privKey.toHex()
51
+ const privKeyBinary = privKey.toArray()
52
+ const privKeyWif = privKey.toWif()
53
+ ```
54
+
55
+ ### Public Keys
56
+ Public keys can be serialized as well, and include helper functions to serialize into common formats such as an address or DER.
57
+
58
+ ```ts
59
+
60
+ const privateKey = PrivateKey.fromRandom()
61
+ const publicKey = privateKey.toPublicKey()
62
+ const publicKeyHex = publicKey.toString()
63
+ const publicKeyAddress = publicKey.toAddress()
64
+ const publicKeyDER = publicKey.toDER()
65
+
66
+ // Serialize a public key using function chaining
67
+ const publicKey = PrivateKey.fromRandom().toPublicKey().toString()
68
+ ```
69
+
70
+ Now you should be able to manage cryptographic keys with the SDK, including generating, serializing, and deserializing both private and public keys. These skills are important for using low-level cryptographic keys, ensuring you have the necessary tools to leverage the security benefits they offer and to implement advanced cryptographic functions within your applications.
@@ -0,0 +1,71 @@
1
+ # Serializing and Deserializing Bitcoin Scripts
2
+
3
+ Bitcoin scripts are the mechanism by which coins are locked and unlocked. They define the constraints and rules that govern transfers, and are therefore instrumental in the functionality of Bitcoin.
4
+
5
+ In this low-level tutorial, we will learn how to serialize and deserialize Bitcoin scripts within your applications using the functions provided by the SDK.
6
+
7
+ First, you will want to make sure you have installed the `@bsv/sdk` library and imported the necessary modules for this tutorial:
8
+
9
+ - **Script** - this class will enable to you create a Bitcoin Script from various sources.
10
+ - **PrivateKey** - Used in the demo of creating a P2PKH locking script.
11
+ - **P2PKH** - This class provides methods to create Pay To Public Key Hash locking and unlocking scripts.
12
+ - **OP** - Bitcoin opcode map used in example scripts.
13
+
14
+ ```ts
15
+ import { Script, PrivateKey, P2PKH, OP } from '@bsv/sdk'
16
+ ```
17
+
18
+ ## Generating and Deserializing Scripts
19
+ First, we will learn how to generate new Bitcoin scripts. This will usually involve deserializing a script from various types the most common being as hex, ASM, and binary.
20
+
21
+ Here is an example of how this can be done:
22
+
23
+ ```ts
24
+ // From Hex
25
+ const buf: number[] = [OP.OP_TRUE]
26
+ const scriptFromHex = Script.fromHex(Utils.toHex([OP.OP_TRUE]))
27
+
28
+ // From ASM
29
+ const scriptFromASM = Script.fromASM('OP_DUP OP_HASH160 1451baa3aad777144a0759998a03538018dd7b4b OP_EQUALVERIFY OP_CHECKSIG')
30
+
31
+ // From Binary
32
+ const buf2 = [OP.OP_PUSHDATA1, 3, 1, 2, 3]
33
+ const scriptFromBinary = Script.fromBinary(buf)
34
+ ```
35
+
36
+ For a more advanced example, the P2PKH class can be used to creating a locking script to a Public Key Hash as follows:
37
+
38
+ ```ts
39
+ const priv = PrivateKey.fromRandom()
40
+ const publicKeyHash = priv.toPublicKey().toHash()
41
+ const lockingScript = new P2PKH().lock(publicKeyHash).toASM()
42
+ // console.log(lockingScript)
43
+ // -> 'OP_DUP OP_HASH160 99829df6ad0df41a759811add7f233e268501ea9 OP_EQUALVERIFY OP_CHECKSIG'
44
+
45
+ ```
46
+
47
+ ## Serializing Scripts
48
+
49
+ Sometimes you will want to convert Scripts into a format that can be more easily transported in your applications such as hex or binary.
50
+
51
+ Let's explore the available functions the SDK provides.
52
+
53
+ ```ts
54
+ // Create initial script
55
+ const script = Script.fromASM('OP_DUP OP_HASH160 1451baa3aad777144a0759998a03538018dd7b4b OP_EQUALVERIFY OP_CHECKSIG')
56
+
57
+ // Serialize script
58
+ const scriptAsHex = script.toHex()
59
+ // console.log(scriptAsHex)
60
+ // -> 76a9141451baa3aad777144a0759998a03538018dd7b4b88ac
61
+
62
+ const scriptAsASM = script.toASM()
63
+ // console.log(scriptAsASM)
64
+ // -> 'OP_DUP OP_HASH160 1451baa3aad777144a0759998a03538018dd7b4b OP_EQUALVERIFY OP_CHECKSIG'
65
+
66
+ const scriptAsBinary = script.toBinary()
67
+ // console.log(scriptAsBinary)
68
+ // -> [118, 169, 20, ..., 172]
69
+ ```
70
+
71
+ We've covered how to interpret scripts from formats like hex, ASM, and binary, as well as how to convert them back for efficient transmission or storage. You should now be equipped to manage Bitcoin scripts efficiently in your journey to develop Bitcoin-powered applications.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsv/sdk",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "type": "module",
5
5
  "description": "BSV Blockchain Standard Development Kit",
6
6
  "main": "dist/cjs/mod.js",
@@ -552,15 +552,15 @@ export default class BigNumber {
552
552
  }
553
553
 
554
554
  /**
555
- * The copy method creates and returns a separate identical copy of the BigNumber.
555
+ * The copy method copies the state of this BigNumber into an exsiting `dest` BigNumber.
556
556
  *
557
557
  * @method copy
558
- * @param dest - The BigNumber instance that will be made into a copy.
558
+ * @param dest - The BigNumber instance that will be updated to become a copy.
559
559
  *
560
560
  * @example
561
561
  * const bn1 = new BigNumber('123456', 10, 'be');
562
562
  * const bn2 = new BigNumber();
563
- * bn1.cop(bn2);
563
+ * bn1.copy(bn2);
564
564
  * // bn2 is now a BigNumber representing 123456
565
565
  */
566
566
  copy (dest: BigNumber): void {
@@ -75,6 +75,8 @@ export default class PrivateKey extends BigNumber {
75
75
  * @param base - The base of number provided. By default is 10. Ignored if number is BigNumber.
76
76
  *
77
77
  * @param endian - The endianness provided. By default is 'big endian'. Ignored if number is BigNumber.
78
+ *
79
+ * @param modN - Optional. Default 'apply. If 'apply', apply modN to input to guarantee a valid PrivateKey. If 'error', if input is out of field throw Error('Input is out of field'). If 'nocheck', assumes input is in field.
78
80
  *
79
81
  * @example
80
82
  * import PrivateKey from './PrivateKey';
@@ -84,7 +86,8 @@ export default class PrivateKey extends BigNumber {
84
86
  constructor (
85
87
  number: BigNumber | number | string | number[] = 0,
86
88
  base: number | 'be' | 'le' | 'hex' = 10,
87
- endian: 'be' | 'le' = 'be'
89
+ endian: 'be' | 'le' = 'be',
90
+ modN: 'apply' | 'nocheck' | 'error' = 'apply'
88
91
  ) {
89
92
  if (number instanceof BigNumber) {
90
93
  super()
@@ -92,6 +95,35 @@ export default class PrivateKey extends BigNumber {
92
95
  } else {
93
96
  super(number, base, endian)
94
97
  }
98
+
99
+ if (modN !== 'nocheck') {
100
+ const check = this.checkInField()
101
+ if (!check.inField) {
102
+ if (modN === 'error') {
103
+ throw new Error('Input is out of field')
104
+ }
105
+ // Force the PrivateKey BigNumber value to lie in the field limited by curve.n
106
+ BigNumber.move(this, check.modN)
107
+ }
108
+ }
109
+ }
110
+
111
+ /**
112
+ * A utility function to check that the value of this PrivateKey lies in the field limited by curve.n
113
+ * @returns { inField, modN } where modN is this PrivateKey's current BigNumber value mod curve.n, and inField is true only if modN equals current BigNumber value.
114
+ */
115
+ checkInField() : { inField: boolean, modN: BigNumber } {
116
+ const curve = new Curve()
117
+ const modN = this.mod(curve.n)
118
+ const inField = this.cmp(modN) === 0
119
+ return { inField, modN }
120
+ }
121
+
122
+ /**
123
+ * @returns true if the PrivateKey's current BigNumber value lies in the field limited by curve.n
124
+ */
125
+ isValid() : boolean {
126
+ return this.checkInField().inField
95
127
  }
96
128
 
97
129
  /**
@@ -161,13 +193,17 @@ export default class PrivateKey extends BigNumber {
161
193
  *
162
194
  * @param prefix defaults to [0x80] for mainnet, set it to [0xef] for testnet.
163
195
  *
196
+ * @throws Error('Value is out of field') if current BigNumber value is out of field limited by curve.n
197
+ *
164
198
  * @example
165
199
  * const privateKey = PrivateKey.fromRandom();
166
200
  * const wif = privateKey.toWif();
167
201
  * const testnetWif = privateKey.toWif([0xef]);
168
202
  */
169
203
  toWif (prefix : number[] = [0x80]): string {
170
- return toBase58Check([...this.toArray(), 1], prefix)
204
+ if (!this.isValid())
205
+ throw new Error('Value is out of field')
206
+ return toBase58Check([...this.toArray("be", 32), 1], prefix)
171
207
  }
172
208
 
173
209
  /**
@@ -0,0 +1,33 @@
1
+ //import { PrivateKey, PublicKey, Curve, Hash, BigNumber } from '..';
2
+ import PublicKey from '../../../dist/cjs/src/primitives/PublicKey'
3
+ //import { PrivateKey } from '..';
4
+ import PrivateKey from '../../../dist/cjs/src/primitives/PrivateKey'
5
+ import Curve from '../../../dist/cjs/src/primitives/Curve'
6
+ import BigNumber from '../../../dist/cjs/src/primitives/BigNumber'
7
+
8
+ describe("bug-31 tests", () => {
9
+
10
+ test("0", () => {
11
+ const c = new Curve()
12
+ const G = c.g
13
+ //const bn = new BigNumber(c.n + 12)
14
+ const bn = c.n.addn(12)
15
+ const sn = new BigNumber(12)
16
+ {
17
+ expect(() => new PrivateKey(bn.toHex(), 'hex', 'be', 'error')).toThrow("Input is out of field")
18
+ }
19
+ const o = PrivateKey.fromString(bn.toHex(), 'hex')
20
+ const os = PrivateKey.fromString(sn.toHex(), 'hex')
21
+ expect(o.cmp(os)).toBe(0)
22
+ const os2 = new PrivateKey(bn.toHex(), 'hex', 'be', 'nocheck')
23
+ expect(o.cmp(os2)).not.toBe(0)
24
+
25
+ const oWif = o.toWif()
26
+ expect(oWif).toBe('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU79MFFcB1G')
27
+ const osWif = os.toWif()
28
+ expect(osWif).toBe('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU79MFFcB1G')
29
+
30
+ expect(() => os2.toWif()).toThrow("Value is out of field")
31
+
32
+ })
33
+ })
@@ -321,9 +321,12 @@ export class Writer {
321
321
  }
322
322
 
323
323
  toArray(): number[] {
324
- const ret = []
324
+ let ret = []
325
325
  for (const x of this.bufs) {
326
- ret.push(...x)
326
+ if (x.length < 65536)
327
+ ret.push(...x)
328
+ else
329
+ ret = ret.concat(x)
327
330
  }
328
331
  return ret
329
332
  }
@@ -1,44 +0,0 @@
1
- # Low-Level: Using AES for the Symmetric Encryption of Data
2
-
3
- The goal of this tutorial is to explore symmetric encryption of data using public and private keys with the Advanced Encryption Standard (AES). We will make use of the functions provided by this library in order to encrypt data with user held keys.
4
-
5
- <img src="./images/symmetric_encryption_diagram.png" width="600" alt=""/>
6
-
7
- If you would like to learn more about AES encryption, here are some general resources that may help:
8
-
9
- - [AES - Wiki](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard)
10
- - [GCM - Wiki](https://en.wikipedia.org/wiki/Galois/Counter_Mode)
11
-
12
-
13
- Let's get started!
14
-
15
- ## Getting Started
16
-
17
- First, you will want to make sure you have installed the required dependencies.
18
-
19
- ```ts
20
- import { SymmetricKey, Utils } from '@bsv/sdk'
21
- ```
22
-
23
- Next, we wil define the keys to be used and the data to encrypt. They encrypt function expects a parameter of type `number[]` so we will use the Utils `toArray` function to convert the UTF8 message to the correct type.
24
-
25
- ```ts
26
- const symmetricKey = SymmetricKey.fromRandom()
27
- const messageToEncrypt: number[] = Utils.toArray('Hello Alice, this is Bob!', 'utf8')
28
- ```
29
-
30
- ### Encrypting and Decrypting
31
-
32
- When you encrypt a message, an initialization vector is prepended to the message to prevent potential key-reuse attacks. Conversely, when decrypting, the initialization vector that was initially added is spliced out and used in the AES-GSM decryption processes.
33
-
34
- We will use the `encrypt` and `decrypt` functions of the SymmetricKey class to transform the message.
35
-
36
- To parse out the text as a UTF8 string from a number array, we will use the Utils `toUTF8` function.
37
- ```ts
38
- const encryptedMessage = SymmetricKey.encrypt(messageToEncrypt)
39
- const plaintext = SymmetricKey.decrypt(encryptedMessage) as number[]
40
- const plaintextMessage = Utils.toUTF8(plaintext)
41
- // console.log(plaintextMessage) --> 'Hello Alice, this is Bob!'
42
- ```
43
-
44
- This is just a basic demonstration of symmetric encryption/decryption using the @bsv/sdk library, however the possibilities of what you can do are endless once you understand these fundamentals.