1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key\PublicKey; |
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; |
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface; |
11
|
|
|
use BitWasp\Buffertools\Buffer; |
12
|
|
|
use BitWasp\Buffertools\BufferInterface; |
13
|
|
|
use BitWasp\Buffertools\Parser; |
14
|
|
|
|
15
|
|
|
class PublicKeySerializer implements PublicKeySerializerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var EcAdapter |
19
|
|
|
*/ |
20
|
|
|
private $ecAdapter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param EcAdapter $ecAdapter |
24
|
|
|
*/ |
25
|
2543 |
|
public function __construct(EcAdapter $ecAdapter) |
26
|
|
|
{ |
27
|
2543 |
|
$this->ecAdapter = $ecAdapter; |
28
|
2543 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param PublicKey $publicKey |
32
|
|
|
* @return BufferInterface |
33
|
|
|
*/ |
34
|
140 |
|
private function doSerialize(PublicKey $publicKey) |
35
|
|
|
{ |
36
|
140 |
|
$serialized = ''; |
37
|
140 |
|
$isCompressed = $publicKey->isCompressed(); |
38
|
140 |
|
if (!secp256k1_ec_pubkey_serialize( |
39
|
140 |
|
$this->ecAdapter->getContext(), |
40
|
140 |
|
$serialized, |
41
|
140 |
|
$publicKey->getResource(), |
42
|
140 |
|
(int) $isCompressed |
|
|
|
|
43
|
|
|
)) { |
44
|
|
|
throw new \RuntimeException('Secp256k1: Failed to serialize public key'); |
45
|
|
|
} |
46
|
|
|
|
47
|
140 |
|
return new Buffer( |
48
|
140 |
|
$serialized, |
49
|
140 |
|
$isCompressed ? PublicKey::LENGTH_COMPRESSED : PublicKey::LENGTH_UNCOMPRESSED |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param PublicKeyInterface $publicKey |
55
|
|
|
* @return BufferInterface |
56
|
|
|
*/ |
57
|
140 |
|
public function serialize(PublicKeyInterface $publicKey): BufferInterface |
58
|
|
|
{ |
59
|
|
|
/** @var PublicKey $publicKey */ |
60
|
140 |
|
return $this->doSerialize($publicKey); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param BufferInterface $buffer |
65
|
|
|
* @return PublicKeyInterface |
66
|
|
|
*/ |
67
|
265 |
|
public function parse(BufferInterface $buffer): PublicKeyInterface |
68
|
|
|
{ |
69
|
265 |
|
$binary = $buffer->getBinary(); |
70
|
265 |
|
$pubkey_t = ''; |
71
|
|
|
/** @var resource $pubkey_t */ |
72
|
265 |
|
if (!secp256k1_ec_pubkey_parse($this->ecAdapter->getContext(), $pubkey_t, $binary)) { |
73
|
14 |
|
throw new \RuntimeException('Secp256k1 failed to parse public key'); |
74
|
|
|
} |
75
|
|
|
|
76
|
254 |
|
return new PublicKey( |
77
|
254 |
|
$this->ecAdapter, |
78
|
254 |
|
$pubkey_t, |
79
|
254 |
|
$buffer->getSize() === 33 |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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: