1 | <?php |
||
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 | $math = $ecAdapter->getMath(); |
|
43 | 7 | if (!is_bool($compressed)) { |
|
44 | throw new \InvalidArgumentException('CompactSignature: compressed must be a boolean'); |
||
45 | } |
||
46 | |||
47 | 7 | if (!is_resource($secp256k1_ecdsa_signature_t) |
|
48 | 7 | || SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t) |
|
49 | ) { |
||
50 | throw new \RuntimeException('CompactSignature: must pass recoverable signature resource'); |
||
51 | } |
||
52 | |||
53 | 7 | $ser = ''; |
|
54 | 7 | $recidout = ''; |
|
55 | 7 | secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $secp256k1_ecdsa_signature_t, $ser, $recidout); |
|
56 | 7 | list ($r, $s) = array_map( |
|
57 | 7 | function ($val) use ($math) { |
|
58 | 7 | return (new Buffer($val))->getGmp(); |
|
59 | 7 | }, |
|
60 | 7 | str_split($ser, 32) |
|
61 | ); |
||
62 | |||
63 | 7 | $this->resource = $secp256k1_ecdsa_signature_t; |
|
64 | 7 | $this->recid = $recid; |
|
65 | 7 | $this->compressed = $compressed; |
|
66 | 7 | $this->ecAdapter = $ecAdapter; |
|
67 | 7 | parent::__construct($ecAdapter, $r, $s, $secp256k1_ecdsa_signature_t); |
|
68 | 7 | } |
|
69 | |||
70 | /** |
||
71 | * @return Signature |
||
72 | */ |
||
73 | public function convert(): Signature |
||
80 | |||
81 | /** |
||
82 | * @return resource |
||
83 | */ |
||
84 | 6 | public function getResource() |
|
88 | |||
89 | /** |
||
90 | * @return int |
||
91 | */ |
||
92 | 5 | public function getRecoveryId(): int |
|
96 | |||
97 | /** |
||
98 | * @return int |
||
99 | */ |
||
100 | 5 | public function getFlags(): int |
|
104 | |||
105 | /** |
||
106 | * @return bool |
||
107 | */ |
||
108 | 6 | public function isCompressed(): bool |
|
112 | |||
113 | /** |
||
114 | * @return BufferInterface |
||
115 | */ |
||
116 | 5 | public function getBuffer(): BufferInterface |
|
120 | } |
||
121 |