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