1 | <?php |
||
14 | class CompactSignatureSerializer implements CompactSignatureSerializerInterface |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var EcAdapter |
||
19 | */ |
||
20 | private $ecAdapter; |
||
21 | |||
22 | /** |
||
23 | * @param EcAdapter $adapter |
||
24 | */ |
||
25 | 10 | public function __construct(EcAdapter $adapter) |
|
29 | |||
30 | /** |
||
31 | * @return \BitWasp\Buffertools\Template |
||
32 | */ |
||
33 | 7 | private function getTemplate() |
|
41 | |||
42 | /** |
||
43 | * @param CompactSignature $signature |
||
44 | * @return BufferInterface |
||
45 | */ |
||
46 | 5 | private function doSerialize(CompactSignature $signature) |
|
47 | { |
||
48 | 5 | return $this->getTemplate()->write([ |
|
49 | 5 | $signature->getFlags(), |
|
50 | 5 | gmp_strval($signature->getR(), 10), |
|
51 | 5 | gmp_strval($signature->getS(), 10) |
|
52 | ]); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param CompactSignatureInterface $signature |
||
57 | * @return BufferInterface |
||
58 | */ |
||
59 | 5 | public function serialize(CompactSignatureInterface $signature) |
|
64 | |||
65 | /** |
||
66 | * @param Parser $parser |
||
67 | * @return CompactSignature |
||
68 | * @throws ParserOutOfRange |
||
69 | */ |
||
70 | 7 | public function fromParser(Parser $parser) |
|
71 | { |
||
72 | 7 | $math = $this->ecAdapter->getMath(); |
|
73 | |||
74 | try { |
||
75 | 7 | list ($byte, $r, $s) = $this->getTemplate()->parse($parser); |
|
76 | |||
77 | 6 | $recoveryFlags = $byte - 27; |
|
78 | 6 | if ($recoveryFlags < 0 || $recoveryFlags > 7) { |
|
79 | throw new \InvalidArgumentException('invalid signature type'); |
||
80 | } |
||
81 | |||
82 | 6 | $isCompressed = $math->cmp($math->bitwiseAnd(gmp_init($recoveryFlags), gmp_init(4)), gmp_init(0)) !== 0; |
|
83 | 6 | $recoveryId = $recoveryFlags - ($isCompressed ? 4 : 0); |
|
84 | 1 | } catch (ParserOutOfRange $e) { |
|
85 | 1 | throw new ParserOutOfRange('Failed to extract full signature from parser'); |
|
86 | } |
||
87 | |||
88 | 6 | return new CompactSignature($this->ecAdapter, gmp_init($r, 10), gmp_init($s, 10), $recoveryId, $isCompressed); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param $string |
||
93 | * @return CompactSignature |
||
94 | * @throws ParserOutOfRange |
||
95 | */ |
||
96 | 7 | public function parse($string) |
|
100 | } |
||
101 |