-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbip0122uriparser.pas
54 lines (45 loc) · 1.15 KB
/
bip0122uriparser.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
unit Bip0122Uriparser;
{$ifdef fpc}{$mode delphi}{$endif}{$H+}
interface
uses
Classes, SysUtils, uriparser;
type
TBIP0122URI = class
public
Chain: string;
UriType: string;
Argument: string;
class function TryParse(const uriString: string; Out uri: TBIP0122URI): Boolean;
end;
implementation
class function TBIP0122URI.TryParse(const uriString: string; Out uri: TBIP0122URI): Boolean;
var
ParsedUri: TURI;
ValidPaths: TStrings;
ValidPathsIndex: integer;
begin
Result := false;
uri := nil;
ParsedUri := UriParser.ParseURI(uriString);
if CompareText(ParsedUri.Protocol, 'blockchain') = 0 then
begin
ValidPaths := TStringList.Create;
try
ValidPaths.Add('address');
ValidPaths.Add('block');
ValidPaths.Add('tx');
ValidPathsIndex := ValidPaths.IndexOf(ParsedUri.Path.Trim('/').ToLowerInvariant);
if ValidPathsIndex > -1 then
begin
uri := TBIP0122URI.Create;
uri.Chain := ParsedUri.Host;
uri.UriType := ValidPaths[ValidPathsIndex];
uri.Argument := ParsedUri.Document;
Result := true;
end;
finally
ValidPaths.Free;
end;
end;
end;
end.