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; |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|