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; |
|
|
|
|
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
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: