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