-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbip0122processor.pas
270 lines (248 loc) · 6.98 KB
/
bip0122processor.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
unit Bip0122Processor;
{$ifdef fpc}{$mode delphi}{$endif}{$H+}
interface
uses
Classes, SysUtils,
fpjson, jsonparser, jsonscanner, httpprotocol,
Generics.Collections,
Bip0122Uriparser;
const
BitcoinMainNet = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f';
type
TBlockExplorer = record
Name: String;
TxUrl: String;
BlockUrl: String;
HeightUrl: String;
AddressUrl: String;
constructor Create(const name: String; const tx: String; const block: String; const height: String; const address: String);
end;
TBlockExplorers = TDictionary<String, TBlockExplorer>;
TChain = class
public
constructor Create(const name: String; const hash: String; const explorers: TBlockExplorers);
destructor Destroy; override;
private
FName: String;
FHash: String;
FExplorers: TBlockExplorers;
end;
TChains = class(TObjectDictionary<String, TChain>)
public
constructor Create; override;
end;
TBIP0122Processor = class
public
constructor Create(const ExplorerJsonFilePaths: TStringArray);
destructor Destroy; override;
function GetUrlForBipUri(ExplorerName: String; Uri: TBIP0122URI): String;
function GetExplorerNames: TStringArray;
private
FChains: TChains;
procedure ParseJSONFiles(Filenames: TStringArray);
procedure ParseJSONData(J: TJSONData);
end;
implementation
constructor TBlockExplorer.Create(const name: String; const tx: String; const block: String; const height: String; const address: String);
begin
self.Name := name;
self.TxUrl := tx;
self.BlockUrl := block;
self.HeightUrl := height;
self.AddressUrl := address;
end;
constructor TChain.Create(const name: String; const hash: String; const explorers: TBlockExplorers);
begin
self.FName := name;
self.FHash := hash;
self.FExplorers := explorers;
end;
destructor TChain.Destroy;
begin
if Assigned(self.FExplorers) then
self.FExplorers.Free;
inherited;
end;
constructor TChains.Create;
begin
inherited Create([doOwnsValues]);
end;
constructor TBIP0122Processor.Create(const ExplorerJsonFilePaths: TStringArray);
begin
FChains := TChains.Create();
ParseJSONFiles(ExplorerJsonFilePaths);
end;
destructor TBIP0122Processor.Destroy;
var
Chain: TChain;
begin
FreeAndNil(FChains);
inherited;
end;
function TBIP0122Processor.GetUrlForBipUri(ExplorerName: string; Uri: TBIP0122URI): String;
var
Url: String;
Argument: String;
HeightNumber: UInt64;
Explorer: TBlockExplorer;
Explorers: TBlockExplorers;
ChainHash: String;
begin
ChainHash := Uri.Chain;
ChainHash := IfThen<string>(ChainHash = '', BitcoinMainNet, ChainHash);
if FChains.ContainsKey(ChainHash) then
begin
Explorers := FChains[ChainHash].FExplorers;
if Explorers.ContainsKey(ExplorerName) then
begin
Explorer := Explorers[ExplorerName];
if Uri.UriType = 'tx' then
begin
Url := Explorer.TxUrl;
end
else if Uri.UriType = 'address' then
begin
Url := Explorer.AddressUrl;
end
else if Uri.UriType = 'block' then
begin
if (Length(Uri.Argument) < 64) and (UInt64.TryParse(Uri.Argument, HeightNumber)) then
begin
Url := Explorer.HeightUrl;
end
else
begin
Url := Explorer.BlockUrl;
end;
end;
Argument := httpprotocol.HTTPEncode(Uri.Argument);
Result := Format(Url, [Argument]);
end;
end;
end;
function TBIP0122Processor.GetExplorerNames: TStringArray;
var
List: TStringList;
ChainKey: String;
Chain: TChain;
i: integer;
begin
List := TStringList.Create;
List.Duplicates := dupIgnore;
List.Sorted := true;
try
for ChainKey in FChains.Keys do
begin
Chain := FChains[ChainKey];
List.AddStrings(Chain.FExplorers.Keys.ToArray);
end;
SetLength(Result, List.Count);
for i := 0 to List.Count -1 do
Result[i] := List[i];
finally
List.Free;
end;
end;
procedure TBIP0122Processor.ParseJSONFiles(FileNames: TStringArray);
var
Fs: TFileStream;
P: TJSONParser;
J: TJSONData;
O: TJSONOptions;
Filename: string;
begin
for Filename in FileNames do
begin
if FileExists(Filename) then
begin
Fs := TFileStream.Create(Filename, fmopenRead);
try
O := [joUTF8, joComments, joIgnoreTrailingComma];
P := TJSONParser.Create(Fs, O);
try
J := P.Parse;
if Assigned(J) then
begin
ParseJSONData(J);
end;
finally
if Assigned(J) then
J.Free;
P.Free;
end;
finally
Fs.Free;
end;
end;
end;
end;
procedure TBIP0122Processor.ParseJSONData(J: TJSONData);
var
O: TJSONObject;
JSONChains: TJSONArray;
JSONChain: TJSONEnum;
ChainObject: TJSONObject;
Chain: TChain;
ChainName: String;
ChainHash: String;
JSONExplorers: TJSONArray;
JSONExplorer: TJSONEnum;
ExplorerObject: TJSONObject;
Explorers: TBlockExplorers;
Explorer: TBlockExplorer;
ExplorerName: String;
DefaultChains: TJSONArray;
DefaultExplorers: TJSONArray;
begin
O := TJSONObject(J);
DefaultChains := TJSONArray.Create();
JSONChains := O.Get('chains', DefaultChains);
for JSONChain in JSONChains do
begin
ChainObject := TJSONObject(JSONChain.Value);
ChainHash := ChainObject.Get('chain', '');
if ChainHash <> '' then
begin
if FChains.ContainsKey(ChainHash) then
begin
Chain := FChains[ChainHash];
Explorers := Chain.FExplorers;
end
else
begin
Explorers := TBlockExplorers.Create();
ChainName := ChainObject.Get('name', '');
Chain := TChain.Create(ChainName, ChainHash, Explorers);
FChains.Add(ChainHash, Chain);
end;
DefaultExplorers := TJSONArray.Create();
JSONExplorers := ChainObject.Get('block-explorers', DefaultExplorers);
for JSONExplorer in JSONExplorers do
begin
ExplorerObject := TJSONObject(JSONExplorer.Value);
ExplorerName := ExplorerObject.Get('name', '');
if ExplorerName <> '' then
begin
Explorer := TBlockExplorer.Create(
ExplorerName,
ExplorerObject.Get('tx', ''),
ExplorerObject.Get('block', ''),
ExplorerObject.Get('height', ''),
ExplorerObject.Get('address', '')
);
if Explorers.ContainsKey(ExplorerName) then
begin
Explorers[ExplorerName] := Explorer;
end
else
begin
Explorers.Add(ExplorerName, Explorer);
end;
end;
end;
FreeAndNil(DefaultExplorers);
end;
end;
FreeAndNil(DefaultChains);
end;
end.