1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Networking\Serializer\Message; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Networking\Messages\Version; |
6
|
|
|
use BitWasp\Bitcoin\Networking\Serializer\Structure\NetworkAddressSerializer; |
7
|
|
|
use BitWasp\Bitcoin\Serializer\Types; |
8
|
|
|
use BitWasp\Buffertools\Buffer; |
9
|
|
|
use BitWasp\Buffertools\BufferInterface; |
10
|
|
|
use BitWasp\Buffertools\Parser; |
11
|
|
|
use BitWasp\Buffertools\TemplateFactory; |
12
|
|
|
|
13
|
|
|
class VersionSerializer |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var NetworkAddressSerializer |
17
|
|
|
*/ |
18
|
|
|
private $netAddr; |
19
|
|
|
private $uint32le; |
20
|
96 |
|
private $uint64le; |
21
|
|
|
private $bs26le; |
22
|
96 |
|
private $varstring; |
23
|
96 |
|
private $uint8le; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param NetworkAddressSerializer $netAddr |
27
|
|
|
*/ |
28
|
21 |
|
public function __construct(NetworkAddressSerializer $netAddr) |
29
|
|
|
{ |
30
|
21 |
|
$this->netAddr = $netAddr; |
31
|
21 |
|
$this->uint32le = Types::uint32le(); |
32
|
21 |
|
$this->uint64le = Types::uint64le(); |
33
|
21 |
|
$this->bs26le = Types::bytestringle(26); |
34
|
21 |
|
$this->varstring = Types::varstring(); |
35
|
21 |
|
$this->uint8le = Types::uint8le(); |
36
|
21 |
|
} |
37
|
21 |
|
|
38
|
21 |
|
/** |
39
|
21 |
|
* @param Parser $parser |
40
|
21 |
|
* @return Version |
41
|
|
|
*/ |
42
|
|
|
public function fromParser(Parser $parser) |
43
|
|
|
{ |
44
|
|
|
list ($version, $services, $timestamp, |
45
|
|
|
$addrRecv, $addrFrom, $nonce, |
46
|
|
|
$userAgent, $startHeight, $relay) = [ |
47
|
15 |
|
$this->uint32le->read($parser), |
48
|
|
|
$this->uint64le->read($parser), |
49
|
15 |
|
$this->uint64le->read($parser), |
50
|
|
|
$this->netAddr->fromParser($parser), |
51
|
15 |
|
$this->netAddr->fromParser($parser), |
52
|
10 |
|
$this->uint64le->read($parser), |
53
|
10 |
|
$this->varstring->read($parser), |
54
|
10 |
|
$this->uint32le->read($parser), |
55
|
15 |
|
$this->uint8le->read($parser), |
56
|
15 |
|
]; |
57
|
10 |
|
|
58
|
10 |
|
return new Version( |
59
|
10 |
|
(int) $version, |
60
|
5 |
|
(int) $services, |
61
|
10 |
|
(int) $timestamp, |
62
|
|
|
$addrRecv, |
63
|
|
|
$addrFrom, |
64
|
|
|
(int) $nonce, |
65
|
|
|
$userAgent, |
66
|
|
|
(int) $startHeight, |
67
|
|
|
(bool)$relay |
68
|
15 |
|
); |
69
|
|
|
} |
70
|
15 |
|
|
71
|
|
|
/** |
72
|
|
|
* @param BufferInterface $string |
73
|
|
|
* @return Version |
74
|
|
|
*/ |
75
|
|
|
public function parse(BufferInterface $string): Version |
76
|
|
|
{ |
77
|
21 |
|
return $this->fromParser(new Parser($string)); |
78
|
|
|
} |
79
|
21 |
|
|
80
|
21 |
|
/** |
81
|
21 |
|
* @param Version $version |
82
|
21 |
|
* @return \BitWasp\Buffertools\Buffer |
83
|
21 |
|
*/ |
84
|
21 |
|
public function serialize(Version $version) |
85
|
21 |
|
{ |
86
|
21 |
|
return new Buffer( |
87
|
21 |
|
sprintf( |
88
|
21 |
|
"%s%s%s%s%s%s%s%s%s", |
89
|
14 |
|
$this->uint32le->write($version->getVersion()), |
90
|
|
|
$this->uint64le->write($version->getServices()), |
91
|
|
|
$this->uint64le->write($version->getTimestamp()), |
92
|
|
|
$this->netAddr->serialize($version->getRecipientAddress())->getBinary(), |
93
|
|
|
$this->netAddr->serialize($version->getSenderAddress())->getBinary(), |
94
|
|
|
$this->uint64le->write($version->getNonce()), |
95
|
|
|
$this->varstring->write($version->getUserAgent()), |
96
|
|
|
$this->uint32le->write($version->getStartHeight()), |
97
|
|
|
$this->uint8le->write((int) $version->getRelay()) |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|