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\EcAdapter\EcSerializer; |
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
11
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface; |
12
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Random; |
13
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
14
|
|
|
use BitWasp\Bitcoin\Serializer\Key\PrivateKey\WifPrivateKeySerializer; |
15
|
|
|
use BitWasp\Buffertools\Buffer; |
16
|
|
|
use BitWasp\Buffertools\BufferInterface; |
17
|
|
|
|
18
|
|
|
class PrivateKeyFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $compressed; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var PrivateKeySerializerInterface |
27
|
|
|
*/ |
28
|
|
|
private $privSerializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var WifPrivateKeySerializer |
32
|
|
|
*/ |
33
|
|
|
private $wifSerializer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* PrivateKeyFactory constructor. |
37
|
|
|
* @param bool $compressed |
38
|
|
|
* @param EcAdapterInterface $ecAdapter |
39
|
|
|
*/ |
40
|
151 |
|
public function __construct(bool $compressed, EcAdapterInterface $ecAdapter = null) |
41
|
|
|
{ |
42
|
151 |
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
43
|
151 |
|
$this->privSerializer = EcSerializer::getSerializer(PrivateKeySerializerInterface::class, true, $ecAdapter); |
44
|
151 |
|
$this->wifSerializer = new WifPrivateKeySerializer($this->privSerializer); |
45
|
151 |
|
$this->compressed = $compressed; |
46
|
151 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
50
|
|
|
* @return PrivateKeyFactory |
51
|
|
|
*/ |
52
|
33 |
|
public static function uncompressed(EcAdapterInterface $ecAdapter = null): PrivateKeyFactory |
53
|
|
|
{ |
54
|
33 |
|
return new self(false, $ecAdapter); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
59
|
|
|
* @return PrivateKeyFactory |
60
|
|
|
*/ |
61
|
102 |
|
public static function compressed(EcAdapterInterface $ecAdapter = null): PrivateKeyFactory |
62
|
|
|
{ |
63
|
102 |
|
return new self(true, $ecAdapter); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
2 |
|
public function isCompressed(): bool |
70
|
|
|
{ |
71
|
2 |
|
return $this->compressed; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Random $random |
76
|
|
|
* @return PrivateKeyInterface |
77
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
78
|
|
|
*/ |
79
|
15 |
|
public function generate(Random $random): PrivateKeyInterface |
80
|
|
|
{ |
81
|
15 |
|
return $this->fromBuffer( |
82
|
15 |
|
$random->bytes(32) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $hex |
88
|
|
|
* @return PrivateKeyInterface |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
47 |
|
public function fromHex(string $hex): PrivateKeyInterface |
92
|
|
|
{ |
93
|
47 |
|
return $this->fromBuffer(Buffer::hex($hex)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param BufferInterface $buffer |
98
|
|
|
* @return PrivateKeyInterface |
99
|
|
|
*/ |
100
|
108 |
|
public function fromBuffer(BufferInterface $buffer): PrivateKeyInterface |
101
|
|
|
{ |
102
|
108 |
|
return $this->privSerializer->parse( |
103
|
108 |
|
$buffer, |
104
|
108 |
|
$this->compressed |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $wif |
110
|
|
|
* @param NetworkInterface $network |
111
|
|
|
* @return PrivateKeyInterface |
112
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
113
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\InvalidPrivateKey |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
8 |
|
public function fromWif(string $wif, NetworkInterface $network = null): PrivateKeyInterface |
117
|
|
|
{ |
118
|
8 |
|
return $this->wifSerializer->parse($wif, $network); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|