Completed
Push — master ( eabe8c...61f277 )
by thomas
25:26
created

Signature::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 4
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 3.0175
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\DerSignatureSerializer;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BitWasp\Bitcoin\Crypto\E...ture\SignatureInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use BitWasp\Bitcoin\Serializable;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class Signature extends Serializable implements SignatureInterface
14
{
15
    /**
16
     * @var \GMP
17
     */
18
    private $r;
19
20
    /**
21
     * @var  \GMP
22
     */
23
    private $s;
24
25
    /**
26
     * @var EcAdapter
27
     */
28
    private $ecAdapter;
29
30
    /**
31
     * @var resource
32
     */
33
    private $secp256k1_sig;
34
35
    /**
36
     * @param EcAdapter $adapter
37
     * @param \GMP $r
38
     * @param \GMP $s
39
     * @param resource $secp256k1_ecdsa_signature_t
40
     */
41 168
    public function __construct(EcAdapter $adapter, \GMP $r, \GMP $s, $secp256k1_ecdsa_signature_t)
42
    {
43 168
        if (!is_resource($secp256k1_ecdsa_signature_t) ||
44 168
            !get_resource_type($secp256k1_ecdsa_signature_t) === SECP256K1_TYPE_SIG
45
        ) {
46
            throw new \InvalidArgumentException('Secp256k1\Signature\Signature expects ' . SECP256K1_TYPE_SIG . ' resource');
47
        }
48
49 168
        $this->secp256k1_sig = $secp256k1_ecdsa_signature_t;
50 168
        $this->ecAdapter = $adapter;
51 168
        $this->r = $r;
52 168
        $this->s = $s;
53 168
    }
54
55
    /**
56
     * @return \GMP
57
     */
58 3
    public function getR()
59
    {
60 3
        return $this->r;
61
    }
62
63
    /**
64
     * @return \GMP
65
     */
66 3
    public function getS()
67
    {
68 3
        return $this->s;
69
    }
70
71
    /**
72
     * @return resource
73
     */
74 152
    public function getResource()
75
    {
76 152
        return $this->secp256k1_sig;
77
    }
78
79
    /**
80
     * @param Signature $other
81
     * @return bool
82
     */
83 25
    private function doEquals(Signature $other): bool
84
    {
85 25
        $a = '';
86 25
        $b = '';
87 25
        secp256k1_ecdsa_signature_serialize_der($this->ecAdapter->getContext(), $a, $this->getResource());
88 25
        secp256k1_ecdsa_signature_serialize_der($this->ecAdapter->getContext(), $b, $other->getResource());
89
90 25
        return hash_equals($a, $b);
91
    }
92
93
    /**
94
     * @param SignatureInterface $signature
95
     * @return bool
96
     */
97 25
    public function equals(SignatureInterface $signature): bool
98
    {
99
        /** @var Signature $signature */
100 25
        return $this->doEquals($signature);
101
    }
102
103
    /**
104
     * @return BufferInterface
105
     */
106
    public function getBuffer(): BufferInterface
107
    {
108
        return (new DerSignatureSerializer($this->ecAdapter))->serialize($this);
109
    }
110
}
111