Completed
Pull Request — master (#591)
by thomas
22:54 queued 09:12
created

CompactSignature::getRecoveryId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Signature\CompactSignatureSerializer;
9
use BitWasp\Buffertools\Buffer;
10
use BitWasp\Buffertools\BufferInterface;
11
12
class CompactSignature extends Signature implements CompactSignatureInterface
13
{
14
    /**
15
     * @var resource
16
     */
17
    private $resource;
18
19
    /**
20
     * @var bool
21
     */
22
    private $compressed;
23
24
    /**
25
     * @var int
26
     */
27
    private $recid;
28
29
    /**
30
     * @var EcAdapter
31
     */
32
    private $ecAdapter;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
33
34
    /**
35
     * @param EcAdapter $ecAdapter
36
     * @param resource $secp256k1_ecdsa_signature_t
37
     * @param int $recid
38
     * @param bool $compressed
39
     */
40 7
    public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t, int $recid, bool $compressed)
41
    {
42 7
        $math = $ecAdapter->getMath();
43 7
        if (!is_bool($compressed)) {
44
            throw new \InvalidArgumentException('CompactSignature: compressed must be a boolean');
45
        }
46
47 7
        if (!is_resource($secp256k1_ecdsa_signature_t)
48 7
            || SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t)
49
        ) {
50
            throw new \RuntimeException('CompactSignature: must pass recoverable signature resource');
51
        }
52
53 7
        $ser = '';
54 7
        $recidout = '';
55 7
        secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $secp256k1_ecdsa_signature_t, $ser, $recidout);
56 7
        list ($r, $s) = array_map(
57 7
            function ($val) use ($math) {
58 7
                return (new Buffer($val))->getGmp();
59 7
            },
60 7
            str_split($ser, 32)
61
        );
62
63 7
        $this->resource = $secp256k1_ecdsa_signature_t;
64 7
        $this->recid = $recid;
65 7
        $this->compressed = $compressed;
66 7
        $this->ecAdapter = $ecAdapter;
67 7
        parent::__construct($ecAdapter, $r, $s, $secp256k1_ecdsa_signature_t);
68 7
    }
69
70
    /**
71
     * @return Signature
72
     */
73
    public function convert(): Signature
74
    {
75
        $sig_t = '';
76
        /** @var resource $sig_t */
77
        secp256k1_ecdsa_recoverable_signature_convert($this->ecAdapter->getContext(), $sig_t, $this->resource);
78
        return new Signature($this->ecAdapter, $this->getR(), $this->getS(), $sig_t);
79
    }
80
81
    /**
82
     * @return resource
83
     */
84 6
    public function getResource()
85
    {
86 6
        return $this->resource;
87
    }
88
89
    /**
90
     * @return int
91
     */
92 5
    public function getRecoveryId(): int
93
    {
94 5
        return $this->recid;
95
    }
96
97
    /**
98
     * @return int
99
     */
100 5
    public function getFlags(): int
101
    {
102 5
        return $this->getRecoveryId() + 27 + ($this->isCompressed() ? 4 : 0);
103
    }
104
105
    /**
106
     * @return bool
107
     */
108 6
    public function isCompressed(): bool
109
    {
110 6
        return $this->compressed;
111
    }
112
113
    /**
114
     * @return BufferInterface
115
     */
116 5
    public function getBuffer(): BufferInterface
117
    {
118 5
        return (new CompactSignatureSerializer($this->ecAdapter))->serialize($this);
119
    }
120
}
121