Completed
Pull Request — master (#591)
by thomas
16: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 96.43%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 27
cts 28
cp 0.9643
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 doSerialize() 0 19 3
A serialize() 0 5 1
A parse() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Key;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key\PublicKey;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
11
use BitWasp\Buffertools\Buffer;
12
use BitWasp\Buffertools\BufferInterface;
13
use BitWasp\Buffertools\Parser;
14
15
class PublicKeySerializer implements PublicKeySerializerInterface
16
{
17
    /**
18
     * @var EcAdapter
19
     */
20
    private $ecAdapter;
21
22
    /**
23
     * @param EcAdapter $ecAdapter
24
     */
25 2544
    public function __construct(EcAdapter $ecAdapter)
26
    {
27 2544
        $this->ecAdapter = $ecAdapter;
28 2544
    }
29
30
    /**
31
     * @param PublicKey $publicKey
32
     * @return BufferInterface
33
     */
34 140
    private function doSerialize(PublicKey $publicKey)
35
    {
36 140
        $serialized = '';
37 140
        $isCompressed = $publicKey->isCompressed();
38 140
        if (!secp256k1_ec_pubkey_serialize(
39 140
            $this->ecAdapter->getContext(),
40 140
            $serialized,
41 140
            $publicKey->getResource(),
42 140
            (int) $isCompressed
0 ignored issues
show
Documentation introduced by
(int) $isCompressed is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
        )) {
44
            throw new \RuntimeException('Secp256k1: Failed to serialize public key');
45
        }
46
47 140
        return new Buffer(
48 140
            $serialized,
49 140
            $isCompressed ? PublicKey::LENGTH_COMPRESSED : PublicKey::LENGTH_UNCOMPRESSED,
50 140
            $this->ecAdapter->getMath()
51
        );
52
    }
53
54
    /**
55
     * @param PublicKeyInterface $publicKey
56
     * @return BufferInterface
57
     */
58 140
    public function serialize(PublicKeyInterface $publicKey): BufferInterface
59
    {
60
        /** @var PublicKey $publicKey */
61 140
        return $this->doSerialize($publicKey);
62
    }
63
64
    /**
65
     * @param \BitWasp\Buffertools\BufferInterface|string $data
66
     * @return PublicKeyInterface
67
     */
68 266
    public function parse($data): PublicKeyInterface
69
    {
70 266
        $buffer = (new Parser($data))->getBuffer();
71 266
        $binary = $buffer->getBinary();
72 266
        $pubkey_t = '';
73
        /** @var resource $pubkey_t */
74 266
        if (!secp256k1_ec_pubkey_parse($this->ecAdapter->getContext(), $pubkey_t, $binary)) {
75 15
            throw new \RuntimeException('Secp256k1 failed to parse public key');
76
        }
77
78 255
        return new PublicKey(
79 255
            $this->ecAdapter,
80 255
            $pubkey_t,
81 255
            $buffer->getSize() === 33
82
        );
83
    }
84
}
85