-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTCPeerNodeUnit.pas
473 lines (369 loc) · 11 KB
/
BTCPeerNodeUnit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
unit BTCPeerNodeUnit;
interface
uses
System.SysUtils,
System.Classes,
System.Generics.Collections,
IPeerNodeUnit,
PeerNodeUnit,
NodeObserverPattern,
ipwcore,
ipwtypes,
ipwipport,
btctypes,
SeSHA256, RPC;
type
TMessageEventVersion = procedure(Sender: TObject;
versionMessage: TVersionMessage) of object;
TMessageEventVerack = procedure(Sender: TObject) of object;
TMessageEvent = procedure(Sender: TObject; const aMessage: string) of Object;
TBTCPeerNode = class(TPeerNode, Inode, IBTCPeerNode)
strict private
fipwIPPort1: TipwIPPort;
fMessage: TMessageEvent;
private
fMessageVersionEvent: TMessageEventVersion;
fMessageVerackEvent: TMessageEventVerack;
procedure Connected(Sender: TObject; StatusCode: Integer;
const Description: String);
procedure DataIn(Sender: TObject; Text: String; TextB: TBytes;
EOL: boolean);
procedure ConnectionStatus(Sender: TObject; const ConnectionEvent: string;
StatusCode: Integer; const Description: string);
procedure ReadyToSend(Sender: TObject);
procedure Error(Sender: TObject; ErrorCode: Integer;
const Description: string);
procedure DoMessageDetected(const aMessageType: string;
const apayload: TBytes);
procedure VerackMessage(Sender: TObject);
procedure SendCommand(const messagename: ansistring);
procedure SendMessage(const messagename: ansistring; const payload: TBytes);
public
constructor Create(OWner: TComponent); override;
destructor Destroy; override;
procedure Connect; virtual;
procedure Disconnect;
procedure GetPeers(); override;
published
// property UserAgent: string read fUserAgent;
property OnMessage: TMessageEvent read fMessage write fMessage;
property OnVersionMessage: TMessageEventVersion read fMessageVersionEvent
write fMessageVersionEvent;
property OnVerackMessage: TMessageEventVerack read fMessageVerackEvent
write fMessageVerackEvent;
// property PeeerDiscovery : TBTCPeerDiscovery read fBTCPeerDiscovery write fBTCPeerDiscovery;
end;
TBTCRPCNode = class(TBTCPeerNode)
private
function GetRPCPassword: string;
function GetRPCUser: string;
procedure SetRPCPassword(const Value: string);
procedure SetRPCUser(const Value: string);
public
// TODO hide to private ->
fRPC: TBCN;
// TODO refactor to properties
rpcport: Integer;
constructor Create(OWner: TComponent); override;
destructor Destroy; override;
procedure GetPeers(); override;
procedure Connect; override;
property rpcuser: string read GetRPCUser write SetRPCUser;
property rpcpassword: string read GetRPCPassword write SetRPCPassword;
end;
procedure Register;
implementation
uses
Winapi.Windows,
dateutils,
dialogs;
function StringBytesToTBytes(const s: string): TBytes;
var
k: Integer;
begin
SetLength(result, length(s) div 2);
for k := 0 to (s.length div 2) - 1 do
result[k] := StrToInt('$' + s[(k * 2) + 1] + s[(k + 1) * 2]);
end;
procedure Register;
begin
RegisterComponents('CryptoCurrency', [TBTCPeerNode]);
end;
procedure TBTCPeerNode.Connect;
begin
if not fipwIPPort1.Connected then
fipwIPPort1.Connect(PeerIp, 8333);
end;
procedure TBTCPeerNode.Connected(Sender: TObject; StatusCode: Integer;
const Description: String);
begin
fServerConnected := true;
ImplNodeObsevable.Notify(msgtserverconnected, self)
end;
procedure TBTCPeerNode.ConnectionStatus(Sender: TObject;
const ConnectionEvent: string; StatusCode: Integer;
const Description: string);
begin
end;
constructor TBTCPeerNode.Create(OWner: TComponent);
begin
inherited;
fipwIPPort1 := TipwIPPort.Create(self);
fipwIPPort1.OnConnected := Connected;
// conectado al nodo pero no al protocolo
fipwIPPort1.OnDataIn := DataIn;
fipwIPPort1.OnConnectionStatus := ConnectionStatus;
fipwIPPort1.OnReadyToSend := ReadyToSend;
fipwIPPort1.OnError := Error;
OnVerackMessage := VerackMessage;
// aBTCThreadMonitor := TBTCThreadMonitor.Create(fIp,self);
end;
procedure TBTCPeerNode.DataIn(Sender: TObject; Text: String; TextB: TBytes;
EOL: boolean);
var
k, j: Integer;
status: Integer;
aMessage: string;
alength: cardinal;
// checksum: cardinal;
payload: TBytes;
begin
k := 0;
status := 0;
alength := 0;
while k < length(TextB) do
begin
case status of
// magic code detection
0:
if TextB[k] = $F9 then
inc(status);
1:
if TextB[k] = $BE then
inc(status);
2:
if TextB[k] = $B4 then
inc(status);
3:
if TextB[k] = $D9 then
inc(status);
// command detection
4:
begin
aMessage := '';
j := 0;
while (j < 12) and (TextB[j + k] <> 0) do
begin
aMessage := aMessage + char(TextB[k + j]);
inc(j);
end;
// showmessage(amessage + ' ' + inttostr(amessage.length));
k := k + 11;
inc(status);
end;
// length detection
5:
begin
alength := TextB[k] + TextB[k + 1] + TextB[k + 2] + TextB[k + 3];
SetLength(payload, alength);
/// ojo, solo el primer byte... añadir los siguientes
// showmessage(inttostr(alength));
k := k + 3;
inc(status);
end;
// checksum detection
6:
begin
/// Please get checksum (hay que calcularlo)
// checksum := TextB[k] + TextB[k + 1] + TextB[k + 2] + TextB[k + 3];
k := k + 4;
if (alength = 0) then
begin
CopyMemory(payload, @TextB[k], alength);
DoMessageDetected(aMessage, payload);
status := 0;
end
else
inc(status)
end;
7:
begin
CopyMemory(payload, @TextB[k - 1], alength);
DoMessageDetected(aMessage, payload);
status := 0;
end;
end;
inc(k);
end;
end;
destructor TBTCPeerNode.Destroy;
begin
if fipwIPPort1.Connected then
fipwIPPort1.Disconnect;
fipwIPPort1.free;
inherited;
end;
procedure TBTCPeerNode.Disconnect;
begin
fipwIPPort1.Disconnect;
end;
procedure TBTCPeerNode.DoMessageDetected(const aMessageType: string;
const apayload: TBytes);
var
versionMessage: TVersionRawMessage;
pversionMessage: ^TVersionRawMessage;
aVersionMessage: TVersionMessage;
k: Integer;
begin
if aMessageType = MESSAGE_TYPE_VERSION then
begin
// Create the version message
pversionMessage := @versionMessage;
CopyMemory(pversionMessage, apayload, length(apayload));
aVersionMessage.protocol_version := versionMessage.protocol_version;
aVersionMessage.node_services := UInt64(versionMessage.node_services);
aVersionMessage.node_timestamp := TTimeZone.Local.ToLocalTime
(unixtodatetime(UInt64(versionMessage.node_timestamp)));
aVersionMessage.receiving_node_ip := '....';
aVersionMessage.receiving_node_port :=
swap(versionMessage.receiving_node_port);
aVersionMessage.emmiting_node_ip := '..-..';
aVersionMessage.emmiting_node_port :=
swap(versionMessage.emmiting_node_port);
// get User Agent
aVersionMessage.user_agent := '';
for k := 1 to versionMessage.user_agent[0] do
aVersionMessage.user_agent := aVersionMessage.user_agent +
char(versionMessage.user_agent[k]);
fUserAgent := aVersionMessage.user_agent;
if assigned(fMessageVersionEvent) then
fMessageVersionEvent(self, aVersionMessage);
ImplNodeObsevable.Notify(msgtrefresh, self);
end
else if aMessageType = MESSAGE_TYPE_VERACK then
begin
if assigned(fMessageVerackEvent) then
fMessageVerackEvent(self);
ImplNodeObsevable.Notify(msgtprotocolconnected, self);
SendCommand('verack');
end
else if assigned(fMessage) then
fMessage(self, aMessageType);
end;
procedure TBTCPeerNode.Error(Sender: TObject; ErrorCode: Integer;
const Description: string);
begin
end;
procedure TBTCPeerNode.GetPeers;
begin
SendCommand('getaddr');
// tb := StringBytesToTBytes('f9beb4d9 676574616464720000000000 00000000 5df6e0e2');
end;
procedure TBTCPeerNode.ReadyToSend(Sender: TObject);
begin
SendMessage('version', StringBytesToTBytes( //
'80110100' + // version
// '0804000000000000' + // services
'0004000000000000' + // services
'0defbc6100000000' + //
'090400000000000000000000000000000000ffff23c121bf208d' + // addr_recv
'0804000000000000000000000000000000000000000000000000' + // addr_from
'fdba00e1964f2006' + // nonce
'10' + // 16 long
'2f5361746f7368693a32322e302e302f' + // s
'25c20300' + // start_height
'01' // relay
));
end;
procedure TBTCPeerNode.SendCommand(const messagename: ansistring);
var
tb: TBytes;
aHeader: TBTCHeader;
begin
aHeader.start_string := BTC_MAIN_MAGIC_VALUE;
aHeader.command_name := StringToCommandName(messagename);
aHeader.payload_size := 0;
aHeader.checksum[1] := char($5D);
aHeader.checksum[2] := char($F6);
aHeader.checksum[3] := char($E0);
aHeader.checksum[4] := char($E2);
SetLength(tb, BTC_HEADER_SIZE);
CopyMemory(tb, @aHeader, BTC_HEADER_SIZE);
fipwIPPort1.send(tb);
end;
procedure TBTCPeerNode.SendMessage(const messagename: ansistring;
const payload: TBytes);
var
tb: TBytes;
aHeader: TBTCHeader;
ams: TMemoryStream;
res: ansistring;
k: Integer;
begin
aHeader.start_string := BTC_MAIN_MAGIC_VALUE;
aHeader.command_name := StringToCommandName(messagename);
aHeader.apayload := payload;
ams := TMemoryStream.Create;
try
// Guardamos el texto en un stream
ams.Position := 0;
ams.WriteBuffer(PAnsiChar(aHeader.apayload)^, length(aHeader.apayload));
ams.Position := 0;
// Calculamos el hash del stream
res := SHA256ToBinaryStr(CalcSHA256(SHA256ToBinaryStr(CalcSHA256(ams))));
aHeader.payload_size := length(aHeader.apayload);
aHeader.checksum[1] := res[1];
aHeader.checksum[2] := res[2];
aHeader.checksum[3] := res[3];
aHeader.checksum[4] := res[4];
finally
ams.free;
end;
SetLength(tb, BTC_HEADER_SIZE + aHeader.payload_size);
CopyMemory(tb, @aHeader, BTC_HEADER_SIZE);
for k := 0 to aHeader.payload_size - 1 do
tb[BTC_HEADER_SIZE + k] := aHeader.apayload[k];
fipwIPPort1.send(tb);
end;
procedure TBTCPeerNode.VerackMessage(Sender: TObject);
begin
fConnected := true;
end;
{ TBTCRPCNode }
procedure TBTCRPCNode.Connect;
begin
fUserAgent := fRPC.GetNetworkInfo.subversion;
fServerConnected := true;
ImplNodeObsevable.Notify(msgtserverconnected, self);
end;
constructor TBTCRPCNode.Create(OWner: TComponent);
begin
inherited;
fRPC := TBCN.Create(self);
end;
destructor TBTCRPCNode.Destroy;
begin
inherited;
end;
procedure TBTCRPCNode.GetPeers;
begin
//inherited;
// self.fRPC.
end;
function TBTCRPCNode.GetRPCPassword: string;
begin
result := fRPC.rpcpassword;
end;
function TBTCRPCNode.GetRPCUser: string;
begin
result := fRPC.rpcuser;
end;
procedure TBTCRPCNode.SetRPCPassword(const Value: string);
begin
fRPC.rpcpassword := Value;
end;
procedure TBTCRPCNode.SetRPCUser(const Value: string);
begin
fRPC.rpcuser := Value;
end;
end.