|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Serializer\Signature; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
|
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter; |
|
9
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature; |
|
10
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface; |
|
11
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface; |
|
12
|
|
|
use BitWasp\Buffertools\Buffer; |
|
13
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
14
|
|
|
use BitWasp\Buffertools\Parser; |
|
15
|
|
|
use BitWasp\Buffertools\Template; |
|
16
|
|
|
use BitWasp\Buffertools\TemplateFactory; |
|
17
|
|
|
|
|
18
|
|
|
class DerSignatureSerializer implements DerSignatureSerializerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EcAdapter |
|
22
|
|
|
*/ |
|
23
|
|
|
private $ecAdapter; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param EcAdapter $ecAdapter |
|
27
|
|
|
*/ |
|
28
|
2450 |
|
public function __construct(EcAdapter $ecAdapter) |
|
29
|
|
|
{ |
|
30
|
2450 |
|
$this->ecAdapter = $ecAdapter; |
|
31
|
2450 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return EcAdapterInterface |
|
35
|
|
|
*/ |
|
36
|
244 |
|
public function getEcAdapter() |
|
37
|
|
|
{ |
|
38
|
244 |
|
return $this->ecAdapter; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Signature $signature |
|
43
|
|
|
* @return BufferInterface |
|
44
|
|
|
*/ |
|
45
|
65 |
|
private function doSerialize(Signature $signature): BufferInterface |
|
46
|
|
|
{ |
|
47
|
65 |
|
$signatureOut = ''; |
|
48
|
65 |
|
if (!secp256k1_ecdsa_signature_serialize_der($this->ecAdapter->getContext(), $signatureOut, $signature->getResource())) { |
|
49
|
|
|
throw new \RuntimeException('Secp256k1: serialize der failure'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
65 |
|
return new Buffer($signatureOut); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param SignatureInterface $signature |
|
57
|
|
|
* @return BufferInterface |
|
58
|
|
|
*/ |
|
59
|
65 |
|
public function serialize(SignatureInterface $signature): BufferInterface |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var Signature $signature */ |
|
62
|
65 |
|
return $this->doSerialize($signature); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return Template |
|
67
|
|
|
*/ |
|
68
|
193 |
|
private function getInnerTemplate() |
|
69
|
|
|
{ |
|
70
|
193 |
|
return (new TemplateFactory()) |
|
71
|
193 |
|
->uint8() |
|
72
|
193 |
|
->varstring() |
|
73
|
193 |
|
->uint8() |
|
74
|
193 |
|
->varstring() |
|
75
|
193 |
|
->getTemplate(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Template |
|
80
|
|
|
*/ |
|
81
|
193 |
|
private function getOuterTemplate() |
|
82
|
|
|
{ |
|
83
|
193 |
|
return (new TemplateFactory()) |
|
84
|
193 |
|
->uint8() |
|
85
|
193 |
|
->varstring() |
|
86
|
193 |
|
->getTemplate(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param BufferInterface $derSignature |
|
91
|
|
|
* @return SignatureInterface |
|
92
|
|
|
*/ |
|
93
|
208 |
|
public function parse(BufferInterface $derSignature): SignatureInterface |
|
94
|
|
|
{ |
|
95
|
208 |
|
$derSignature = (new Parser($derSignature))->getBuffer(); |
|
96
|
208 |
|
$binary = $derSignature->getBinary(); |
|
97
|
|
|
|
|
98
|
208 |
|
$sig_t = ''; |
|
99
|
|
|
/** @var resource $sig_t */ |
|
100
|
208 |
|
if (!ecdsa_signature_parse_der_lax($this->ecAdapter->getContext(), $sig_t, $binary)) { |
|
101
|
15 |
|
throw new \RuntimeException('Secp256k1: parse der failure'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Unfortunately, we need to use the Parser here to get r and s :/ |
|
105
|
193 |
|
list (, $inner) = $this->getOuterTemplate()->parse(new Parser($derSignature)); |
|
106
|
193 |
|
list (, $r, , $s) = $this->getInnerTemplate()->parse(new Parser($inner)); |
|
107
|
|
|
/** @var Buffer $r */ |
|
108
|
|
|
/** @var Buffer $s */ |
|
109
|
|
|
|
|
110
|
193 |
|
return new Signature($this->ecAdapter, $r->getGmp(), $s->getGmp(), $sig_t); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|