1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Networking\Messages; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Networking\Message; |
6
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface; |
7
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressTimestamp; |
8
|
|
|
use BitWasp\Bitcoin\Networking\Serializer\Message\VersionSerializer; |
9
|
|
|
use BitWasp\Bitcoin\Networking\Serializer\Structure\NetworkAddressSerializer; |
10
|
|
|
use BitWasp\Buffertools\Buffer; |
11
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Random; |
12
|
|
|
use BitWasp\Bitcoin\Networking\NetworkSerializable; |
13
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress; |
14
|
|
|
use BitWasp\Buffertools\BufferInterface; |
15
|
|
|
|
16
|
|
|
class Version extends NetworkSerializable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int|string |
20
|
|
|
*/ |
21
|
|
|
private $version; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $services; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int|string |
30
|
|
|
*/ |
31
|
|
|
private $timestamp; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var NetworkAddressInterface |
35
|
|
|
*/ |
36
|
|
|
private $addrRecv; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var NetworkAddressInterface |
40
|
|
|
*/ |
41
|
|
|
private $addrFrom; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var BufferInterface |
45
|
|
|
*/ |
46
|
|
|
private $userAgent; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int|string |
50
|
|
|
*/ |
51
|
|
|
private $startHeight; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
private $relay; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var integer|string |
60
|
|
|
*/ |
61
|
|
|
private $nonce; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $version |
65
|
|
|
* @param int $services |
66
|
|
|
* @param int $timestamp |
67
|
|
|
* @param NetworkAddressInterface $addrRecv |
68
|
|
|
* @param NetworkAddressInterface $addrFrom |
69
|
|
|
* @param int $nonce |
70
|
|
|
* @param BufferInterface $userAgent |
71
|
|
|
* @param int $startHeight |
72
|
|
|
* @param bool $relay |
73
|
|
|
*/ |
74
|
63 |
|
public function __construct( |
75
|
|
|
$version, |
76
|
|
|
$services, |
77
|
|
|
$timestamp, |
78
|
|
|
NetworkAddressInterface $addrRecv, |
79
|
|
|
NetworkAddressInterface $addrFrom, |
80
|
|
|
$nonce, |
81
|
|
|
BufferInterface $userAgent, |
82
|
|
|
$startHeight, |
83
|
|
|
$relay |
84
|
|
|
) { |
85
|
|
|
|
86
|
63 |
|
if ($addrRecv instanceof NetworkAddressTimestamp) { |
87
|
|
|
$addrRecv = $addrRecv->withoutTimestamp(); |
88
|
|
|
} |
89
|
63 |
|
if ($addrFrom instanceof NetworkAddressTimestamp) { |
90
|
|
|
$addrFrom = $addrFrom->withoutTimestamp(); |
91
|
|
|
} |
92
|
|
|
|
93
|
63 |
|
$random = new Random(); |
94
|
63 |
|
$this->nonce = $random->bytes(8)->getInt(); |
95
|
63 |
|
$this->version = $version; |
96
|
63 |
|
$this->services = $services; |
97
|
63 |
|
$this->timestamp = $timestamp; |
98
|
63 |
|
$this->addrRecv = $addrRecv; |
99
|
63 |
|
$this->nonce = $nonce; |
100
|
63 |
|
$this->addrFrom = $addrFrom; |
101
|
63 |
|
$this->userAgent = $userAgent; |
102
|
63 |
|
$this->startHeight = $startHeight; |
103
|
63 |
|
if (! is_bool($relay)) { |
104
|
3 |
|
throw new \InvalidArgumentException('Relay must be a boolean'); |
105
|
|
|
} |
106
|
60 |
|
$this->relay = $relay; |
107
|
60 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
* @see \BitWasp\Bitcoin\Network\NetworkSerializableInterface::getNetworkCommand() |
112
|
|
|
*/ |
113
|
24 |
|
public function getNetworkCommand() |
114
|
|
|
{ |
115
|
24 |
|
return Message::VERSION; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return Buffer|int|string |
120
|
|
|
*/ |
121
|
24 |
|
public function getNonce() |
122
|
|
|
{ |
123
|
24 |
|
return $this->nonce; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int|string |
128
|
|
|
*/ |
129
|
27 |
|
public function getVersion() |
130
|
|
|
{ |
131
|
27 |
|
return $this->version; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
24 |
|
public function getServices() |
138
|
|
|
{ |
139
|
24 |
|
return $this->services; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return int|string |
144
|
|
|
*/ |
145
|
27 |
|
public function getTimestamp() |
146
|
|
|
{ |
147
|
27 |
|
return $this->timestamp; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return NetworkAddress |
152
|
|
|
*/ |
153
|
30 |
|
public function getRecipientAddress() |
154
|
|
|
{ |
155
|
30 |
|
return $this->addrRecv; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return NetworkAddress |
160
|
|
|
*/ |
161
|
39 |
|
public function getSenderAddress() |
162
|
|
|
{ |
163
|
39 |
|
return $this->addrFrom; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return BufferInterface |
168
|
|
|
*/ |
169
|
27 |
|
public function getUserAgent() |
170
|
|
|
{ |
171
|
27 |
|
return $this->userAgent; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return int|string |
176
|
|
|
*/ |
177
|
30 |
|
public function getStartHeight() |
178
|
|
|
{ |
179
|
30 |
|
return $this->startHeight; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
27 |
|
public function getRelay() |
186
|
|
|
{ |
187
|
27 |
|
return $this->relay; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return BufferInterface |
192
|
|
|
*/ |
193
|
21 |
|
public function getBuffer() |
194
|
|
|
{ |
195
|
21 |
|
return (new VersionSerializer(new NetworkAddressSerializer()))->serialize($this); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|