Completed
Pull Request — 0.0.35 (#660)
by thomas
22:14
created

HierarchicalKey::__construct()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.7555

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 6
nop 7
dl 0
loc 30
ccs 15
cts 19
cp 0.7895
crap 9.7555
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\Deterministic;
4
5
use BitWasp\Bitcoin\Address\BaseAddressCreator;
6
use BitWasp\Bitcoin\Bitcoin;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
11
use BitWasp\Bitcoin\Crypto\Hash;
12
use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData;
13
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory;
14
use BitWasp\Bitcoin\Network\NetworkInterface;
15
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer;
16
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer;
17
use BitWasp\Bitcoin\Util\IntRange;
18
use BitWasp\Buffertools\Buffer;
19
use BitWasp\Buffertools\BufferInterface;
20
use BitWasp\Buffertools\Buffertools;
21
use BitWasp\Buffertools\Parser;
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 75
    public function __construct(EcAdapterInterface $ecAdapter, ScriptDataFactory $scriptDataFactory, $depth, $parentFingerprint, $sequence, BufferInterface $chainCode, KeyInterface $key)
75
    {
76 75
        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 75
        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 75
        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 75
        if ($chainCode->getSize() !== 32) {
89
            throw new \RuntimeException('Chaincode should be 32 bytes');
90
        }
91
92 75
        if (!$key->isCompressed()) {
93 1
            throw new \InvalidArgumentException('A HierarchicalKey must always be compressed');
94
        }
95
96 74
        $this->ecAdapter = $ecAdapter;
97 74
        $this->depth = $depth;
98 74
        $this->sequence = $sequence;
99 74
        $this->parentFingerprint = $parentFingerprint;
100 74
        $this->chainCode = $chainCode;
101 74
        $this->key = $key;
102 74
        $this->scriptDataFactory = $scriptDataFactory;
103 74
    }
104
105
    /**
106
     * Return the depth of this key. This is limited to 256 sequential derivations.
107
     *
108
     * @return int
109
     */
110 34
    public function getDepth()
111
    {
112 34
        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 29
    public function getSequence()
123
    {
124 29
        return $this->sequence;
125
    }
126
127
    /**
128
     * Get the fingerprint of the parent key. For master keys, this is 00000000.
129
     *
130
     * @return string
131
     */
132 30
    public function getFingerprint()
133
    {
134 30
        if ($this->getDepth() === 0) {
135 14
            return 0;
136
        }
137
138 30
        return $this->parentFingerprint;
139
    }
140
141
    /**
142
     * Return the fingerprint to be used for child keys.
143
     * @return int
144
     */
145 37
    public function getChildFingerprint()
146
    {
147 37
        return $this->getPublicKey()->getPubKeyHash()->slice(0, 4)->getInt();
148
    }
149
150
    /**
151
     * Return the chain code - a deterministic 'salt' for HMAC-SHA512
152
     * in child derivations
153
     *
154
     * @return BufferInterface
155
     */
156 28
    public function getChainCode()
157
    {
158 28
        return $this->chainCode;
159
    }
160
161
    /**
162
     * @return PrivateKeyInterface
163
     */
164 46
    public function getPrivateKey()
165
    {
166 46
        if ($this->key->isPrivate()) {
167
            /** @var PrivateKeyInterface $key */
168 44
            $key = $this->key;
169 44
            return $key;
170
        }
171
172 2
        throw new \RuntimeException('Unable to get private key, not known');
173
    }
174
175
    /**
176
     * Get the public key the private key or public key.
177
     *
178
     * @return PublicKeyInterface
179
     */
180 48
    public function getPublicKey()
181
    {
182 48
        if ($this->isPrivate()) {
183 40
            return $this->getPrivateKey()->getPublicKey();
184
        } else {
185
            /** @var PublicKeyInterface $key */
186 31
            $key = $this->key;
187 31
            return $key;
188
        }
189
    }
190
191
    /**
192
     * @return HierarchicalKey
193
     */
194 25
    public function withoutPrivateKey()
195
    {
196 25
        $clone = clone $this;
197 25
        $clone->key = $clone->getPublicKey();
198 25
        return $clone;
199
    }
200
201
    /**
202
     * @param ScriptDataFactory $factory
203
     * @return HierarchicalKey
204
     */
205 1
    public function withScriptFactory(ScriptDataFactory $factory)
206
    {
207 1
        $clone = clone $this;
208 1
        $clone->scriptDataFactory = $factory;
209 1
        return $clone;
210
    }
211
212
    /**
213
     * @return ScriptDataFactory
214
     */
215 28
    public function getScriptDataFactory()
216
    {
217 28
        return $this->scriptDataFactory;
218
    }
219
220
    /**
221
     * @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData
222
     */
223 23
    public function getScriptAndSignData()
224
    {
225 23
        if (null === $this->scriptAndSignData) {
226 23
            $this->scriptAndSignData = $this->scriptDataFactory->convertKey($this->key);
227
        }
228
229 23
        return $this->scriptAndSignData;
230
    }
231
232
    /**
233
     * @param BaseAddressCreator $addressCreator
234
     * @return \BitWasp\Bitcoin\Address\Address
235
     */
236 17
    public function getAddress(BaseAddressCreator $addressCreator)
237
    {
238 17
        return $this->getScriptAndSignData()->getAddress($addressCreator);
239
    }
240
241
    /**
242
     * Return whether this is a private key
243
     *
244
     * @return bool
245
     */
246 52
    public function isPrivate()
247
    {
248 52
        return $this->key->isPrivate();
249
    }
250
251
    /**
252
     * Return whether the key is hardened
253
     *
254
     * @return bool
255
     */
256 2
    public function isHardened()
257
    {
258 2
        return ($this->sequence >> 31) === 1;
259
    }
260
261
    /**
262
     * Create a buffer containing data to be hashed hashed to yield the child offset
263
     *
264
     * @param int $sequence
265
     * @return BufferInterface
266
     * @throws \Exception
267
     */
268 43
    public function getHmacSeed($sequence)
269
    {
270 43
        if ($sequence < 0 || $sequence > IntRange::U32_MAX) {
271 6
            throw new \InvalidArgumentException("Sequence is outside valid range, must be >= 0 && <= (2^31)-1");
272
        }
273
274 37
        if (($sequence >> 31) === 1) {
275 34
            if ($this->isPrivate() === false) {
276 2
                throw new \Exception("Can't derive a hardened key without the private key");
277
            }
278
279 32
            $buffer = Buffertools::concat(new Buffer("\x00"), $this->getPrivateKey()->getBuffer());
280
        } else {
281 35
            $buffer = $this->getPublicKey()->getBuffer();
282
        }
283
284 35
        return (new Parser($buffer))
285 35
            ->writeBytes(4, Buffer::int($sequence, 4))
286 35
            ->getBuffer();
287
    }
288
289
    /**
290
     * Derive a child key
291
     *
292
     * @param int $sequence
293
     * @return HierarchicalKey
294
     * @throws \Exception
295
     */
296 40
    public function deriveChild($sequence)
297
    {
298 40
        $nextDepth = $this->depth + 1;
299 40
        if ($nextDepth > 255) {
300
            throw new \InvalidArgumentException('Invalid depth for BIP32 key, cannot exceed 255');
301
        }
302
303 40
        $hash = Hash::hmac('sha512', $this->getHmacSeed($sequence), $this->chainCode);
304 35
        $offset = $hash->slice(0, 32);
305 35
        $chain = $hash->slice(32);
306
307 35
        if (false === $this->ecAdapter->validatePrivateKey($offset)) {
308 1
            return $this->deriveChild($sequence + 1);
309
        }
310
311 35
        $key = $this->isPrivate() ? $this->getPrivateKey() : $this->getPublicKey();
312 35
        $key = $key->tweakAdd($offset->getGmp());
313
314 35
        return new HierarchicalKey(
315 35
            $this->ecAdapter,
316 35
            $this->scriptDataFactory,
317 35
            $nextDepth,
318 35
            $this->getChildFingerprint(),
319 35
            $sequence,
320 35
            $chain,
321 35
            $key
322
        );
323
    }
324
325
    /**
326
     * @param array|\stdClass|\Traversable $list
327
     * @return HierarchicalKey
328
     * @throws \Exception
329
     */
330 30
    public function deriveFromList($list)
331
    {
332 30
        if (!is_array($list) && !$list instanceof \Traversable && !$list instanceof \stdClass) {
333
            throw new \InvalidArgumentException('List must be an array or \Traversable');
334
        }
335
336 30
        $key = $this;
337 30
        foreach ($list as $sequence) {
0 ignored issues
show
Bug introduced by
The expression $list of type array|object<Traversable>|object<stdClass> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
338 30
            $key = $key->deriveChild($sequence);
339
        }
340
341 30
        return $key;
342
    }
343
344
    /**
345
     * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key
346
     *
347
     * @param string $path
348
     * @return HierarchicalKey
349
     * @throws \Exception
350
     */
351 30
    public function derivePath($path)
352
    {
353 30
        $sequences = new HierarchicalKeySequence();
354 30
        return $this->deriveFromList($sequences->decodePath($path));
355
    }
356
357
    /**
358
     * Serializes the instance according to whether it wraps a private or public key.
359
     * @param NetworkInterface $network
360
     * @return string
361
     */
362 16
    public function toExtendedKey(NetworkInterface $network = null)
363
    {
364 16
        $network = $network ?: Bitcoin::getNetwork();
365
366 16
        $extendedSerializer = new Base58ExtendedKeySerializer(new ExtendedKeySerializer($this->ecAdapter));
367 16
        $extended = $extendedSerializer->serialize($network, $this);
368 16
        return $extended;
369
    }
370
371
    /**
372
     * Explicitly serialize as a private key. Throws an exception if
373
     * the key isn't private.
374
     *
375
     * @param NetworkInterface $network
376
     * @return string
377
     */
378 16
    public function toExtendedPrivateKey(NetworkInterface $network = null)
379
    {
380 16
        if (!$this->isPrivate()) {
381 2
            throw new \LogicException('Cannot create extended private key from public');
382
        }
383
384 14
        return $this->toExtendedKey($network);
385
    }
386
387
    /**
388
     * Explicitly serialize as a public key. This will always work.
389
     *
390
     * @param NetworkInterface $network
391
     * @return string
392
     */
393 13
    public function toExtendedPublicKey(NetworkInterface $network = null)
394
    {
395 13
        return $this->withoutPrivateKey()->toExtendedKey($network);
396
    }
397
}
398