-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
web3.eth.ens.pas
150 lines (136 loc) · 5.41 KB
/
web3.eth.ens.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2019 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under GNU AGPL v3.0 with Commons Clause }
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as published }
{ by the Free Software Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/> }
{ }
{******************************************************************************}
unit web3.eth.ens;
{$I web3.inc}
interface
uses
// Delphi
System.SysUtils,
// web3
web3,
web3.eth.types;
function namehash(const name: string): string;
// resolve a name to an Ethereum address
procedure addr(const client: IWeb3; const name: string; const callback: TProc<TAddress, IError>);
// retrieves text metadata for a name
procedure text(const client: IWeb3; const name, key: string; const callback: TProc<TTuple, IError>);
// reverse resolution maps from an address back to a name
procedure reverse(const client: IWeb3; const addr: TAddress; const callback: TProc<string, IError>);
implementation
uses
// Delphi
System.Classes,
System.TypInfo,
// web3
web3.eth,
web3.utils;
const
ENS_REGISTRY: TAddress = '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e';
function namehash(const name: string): string;
begin
const node = (function: TBytes
begin
SetLength(Result, 32);
if name <> '' then
begin
const labels = TStringList.Create;
try
labels.Delimiter := '.';
labels.DelimitedText := name;
for var idx := labels.Count - 1 downto 0 do
begin
const &label = TEncoding.UTF8.GetBytes(labels[idx]);
Result := web3.utils.sha3(Result + web3.utils.sha3(&label));
end;
finally
labels.Free;
end;
end;
end)();
Result := web3.utils.toHex(node);
end;
procedure resolver(const client: IWeb3; const name: string; const callback: TProc<TAddress, IError>);
begin
web3.eth.call(client, ENS_REGISTRY, 'resolver(bytes32)', [namehash(name)], procedure(hex: string; err: IError)
begin
if Assigned(err) then
callback(TAddress.Zero, err)
else
callback(TAddress.Create(hex), nil);
end);
end;
// resolve a name to an Ethereum address
procedure addr(const client: IWeb3; const name: string; const callback: TProc<TAddress, IError>);
begin
resolver(client, name, procedure(resolver: TAddress; err: IError)
begin
if Assigned(err) then
callback(TAddress.Zero, err)
else
web3.eth.call(client, resolver, 'addr(bytes32)', [namehash(name)], procedure(hex: string; err: IError)
begin
if Assigned(err) then
callback(TAddress.Zero, err)
else
callback(TAddress.Create(hex), nil);
end);
end);
end;
// retrieves text metadata for a name.
// each name may have multiple pieces of metadata, identified by a unique string key.
// if no text data exists for node with the key key, the empty string is returned.
procedure text(const client: IWeb3; const name, key: string; const callback: TProc<TTuple, IError>);
begin
resolver(client, name, procedure(resolver: TAddress; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
web3.eth.call(client, resolver, 'text(bytes32,string)', [namehash(name), key], callback);
end);
end;
// reverse resolution maps from an address back to a name
procedure reverse(const client: IWeb3; const addr: TAddress; const callback: TProc<string, IError>);
begin
var name := string(addr).ToLower + '.addr.reverse';
while Copy(name, System.Low(name), 2).ToLower = '0x' do
Delete(name, System.Low(name), 2);
resolver(client, name, procedure(resolver: TAddress; err: IError)
begin
if Assigned(err) then
callback('', err)
else
if resolver.IsZero then
callback(string(addr), nil)
else
web3.eth.call(client, resolver, 'name(bytes32)', [namehash(name)], procedure(tup: TTuple; err: IError)
begin
if Assigned(err) then
callback('', err)
else
callback(tup.ToString, nil);
end);
end);
end;
end.