@bsv/sdk 1.0.13 → 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.
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/compat/BSM.js.map +1 -1
- package/dist/cjs/src/compat/ECIES.js +105 -76
- package/dist/cjs/src/compat/ECIES.js.map +1 -1
- package/dist/cjs/src/compat/HD.js +65 -65
- package/dist/cjs/src/compat/HD.js.map +1 -1
- package/dist/cjs/src/compat/Mnemonic.js +79 -79
- package/dist/cjs/src/compat/Mnemonic.js.map +1 -1
- package/dist/cjs/src/compat/bip-39-wordlist-en.js +2 -2
- package/dist/cjs/src/compat/bip-39-wordlist-en.js.map +1 -1
- package/dist/cjs/src/primitives/AESGCM.js.map +1 -1
- package/dist/cjs/src/primitives/BigNumber.js.map +1 -1
- package/dist/cjs/src/primitives/DRBG.js.map +1 -1
- package/dist/cjs/src/primitives/ECDSA.js.map +1 -1
- package/dist/cjs/src/primitives/Hash.js +26 -13
- package/dist/cjs/src/primitives/Hash.js.map +1 -1
- package/dist/cjs/src/primitives/PrivateKey.js +3 -2
- package/dist/cjs/src/primitives/PrivateKey.js.map +1 -1
- package/dist/cjs/src/primitives/PublicKey.js +1 -2
- package/dist/cjs/src/primitives/PublicKey.js.map +1 -1
- package/dist/cjs/src/primitives/Random.js +2 -2
- package/dist/cjs/src/primitives/Random.js.map +1 -1
- package/dist/cjs/src/primitives/Signature.js +141 -4
- package/dist/cjs/src/primitives/Signature.js.map +1 -1
- package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -1
- package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -1
- package/dist/cjs/src/primitives/utils.js +14 -9
- package/dist/cjs/src/primitives/utils.js.map +1 -1
- package/dist/cjs/src/script/Spend.js.map +1 -1
- package/dist/cjs/src/script/templates/P2PKH.js +1 -1
- package/dist/cjs/src/script/templates/P2PKH.js.map +1 -1
- package/dist/cjs/src/transaction/MerklePath.js +3 -3
- package/dist/cjs/src/transaction/MerklePath.js.map +1 -1
- package/dist/cjs/src/transaction/Transaction.js +2 -2
- package/dist/cjs/src/transaction/Transaction.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/compat/BSM.js.map +1 -1
- package/dist/esm/src/compat/ECIES.js +105 -76
- package/dist/esm/src/compat/ECIES.js.map +1 -1
- package/dist/esm/src/compat/HD.js +65 -65
- package/dist/esm/src/compat/HD.js.map +1 -1
- package/dist/esm/src/compat/Mnemonic.js +79 -79
- package/dist/esm/src/compat/Mnemonic.js.map +1 -1
- package/dist/esm/src/compat/bip-39-wordlist-en.js +2 -2
- package/dist/esm/src/compat/bip-39-wordlist-en.js.map +1 -1
- package/dist/esm/src/primitives/AESGCM.js.map +1 -1
- package/dist/esm/src/primitives/BigNumber.js.map +1 -1
- package/dist/esm/src/primitives/DRBG.js.map +1 -1
- package/dist/esm/src/primitives/ECDSA.js.map +1 -1
- package/dist/esm/src/primitives/Hash.js +26 -13
- package/dist/esm/src/primitives/Hash.js.map +1 -1
- package/dist/esm/src/primitives/PrivateKey.js +3 -2
- package/dist/esm/src/primitives/PrivateKey.js.map +1 -1
- package/dist/esm/src/primitives/PublicKey.js +1 -2
- package/dist/esm/src/primitives/PublicKey.js.map +1 -1
- package/dist/esm/src/primitives/Random.js +2 -2
- package/dist/esm/src/primitives/Random.js.map +1 -1
- package/dist/esm/src/primitives/Signature.js +141 -4
- package/dist/esm/src/primitives/Signature.js.map +1 -1
- package/dist/esm/src/primitives/SymmetricKey.js.map +1 -1
- package/dist/esm/src/primitives/TransactionSignature.js.map +1 -1
- package/dist/esm/src/primitives/utils.js +14 -9
- package/dist/esm/src/primitives/utils.js.map +1 -1
- package/dist/esm/src/script/Spend.js.map +1 -1
- package/dist/esm/src/script/templates/P2PKH.js +1 -1
- package/dist/esm/src/script/templates/P2PKH.js.map +1 -1
- package/dist/esm/src/transaction/MerklePath.js +3 -3
- package/dist/esm/src/transaction/MerklePath.js.map +1 -1
- package/dist/esm/src/transaction/Transaction.js +2 -2
- package/dist/esm/src/transaction/Transaction.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/compat/ECIES.d.ts +36 -36
- package/dist/types/src/compat/ECIES.d.ts.map +1 -1
- package/dist/types/src/compat/HD.d.ts +65 -65
- package/dist/types/src/compat/HD.d.ts.map +1 -1
- package/dist/types/src/compat/Mnemonic.d.ts +79 -79
- package/dist/types/src/compat/Mnemonic.d.ts.map +1 -1
- package/dist/types/src/primitives/AESGCM.d.ts.map +1 -1
- package/dist/types/src/primitives/BigNumber.d.ts.map +1 -1
- package/dist/types/src/primitives/Hash.d.ts.map +1 -1
- package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -1
- package/dist/types/src/primitives/PublicKey.d.ts.map +1 -1
- package/dist/types/src/primitives/Signature.d.ts +62 -0
- package/dist/types/src/primitives/Signature.d.ts.map +1 -1
- package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -1
- package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -1
- package/dist/types/src/primitives/utils.d.ts.map +1 -1
- package/dist/types/src/script/Spend.d.ts.map +1 -1
- package/dist/types/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/docs/compat.md +4 -4
- package/docs/primitives.md +288 -79
- package/package.json +1 -1
- package/src/compat/BSM.ts +12 -12
- package/src/compat/ECIES.ts +417 -418
- package/src/compat/HD.ts +228 -228
- package/src/compat/Mnemonic.ts +173 -173
- package/src/compat/__tests/BSM.test.ts +13 -2
- package/src/compat/bip-39-wordlist-en.ts +2052 -2052
- package/src/primitives/AESGCM.ts +30 -30
- package/src/primitives/BigNumber.ts +0 -1
- package/src/primitives/DRBG.ts +5 -5
- package/src/primitives/ECDSA.ts +1 -1
- package/src/primitives/Hash.ts +278 -293
- package/src/primitives/PrivateKey.ts +18 -19
- package/src/primitives/PublicKey.ts +9 -10
- package/src/primitives/Random.ts +4 -4
- package/src/primitives/Signature.ts +158 -14
- package/src/primitives/SymmetricKey.ts +3 -3
- package/src/primitives/TransactionSignature.ts +9 -9
- package/src/primitives/index.ts +1 -1
- package/src/primitives/utils.ts +60 -64
- package/src/script/Spend.ts +12 -12
- package/src/script/index.ts +1 -1
- package/src/script/templates/P2PKH.ts +1 -1
- package/src/transaction/MerklePath.ts +3 -3
- package/src/transaction/Transaction.ts +23 -23
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
84
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
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
|
-
|
|
141
|
-
|
|
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
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
185
|
+
public derive (path: string): HD {
|
|
186
|
+
if (path === 'm') {
|
|
187
|
+
return this
|
|
188
|
+
}
|
|
189
189
|
|
|
190
|
-
|
|
190
|
+
const e = path.split('/')
|
|
191
191
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
197
|
+
if (i === '0') {
|
|
198
|
+
if (c !== 'm') {
|
|
199
|
+
throw new Error('invalid path')
|
|
200
|
+
}
|
|
201
|
+
continue
|
|
202
|
+
}
|
|
203
203
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
204
|
+
if (parseInt(c.replace("'", ''), 10).toString() !== c.replace("'", '')) {
|
|
205
|
+
throw new Error('invalid path')
|
|
206
|
+
}
|
|
207
207
|
|
|
208
|
-
|
|
209
|
-
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
211
|
+
if (usePrivate) {
|
|
212
|
+
childIndex += 0x80000000
|
|
213
|
+
}
|
|
214
214
|
|
|
215
|
-
|
|
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
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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
|
-
|
|
239
|
+
const usePrivate = (i & 0x80000000) !== 0
|
|
240
240
|
|
|
241
|
-
|
|
241
|
+
const isPrivate = this.versionBytesNum === this.constants.privKey
|
|
242
242
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
243
|
+
if (usePrivate && (!this.privKey || !isPrivate)) {
|
|
244
|
+
throw new Error('Cannot do private key derivation without private key')
|
|
245
|
+
}
|
|
246
246
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
247
|
+
let ret = null
|
|
248
|
+
if (this.privKey) {
|
|
249
|
+
let data = null
|
|
250
250
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
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
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
262
|
-
|
|
261
|
+
// ki = IL + kpar (mod n).
|
|
262
|
+
const k = il.add(this.privKey).mod(new Curve().n)
|
|
263
263
|
|
|
264
|
-
|
|
265
|
-
|
|
264
|
+
ret = new HD()
|
|
265
|
+
ret.chainCode = ir
|
|
266
266
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
|
|
282
|
-
|
|
281
|
+
ret = new HD()
|
|
282
|
+
ret.chainCode = ir
|
|
283
283
|
|
|
284
|
-
|
|
285
|
-
|
|
284
|
+
ret.pubKey = newpub
|
|
285
|
+
}
|
|
286
286
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
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
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
-
|
|
346
|
-
|
|
347
|
-
|
|
345
|
+
public isPrivate (): boolean {
|
|
346
|
+
return this.versionBytesNum === this.constants.privKey
|
|
347
|
+
}
|
|
348
348
|
}
|