Completed
Pull Request — master (#371)
by Ruben de
21:14
created

PublicKeySerializer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 72.5%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 5 1
A parse() 0 16 2
A doSerialize() 0 19 3
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\Serializer\Key\PublicKeySerializerInterface;
8
use BitWasp\Buffertools\Buffer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
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 456
    public function __construct(EcAdapter $ecAdapter)
24
    {
25 456
        $this->ecAdapter = $ecAdapter;
26 456
    }
27
28
    /**
29
     * @param PublicKey $publicKey
30
     * @return BufferInterface
31
     */
32 376
    private function doSerialize(PublicKey $publicKey)
33
    {
34 376
        $serialized = '';
35 376
        $isCompressed = $publicKey->isCompressed();
36 376
        if (!secp256k1_ec_pubkey_serialize(
37 376
            $this->ecAdapter->getContext(),
38 188
            $serialized,
39 376
            $publicKey->getResource(),
40
            $isCompressed
41 188
        )) {
42
            throw new \RuntimeException('Secp256k1: Failed to serialize public key');
43
        }
44
45 376
        return new Buffer(
46 188
            $serialized,
47 376
            $isCompressed ? PublicKey::LENGTH_COMPRESSED : PublicKey::LENGTH_UNCOMPRESSED,
48 376
            $this->ecAdapter->getMath()
49 188
        );
50
    }
51
52
    /**
53
     * @param PublicKeyInterface $publicKey
54
     * @return BufferInterface
55
     */
56 376
    public function serialize(PublicKeyInterface $publicKey)
57
    {
58
        /** @var PublicKey $publicKey */
59 376
        return $this->doSerialize($publicKey);
60
    }
61
62
    /**
63
     * @param \BitWasp\Buffertools\BufferInterface|string $data
64
     * @return PublicKey
65
     */
66 324
    public function parse($data)
67
    {
68 324
        $buffer = (new Parser($data))->getBuffer();
69 324
        $binary = $buffer->getBinary();
70 324
        $pubkey_t = '';
71
        /** @var resource $pubkey_t */
72 324
        if (!secp256k1_ec_pubkey_parse($this->ecAdapter->getContext(), $pubkey_t, $binary)) {
73 16
            throw new \RuntimeException('Secp256k1 failed to parse public key');
74
        }
75
76 312
        return new PublicKey(
77 312
            $this->ecAdapter,
78 156
            $pubkey_t,
79 312
            $buffer->getSize() === 33
80 156
        );
81
    }
82
}
83