|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter; |
|
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key\PrivateKeySerializer; |
|
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\CompactSignature; |
|
11
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature; |
|
12
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\Key; |
|
13
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; |
|
14
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
|
15
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\CompactSignatureInterface; |
|
16
|
|
|
use BitWasp\Bitcoin\Crypto\Random\RbgInterface; |
|
17
|
|
|
use BitWasp\Bitcoin\Exceptions\InvalidPrivateKey; |
|
18
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
|
19
|
|
|
use BitWasp\Bitcoin\Serializer\Key\PrivateKey\WifPrivateKeySerializer; |
|
20
|
|
|
use BitWasp\Buffertools\Buffer; |
|
21
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
22
|
|
|
|
|
23
|
|
|
class PrivateKey extends Key implements PrivateKeyInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var \GMP |
|
27
|
|
|
*/ |
|
28
|
|
|
private $secret; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $secretBin; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $compressed; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var PublicKey |
|
42
|
|
|
*/ |
|
43
|
|
|
private $publicKey; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var EcAdapter |
|
47
|
|
|
*/ |
|
48
|
|
|
private $ecAdapter; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param EcAdapter $adapter |
|
52
|
|
|
* @param \GMP $secret |
|
53
|
|
|
* @param bool|false $compressed |
|
54
|
|
|
* @throws \Exception |
|
55
|
|
|
*/ |
|
56
|
76 |
|
public function __construct(EcAdapter $adapter, \GMP $secret, bool $compressed = false) |
|
57
|
|
|
{ |
|
58
|
76 |
|
$buffer = Buffer::int(gmp_strval($secret, 10), 32); |
|
59
|
76 |
|
if (!$adapter->validatePrivateKey($buffer)) { |
|
60
|
1 |
|
throw new InvalidPrivateKey('Invalid private key'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
75 |
|
if (false === is_bool($compressed)) { |
|
64
|
|
|
throw new \InvalidArgumentException('PrivateKey: Compressed argument must be a boolean'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
75 |
|
$this->ecAdapter = $adapter; |
|
68
|
75 |
|
$this->secret = $secret; |
|
69
|
75 |
|
$this->secretBin = $buffer->getBinary(); |
|
70
|
75 |
|
$this->compressed = $compressed; |
|
71
|
75 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param BufferInterface $msg32 |
|
75
|
|
|
* @param RbgInterface|null $rbgInterface |
|
76
|
|
|
* @return Signature |
|
77
|
|
|
*/ |
|
78
|
66 |
|
public function sign(BufferInterface $msg32, RbgInterface $rbgInterface = null): Signature |
|
79
|
|
|
{ |
|
80
|
66 |
|
$context = $this->ecAdapter->getContext(); |
|
81
|
|
|
|
|
82
|
|
|
/** @var resource $sig_t */ |
|
83
|
66 |
|
$sig_t = ''; |
|
84
|
66 |
|
if (1 !== secp256k1_ecdsa_sign($context, $sig_t, $msg32->getBinary(), $this->secretBin)) { |
|
85
|
|
|
throw new \RuntimeException('Secp256k1: failed to sign'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
66 |
|
$derSig = ''; |
|
89
|
66 |
|
secp256k1_ecdsa_signature_serialize_der($context, $derSig, $sig_t); |
|
90
|
|
|
|
|
91
|
66 |
|
$rL = ord($derSig[3]); |
|
92
|
66 |
|
$r = (new Buffer(substr($derSig, 4, $rL), $rL))->getGmp(); |
|
93
|
|
|
|
|
94
|
66 |
|
$sL = ord($derSig[4+$rL + 1]); |
|
95
|
66 |
|
$s = (new Buffer(substr($derSig, 4 + $rL + 2, $sL), $sL))->getGmp(); |
|
96
|
|
|
|
|
97
|
66 |
|
return new Signature($this->ecAdapter, $r, $s, $sig_t); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param BufferInterface $msg32 |
|
102
|
|
|
* @param RbgInterface|null $rbfInterface |
|
103
|
|
|
* @return CompactSignature |
|
104
|
|
|
*/ |
|
105
|
5 |
|
public function signCompact(BufferInterface $msg32, RbgInterface $rbfInterface = null): CompactSignatureInterface |
|
106
|
|
|
{ |
|
107
|
5 |
|
$context = $this->ecAdapter->getContext(); |
|
108
|
|
|
|
|
109
|
5 |
|
$sig_t = ''; |
|
110
|
5 |
|
if (1 !== secp256k1_ecdsa_sign_recoverable($context, $sig_t, $msg32->getBinary(), $this->secretBin)) { |
|
|
|
|
|
|
111
|
|
|
throw new \RuntimeException('Secp256k1: failed to sign'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
5 |
|
$recid = ''; |
|
115
|
5 |
|
$ser = ''; |
|
116
|
5 |
|
if (!secp256k1_ecdsa_recoverable_signature_serialize_compact($context, $sig_t, $ser, $recid)) { |
|
|
|
|
|
|
117
|
|
|
throw new \RuntimeException('Failed to obtain recid'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** @var resource $sig_t */ |
|
121
|
|
|
/** @var int $recid */ |
|
122
|
|
|
|
|
123
|
5 |
|
unset($ser); |
|
124
|
5 |
|
return new CompactSignature( |
|
125
|
5 |
|
$this->ecAdapter, |
|
126
|
5 |
|
$sig_t, |
|
|
|
|
|
|
127
|
5 |
|
$recid, |
|
128
|
5 |
|
$this->isCompressed() |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return bool |
|
134
|
|
|
*/ |
|
135
|
61 |
|
public function isCompressed(): bool |
|
136
|
|
|
{ |
|
137
|
61 |
|
return $this->compressed; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return \GMP |
|
142
|
|
|
*/ |
|
143
|
52 |
|
public function getSecret() |
|
144
|
|
|
{ |
|
145
|
52 |
|
return $this->secret; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
90 |
|
public function getSecretBinary(): string |
|
152
|
|
|
{ |
|
153
|
90 |
|
return $this->secretBin; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return PublicKey |
|
158
|
|
|
*/ |
|
159
|
114 |
|
public function getPublicKey() |
|
160
|
|
|
{ |
|
161
|
114 |
|
if (null === $this->publicKey) { |
|
162
|
84 |
|
$context = $this->ecAdapter->getContext(); |
|
163
|
84 |
|
$publicKey_t = ''; |
|
164
|
|
|
/** @var resource $publicKey_t */ |
|
165
|
84 |
|
if (1 !== secp256k1_ec_pubkey_create($context, $publicKey_t, $this->getBinary())) { |
|
166
|
|
|
throw new \RuntimeException('Failed to create public key'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
84 |
|
$this->publicKey = new PublicKey($this->ecAdapter, $publicKey_t, $this->compressed); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
114 |
|
return $this->publicKey; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param \GMP $tweak |
|
177
|
|
|
* @return KeyInterface |
|
178
|
|
|
*/ |
|
179
|
8 |
|
public function tweakAdd(\GMP $tweak): KeyInterface |
|
180
|
|
|
{ |
|
181
|
8 |
|
$adapter = $this->ecAdapter; |
|
182
|
8 |
|
$math = $adapter->getMath(); |
|
183
|
8 |
|
$context = $adapter->getContext(); |
|
184
|
|
|
|
|
185
|
8 |
|
$privateKey = $this->getBinary(); // mod by reference |
|
186
|
8 |
|
echo " privateKey = " . unpack("H*", $privateKey)[1] . PHP_EOL; |
|
187
|
|
|
|
|
188
|
8 |
|
$tweak = Buffer::int($math->toString($tweak), 32)->getBinary(); |
|
189
|
8 |
|
echo " tweak = " . unpack("H*", $tweak)[1] . PHP_EOL; |
|
190
|
|
|
|
|
191
|
8 |
|
$ret = \secp256k1_ec_privkey_tweak_add( |
|
192
|
8 |
|
$context, |
|
193
|
|
|
$privateKey, |
|
194
|
8 |
|
$tweak |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
8 |
|
if ($ret !== 1) { |
|
198
|
|
|
throw new \RuntimeException('Secp256k1 privkey tweak add: failed'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
8 |
|
echo " privateKey.tweaked = " . unpack("H*", $privateKey)[1] . PHP_EOL; |
|
202
|
8 |
|
echo " [originValue = " . unpack("H*", $this->getBinary())[1] . "]" . PHP_EOL; |
|
203
|
8 |
|
$secret = new Buffer($privateKey, 32); |
|
204
|
8 |
|
return $adapter->getPrivateKey($secret->getGmp(), $this->compressed); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param \GMP $tweak |
|
209
|
|
|
* @return KeyInterface |
|
210
|
|
|
*/ |
|
211
|
1 |
|
public function tweakMul(\GMP $tweak): KeyInterface |
|
212
|
|
|
{ |
|
213
|
1 |
|
$privateKey = $this->getBinary(); |
|
214
|
1 |
|
$math = $this->ecAdapter->getMath(); |
|
215
|
1 |
|
$tweak = Buffer::int($math->toString($tweak), 32)->getBinary(); |
|
216
|
1 |
|
$ret = \secp256k1_ec_privkey_tweak_mul( |
|
217
|
1 |
|
$this->ecAdapter->getContext(), |
|
218
|
|
|
$privateKey, |
|
219
|
1 |
|
$tweak |
|
220
|
|
|
); |
|
221
|
|
|
|
|
222
|
1 |
|
if ($ret !== 1) { |
|
223
|
|
|
throw new \RuntimeException('Secp256k1 privkey tweak mul: failed'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
1 |
|
$secret = new Buffer($privateKey); |
|
227
|
|
|
|
|
228
|
1 |
|
return $this->ecAdapter->getPrivateKey($secret->getGmp(), $this->compressed); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param NetworkInterface $network |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
3 |
|
public function toWif(NetworkInterface $network = null): string |
|
236
|
|
|
{ |
|
237
|
3 |
|
$network = $network ?: Bitcoin::getNetwork(); |
|
238
|
3 |
|
$wifSerializer = new WifPrivateKeySerializer($this->ecAdapter, new PrivateKeySerializer($this->ecAdapter)); |
|
239
|
3 |
|
return $wifSerializer->serialize($network, $this); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @return BufferInterface |
|
244
|
|
|
*/ |
|
245
|
88 |
|
public function getBuffer(): BufferInterface |
|
246
|
|
|
{ |
|
247
|
88 |
|
return (new PrivateKeySerializer($this->ecAdapter))->serialize($this); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: