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

PublicKeySerializer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 67.5%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 27
cts 40
cp 0.675
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 5 1
A doSerialize() 0 19 3
A parse() 0 16 2
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