Completed
Pull Request — master (#665)
by thomas
15:35
created

HierarchicalKeyFactory::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\Factory;
6
7
use BitWasp\Bitcoin\Bitcoin;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
9
use BitWasp\Bitcoin\Crypto\Hash;
10
use BitWasp\Bitcoin\Crypto\Random\Random;
11
use BitWasp\Bitcoin\Key\Deterministic\HierarchicalKey;
12
use BitWasp\Bitcoin\Network\NetworkInterface;
13
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer;
14
use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer;
15
use BitWasp\Buffertools\Buffer;
16
use BitWasp\Buffertools\BufferInterface;
17
18
class HierarchicalKeyFactory
19
{
20
    /**
21
     * @var EcAdapterInterface
22
     */
23
    private $adapter;
24
25
    /**
26
     * @var Base58ExtendedKeySerializer
27
     */
28
    private $serializer;
29
30
    /**
31
     * @var PrivateKeyFactory
32
     */
33
    private $privFactory;
34
35
    /**
36
     * HierarchicalKeyFactory constructor.
37
     * @param EcAdapterInterface|null $ecAdapter
38
     * @param Base58ExtendedKeySerializer|null $serializer
39
     * @throws \Exception
40
     */
41 52
    public function __construct(EcAdapterInterface $ecAdapter = null, Base58ExtendedKeySerializer $serializer = null)
42
    {
43 52
        $this->adapter = $ecAdapter ?: Bitcoin::getEcAdapter();
44 52
        $this->privFactory = PrivateKeyFactory::compressed($this->adapter);
0 ignored issues
show
Documentation Bug introduced by
It seems like \BitWasp\Bitcoin\Key\Fac...pressed($this->adapter) of type object<self> is incompatible with the declared type object<BitWasp\Bitcoin\K...tory\PrivateKeyFactory> of property $privFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45 52
        $this->serializer = $serializer ?: new Base58ExtendedKeySerializer(
46 52
            new ExtendedKeySerializer($this->adapter)
47
        );
48 52
    }
49
50
    /**
51
     * @param Random $random
52
     * @return HierarchicalKey
53
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
54
     * @throws \Exception
55
     */
56 2
    public function generateMasterKey(Random $random): HierarchicalKey
57
    {
58 2
        return $this->fromEntropy(
59 2
            $random->bytes(64)
60
        );
61
    }
62
63
    /**
64
     * @param BufferInterface $entropy
65
     * @return HierarchicalKey
66
     * @throws \Exception
67
     */
68 18
    public function fromEntropy(BufferInterface $entropy): HierarchicalKey
69
    {
70 18
        $seed = Hash::hmac('sha512', $entropy, new Buffer('Bitcoin seed'));
71 18
        $privSecret = $seed->slice(0, 32);
72 18
        $chainCode = $seed->slice(32, 32);
73 18
        return new HierarchicalKey($this->adapter, 0, 0, 0, $chainCode, $this->privFactory->fromBuffer($privSecret));
74
    }
75
76
    /**
77
     * @param string $extendedKey
78
     * @param NetworkInterface|null $network
79
     * @return HierarchicalKey
80
     * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure
81
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
82
     */
83 34
    public function fromExtended(string $extendedKey, NetworkInterface $network = null): HierarchicalKey
84
    {
85 34
        return $this->serializer->parse($network ?: Bitcoin::getNetwork(), $extendedKey);
86
    }
87
}
88