1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Networking\Messages; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Block\BlockInterface; |
8
|
|
|
use BitWasp\Bitcoin\Block\FilteredBlock; |
9
|
|
|
use BitWasp\Bitcoin\Bloom\BloomFilter; |
10
|
|
|
use BitWasp\Bitcoin\Chain\BlockLocator; |
11
|
|
|
use BitWasp\Bitcoin\Crypto\Random\Random; |
12
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
13
|
|
|
use BitWasp\Bitcoin\Networking\NetworkMessage; |
14
|
|
|
use BitWasp\Bitcoin\Networking\Structure\AlertDetail; |
15
|
|
|
use BitWasp\Bitcoin\Networking\Structure\Inventory; |
16
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress; |
17
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface; |
18
|
|
|
use BitWasp\Bitcoin\Networking\Serializer\NetworkMessageSerializer; |
19
|
|
|
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressTimestamp; |
20
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface; |
21
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInterface; |
22
|
|
|
use BitWasp\Buffertools\Buffer; |
23
|
|
|
use BitWasp\Buffertools\BufferInterface; |
24
|
|
|
use BitWasp\Buffertools\Parser; |
25
|
|
|
|
26
|
|
|
class Factory |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var NetworkInterface |
30
|
|
|
*/ |
31
|
|
|
private $network; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Random |
35
|
|
|
*/ |
36
|
|
|
private $random; |
37
|
|
|
|
38
|
|
|
/** |
39
|
126 |
|
* @var NetworkMessageSerializer |
40
|
|
|
*/ |
41
|
126 |
|
private $serializer; |
42
|
126 |
|
|
43
|
126 |
|
/** |
44
|
|
|
* @param NetworkInterface $network |
45
|
|
|
* @param Random $random |
46
|
|
|
*/ |
47
|
|
|
public function __construct(NetworkInterface $network, Random $random) |
48
|
|
|
{ |
49
|
|
|
$this->network = $network; |
50
|
|
|
$this->random = $random; |
51
|
|
|
$this->serializer = new NetworkMessageSerializer($this->network); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $version |
56
|
51 |
|
* @param int $services |
57
|
|
|
* @param int $timestamp |
58
|
|
|
* @param NetworkAddress $addrRecv |
59
|
|
|
* @param NetworkAddress $addrFrom |
60
|
|
|
* @param BufferInterface $userAgent |
61
|
|
|
* @param int $startHeight |
62
|
|
|
* @param bool $relay |
63
|
|
|
* @return Version |
64
|
|
|
*/ |
65
|
|
|
public function version( |
66
|
51 |
|
int $version, |
67
|
34 |
|
$services, |
68
|
34 |
|
$timestamp, |
69
|
34 |
|
NetworkAddress $addrRecv, |
70
|
34 |
|
NetworkAddress $addrFrom, |
71
|
34 |
|
BufferInterface $userAgent, |
72
|
51 |
|
$startHeight, |
73
|
34 |
|
$relay |
74
|
34 |
|
): Version { |
75
|
|
|
return new Version( |
76
|
34 |
|
$version, |
77
|
|
|
$services, |
78
|
|
|
$timestamp, |
79
|
|
|
$addrRecv, |
80
|
|
|
$addrFrom, |
81
|
|
|
(int) $this->random->bytes(8)->getInt(), |
82
|
15 |
|
$userAgent, |
83
|
|
|
$startHeight, |
84
|
15 |
|
$relay |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return VerAck |
90
|
3 |
|
*/ |
91
|
|
|
public function verack(): VerAck |
92
|
3 |
|
{ |
93
|
|
|
return new VerAck(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return SendHeaders |
98
|
|
|
*/ |
99
|
3 |
|
public function sendheaders(): SendHeaders |
100
|
|
|
{ |
101
|
3 |
|
return new SendHeaders(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param NetworkAddressTimestamp[] $addrs |
106
|
|
|
* @return Addr |
107
|
|
|
*/ |
108
|
3 |
|
public function addr(array $addrs): Addr |
109
|
|
|
{ |
110
|
3 |
|
return new Addr($addrs); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Inventory[] $vectors |
115
|
|
|
* @return Inv |
116
|
|
|
*/ |
117
|
3 |
|
public function inv(array $vectors): Inv |
118
|
|
|
{ |
119
|
3 |
|
return new Inv($vectors); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Inventory[] $vectors |
124
|
|
|
* @return GetData |
125
|
|
|
*/ |
126
|
3 |
|
public function getdata(array $vectors): GetData |
127
|
|
|
{ |
128
|
3 |
|
return new GetData($vectors); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Inventory[] $vectors |
133
|
|
|
* @return NotFound |
134
|
|
|
*/ |
135
|
|
|
public function notfound(array $vectors): NotFound |
136
|
3 |
|
{ |
137
|
|
|
return new NotFound($vectors); |
138
|
3 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param int $version |
142
|
|
|
* @param BlockLocator $blockLocator |
143
|
|
|
* @return GetBlocks |
144
|
|
|
*/ |
145
|
|
|
public function getblocks(int $version, BlockLocator $blockLocator): GetBlocks |
146
|
3 |
|
{ |
147
|
|
|
return new GetBlocks($version, $blockLocator); |
148
|
3 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param int $version |
152
|
|
|
* @param BlockLocator $blockLocator |
153
|
|
|
* @return GetHeaders |
154
|
|
|
*/ |
155
|
3 |
|
public function getheaders(int $version, BlockLocator $blockLocator): GetHeaders |
156
|
|
|
{ |
157
|
3 |
|
return new GetHeaders($version, $blockLocator); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param TransactionInterface $tx |
162
|
|
|
* @return Tx |
163
|
|
|
*/ |
164
|
3 |
|
public function tx(TransactionInterface $tx): Tx |
165
|
|
|
{ |
166
|
3 |
|
return new Tx($tx); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param BlockInterface $block |
171
|
|
|
* @return Block |
172
|
|
|
*/ |
173
|
3 |
|
public function block(BlockInterface $block): Block |
174
|
|
|
{ |
175
|
3 |
|
return new Block($block); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param \BitWasp\Bitcoin\Block\BlockHeaderInterface[] $headers |
180
|
|
|
* @return Headers |
181
|
3 |
|
*/ |
182
|
|
|
public function headers(array $headers): Headers |
183
|
3 |
|
{ |
184
|
|
|
return new Headers($headers); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return GetAddr |
189
|
3 |
|
*/ |
190
|
|
|
public function getaddr(): GetAddr |
191
|
3 |
|
{ |
192
|
|
|
return new GetAddr(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return MemPool |
197
|
|
|
*/ |
198
|
3 |
|
public function mempool(): MemPool |
199
|
|
|
{ |
200
|
3 |
|
return new MemPool(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param int $feeRate |
205
|
|
|
* @return FeeFilter |
206
|
|
|
*/ |
207
|
3 |
|
public function feefilter($feeRate): FeeFilter |
208
|
|
|
{ |
209
|
3 |
|
return new FeeFilter($feeRate); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param BufferInterface $data |
214
|
|
|
* @return FilterAdd |
215
|
|
|
*/ |
216
|
3 |
|
public function filteradd(BufferInterface $data): FilterAdd |
217
|
|
|
{ |
218
|
3 |
|
return new FilterAdd($data); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param BloomFilter $filter |
223
|
|
|
* @return FilterLoad |
224
|
3 |
|
*/ |
225
|
|
|
public function filterload(BloomFilter $filter): FilterLoad |
226
|
3 |
|
{ |
227
|
|
|
return new FilterLoad($filter); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return FilterClear |
232
|
|
|
*/ |
233
|
3 |
|
public function filterclear(): FilterClear |
234
|
|
|
{ |
235
|
3 |
|
return new FilterClear(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param FilteredBlock $filtered |
240
|
|
|
* @return MerkleBlock |
241
|
6 |
|
*/ |
242
|
|
|
public function merkleblock(FilteredBlock $filtered): MerkleBlock |
243
|
6 |
|
{ |
244
|
|
|
return new MerkleBlock($filtered); |
245
|
|
|
} |
246
|
|
|
/** |
247
|
|
|
* @return Ping |
248
|
|
|
* @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
249
|
|
|
*/ |
250
|
3 |
|
public function ping(): Ping |
251
|
|
|
{ |
252
|
3 |
|
return Ping::generate($this->random); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param Ping $ping |
257
|
|
|
* @return Pong |
258
|
|
|
*/ |
259
|
|
|
public function pong(Ping $ping): Pong |
260
|
|
|
{ |
261
|
|
|
return new Pong($ping->getNonce()); |
262
|
9 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param BufferInterface $message |
266
|
|
|
* @param int $code |
267
|
|
|
* @param BufferInterface $reason |
268
|
9 |
|
* @param BufferInterface|null $data |
269
|
3 |
|
* @return Reject |
270
|
2 |
|
*/ |
271
|
|
|
public function reject( |
272
|
9 |
|
BufferInterface $message, |
273
|
6 |
|
$code, |
274
|
6 |
|
BufferInterface $reason, |
275
|
6 |
|
BufferInterface $data = null |
276
|
|
|
): Reject { |
277
|
6 |
|
if (null === $data) { |
278
|
|
|
$data = new Buffer(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return new Reject( |
282
|
|
|
$message, |
283
|
|
|
$code, |
284
|
|
|
$reason, |
285
|
3 |
|
$data |
286
|
|
|
); |
287
|
3 |
|
} |
288
|
2 |
|
|
289
|
|
|
/** |
290
|
2 |
|
* @param AlertDetail $detail |
291
|
|
|
* @param SignatureInterface $sig |
292
|
|
|
* @return Alert |
293
|
|
|
*/ |
294
|
|
|
public function alert(AlertDetail $detail, SignatureInterface $sig): Alert |
295
|
|
|
{ |
296
|
|
|
return new Alert( |
297
|
36 |
|
$detail, |
298
|
|
|
$sig |
299
|
36 |
|
); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param Parser $parser |
304
|
|
|
* @return NetworkMessage |
305
|
12 |
|
*/ |
306
|
|
|
public function parse(Parser $parser): NetworkMessage |
307
|
12 |
|
{ |
308
|
|
|
return $this->serializer->fromParser($parser); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return NetworkMessageSerializer |
313
|
|
|
*/ |
314
|
|
|
public function getSerializer(): NetworkMessageSerializer |
315
|
|
|
{ |
316
|
|
|
return $this->serializer; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return NetworkInterface |
321
|
|
|
*/ |
322
|
|
|
public function getNetwork(): NetworkInterface |
323
|
|
|
{ |
324
|
|
|
return $this->network; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|