1 | <?php |
||
12 | class PublicKeySerializer implements PublicKeySerializerInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var EcAdapter |
||
16 | */ |
||
17 | private $ecAdapter; |
||
18 | |||
19 | /** |
||
20 | * @param EcAdapter $ecAdapter |
||
21 | */ |
||
22 | 393 | public function __construct(EcAdapter $ecAdapter) |
|
26 | |||
27 | /** |
||
28 | * @param PublicKey $publicKey |
||
29 | * @return Buffer |
||
30 | */ |
||
31 | 180 | private function doSerialize(PublicKey $publicKey) |
|
32 | { |
||
33 | 180 | $serialized = ''; |
|
34 | 180 | $isCompressed = $publicKey->isCompressed(); |
|
35 | 180 | if (!secp256k1_ec_pubkey_serialize( |
|
36 | 180 | $this->ecAdapter->getContext(), |
|
37 | 180 | $publicKey->getResource(), |
|
38 | 180 | $isCompressed, |
|
39 | $serialized |
||
40 | 180 | )) { |
|
41 | throw new \RuntimeException('Secp256k1: Failed to serialize public key'); |
||
42 | } |
||
43 | |||
44 | 180 | return new Buffer( |
|
45 | 180 | $serialized, |
|
46 | 180 | $isCompressed ? PublicKey::LENGTH_COMPRESSED : PublicKey::LENGTH_UNCOMPRESSED, |
|
47 | 180 | $this->ecAdapter->getMath() |
|
48 | 180 | ); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param PublicKeyInterface $publicKey |
||
53 | * @return Buffer |
||
54 | */ |
||
55 | 180 | public function serialize(PublicKeyInterface $publicKey) |
|
60 | |||
61 | /** |
||
62 | * @param \BitWasp\Buffertools\Buffer|string $data |
||
63 | * @return PublicKey |
||
64 | */ |
||
65 | 237 | public function parse($data) |
|
81 | } |
||
82 |