1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Key\Deterministic; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Address\BaseAddressCreator; |
8
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; |
11
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
12
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; |
13
|
|
|
use BitWasp\Bitcoin\Crypto\Hash; |
14
|
|
|
use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData; |
15
|
|
|
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory; |
16
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
17
|
|
|
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer; |
18
|
|
|
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer; |
19
|
|
|
use BitWasp\Bitcoin\Util\IntRange; |
20
|
|
|
use BitWasp\Buffertools\Buffer; |
21
|
|
|
use BitWasp\Buffertools\BufferInterface; |
22
|
|
|
|
23
|
|
|
class HierarchicalKey |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EcAdapterInterface |
27
|
|
|
*/ |
28
|
|
|
protected $ecAdapter; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $depth; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $parentFingerprint; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $sequence; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var BufferInterface |
47
|
|
|
*/ |
48
|
|
|
private $chainCode; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var KeyInterface |
52
|
|
|
*/ |
53
|
|
|
private $key; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ScriptDataFactory |
57
|
|
|
*/ |
58
|
|
|
private $scriptDataFactory; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var ScriptAndSignData|null |
62
|
|
|
*/ |
63
|
|
|
private $scriptAndSignData; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param EcAdapterInterface $ecAdapter |
67
|
|
|
* @param ScriptDataFactory $scriptDataFactory |
68
|
|
|
* @param int $depth |
69
|
|
|
* @param int $parentFingerprint |
70
|
|
|
* @param int $sequence |
71
|
|
|
* @param BufferInterface $chainCode |
72
|
|
|
* @param KeyInterface $key |
73
|
|
|
*/ |
74
|
71 |
|
public function __construct(EcAdapterInterface $ecAdapter, ScriptDataFactory $scriptDataFactory, int $depth, int $parentFingerprint, int $sequence, BufferInterface $chainCode, KeyInterface $key) |
75
|
|
|
{ |
76
|
71 |
|
if ($depth < 0 || $depth > IntRange::U8_MAX) { |
77
|
|
|
throw new \InvalidArgumentException('Invalid depth for BIP32 key, must be in range [0 - 255] inclusive'); |
78
|
|
|
} |
79
|
|
|
|
80
|
71 |
|
if ($parentFingerprint < 0 || $parentFingerprint > IntRange::U32_MAX) { |
81
|
|
|
throw new \InvalidArgumentException('Invalid fingerprint for BIP32 key, must be in range [0 - (2^31)-1] inclusive'); |
82
|
|
|
} |
83
|
|
|
|
84
|
71 |
|
if ($sequence < 0 || $sequence > IntRange::U32_MAX) { |
85
|
|
|
throw new \InvalidArgumentException('Invalid sequence for BIP32 key, must be in range [0 - (2^31)-1] inclusive'); |
86
|
|
|
} |
87
|
|
|
|
88
|
71 |
|
if ($chainCode->getSize() !== 32) { |
89
|
|
|
throw new \RuntimeException('Chaincode should be 32 bytes'); |
90
|
|
|
} |
91
|
|
|
|
92
|
71 |
|
if (!$key->isCompressed()) { |
93
|
1 |
|
throw new \InvalidArgumentException('A HierarchicalKey must always be compressed'); |
94
|
|
|
} |
95
|
|
|
|
96
|
70 |
|
$this->ecAdapter = $ecAdapter; |
97
|
70 |
|
$this->depth = $depth; |
98
|
70 |
|
$this->sequence = $sequence; |
99
|
70 |
|
$this->parentFingerprint = $parentFingerprint; |
100
|
70 |
|
$this->chainCode = $chainCode; |
101
|
70 |
|
$this->key = $key; |
102
|
70 |
|
$this->scriptDataFactory = $scriptDataFactory; |
103
|
70 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return the depth of this key. This is limited to 256 sequential derivations. |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
30 |
|
public function getDepth(): int |
111
|
|
|
{ |
112
|
30 |
|
return $this->depth; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the sequence number for this address. Hardened keys are |
117
|
|
|
* created with sequence > 0x80000000. a sequence number lower |
118
|
|
|
* than this can be derived with the public key. |
119
|
|
|
* |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
25 |
|
public function getSequence(): int |
123
|
|
|
{ |
124
|
25 |
|
return $this->sequence; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the fingerprint of the parent key. For master keys, this is 00000000. |
129
|
|
|
* |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
26 |
|
public function getFingerprint(): int |
133
|
|
|
{ |
134
|
26 |
|
if ($this->getDepth() === 0) { |
135
|
14 |
|
return 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
22 |
|
return $this->parentFingerprint; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return the fingerprint to be used for child keys. |
143
|
|
|
* @return int |
144
|
|
|
*/ |
145
|
33 |
|
public function getChildFingerprint(): int |
146
|
|
|
{ |
147
|
33 |
|
$pubKeyHash = $this->getPublicKey()->getPubKeyHash(); |
148
|
33 |
|
return (int) $pubKeyHash->slice(0, 4)->getInt(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
153
|
|
|
* in child derivations |
154
|
|
|
* |
155
|
|
|
* @return BufferInterface |
156
|
|
|
*/ |
157
|
24 |
|
public function getChainCode(): BufferInterface |
158
|
|
|
{ |
159
|
24 |
|
return $this->chainCode; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return PrivateKeyInterface |
164
|
|
|
*/ |
165
|
42 |
|
public function getPrivateKey(): PrivateKeyInterface |
166
|
|
|
{ |
167
|
42 |
|
if ($this->key->isPrivate()) { |
168
|
|
|
/** @var PrivateKeyInterface $key */ |
169
|
40 |
|
$key = $this->key; |
170
|
40 |
|
return $key; |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
throw new \RuntimeException('Unable to get private key, not known'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the public key the private key or public key. |
178
|
|
|
* |
179
|
|
|
* @return PublicKeyInterface |
180
|
|
|
*/ |
181
|
44 |
|
public function getPublicKey(): PublicKeyInterface |
182
|
|
|
{ |
183
|
44 |
|
if ($this->isPrivate()) { |
184
|
36 |
|
return $this->getPrivateKey()->getPublicKey(); |
185
|
|
|
} else { |
186
|
|
|
/** @var PublicKeyInterface $key */ |
187
|
27 |
|
$key = $this->key; |
188
|
27 |
|
return $key; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return HierarchicalKey |
194
|
|
|
*/ |
195
|
21 |
|
public function withoutPrivateKey(): HierarchicalKey |
196
|
|
|
{ |
197
|
21 |
|
$clone = clone $this; |
198
|
21 |
|
$clone->key = $clone->getPublicKey(); |
199
|
21 |
|
return $clone; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param ScriptDataFactory $factory |
204
|
|
|
* @return HierarchicalKey |
205
|
|
|
*/ |
206
|
1 |
|
public function withScriptFactory(ScriptDataFactory $factory) |
207
|
|
|
{ |
208
|
1 |
|
$clone = clone $this; |
209
|
1 |
|
$clone->scriptDataFactory = $factory; |
210
|
1 |
|
return $clone; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return ScriptDataFactory |
215
|
|
|
*/ |
216
|
24 |
|
public function getScriptDataFactory() |
217
|
|
|
{ |
218
|
24 |
|
return $this->scriptDataFactory; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData |
223
|
|
|
*/ |
224
|
19 |
|
public function getScriptAndSignData() |
225
|
|
|
{ |
226
|
19 |
|
if (null === $this->scriptAndSignData) { |
227
|
19 |
|
$this->scriptAndSignData = $this->scriptDataFactory->convertKey($this->key); |
228
|
|
|
} |
229
|
|
|
|
230
|
19 |
|
return $this->scriptAndSignData; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param BaseAddressCreator $addressCreator |
235
|
|
|
* @return \BitWasp\Bitcoin\Address\Address |
236
|
|
|
*/ |
237
|
13 |
|
public function getAddress(BaseAddressCreator $addressCreator) |
238
|
|
|
{ |
239
|
13 |
|
return $this->getScriptAndSignData()->getAddress($addressCreator); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Return whether this is a private key |
244
|
|
|
* |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
48 |
|
public function isPrivate(): bool |
248
|
|
|
{ |
249
|
48 |
|
return $this->key->isPrivate(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Return whether the key is hardened |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
2 |
|
public function isHardened(): bool |
258
|
|
|
{ |
259
|
2 |
|
return ($this->sequence >> 31) === 1; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Create a buffer containing data to be hashed hashed to yield the child offset |
264
|
|
|
* |
265
|
|
|
* @param int $sequence |
266
|
|
|
* @return BufferInterface |
267
|
|
|
* @throws \Exception |
268
|
|
|
*/ |
269
|
39 |
|
public function getHmacSeed(int $sequence): BufferInterface |
270
|
|
|
{ |
271
|
39 |
|
if ($sequence < 0 || $sequence > IntRange::U32_MAX) { |
272
|
6 |
|
throw new \InvalidArgumentException("Sequence is outside valid range, must be >= 0 && <= (2^31)-1"); |
273
|
|
|
} |
274
|
|
|
|
275
|
33 |
|
if (($sequence >> 31) === 1) { |
276
|
30 |
|
if ($this->isPrivate() === false) { |
277
|
2 |
|
throw new \Exception("Can't derive a hardened key without the private key"); |
278
|
|
|
} |
279
|
|
|
|
280
|
28 |
|
$data = "\x00{$this->getPrivateKey()->getBinary()}"; |
281
|
|
|
} else { |
282
|
31 |
|
$data = $this->getPublicKey()->getBinary(); |
283
|
|
|
} |
284
|
|
|
|
285
|
31 |
|
return new Buffer($data . pack("N", $sequence)); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Derive a child key |
290
|
|
|
* |
291
|
|
|
* @param int $sequence |
292
|
|
|
* @return HierarchicalKey |
293
|
|
|
* @throws \Exception |
294
|
|
|
*/ |
295
|
36 |
|
public function deriveChild(int $sequence): HierarchicalKey |
296
|
|
|
{ |
297
|
36 |
|
$nextDepth = $this->depth + 1; |
298
|
36 |
|
if ($nextDepth > 255) { |
299
|
|
|
throw new \InvalidArgumentException('Invalid depth for BIP32 key, cannot exceed 255'); |
300
|
|
|
} |
301
|
|
|
|
302
|
36 |
|
$hash = Hash::hmac('sha512', $this->getHmacSeed($sequence), $this->chainCode); |
303
|
31 |
|
$offset = $hash->slice(0, 32); |
304
|
31 |
|
$chain = $hash->slice(32); |
305
|
|
|
|
306
|
31 |
|
if (false === $this->ecAdapter->validatePrivateKey($offset)) { |
307
|
1 |
|
return $this->deriveChild($sequence + 1); |
308
|
|
|
} |
309
|
|
|
|
310
|
31 |
|
$key = $this->isPrivate() ? $this->getPrivateKey() : $this->getPublicKey(); |
311
|
31 |
|
$key = $key->tweakAdd($offset->getGmp()); |
312
|
|
|
|
313
|
31 |
|
return new HierarchicalKey( |
314
|
31 |
|
$this->ecAdapter, |
315
|
31 |
|
$this->scriptDataFactory, |
316
|
31 |
|
$nextDepth, |
317
|
31 |
|
$this->getChildFingerprint(), |
318
|
31 |
|
$sequence, |
319
|
31 |
|
$chain, |
320
|
31 |
|
$key |
321
|
|
|
); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param array|\stdClass|\Traversable $list |
326
|
|
|
* @return HierarchicalKey |
327
|
|
|
* @throws \Exception |
328
|
|
|
*/ |
329
|
26 |
|
public function deriveFromList($list): HierarchicalKey |
330
|
|
|
{ |
331
|
26 |
|
if (!is_array($list) && !$list instanceof \Traversable && !$list instanceof \stdClass) { |
332
|
|
|
throw new \InvalidArgumentException('List must be an array or \Traversable'); |
333
|
|
|
} |
334
|
|
|
|
335
|
26 |
|
$key = $this; |
336
|
26 |
|
foreach ($list as $sequence) { |
|
|
|
|
337
|
26 |
|
$key = $key->deriveChild((int) $sequence); |
338
|
|
|
} |
339
|
|
|
|
340
|
26 |
|
return $key; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
345
|
|
|
* |
346
|
|
|
* @param string $path |
347
|
|
|
* @return HierarchicalKey |
348
|
|
|
* @throws \Exception |
349
|
|
|
*/ |
350
|
26 |
|
public function derivePath(string $path): HierarchicalKey |
351
|
|
|
{ |
352
|
26 |
|
$sequences = new HierarchicalKeySequence(); |
353
|
26 |
|
return $this->deriveFromList($sequences->decodePath($path)); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Serializes the instance according to whether it wraps a private or public key. |
358
|
|
|
* @param NetworkInterface $network |
359
|
|
|
* @return string |
360
|
|
|
*/ |
361
|
16 |
|
public function toExtendedKey(NetworkInterface $network = null): string |
362
|
|
|
{ |
363
|
16 |
|
$network = $network ?: Bitcoin::getNetwork(); |
364
|
|
|
|
365
|
16 |
|
$extendedSerializer = new Base58ExtendedKeySerializer(new ExtendedKeySerializer($this->ecAdapter)); |
366
|
16 |
|
$extended = $extendedSerializer->serialize($network, $this); |
367
|
16 |
|
return $extended; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Explicitly serialize as a private key. Throws an exception if |
372
|
|
|
* the key isn't private. |
373
|
|
|
* |
374
|
|
|
* @param NetworkInterface $network |
375
|
|
|
* @return string |
376
|
|
|
*/ |
377
|
16 |
|
public function toExtendedPrivateKey(NetworkInterface $network = null): string |
378
|
|
|
{ |
379
|
16 |
|
if (!$this->isPrivate()) { |
380
|
2 |
|
throw new \LogicException('Cannot create extended private key from public'); |
381
|
|
|
} |
382
|
|
|
|
383
|
14 |
|
return $this->toExtendedKey($network); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Explicitly serialize as a public key. This will always work. |
388
|
|
|
* |
389
|
|
|
* @param NetworkInterface $network |
390
|
|
|
* @return string |
391
|
|
|
*/ |
392
|
13 |
|
public function toExtendedPublicKey(NetworkInterface $network = null): string |
393
|
|
|
{ |
394
|
13 |
|
return $this->withoutPrivateKey()->toExtendedKey($network); |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.