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\CompactSignatureSerializer; |
7
|
|
|
use BitWasp\Buffertools\Buffer; |
8
|
|
|
use BitWasp\Buffertools\BufferInterface; |
9
|
|
|
|
10
|
|
|
class CompactSignature extends Signature implements CompactSignatureInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var resource |
14
|
|
|
*/ |
15
|
|
|
private $resource; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
private $compressed; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $recid; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EcAdapter |
29
|
|
|
*/ |
30
|
|
|
private $ecAdapter; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param EcAdapter $ecAdapter |
34
|
|
|
* @param resource $secp256k1_ecdsa_signature_t |
35
|
|
|
* @param int $recid |
36
|
|
|
* @param bool $compressed |
37
|
|
|
*/ |
38
|
32 |
|
public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t, $recid, $compressed) |
39
|
|
|
{ |
40
|
32 |
|
$math = $ecAdapter->getMath(); |
41
|
32 |
|
if (!is_bool($compressed)) { |
42
|
|
|
throw new \InvalidArgumentException('CompactSignature: compressed must be a boolean'); |
43
|
|
|
} |
44
|
|
|
|
45
|
32 |
|
if (!is_resource($secp256k1_ecdsa_signature_t) |
46
|
32 |
|
|| SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t) |
47
|
16 |
|
) { |
48
|
|
|
throw new \RuntimeException('CompactSignature: must pass recoverable signature resource'); |
49
|
|
|
} |
50
|
|
|
|
51
|
32 |
|
$ser = ''; |
52
|
32 |
|
$recidout = ''; |
53
|
32 |
|
secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $secp256k1_ecdsa_signature_t, $ser, $recidout); |
54
|
32 |
|
list ($r, $s) = array_map( |
55
|
32 |
|
function ($val) use ($math) { |
56
|
32 |
|
return (new Buffer($val))->getGmp(); |
57
|
32 |
|
}, |
58
|
32 |
|
str_split($ser, 32) |
59
|
16 |
|
); |
60
|
|
|
|
61
|
32 |
|
$this->resource = $secp256k1_ecdsa_signature_t; |
62
|
32 |
|
$this->recid = $recid; |
63
|
32 |
|
$this->compressed = $compressed; |
64
|
32 |
|
$this->ecAdapter = $ecAdapter; |
65
|
32 |
|
parent::__construct($ecAdapter, $r, $s, $secp256k1_ecdsa_signature_t); |
66
|
32 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Signature |
70
|
|
|
*/ |
71
|
|
|
public function convert() |
72
|
|
|
{ |
73
|
|
|
$sig_t = ''; |
74
|
|
|
/** @var resource $sig_t */ |
75
|
|
|
secp256k1_ecdsa_recoverable_signature_convert($this->ecAdapter->getContext(), $sig_t, $this->resource); |
76
|
|
|
return new Signature($this->ecAdapter, $this->getR(), $this->getS(), $sig_t); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return resource |
81
|
|
|
*/ |
82
|
28 |
|
public function getResource() |
83
|
|
|
{ |
84
|
28 |
|
return $this->resource; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
24 |
|
public function getRecoveryId() |
91
|
|
|
{ |
92
|
24 |
|
return $this->recid; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return int|string |
97
|
|
|
*/ |
98
|
24 |
|
public function getFlags() |
99
|
|
|
{ |
100
|
24 |
|
return $this->getRecoveryId() + 27 + ($this->isCompressed() ? 4 : 0); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
28 |
|
public function isCompressed() |
107
|
|
|
{ |
108
|
28 |
|
return $this->compressed; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return BufferInterface |
113
|
|
|
*/ |
114
|
24 |
|
public function getBuffer() |
115
|
|
|
{ |
116
|
24 |
|
return (new CompactSignatureSerializer($this->ecAdapter))->serialize($this); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|