|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Key; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
|
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
|
10
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Random; |
|
11
|
|
|
use BitWasp\Bitcoin\Exceptions\InvalidPrivateKey; |
|
12
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
13
|
|
|
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory as PrivKeyFactory; |
|
14
|
|
|
use BitWasp\Buffertools\Buffer; |
|
15
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
16
|
|
|
|
|
17
|
|
|
class PrivateKeyFactory |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param bool $compressed |
|
21
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
|
22
|
|
|
* @return PrivateKeyInterface |
|
23
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function create(bool $compressed = false, EcAdapterInterface $ecAdapter = null): PrivateKeyInterface |
|
26
|
|
|
{ |
|
27
|
|
|
return (new PrivKeyFactory($compressed, $ecAdapter ?: Bitcoin::getEcAdapter())) |
|
28
|
|
|
->generate(new Random()) |
|
29
|
|
|
; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Generate a buffer containing a valid key |
|
34
|
|
|
* |
|
35
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
|
36
|
|
|
* @return BufferInterface |
|
37
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function generateSecret(EcAdapterInterface $ecAdapter = null): BufferInterface |
|
40
|
|
|
{ |
|
41
|
|
|
$random = new Random(); |
|
42
|
|
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
|
43
|
|
|
|
|
44
|
|
|
do { |
|
45
|
|
|
$buffer = $random->bytes(32); |
|
46
|
|
|
} while (!$ecAdapter->validatePrivateKey($buffer)); |
|
47
|
|
|
|
|
48
|
|
|
return $buffer; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $hex |
|
53
|
|
|
* @param bool $compressed |
|
54
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
|
55
|
|
|
* @return PrivateKeyInterface |
|
56
|
|
|
* @throws \Exception |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function fromHex(string $hex, bool $compressed = false, EcAdapterInterface $ecAdapter = null): PrivateKeyInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return (new PrivKeyFactory($compressed, $ecAdapter ?: Bitcoin::getEcAdapter())) |
|
61
|
|
|
->fromHex($hex) |
|
62
|
|
|
; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param BufferInterface $buffer |
|
67
|
|
|
* @param bool $compressed |
|
68
|
|
|
* @param EcAdapterInterface $ecAdapter |
|
69
|
|
|
* @return PrivateKeyInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function fromBuffer(BufferInterface $buffer, bool $compressed, EcAdapterInterface $ecAdapter = null): PrivateKeyInterface |
|
72
|
|
|
{ |
|
73
|
|
|
return (new PrivKeyFactory($compressed, $ecAdapter ?: Bitcoin::getEcAdapter())) |
|
74
|
|
|
->fromBuffer($buffer) |
|
75
|
|
|
; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param int|string $int |
|
80
|
|
|
* @param bool $compressed |
|
81
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
|
82
|
|
|
* @return PrivateKeyInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function fromInt($int, bool $compressed = false, EcAdapterInterface $ecAdapter = null) |
|
85
|
|
|
{ |
|
86
|
|
|
return (new PrivKeyFactory($compressed, $ecAdapter ?: Bitcoin::getEcAdapter())) |
|
87
|
|
|
->fromBuffer(Buffer::int($int, 32)) |
|
88
|
|
|
; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $wif |
|
93
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
|
94
|
|
|
* @param NetworkInterface|null $network |
|
95
|
|
|
* @return PrivateKeyInterface |
|
96
|
|
|
* @throws InvalidPrivateKey |
|
97
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function fromWif(string $wif, EcAdapterInterface $ecAdapter = null, NetworkInterface $network = null) |
|
100
|
|
|
{ |
|
101
|
|
|
return (new PrivKeyFactory(true, $ecAdapter ?: Bitcoin::getEcAdapter())) |
|
102
|
|
|
->fromWif($wif, $network) |
|
103
|
|
|
; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|