Completed
Push — master ( e12674...2b8220 )
by thomas
28:40
created

PublicKeySerializer::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0932

Importance

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