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