|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter; |
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key\PrivateKeySerializer; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature; |
|
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key; |
|
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
|
11
|
|
|
use BitWasp\Bitcoin\Crypto\Random\RbgInterface; |
|
12
|
|
|
use BitWasp\Bitcoin\Exceptions\InvalidPrivateKey; |
|
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 PrivateKey extends Key implements PrivateKeyInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var \GMP |
|
22
|
|
|
*/ |
|
23
|
|
|
private $secret; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $secretBin; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $compressed; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var PublicKey |
|
37
|
|
|
*/ |
|
38
|
|
|
private $publicKey; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var EcAdapter |
|
42
|
|
|
*/ |
|
43
|
|
|
private $ecAdapter; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param EcAdapter $adapter |
|
47
|
|
|
* @param \GMP $secret |
|
48
|
|
|
* @param bool|false $compressed |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(EcAdapter $adapter, \GMP $secret, $compressed = false) |
|
52
|
|
|
{ |
|
53
|
|
|
$buffer = Buffer::int(gmp_strval($secret, 10), 32, $adapter->getMath()); |
|
54
|
|
|
if (!$adapter->validatePrivateKey($buffer)) { |
|
55
|
|
|
throw new InvalidPrivateKey('Invalid private key'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (false === is_bool($compressed)) { |
|
59
|
|
|
throw new \InvalidArgumentException('PrivateKey: Compressed argument must be a boolean'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->ecAdapter = $adapter; |
|
63
|
|
|
$this->secret = $secret; |
|
64
|
|
|
$this->secretBin = $buffer->getBinary(); |
|
65
|
|
|
$this->compressed = $compressed; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param BufferInterface $msg32 |
|
70
|
|
|
* @param RbgInterface|null $rbgInterface |
|
71
|
|
|
* @return Signature |
|
72
|
|
|
*/ |
|
73
|
|
|
public function sign(BufferInterface $msg32, RbgInterface $rbgInterface = null) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->ecAdapter->sign($msg32, $this, $rbgInterface); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return bool|false |
|
80
|
|
|
*/ |
|
81
|
|
|
public function isCompressed() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->compressed; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return int|string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getSecret() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->secret; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getSecretBinary() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->secretBin; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return PublicKey |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getPublicKey() |
|
106
|
|
|
{ |
|
107
|
|
|
if (null === $this->publicKey) { |
|
108
|
|
|
$context = $this->ecAdapter->getContext(); |
|
109
|
|
|
$publicKey_t = ''; |
|
110
|
|
|
/** @var resource $publicKey_t */ |
|
111
|
|
|
if (1 !== secp256k1_ec_pubkey_create($context, $publicKey_t, $this->getBinary())) { |
|
112
|
|
|
throw new \RuntimeException('Failed to create public key'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->publicKey = new PublicKey($this->ecAdapter, $publicKey_t, $this->compressed); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->publicKey; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param \GMP $tweak |
|
123
|
|
|
* @return PrivateKey |
|
124
|
|
|
*/ |
|
125
|
|
|
public function tweakAdd(\GMP $tweak) |
|
126
|
|
|
{ |
|
127
|
|
|
$adapter = $this->ecAdapter; |
|
128
|
|
|
$math = $adapter->getMath(); |
|
129
|
|
|
$context = $adapter->getContext(); |
|
130
|
|
|
$privateKey = $this->getBinary(); // mod by reference |
|
131
|
|
|
$tweak = Buffer::int($math->toString($tweak), 32, $math)->getBinary(); |
|
132
|
|
|
$ret = \secp256k1_ec_privkey_tweak_add( |
|
133
|
|
|
$context, |
|
134
|
|
|
$privateKey, |
|
135
|
|
|
$tweak |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
if ($ret !== 1) { |
|
139
|
|
|
throw new \RuntimeException('Secp256k1 privkey tweak add: failed'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$secret = new Buffer($privateKey); |
|
143
|
|
|
return $adapter->getPrivateKey($secret->getGmp(), $this->compressed); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param \GMP $tweak |
|
148
|
|
|
* @return PrivateKey |
|
149
|
|
|
*/ |
|
150
|
|
|
public function tweakMul(\GMP $tweak) |
|
151
|
|
|
{ |
|
152
|
|
|
$privateKey = $this->getBinary(); |
|
153
|
|
|
$math = $this->ecAdapter->getMath(); |
|
154
|
|
|
$tweak = Buffer::int($math->toString($tweak), 32, $math)->getBinary(); |
|
155
|
|
|
$ret = \secp256k1_ec_privkey_tweak_mul( |
|
156
|
|
|
$this->ecAdapter->getContext(), |
|
157
|
|
|
$privateKey, |
|
158
|
|
|
$tweak |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
if ($ret !== 1) { |
|
162
|
|
|
throw new \RuntimeException('Secp256k1 privkey tweak mul: failed'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$secret = new Buffer($privateKey); |
|
166
|
|
|
|
|
167
|
|
|
return $this->ecAdapter->getPrivateKey($secret->getGmp(), $this->compressed); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param NetworkInterface $network |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public function toWif(NetworkInterface $network = null) |
|
175
|
|
|
{ |
|
176
|
|
|
$network = $network ?: Bitcoin::getNetwork(); |
|
177
|
|
|
$wifSerializer = new WifPrivateKeySerializer($this->ecAdapter->getMath(), new PrivateKeySerializer($this->ecAdapter)); |
|
178
|
|
|
return $wifSerializer->serialize($network, $this); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return BufferInterface |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getBuffer() |
|
185
|
|
|
{ |
|
186
|
|
|
return (new PrivateKeySerializer($this->ecAdapter))->serialize($this); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|