1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Key; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer; |
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface; |
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
10
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Random; |
11
|
|
|
use BitWasp\Bitcoin\Exceptions\InvalidPrivateKey; |
12
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
13
|
|
|
use BitWasp\Bitcoin\Serializer\Key\PrivateKey\WifPrivateKeySerializer; |
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
|
|
|
*/ |
24
|
48 |
|
public static function create($compressed = false, EcAdapterInterface $ecAdapter = null) |
25
|
|
|
{ |
26
|
48 |
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
27
|
48 |
|
return self::fromHex(self::generateSecret(), $compressed, $ecAdapter); |
28
|
|
|
} |
29
|
|
|
|
30
|
48 |
|
/** |
31
|
48 |
|
* Generate a buffer containing a valid key |
32
|
|
|
* |
33
|
48 |
|
* @param EcAdapterInterface|null $ecAdapter |
34
|
|
|
* @return \BitWasp\Buffertools\BufferInterface |
35
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
36
|
|
|
*/ |
37
|
|
|
public static function generateSecret(EcAdapterInterface $ecAdapter = null) |
38
|
|
|
{ |
39
|
|
|
$random = new Random(); |
40
|
|
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
41
|
|
|
|
42
|
84 |
|
do { |
43
|
|
|
$buffer = $random->bytes(32); |
44
|
84 |
|
} while (!$ecAdapter->validatePrivateKey($buffer)); |
45
|
84 |
|
|
46
|
|
|
return $buffer; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param BufferInterface|string $hex |
51
|
|
|
* @param bool $compressed |
52
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
53
|
48 |
|
* @return PrivateKeyInterface |
54
|
|
|
*/ |
55
|
48 |
|
public static function fromHex($hex, $compressed = false, EcAdapterInterface $ecAdapter = null) |
56
|
48 |
|
{ |
57
|
48 |
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
58
|
|
|
|
59
|
|
|
/** @var PrivateKeySerializerInterface $serializer */ |
60
|
|
|
$serializer = EcSerializer::getSerializer(PrivateKeySerializerInterface::class); |
61
|
|
|
|
62
|
|
|
$parsed = $serializer->parse($hex); |
63
|
|
|
if ($compressed) { |
64
|
|
|
$parsed = $ecAdapter->getPrivateKey($parsed->getSecret(), $compressed); |
65
|
|
|
} |
66
|
|
|
|
67
|
12 |
|
return $parsed; |
68
|
|
|
} |
69
|
12 |
|
|
70
|
12 |
|
/** |
71
|
12 |
|
* @param int|string $int |
72
|
12 |
|
* @param bool $compressed |
73
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
74
|
12 |
|
* @return PrivateKeyInterface |
75
|
|
|
*/ |
76
|
|
|
public static function fromInt($int, $compressed = false, EcAdapterInterface $ecAdapter = null) |
77
|
|
|
{ |
78
|
|
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
79
|
|
|
$secret = Buffer::int($int, 32, $ecAdapter->getMath())->getGmp(); |
80
|
|
|
return $ecAdapter->getPrivateKey($secret, $compressed); |
81
|
|
|
} |
82
|
|
|
|
83
|
162 |
|
/** |
84
|
|
|
* @param string $wif |
85
|
162 |
|
* @param EcAdapterInterface|null $ecAdapter |
86
|
|
|
* @param NetworkInterface $network |
87
|
|
|
* @return \BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface |
88
|
162 |
|
* @throws InvalidPrivateKey |
89
|
|
|
*/ |
90
|
162 |
|
public static function fromWif($wif, EcAdapterInterface $ecAdapter = null, NetworkInterface $network = null) |
91
|
162 |
|
{ |
92
|
150 |
|
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); |
93
|
75 |
|
$network = $network ?: Bitcoin::getNetwork(); |
94
|
|
|
$serializer = EcSerializer::getSerializer(PrivateKeySerializerInterface::class); |
95
|
162 |
|
$wifSerializer = new WifPrivateKeySerializer($ecAdapter->getMath(), $serializer); |
96
|
|
|
|
97
|
|
|
return $wifSerializer->parse($wif, $network); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|