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); |
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
|
|
|
|