Completed
Pull Request — master (#745)
by thomas
27:01 queued 05:32
created

CompactSignature   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 84.38%

Importance

Changes 0
Metric Value
dl 0
loc 104
ccs 27
cts 32
cp 0.8438
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 24 3
A convert() 0 7 1
A getResource() 0 4 1
A getRecoveryId() 0 4 1
A getFlags() 0 4 2
A isCompressed() 0 4 1
A getBuffer() 0 4 1
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
        if (!is_resource($secp256k1_ecdsa_signature_t)
43 7
            || SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t)
44
        ) {
45
            throw new \RuntimeException('CompactSignature: must pass recoverable signature resource');
46
        }
47
48 7
        $ser = '';
49 7
        $recidout = 0;
50 7
        secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $ser, $recidout, $secp256k1_ecdsa_signature_t);
51 7
        list ($r, $s) = array_map(
52 7
            function ($val) {
53 7
                return (new Buffer($val))->getGmp();
54 7
            },
55 7
            str_split($ser, 32)
56
        );
57
58 7
        $this->resource = $secp256k1_ecdsa_signature_t;
59 7
        $this->recid = $recid;
60 7
        $this->compressed = $compressed;
61 7
        $this->ecAdapter = $ecAdapter;
62 7
        parent::__construct($ecAdapter, $r, $s, $secp256k1_ecdsa_signature_t);
63 7
    }
64
65
    /**
66
     * @return Signature
67
     */
68
    public function convert(): Signature
69
    {
70
        $sig_t = '';
71
        /** @var resource $sig_t */
72
        secp256k1_ecdsa_recoverable_signature_convert($this->ecAdapter->getContext(), $sig_t, $this->resource);
73
        return new Signature($this->ecAdapter, $this->getR(), $this->getS(), $sig_t);
0 ignored issues
show
Bug introduced by
It seems like $sig_t defined by '' on line 70 can also be of type null; however, BitWasp\Bitcoin\Crypto\E...ignature::__construct() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
    }
75
76
    /**
77
     * @return resource
78
     */
79 6
    public function getResource()
80
    {
81 6
        return $this->resource;
82
    }
83
84
    /**
85
     * @return int
86
     */
87 5
    public function getRecoveryId(): int
88
    {
89 5
        return $this->recid;
90
    }
91
92
    /**
93
     * @return int
94
     */
95 5
    public function getFlags(): int
96
    {
97 5
        return $this->getRecoveryId() + 27 + ($this->isCompressed() ? 4 : 0);
98
    }
99
100
    /**
101
     * @return bool
102
     */
103 6
    public function isCompressed(): bool
104
    {
105 6
        return $this->compressed;
106
    }
107
108
    /**
109
     * @return BufferInterface
110
     */
111 5
    public function getBuffer(): BufferInterface
112
    {
113 5
        return (new CompactSignatureSerializer($this->ecAdapter))->serialize($this);
114
    }
115
}
116