1 | <?php |
||
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 | public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t, $recid, $compressed) |
||
39 | { |
||
40 | $math = $ecAdapter->getMath(); |
||
41 | if (!is_bool($compressed)) { |
||
42 | throw new \InvalidArgumentException('CompactSignature: compressed must be a boolean'); |
||
43 | } |
||
44 | |||
45 | if (!is_resource($secp256k1_ecdsa_signature_t) |
||
46 | || SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t) |
||
47 | ) { |
||
48 | throw new \RuntimeException('CompactSignature: must pass recoverable signature resource'); |
||
49 | } |
||
50 | |||
51 | $ser = ''; |
||
52 | $recidout = ''; |
||
53 | secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $secp256k1_ecdsa_signature_t, $ser, $recidout); |
||
54 | list ($r, $s) = array_map( |
||
55 | function ($val) use ($math) { |
||
56 | return (new Buffer($val))->getGmp(); |
||
57 | }, |
||
58 | str_split($ser, 32) |
||
59 | ); |
||
60 | |||
61 | $this->resource = $secp256k1_ecdsa_signature_t; |
||
62 | $this->recid = $recid; |
||
63 | $this->compressed = $compressed; |
||
64 | $this->ecAdapter = $ecAdapter; |
||
65 | parent::__construct($ecAdapter, $r, $s, $secp256k1_ecdsa_signature_t); |
||
66 | } |
||
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 | public function getResource() |
||
83 | { |
||
84 | return $this->resource; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return int |
||
89 | */ |
||
90 | public function getRecoveryId() |
||
91 | { |
||
92 | return $this->recid; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return int|string |
||
97 | */ |
||
98 | public function getFlags() |
||
102 | |||
103 | /** |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function isCompressed() |
||
110 | |||
111 | /** |
||
112 | * @return BufferInterface |
||
113 | */ |
||
114 | public function getBuffer() |
||
118 | } |
||
119 |