@bsv/sdk 1.0.12 → 1.0.14

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 (119) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/compat/BSM.js.map +1 -1
  3. package/dist/cjs/src/compat/ECIES.js +105 -76
  4. package/dist/cjs/src/compat/ECIES.js.map +1 -1
  5. package/dist/cjs/src/compat/HD.js +65 -65
  6. package/dist/cjs/src/compat/HD.js.map +1 -1
  7. package/dist/cjs/src/compat/Mnemonic.js +79 -79
  8. package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
  9. package/dist/cjs/src/compat/bip-39-wordlist-en.js +2 -2
  10. package/dist/cjs/src/compat/bip-39-wordlist-en.js.map +1 -1
  11. package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
  12. package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
  13. package/dist/cjs/src/primitives/DRBG.js.map +1 -1
  14. package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
  15. package/dist/cjs/src/primitives/Hash.js +26 -13
  16. package/dist/cjs/src/primitives/Hash.js.map +1 -1
  17. package/dist/cjs/src/primitives/PrivateKey.js +3 -2
  18. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
  19. package/dist/cjs/src/primitives/PublicKey.js +1 -2
  20. package/dist/cjs/src/primitives/PublicKey.js.map +1 -1
  21. package/dist/cjs/src/primitives/Random.js +2 -2
  22. package/dist/cjs/src/primitives/Random.js.map +1 -1
  23. package/dist/cjs/src/primitives/Signature.js +141 -4
  24. package/dist/cjs/src/primitives/Signature.js.map +1 -1
  25. package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -1
  26. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
  27. package/dist/cjs/src/primitives/utils.js +14 -9
  28. package/dist/cjs/src/primitives/utils.js.map +1 -1
  29. package/dist/cjs/src/script/Spend.js.map +1 -1
  30. package/dist/cjs/src/script/templates/P2PKH.js +1 -1
  31. package/dist/cjs/src/script/templates/P2PKH.js.map +1 -1
  32. package/dist/cjs/src/transaction/MerklePath.js +3 -3
  33. package/dist/cjs/src/transaction/MerklePath.js.map +1 -1
  34. package/dist/cjs/src/transaction/Transaction.js +15 -4
  35. package/dist/cjs/src/transaction/Transaction.js.map +1 -1
  36. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  37. package/dist/esm/src/compat/BSM.js.map +1 -1
  38. package/dist/esm/src/compat/ECIES.js +105 -76
  39. package/dist/esm/src/compat/ECIES.js.map +1 -1
  40. package/dist/esm/src/compat/HD.js +65 -65
  41. package/dist/esm/src/compat/HD.js.map +1 -1
  42. package/dist/esm/src/compat/Mnemonic.js +79 -79
  43. package/dist/esm/src/compat/Mnemonic.js.map +1 -1
  44. package/dist/esm/src/compat/bip-39-wordlist-en.js +2 -2
  45. package/dist/esm/src/compat/bip-39-wordlist-en.js.map +1 -1
  46. package/dist/esm/src/primitives/AESGCM.js.map +1 -1
  47. package/dist/esm/src/primitives/BigNumber.js.map +1 -1
  48. package/dist/esm/src/primitives/DRBG.js.map +1 -1
  49. package/dist/esm/src/primitives/ECDSA.js.map +1 -1
  50. package/dist/esm/src/primitives/Hash.js +26 -13
  51. package/dist/esm/src/primitives/Hash.js.map +1 -1
  52. package/dist/esm/src/primitives/PrivateKey.js +3 -2
  53. package/dist/esm/src/primitives/PrivateKey.js.map +1 -1
  54. package/dist/esm/src/primitives/PublicKey.js +1 -2
  55. package/dist/esm/src/primitives/PublicKey.js.map +1 -1
  56. package/dist/esm/src/primitives/Random.js +2 -2
  57. package/dist/esm/src/primitives/Random.js.map +1 -1
  58. package/dist/esm/src/primitives/Signature.js +141 -4
  59. package/dist/esm/src/primitives/Signature.js.map +1 -1
  60. package/dist/esm/src/primitives/SymmetricKey.js.map +1 -1
  61. package/dist/esm/src/primitives/TransactionSignature.js.map +1 -1
  62. package/dist/esm/src/primitives/utils.js +14 -9
  63. package/dist/esm/src/primitives/utils.js.map +1 -1
  64. package/dist/esm/src/script/Spend.js.map +1 -1
  65. package/dist/esm/src/script/templates/P2PKH.js +1 -1
  66. package/dist/esm/src/script/templates/P2PKH.js.map +1 -1
  67. package/dist/esm/src/transaction/MerklePath.js +3 -3
  68. package/dist/esm/src/transaction/MerklePath.js.map +1 -1
  69. package/dist/esm/src/transaction/Transaction.js +16 -4
  70. package/dist/esm/src/transaction/Transaction.js.map +1 -1
  71. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  72. package/dist/types/src/compat/ECIES.d.ts +36 -36
  73. package/dist/types/src/compat/ECIES.d.ts.map +1 -1
  74. package/dist/types/src/compat/HD.d.ts +65 -65
  75. package/dist/types/src/compat/HD.d.ts.map +1 -1
  76. package/dist/types/src/compat/Mnemonic.d.ts +79 -79
  77. package/dist/types/src/compat/Mnemonic.d.ts.map +1 -1
  78. package/dist/types/src/primitives/AESGCM.d.ts.map +1 -1
  79. package/dist/types/src/primitives/BigNumber.d.ts.map +1 -1
  80. package/dist/types/src/primitives/Hash.d.ts.map +1 -1
  81. package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -1
  82. package/dist/types/src/primitives/PublicKey.d.ts.map +1 -1
  83. package/dist/types/src/primitives/Signature.d.ts +62 -0
  84. package/dist/types/src/primitives/Signature.d.ts.map +1 -1
  85. package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -1
  86. package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -1
  87. package/dist/types/src/primitives/utils.d.ts.map +1 -1
  88. package/dist/types/src/script/Spend.d.ts.map +1 -1
  89. package/dist/types/src/transaction/Transaction.d.ts +1 -0
  90. package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
  91. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  92. package/docs/compat.md +4 -4
  93. package/docs/primitives.md +288 -79
  94. package/package.json +1 -1
  95. package/src/compat/BSM.ts +12 -12
  96. package/src/compat/ECIES.ts +417 -418
  97. package/src/compat/HD.ts +228 -228
  98. package/src/compat/Mnemonic.ts +173 -173
  99. package/src/compat/__tests/BSM.test.ts +13 -2
  100. package/src/compat/bip-39-wordlist-en.ts +2052 -2052
  101. package/src/primitives/AESGCM.ts +30 -30
  102. package/src/primitives/BigNumber.ts +0 -1
  103. package/src/primitives/DRBG.ts +5 -5
  104. package/src/primitives/ECDSA.ts +1 -1
  105. package/src/primitives/Hash.ts +278 -293
  106. package/src/primitives/PrivateKey.ts +18 -19
  107. package/src/primitives/PublicKey.ts +9 -10
  108. package/src/primitives/Random.ts +4 -4
  109. package/src/primitives/Signature.ts +158 -14
  110. package/src/primitives/SymmetricKey.ts +3 -3
  111. package/src/primitives/TransactionSignature.ts +9 -9
  112. package/src/primitives/index.ts +1 -1
  113. package/src/primitives/utils.ts +60 -64
  114. package/src/script/Spend.ts +12 -12
  115. package/src/script/index.ts +1 -1
  116. package/src/script/templates/P2PKH.ts +1 -1
  117. package/src/transaction/MerklePath.ts +3 -3
  118. package/src/transaction/Transaction.ts +36 -26
  119. package/src/transaction/__tests/Transaction.test.ts +5 -5
package/src/compat/HD.ts CHANGED
@@ -12,24 +12,24 @@ import BigNumber from '../primitives/BigNumber.js'
12
12
  * It allows the generation of child keys from a master key, ensuring a tree-like structure of keys and addresses.
13
13
  * This class is deprecated due to the introduction of BRC-42, which offers an enhanced key derivation scheme.
14
14
  * BRC-42 uses invoice numbers for key derivation, improving privacy and scalability compared to BIP32.
15
- *
15
+ *
16
16
  * @class HD
17
17
  * @deprecated Replaced by BRC-42 which uses invoice numbers and supports private derivation.
18
18
  */
19
19
  export default class HD {
20
- versionBytesNum: number
21
- depth: number
22
- parentFingerPrint: number[]
23
- childIndex: number
24
- chainCode: number[]
25
- privKey: PrivateKey
26
- pubKey: PublicKey
27
- constants = {
28
- pubKey: 0x0488b21e,
29
- privKey: 0x0488ade4
30
- }
31
-
32
- /**
20
+ versionBytesNum: number
21
+ depth: number
22
+ parentFingerPrint: number[]
23
+ childIndex: number
24
+ chainCode: number[]
25
+ privKey: PrivateKey
26
+ pubKey: PublicKey
27
+ constants = {
28
+ pubKey: 0x0488b21e,
29
+ privKey: 0x0488ade4
30
+ }
31
+
32
+ /**
33
33
  * Constructor for the BIP32 HD wallet.
34
34
  * Initializes an HD wallet with optional parameters for version bytes, depth, parent fingerprint, child index, chain code, private key, and public key.
35
35
  * @param versionBytesNum - Version bytes number for the wallet.
@@ -40,309 +40,309 @@ export default class HD {
40
40
  * @param privKey - Private key of the wallet.
41
41
  * @param pubKey - Public key of the wallet.
42
42
  */
43
- constructor(
44
- versionBytesNum?: number,
45
- depth?: number,
46
- parentFingerPrint?: number[],
47
- childIndex?: number,
48
- chainCode?: number[],
49
- privKey?: PrivateKey,
50
- pubKey?: PublicKey
51
- ) {
52
- this.versionBytesNum = versionBytesNum
53
- this.depth = depth
54
- this.parentFingerPrint = parentFingerPrint
55
- this.childIndex = childIndex
56
- this.chainCode = chainCode
57
- this.privKey = privKey
58
- this.pubKey = pubKey
59
- }
60
-
61
- /**
43
+ constructor (
44
+ versionBytesNum?: number,
45
+ depth?: number,
46
+ parentFingerPrint?: number[],
47
+ childIndex?: number,
48
+ chainCode?: number[],
49
+ privKey?: PrivateKey,
50
+ pubKey?: PublicKey
51
+ ) {
52
+ this.versionBytesNum = versionBytesNum
53
+ this.depth = depth
54
+ this.parentFingerPrint = parentFingerPrint
55
+ this.childIndex = childIndex
56
+ this.chainCode = chainCode
57
+ this.privKey = privKey
58
+ this.pubKey = pubKey
59
+ }
60
+
61
+ /**
62
62
  * Generates a new HD wallet with random keys.
63
63
  * This method creates a root HD wallet with randomly generated private and public keys.
64
64
  * @returns {HD} The current HD instance with generated keys.
65
65
  */
66
- public fromRandom(): this {
67
- this.versionBytesNum = this.constants.privKey
68
- this.depth = 0x00
69
- this.parentFingerPrint = [0, 0, 0, 0]
70
- this.childIndex = 0
71
- this.chainCode = Random(32)
72
- this.privKey = PrivateKey.fromRandom()
73
- this.pubKey = this.privKey.toPublicKey()
74
- return this
75
- }
76
-
77
- /**
66
+ public fromRandom (): this {
67
+ this.versionBytesNum = this.constants.privKey
68
+ this.depth = 0x00
69
+ this.parentFingerPrint = [0, 0, 0, 0]
70
+ this.childIndex = 0
71
+ this.chainCode = Random(32)
72
+ this.privKey = PrivateKey.fromRandom()
73
+ this.pubKey = this.privKey.toPublicKey()
74
+ return this
75
+ }
76
+
77
+ /**
78
78
  * Generates a new HD wallet with random keys.
79
79
  * This method creates a root HD wallet with randomly generated private and public keys.
80
80
  * @returns {HD} A new HD instance with generated keys.
81
81
  * @static
82
82
  */
83
- public static fromRandom(): HD {
84
- return new this().fromRandom()
85
- }
83
+ public static fromRandom (): HD {
84
+ return new this().fromRandom()
85
+ }
86
86
 
87
- /**
87
+ /**
88
88
  * Initializes the HD wallet from a given base58 encoded string.
89
89
  * This method decodes a provided string to set up the HD wallet's properties.
90
90
  * @param str - A base58 encoded string representing the wallet.
91
91
  * @returns {HD} The current instance with properties set from the string.
92
92
  */
93
- public fromString(str: string): this {
94
- const decoded = fromBase58Check(str)
95
- return this.fromBinary([...decoded.prefix, ...decoded.data] as number[])
96
- }
93
+ public fromString (str: string): this {
94
+ const decoded = fromBase58Check(str)
95
+ return this.fromBinary([...decoded.prefix, ...decoded.data] as number[])
96
+ }
97
97
 
98
- /**
98
+ /**
99
99
  * Converts the HD wallet to a base58 encoded string.
100
100
  * This method provides a string representation of the HD wallet's current state.
101
101
  * @returns {string} A base58 encoded string of the HD wallet.
102
102
  */
103
- public toString(): string {
104
- const bin = this.toBinary()
105
- return toBase58Check(bin, [])
106
- }
103
+ public toString (): string {
104
+ const bin = this.toBinary()
105
+ return toBase58Check(bin, [])
106
+ }
107
107
 
108
- /**
108
+ /**
109
109
  * Initializes the HD wallet from a seed.
110
110
  * This method generates keys and other properties from a given seed, conforming to the BIP32 specification.
111
111
  * @param bytes - An array of bytes representing the seed.
112
112
  * @returns {HD} The current instance with properties set from the seed.
113
113
  */
114
- public fromSeed(bytes: number[]): this {
115
- if (bytes.length < 128 / 8) {
116
- throw new Error('Need more than 128 bits of entropy')
117
- }
118
- if (bytes.length > 512 / 8) {
119
- throw new Error('More than 512 bits of entropy is nonstandard')
120
- }
121
- const hash: number[] = Hash.sha512hmac(toArray('Bitcoin seed', 'utf8'), bytes) as number[]
114
+ public fromSeed (bytes: number[]): this {
115
+ if (bytes.length < 128 / 8) {
116
+ throw new Error('Need more than 128 bits of entropy')
117
+ }
118
+ if (bytes.length > 512 / 8) {
119
+ throw new Error('More than 512 bits of entropy is nonstandard')
120
+ }
121
+ const hash: number[] = Hash.sha512hmac(toArray('Bitcoin seed', 'utf8'), bytes)
122
122
 
123
- this.depth = 0x00
124
- this.parentFingerPrint = [0, 0, 0, 0]
125
- this.childIndex = 0
126
- this.chainCode = hash.slice(32, 64)
127
- this.versionBytesNum = this.constants.privKey
128
- this.privKey = new PrivateKey(hash.slice(0, 32))
129
- this.pubKey = this.privKey.toPublicKey()
123
+ this.depth = 0x00
124
+ this.parentFingerPrint = [0, 0, 0, 0]
125
+ this.childIndex = 0
126
+ this.chainCode = hash.slice(32, 64)
127
+ this.versionBytesNum = this.constants.privKey
128
+ this.privKey = new PrivateKey(hash.slice(0, 32))
129
+ this.pubKey = this.privKey.toPublicKey()
130
130
 
131
- return this
132
- }
131
+ return this
132
+ }
133
133
 
134
- /**
134
+ /**
135
135
  * Initializes the HD wallet from a seed.
136
136
  * This method generates keys and other properties from a given seed, conforming to the BIP32 specification.
137
137
  * @param bytes - An array of bytes representing the seed.
138
138
  * @returns {HD} The current instance with properties set from the seed.
139
139
  */
140
- public static fromSeed(bytes: number[]): HD {
141
- return new this().fromSeed(bytes)
142
- }
140
+ public static fromSeed (bytes: number[]): HD {
141
+ return new this().fromSeed(bytes)
142
+ }
143
143
 
144
- /**
144
+ /**
145
145
  * Initializes the HD wallet from a binary buffer.
146
146
  * Parses a binary buffer to set up the wallet's properties.
147
147
  * @param buf - A buffer containing the wallet data.
148
148
  * @returns {HD} The current instance with properties set from the buffer.
149
149
  */
150
- public fromBinary(buf: number[]): this {
151
- // Both pub and private extended keys are 78 buf
152
- if (buf.length !== 78) {
153
- throw new Error('incorrect bip32 data length')
154
- }
155
- const reader = new Reader(buf)
156
-
157
- this.versionBytesNum = reader.readUInt32BE()
158
- this.depth = reader.readUInt8()
159
- this.parentFingerPrint = reader.read(4)
160
- this.childIndex = reader.readUInt32BE()
161
- this.chainCode = reader.read(32)
162
- const keyBytes = reader.read(33)
163
-
164
- const isPrivate = this.versionBytesNum === this.constants.privKey
165
- const isPublic = this.versionBytesNum === this.constants.pubKey
166
-
167
- if (isPrivate && keyBytes[0] === 0) {
168
- this.privKey = new PrivateKey(keyBytes.slice(1, 33))
169
- this.pubKey = this.privKey.toPublicKey()
170
- } else if (isPublic && (keyBytes[0] === 0x02 || keyBytes[0] === 0x03)) {
171
- this.pubKey = PublicKey.fromString(toHex(keyBytes))
172
- } else {
173
- throw new Error('Invalid key')
174
- }
175
-
176
- return this
150
+ public fromBinary (buf: number[]): this {
151
+ // Both pub and private extended keys are 78 buf
152
+ if (buf.length !== 78) {
153
+ throw new Error('incorrect bip32 data length')
154
+ }
155
+ const reader = new Reader(buf)
156
+
157
+ this.versionBytesNum = reader.readUInt32BE()
158
+ this.depth = reader.readUInt8()
159
+ this.parentFingerPrint = reader.read(4)
160
+ this.childIndex = reader.readUInt32BE()
161
+ this.chainCode = reader.read(32)
162
+ const keyBytes = reader.read(33)
163
+
164
+ const isPrivate = this.versionBytesNum === this.constants.privKey
165
+ const isPublic = this.versionBytesNum === this.constants.pubKey
166
+
167
+ if (isPrivate && keyBytes[0] === 0) {
168
+ this.privKey = new PrivateKey(keyBytes.slice(1, 33))
169
+ this.pubKey = this.privKey.toPublicKey()
170
+ } else if (isPublic && (keyBytes[0] === 0x02 || keyBytes[0] === 0x03)) {
171
+ this.pubKey = PublicKey.fromString(toHex(keyBytes))
172
+ } else {
173
+ throw new Error('Invalid key')
177
174
  }
178
175
 
179
- /**
176
+ return this
177
+ }
178
+
179
+ /**
180
180
  * Derives a child HD wallet based on a given path.
181
181
  * The path specifies the hierarchy of the child key to be derived.
182
182
  * @param path - A string representing the derivation path (e.g., 'm/0'/1).
183
183
  * @returns {HD} A new HD instance representing the derived child wallet.
184
184
  */
185
- public derive(path: string): HD {
186
- if (path === 'm') {
187
- return this
188
- }
185
+ public derive (path: string): HD {
186
+ if (path === 'm') {
187
+ return this
188
+ }
189
189
 
190
- const e = path.split('/')
190
+ const e = path.split('/')
191
191
 
192
- // eslint-disable-next-line @typescript-eslint/no-this-alias
193
- let bip32: HD = this
194
- for (const i in e) {
195
- const c = e[i]
192
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
193
+ let bip32: HD = this
194
+ for (const i in e) {
195
+ const c = e[i]
196
196
 
197
- if (i === '0') {
198
- if (c !== 'm') {
199
- throw new Error('invalid path')
200
- }
201
- continue
202
- }
197
+ if (i === '0') {
198
+ if (c !== 'm') {
199
+ throw new Error('invalid path')
200
+ }
201
+ continue
202
+ }
203
203
 
204
- if (parseInt(c.replace("'", ''), 10).toString() !== c.replace("'", '')) {
205
- throw new Error('invalid path')
206
- }
204
+ if (parseInt(c.replace("'", ''), 10).toString() !== c.replace("'", '')) {
205
+ throw new Error('invalid path')
206
+ }
207
207
 
208
- const usePrivate = c.length > 1 && c[c.length - 1] === "'"
209
- let childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c, 10) & 0x7fffffff
208
+ const usePrivate = c.length > 1 && c[c.length - 1] === "'"
209
+ let childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c, 10) & 0x7fffffff
210
210
 
211
- if (usePrivate) {
212
- childIndex += 0x80000000
213
- }
211
+ if (usePrivate) {
212
+ childIndex += 0x80000000
213
+ }
214
214
 
215
- bip32 = bip32.deriveChild(childIndex)
216
- }
217
-
218
- return bip32
215
+ bip32 = bip32.deriveChild(childIndex)
219
216
  }
220
217
 
221
- /**
218
+ return bip32
219
+ }
220
+
221
+ /**
222
222
  * Derives a child HD wallet from the current wallet based on an index.
223
223
  * This method generates either a private or public child key depending on the current wallet's state.
224
224
  * @param i - The index of the child key to derive.
225
225
  * @returns {HD} A new HD instance representing the derived child wallet.
226
226
  */
227
- public deriveChild(i: number): HD {
228
- if (typeof i !== 'number') {
229
- throw new Error('i must be a number')
230
- }
227
+ public deriveChild (i: number): HD {
228
+ if (typeof i !== 'number') {
229
+ throw new Error('i must be a number')
230
+ }
231
231
 
232
- const ibc: number[] = []
233
- ibc.push((i >> 24) & 0xff)
234
- ibc.push((i >> 16) & 0xff)
235
- ibc.push((i >> 8) & 0xff)
236
- ibc.push(i & 0xff)
237
- const ib = [...ibc]
232
+ const ibc: number[] = []
233
+ ibc.push((i >> 24) & 0xff)
234
+ ibc.push((i >> 16) & 0xff)
235
+ ibc.push((i >> 8) & 0xff)
236
+ ibc.push(i & 0xff)
237
+ const ib = [...ibc]
238
238
 
239
- const usePrivate = (i & 0x80000000) !== 0
239
+ const usePrivate = (i & 0x80000000) !== 0
240
240
 
241
- const isPrivate = this.versionBytesNum === this.constants.privKey
241
+ const isPrivate = this.versionBytesNum === this.constants.privKey
242
242
 
243
- if (usePrivate && (!this.privKey || !isPrivate)) {
244
- throw new Error('Cannot do private key derivation without private key')
245
- }
243
+ if (usePrivate && (!this.privKey || !isPrivate)) {
244
+ throw new Error('Cannot do private key derivation without private key')
245
+ }
246
246
 
247
- let ret = null
248
- if (this.privKey) {
249
- let data = null
247
+ let ret = null
248
+ if (this.privKey) {
249
+ let data = null
250
250
 
251
- if (usePrivate) {
252
- data = [0, ...this.privKey.toArray('be', 32), ...ib]
253
- } else {
254
- data = [...this.pubKey.encode(true) as number[], ...ib]
255
- }
251
+ if (usePrivate) {
252
+ data = [0, ...this.privKey.toArray('be', 32), ...ib]
253
+ } else {
254
+ data = [...this.pubKey.encode(true) as number[], ...ib]
255
+ }
256
256
 
257
- const hash = Hash.sha512hmac(this.chainCode, data)
258
- const il = new BigNumber(hash.slice(0, 32))
259
- const ir = hash.slice(32, 64)
257
+ const hash = Hash.sha512hmac(this.chainCode, data)
258
+ const il = new BigNumber(hash.slice(0, 32))
259
+ const ir = hash.slice(32, 64)
260
260
 
261
- // ki = IL + kpar (mod n).
262
- const k = il.add(this.privKey).mod(new Curve().n)
261
+ // ki = IL + kpar (mod n).
262
+ const k = il.add(this.privKey).mod(new Curve().n)
263
263
 
264
- ret = new HD()
265
- ret.chainCode = ir
264
+ ret = new HD()
265
+ ret.chainCode = ir
266
266
 
267
- ret.privKey = new PrivateKey(k.toArray())
268
- ret.pubKey = ret.privKey.toPublicKey()
269
- } else {
270
- const data = [...this.pubKey.encode(true) as number[], ...ib]
271
- const hash = Hash.sha512hmac(this.chainCode, data)
272
- const il = new BigNumber(hash.slice(0, 32))
273
- const ir = hash.slice(32, 64)
267
+ ret.privKey = new PrivateKey(k.toArray())
268
+ ret.pubKey = ret.privKey.toPublicKey()
269
+ } else {
270
+ const data = [...this.pubKey.encode(true) as number[], ...ib]
271
+ const hash = Hash.sha512hmac(this.chainCode, data)
272
+ const il = new BigNumber(hash.slice(0, 32))
273
+ const ir = hash.slice(32, 64)
274
274
 
275
- // Ki = (IL + kpar)*G = IL*G + Kpar
276
- const ilG = new Curve().g.mul(il)
277
- const Kpar = this.pubKey
278
- const Ki = ilG.add(Kpar)
279
- const newpub = new PublicKey(Ki.x, Ki.y)
275
+ // Ki = (IL + kpar)*G = IL*G + Kpar
276
+ const ilG = new Curve().g.mul(il)
277
+ const Kpar = this.pubKey
278
+ const Ki = ilG.add(Kpar)
279
+ const newpub = new PublicKey(Ki.x, Ki.y)
280
280
 
281
- ret = new HD()
282
- ret.chainCode = ir
281
+ ret = new HD()
282
+ ret.chainCode = ir
283
283
 
284
- ret.pubKey = newpub
285
- }
284
+ ret.pubKey = newpub
285
+ }
286
286
 
287
- ret.childIndex = i
288
- const pubKeyhash = Hash.hash160(this.pubKey.encode(true))
289
- ret.parentFingerPrint = pubKeyhash.slice(0, 4)
290
- ret.versionBytesNum = this.versionBytesNum
291
- ret.depth = this.depth + 1
287
+ ret.childIndex = i
288
+ const pubKeyhash = Hash.hash160(this.pubKey.encode(true))
289
+ ret.parentFingerPrint = pubKeyhash.slice(0, 4)
290
+ ret.versionBytesNum = this.versionBytesNum
291
+ ret.depth = this.depth + 1
292
292
 
293
- return ret
294
- }
293
+ return ret
294
+ }
295
295
 
296
- /**
296
+ /**
297
297
  * Converts the current HD wallet to a public-only wallet.
298
298
  * This method strips away the private key information, leaving only the public part.
299
299
  * @returns {HD} A new HD instance representing the public-only wallet.
300
300
  */
301
- public toPublic(): HD {
302
- const bip32 = new HD(this.versionBytesNum, this.depth, this.parentFingerPrint, this.childIndex, this.chainCode, this.privKey, this.pubKey)
303
- bip32.versionBytesNum = this.constants.pubKey
304
- bip32.privKey = undefined
305
- return bip32
306
- }
307
-
308
- /**
301
+ public toPublic (): HD {
302
+ const bip32 = new HD(this.versionBytesNum, this.depth, this.parentFingerPrint, this.childIndex, this.chainCode, this.privKey, this.pubKey)
303
+ bip32.versionBytesNum = this.constants.pubKey
304
+ bip32.privKey = undefined
305
+ return bip32
306
+ }
307
+
308
+ /**
309
309
  * Converts the HD wallet into a binary representation.
310
310
  * This method serializes the wallet's properties into a binary format.
311
311
  * @returns {number[]} An array of numbers representing the binary data of the wallet.
312
312
  */
313
- public toBinary(): number[] {
314
- const isPrivate = this.versionBytesNum === this.constants.privKey
315
- const isPublic = this.versionBytesNum === this.constants.pubKey
316
- if (isPrivate) {
317
- return new Writer()
318
- .writeUInt32BE(this.versionBytesNum)
319
- .writeUInt8(this.depth)
320
- .write(this.parentFingerPrint)
321
- .writeUInt32BE(this.childIndex)
322
- .write(this.chainCode)
323
- .writeUInt8(0)
324
- .write(this.privKey.toArray('be', 32))
325
- .toArray()
326
- } else if (isPublic) {
327
- return new Writer()
328
- .writeUInt32BE(this.versionBytesNum)
329
- .writeUInt8(this.depth)
330
- .write(this.parentFingerPrint)
331
- .writeUInt32BE(this.childIndex)
332
- .write(this.chainCode)
333
- .write(this.pubKey.encode(true) as number[])
334
- .toArray()
335
- } else {
336
- throw new Error('bip32: invalid versionBytesNum byte')
337
- }
313
+ public toBinary (): number[] {
314
+ const isPrivate = this.versionBytesNum === this.constants.privKey
315
+ const isPublic = this.versionBytesNum === this.constants.pubKey
316
+ if (isPrivate) {
317
+ return new Writer()
318
+ .writeUInt32BE(this.versionBytesNum)
319
+ .writeUInt8(this.depth)
320
+ .write(this.parentFingerPrint)
321
+ .writeUInt32BE(this.childIndex)
322
+ .write(this.chainCode)
323
+ .writeUInt8(0)
324
+ .write(this.privKey.toArray('be', 32))
325
+ .toArray()
326
+ } else if (isPublic) {
327
+ return new Writer()
328
+ .writeUInt32BE(this.versionBytesNum)
329
+ .writeUInt8(this.depth)
330
+ .write(this.parentFingerPrint)
331
+ .writeUInt32BE(this.childIndex)
332
+ .write(this.chainCode)
333
+ .write(this.pubKey.encode(true) as number[])
334
+ .toArray()
335
+ } else {
336
+ throw new Error('bip32: invalid versionBytesNum byte')
338
337
  }
338
+ }
339
339
 
340
- /**
340
+ /**
341
341
  * Checks if the HD wallet contains a private key.
342
342
  * This method determines whether the wallet is a private key wallet or a public key only wallet.
343
343
  * @returns {boolean} A boolean value indicating whether the wallet has a private key (true) or not (false).
344
344
  */
345
- public isPrivate(): boolean {
346
- return this.versionBytesNum === this.constants.privKey
347
- }
345
+ public isPrivate (): boolean {
346
+ return this.versionBytesNum === this.constants.privKey
347
+ }
348
348
  }