Completed
Pull Request — master (#758)
by thomas
22:04
created

XOnlyPublicKeySerializer::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 6
rs 10
1
<?php declare(strict_types=1);
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\Impl\Secp256k1\Key\XOnlyPublicKey;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\XOnlyPubKeyInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\XOnlyPublicKeySerializerInterface;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class XOnlyPublicKeySerializer implements XOnlyPublicKeySerializerInterface
14
{
15
    /**
16
     * @var EcAdapter
17
     */
18
    private $ecAdapter;
19
20
    /**
21
     * @param EcAdapter $ecAdapter
22
     */
23 2447
    public function __construct(EcAdapter $ecAdapter)
24
    {
25 2447
        $this->ecAdapter = $ecAdapter;
26 2447
    }
27
28
    /**
29
     * @param PublicKey $publicKey
30
     * @return BufferInterface
31
     */
32
    private function doSerialize(XOnlyPublicKey $publicKey)
33
    {
34
        $serialized = '';
35
        if (!secp256k1_xonly_pubkey_serialize(
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_serialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        if (!/** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_serialize(
Loading history...
36
            $this->ecAdapter->getContext(),
37
            $serialized,
38
            $publicKey->getResource()
0 ignored issues
show
Bug introduced by
The method getResource() does not exist on BitWasp\Bitcoin\Crypto\E...56k1\Key\XOnlyPublicKey. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            $publicKey->/** @scrutinizer ignore-call */ 
39
                        getResource()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        )) {
40
            throw new \RuntimeException('Secp256k1: Failed to serialize xonly public key');
41
        }
42
43
        return new Buffer($serialized, 32);
44
    }
45
46
    /**
47
     * @param XOnlyPubKeyInterface $publicKey
48
     * @return BufferInterface
49
     */
50
    public function serialize(XOnlyPubKeyInterface $publicKey): BufferInterface
51
    {
52
        /** @var PublicKey $publicKey */
53
        return $this->doSerialize($publicKey);
0 ignored issues
show
Bug introduced by
$publicKey of type BitWasp\Bitcoin\Crypto\E...Secp256k1\Key\PublicKey is incompatible with the type BitWasp\Bitcoin\Crypto\E...56k1\Key\XOnlyPublicKey expected by parameter $publicKey of BitWasp\Bitcoin\Crypto\E...rializer::doSerialize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        return $this->doSerialize(/** @scrutinizer ignore-type */ $publicKey);
Loading history...
54
    }
55
56
    /**
57
     * @param BufferInterface $buffer
58
     * @return XOnlyPubKeyInterface
59
     */
60
    public function parse(BufferInterface $buffer): XOnlyPubKeyInterface
61
    {
62
        $binary = $buffer->getBinary();
63
        $xonlyPubkey = null;
64
        if (!secp256k1_xonly_pubkey_parse($this->ecAdapter->getContext(), $xonlyPubkey, $binary)) {
0 ignored issues
show
Bug introduced by
The function secp256k1_xonly_pubkey_parse was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        if (!/** @scrutinizer ignore-call */ secp256k1_xonly_pubkey_parse($this->ecAdapter->getContext(), $xonlyPubkey, $binary)) {
Loading history...
65
            throw new \RuntimeException('Secp256k1 failed to parse xonly public key');
66
        }
67
        /** @var resource $xonlyPubkey */
68
        return new XOnlyPublicKey(
69
            $this->ecAdapter->getContext(),
70
            $xonlyPubkey
71
        );
72
    }
73
}
74