1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking\Serializer\Message; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer; |
9
|
|
|
use BitWasp\Bitcoin\Networking\Messages\Alert; |
10
|
|
|
use BitWasp\Bitcoin\Networking\Serializer\Structure\AlertDetailSerializer; |
11
|
|
|
use BitWasp\Bitcoin\Serializer\Types; |
12
|
|
|
use BitWasp\Buffertools\Buffer; |
13
|
|
|
use BitWasp\Buffertools\BufferInterface; |
14
|
|
|
use BitWasp\Buffertools\Parser; |
15
|
|
|
|
16
|
|
|
class AlertSerializer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var AlertDetailSerializer |
20
|
|
|
*/ |
21
|
|
|
private $detail; |
22
|
|
|
|
23
|
96 |
|
/** |
24
|
|
|
* @var \BitWasp\Buffertools\Types\VarString |
25
|
96 |
|
*/ |
26
|
96 |
|
private $varstring; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param AlertDetailSerializer $detail |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct(AlertDetailSerializer $detail) |
32
|
|
|
{ |
33
|
3 |
|
$this->detail = $detail; |
34
|
3 |
|
$this->varstring = Types::varstring(); |
35
|
3 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Parser $parser |
39
|
|
|
* @return Alert |
40
|
|
|
*/ |
41
|
|
|
public function fromParser(Parser $parser): Alert |
42
|
3 |
|
{ |
43
|
|
|
$detailBuffer = $this->varstring->read($parser); |
44
|
3 |
|
$detail = $this->detail->fromParser(new Parser($detailBuffer)); |
45
|
|
|
|
46
|
3 |
|
$sigBuffer = $this->varstring->read($parser); |
47
|
3 |
|
$adapter = Bitcoin::getEcAdapter(); |
48
|
3 |
|
$serializer = EcSerializer::getSerializer('BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface', true, $adapter); |
49
|
3 |
|
$sig = $serializer->parse($sigBuffer); |
50
|
|
|
|
51
|
3 |
|
return new Alert($detail, $sig); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
2 |
|
/** |
55
|
|
|
* @param BufferInterface $data |
56
|
|
|
* @return Alert |
57
|
|
|
*/ |
58
|
|
|
public function parse(BufferInterface $data): Alert |
59
|
|
|
{ |
60
|
|
|
return $this->fromParser(new Parser($data)); |
61
|
3 |
|
} |
62
|
|
|
|
63
|
3 |
|
/** |
64
|
|
|
* @param Alert $alert |
65
|
|
|
* @return BufferInterface |
66
|
|
|
*/ |
67
|
|
|
public function serialize(Alert $alert): BufferInterface |
68
|
|
|
{ |
69
|
|
|
return new Buffer("{$this->varstring->write($alert->getDetail()->getBuffer())}{$this->varstring->write($alert->getSignature()->getBuffer())}"); |
70
|
3 |
|
} |
71
|
|
|
} |
72
|
|
|
|