Completed
Pull Request — master (#359)
by Ruben de
14:27
created

Signature::getS()   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.125

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 4
cp 0.5
crap 1.125
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Signature\DerSignatureSerializer;
7
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.

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