@beclab/olaresid 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +49 -0
- package/README.md +93 -0
- package/dist/abi/ABITypeABI.d.ts +88 -0
- package/dist/abi/ABITypeABI.d.ts.map +1 -0
- package/dist/abi/ABITypeABI.js +382 -0
- package/dist/abi/ABITypeABI.js.map +1 -0
- package/dist/abi/RegistryABI.d.ts +77 -0
- package/dist/abi/RegistryABI.d.ts.map +1 -0
- package/dist/abi/RegistryABI.js +462 -0
- package/dist/abi/RegistryABI.js.map +1 -0
- package/dist/abi/RootResolverABI.d.ts +78 -0
- package/dist/abi/RootResolverABI.d.ts.map +1 -0
- package/dist/abi/RootResolverABI.js +372 -0
- package/dist/abi/RootResolverABI.js.map +1 -0
- package/dist/abi/TerminusDIDABI.d.ts +80 -0
- package/dist/abi/TerminusDIDABI.d.ts.map +1 -0
- package/dist/abi/TerminusDIDABI.js +1877 -0
- package/dist/abi/TerminusDIDABI.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +199 -0
- package/dist/cli.js.map +1 -0
- package/dist/debug.d.ts +46 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +129 -0
- package/dist/debug.js.map +1 -0
- package/dist/domain/index.d.ts +59 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +431 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +166 -0
- package/dist/index.js.map +1 -0
- package/dist/tag/address.d.ts +11 -0
- package/dist/tag/address.d.ts.map +1 -0
- package/dist/tag/address.js +44 -0
- package/dist/tag/address.js.map +1 -0
- package/dist/tag/array.d.ts +14 -0
- package/dist/tag/array.d.ts.map +1 -0
- package/dist/tag/array.js +72 -0
- package/dist/tag/array.js.map +1 -0
- package/dist/tag/bool.d.ts +11 -0
- package/dist/tag/bool.d.ts.map +1 -0
- package/dist/tag/bool.js +43 -0
- package/dist/tag/bool.js.map +1 -0
- package/dist/tag/bytes.d.ts +11 -0
- package/dist/tag/bytes.d.ts.map +1 -0
- package/dist/tag/bytes.js +37 -0
- package/dist/tag/bytes.js.map +1 -0
- package/dist/tag/flarray.d.ts +15 -0
- package/dist/tag/flarray.d.ts.map +1 -0
- package/dist/tag/flarray.js +81 -0
- package/dist/tag/flarray.js.map +1 -0
- package/dist/tag/flbytes.d.ts +11 -0
- package/dist/tag/flbytes.d.ts.map +1 -0
- package/dist/tag/flbytes.js +47 -0
- package/dist/tag/flbytes.js.map +1 -0
- package/dist/tag/index.d.ts +32 -0
- package/dist/tag/index.d.ts.map +1 -0
- package/dist/tag/index.js +121 -0
- package/dist/tag/index.js.map +1 -0
- package/dist/tag/int.d.ts +12 -0
- package/dist/tag/int.d.ts.map +1 -0
- package/dist/tag/int.js +49 -0
- package/dist/tag/int.js.map +1 -0
- package/dist/tag/string.d.ts +11 -0
- package/dist/tag/string.d.ts.map +1 -0
- package/dist/tag/string.js +37 -0
- package/dist/tag/string.js.map +1 -0
- package/dist/tag/tag.d.ts +67 -0
- package/dist/tag/tag.d.ts.map +1 -0
- package/dist/tag/tag.js +157 -0
- package/dist/tag/tag.js.map +1 -0
- package/dist/tag/tuple.d.ts +17 -0
- package/dist/tag/tuple.d.ts.map +1 -0
- package/dist/tag/tuple.js +162 -0
- package/dist/tag/tuple.js.map +1 -0
- package/dist/tag/uint.d.ts +12 -0
- package/dist/tag/uint.d.ts.map +1 -0
- package/dist/tag/uint.js +49 -0
- package/dist/tag/uint.js.map +1 -0
- package/dist/test/did.d.ts +2 -0
- package/dist/test/did.d.ts.map +1 -0
- package/dist/test/did.js +177 -0
- package/dist/test/did.js.map +1 -0
- package/examples/index.ts +15 -0
- package/examples/legacy.ts +33 -0
- package/package.json +50 -0
- package/project.json +6 -0
- package/src/abi/ABITypeABI.ts +379 -0
- package/src/abi/RegistryABI.ts +459 -0
- package/src/abi/RootResolverABI.ts +369 -0
- package/src/abi/TerminusDIDABI.ts +1874 -0
- package/src/cli.ts +230 -0
- package/src/debug.ts +170 -0
- package/src/domain/index.ts +659 -0
- package/src/index.ts +302 -0
- package/src/tag/address.ts +48 -0
- package/src/tag/array.ts +80 -0
- package/src/tag/bool.ts +43 -0
- package/src/tag/bytes.ts +38 -0
- package/src/tag/flarray.ts +99 -0
- package/src/tag/flbytes.ts +48 -0
- package/src/tag/index.ts +170 -0
- package/src/tag/int.ts +51 -0
- package/src/tag/string.ts +38 -0
- package/src/tag/tag.ts +229 -0
- package/src/tag/tuple.ts +193 -0
- package/src/tag/uint.ts +51 -0
- package/src/test/did.ts +346 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import DIDTag from '../tag/index';
|
|
3
|
+
import { debug } from '../debug';
|
|
4
|
+
|
|
5
|
+
const getBase64 = (data: any) => {
|
|
6
|
+
const hex = data;
|
|
7
|
+
const arr = [];
|
|
8
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
9
|
+
const code = parseInt(hex.slice(i, i + 2), 16);
|
|
10
|
+
arr.push(code);
|
|
11
|
+
}
|
|
12
|
+
debug.info(arr);
|
|
13
|
+
|
|
14
|
+
let str = '';
|
|
15
|
+
for (let i = 0; i < arr.length; i++) {
|
|
16
|
+
const char = String.fromCharCode(arr[i]);
|
|
17
|
+
str += char;
|
|
18
|
+
}
|
|
19
|
+
debug.info(str);
|
|
20
|
+
const base64 = btoa(str);
|
|
21
|
+
debug.info(base64);
|
|
22
|
+
|
|
23
|
+
return base64;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getHex = (data: any) => {
|
|
27
|
+
let hex = '';
|
|
28
|
+
for (let i = 0; i < data.length; i++) {
|
|
29
|
+
const code = data.charCodeAt(i);
|
|
30
|
+
let hexCode = code.toString(16);
|
|
31
|
+
if (hexCode.length < 2) {
|
|
32
|
+
hexCode = '0' + hexCode;
|
|
33
|
+
}
|
|
34
|
+
hex += hexCode;
|
|
35
|
+
}
|
|
36
|
+
debug.info(hex);
|
|
37
|
+
return hex;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export namespace PackageDomain {
|
|
41
|
+
export interface Tag {
|
|
42
|
+
definition: any;
|
|
43
|
+
from: string;
|
|
44
|
+
to: string;
|
|
45
|
+
name: string;
|
|
46
|
+
valueFormated: any;
|
|
47
|
+
valueSource: any;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface DomainData {
|
|
51
|
+
id: string;
|
|
52
|
+
name: string;
|
|
53
|
+
did: string;
|
|
54
|
+
note: string;
|
|
55
|
+
allowSubdomain: boolean;
|
|
56
|
+
owner: string;
|
|
57
|
+
subdomains: DomainData[];
|
|
58
|
+
tags: Tag[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface OfficialExtendedInformation {
|
|
62
|
+
ip: string;
|
|
63
|
+
rsaKey: string;
|
|
64
|
+
authenticateds: any[];
|
|
65
|
+
latestDID: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class Domain implements DomainData {
|
|
69
|
+
id: string;
|
|
70
|
+
name: string;
|
|
71
|
+
did: string;
|
|
72
|
+
note: string;
|
|
73
|
+
allowSubdomain: boolean;
|
|
74
|
+
owner: string;
|
|
75
|
+
subdomains: DomainData[];
|
|
76
|
+
tags: Tag[];
|
|
77
|
+
|
|
78
|
+
constructor(
|
|
79
|
+
id: string,
|
|
80
|
+
name: string,
|
|
81
|
+
did: string,
|
|
82
|
+
note: string,
|
|
83
|
+
allowSubdomain: boolean,
|
|
84
|
+
owner: string,
|
|
85
|
+
subdomains: Domain[],
|
|
86
|
+
tags: Tag[]
|
|
87
|
+
) {
|
|
88
|
+
this.id = id;
|
|
89
|
+
this.name = name;
|
|
90
|
+
this.did = did;
|
|
91
|
+
this.note = note;
|
|
92
|
+
this.allowSubdomain = allowSubdomain;
|
|
93
|
+
this.owner = owner;
|
|
94
|
+
this.subdomains = subdomains;
|
|
95
|
+
this.tags = tags;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export const syncById = (tokenId: string, contract: ethers.Contract) =>
|
|
100
|
+
new Promise<Domain>(async (resolve, reject) => {
|
|
101
|
+
let meta: any | undefined = undefined;
|
|
102
|
+
let owner: string | undefined = undefined;
|
|
103
|
+
let times = 0;
|
|
104
|
+
while (
|
|
105
|
+
tokenId == undefined ||
|
|
106
|
+
meta == undefined ||
|
|
107
|
+
owner == undefined
|
|
108
|
+
) {
|
|
109
|
+
try {
|
|
110
|
+
meta = await contract.getMetadata(tokenId);
|
|
111
|
+
owner = await contract.ownerOf(tokenId);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
times++;
|
|
114
|
+
if (times >= 5) {
|
|
115
|
+
throw new Error('network error');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// debug.info('meta', meta);
|
|
120
|
+
const domain = {
|
|
121
|
+
id: ethers.toBeHex(tokenId),
|
|
122
|
+
name: meta['0'],
|
|
123
|
+
did: meta['1'],
|
|
124
|
+
note: meta['2'],
|
|
125
|
+
allowSubdomain: meta['3'],
|
|
126
|
+
subdomains: [],
|
|
127
|
+
owner: owner,
|
|
128
|
+
tags: []
|
|
129
|
+
} as Domain;
|
|
130
|
+
|
|
131
|
+
const tags = await fetchAllTagType(domain, contract);
|
|
132
|
+
domain.tags = tags;
|
|
133
|
+
|
|
134
|
+
await fetchAllTagValue(domain, contract);
|
|
135
|
+
|
|
136
|
+
resolve(domain);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
export const syncByName = (name: string, contract: ethers.Contract) =>
|
|
140
|
+
new Promise<Domain | undefined>(async (resolve, reject) => {
|
|
141
|
+
let tokenId: string | undefined = await contract.tokenIdOf(name);
|
|
142
|
+
debug.info('syncByName', name, tokenId);
|
|
143
|
+
if (tokenId == undefined) {
|
|
144
|
+
resolve(undefined);
|
|
145
|
+
} else {
|
|
146
|
+
resolve(await syncById(tokenId, contract));
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
export const syncByIndex = (index: number, contract: ethers.Contract) =>
|
|
151
|
+
new Promise<Domain>(async (resolve, reject) => {
|
|
152
|
+
let tokenId: string | undefined = undefined;
|
|
153
|
+
|
|
154
|
+
let times = 0;
|
|
155
|
+
while (tokenId == undefined) {
|
|
156
|
+
try {
|
|
157
|
+
tokenId = await contract.tokenByIndex(index);
|
|
158
|
+
} catch (error) {
|
|
159
|
+
times++;
|
|
160
|
+
if (times >= 5) {
|
|
161
|
+
throw new Error('network error');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
resolve(await syncById(tokenId, contract));
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
export const buildTree = (allDomains: Domain[]): Domain[] => {
|
|
169
|
+
allDomains.forEach((item) => {
|
|
170
|
+
const domains = findSubdomain(item, allDomains);
|
|
171
|
+
item.subdomains = domains;
|
|
172
|
+
});
|
|
173
|
+
return allDomains.filter((item) => item.name.split('.').length == 1);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
export const findSubdomain = (domain: Domain, allDomains: Domain[]) => {
|
|
177
|
+
const arr: Domain[] = [];
|
|
178
|
+
allDomains.forEach((item) => {
|
|
179
|
+
if (
|
|
180
|
+
item.name.endsWith('.' + domain.name) &&
|
|
181
|
+
domain.name.split('.').length + 1 == item.name.split('.').length
|
|
182
|
+
) {
|
|
183
|
+
const subdomains = findSubdomain(item, allDomains);
|
|
184
|
+
|
|
185
|
+
item.subdomains = subdomains;
|
|
186
|
+
arr.push(item);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
return arr;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export const findASubtree = (
|
|
193
|
+
name: string,
|
|
194
|
+
allDomains: Domain[]
|
|
195
|
+
): Domain | undefined => {
|
|
196
|
+
for (const domain of allDomains) {
|
|
197
|
+
if (domain.name == name) {
|
|
198
|
+
return domain;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return undefined;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export const findSubtreesByOwner = (
|
|
205
|
+
owner: string,
|
|
206
|
+
allDomains: Domain[]
|
|
207
|
+
): Domain[] => {
|
|
208
|
+
let arr: Domain[] = [];
|
|
209
|
+
|
|
210
|
+
arr.push(
|
|
211
|
+
...allDomains.filter(
|
|
212
|
+
(d) => d.owner.toLocaleLowerCase() == owner.toLocaleLowerCase()
|
|
213
|
+
)
|
|
214
|
+
);
|
|
215
|
+
for (const d of allDomains) {
|
|
216
|
+
let subArr = findSubtreesByOwner(owner, d.subdomains);
|
|
217
|
+
arr.push(...subArr);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return arr;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export const findSubtreesByDid = (
|
|
224
|
+
did: string,
|
|
225
|
+
allDomains: Domain[]
|
|
226
|
+
): Domain[] => {
|
|
227
|
+
let arr: Domain[] = [];
|
|
228
|
+
|
|
229
|
+
arr.push(...allDomains.filter((d) => d.did.startsWith(did)));
|
|
230
|
+
for (const d of allDomains) {
|
|
231
|
+
let subArr = findSubtreesByDid(did, d.subdomains);
|
|
232
|
+
arr.push(...subArr);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return arr;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
export const fetchAllTagType = (
|
|
239
|
+
domain: Domain,
|
|
240
|
+
contract: ethers.Contract
|
|
241
|
+
) =>
|
|
242
|
+
new Promise<Tag[]>(async (resolve, reject) => {
|
|
243
|
+
// debug.info('fetchAllTagType', domain);
|
|
244
|
+
|
|
245
|
+
let d = domain.name.split('.');
|
|
246
|
+
let fromArr = [''];
|
|
247
|
+
for (let index = 1; index <= d.length; index++) {
|
|
248
|
+
fromArr.push(d.slice(d.length - index).join('.'));
|
|
249
|
+
}
|
|
250
|
+
debug.info('fromArr', fromArr);
|
|
251
|
+
|
|
252
|
+
let arr: Tag[] = [];
|
|
253
|
+
for (const from of fromArr) {
|
|
254
|
+
debug.group(from);
|
|
255
|
+
let count = await contract.getDefinedTagCount(from);
|
|
256
|
+
debug.info('count', count);
|
|
257
|
+
|
|
258
|
+
for (let i = 0; i < count; i++) {
|
|
259
|
+
let name = await contract.getDefinedTagNameByIndex(from, i);
|
|
260
|
+
debug.info('name', name);
|
|
261
|
+
|
|
262
|
+
arr.push({
|
|
263
|
+
from: from,
|
|
264
|
+
to: domain.name,
|
|
265
|
+
name: name,
|
|
266
|
+
valueFormated: undefined,
|
|
267
|
+
valueSource: undefined
|
|
268
|
+
} as Tag);
|
|
269
|
+
}
|
|
270
|
+
debug.groupEnd();
|
|
271
|
+
}
|
|
272
|
+
domain.tags = arr;
|
|
273
|
+
resolve(domain.tags);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
export const fetchTagStructure = (tag: Tag, contract: ethers.Contract) =>
|
|
277
|
+
new Promise<DIDTag.TagParser>(async (resolve, reject) => {
|
|
278
|
+
debug.info('fetchTagStructure', tag.from, tag.to, tag.name);
|
|
279
|
+
|
|
280
|
+
const tagRawType = await contract.getTagABIType(tag.from, tag.name);
|
|
281
|
+
// debug.info(`tag type raw bytes:`);
|
|
282
|
+
// debug.info(tagRawType);
|
|
283
|
+
|
|
284
|
+
const tagType = DIDTag.doFormatType(tagRawType);
|
|
285
|
+
// debug.info(`tag type:`);
|
|
286
|
+
// debug.info(tagType);
|
|
287
|
+
|
|
288
|
+
if (
|
|
289
|
+
(tagType as DIDTag.TagParserBase)
|
|
290
|
+
.getAbiTypeString()
|
|
291
|
+
.includes('tuple')
|
|
292
|
+
) {
|
|
293
|
+
let [_, fieldNamesHashs] = await contract.getTagType(
|
|
294
|
+
tag.from,
|
|
295
|
+
tag.name
|
|
296
|
+
);
|
|
297
|
+
let fieldNames: any[] = [];
|
|
298
|
+
for (let hash of fieldNamesHashs) {
|
|
299
|
+
let blockNum = await contract.getFieldNamesEventBlock(hash);
|
|
300
|
+
// debug.info(`field name hash: ${hash}`);
|
|
301
|
+
// debug.info(`block num: ${blockNum}`);
|
|
302
|
+
const events = await contract.queryFilter(
|
|
303
|
+
'OffchainStringArray',
|
|
304
|
+
Number(blockNum),
|
|
305
|
+
Number(blockNum)
|
|
306
|
+
);
|
|
307
|
+
for (let event of events) {
|
|
308
|
+
// debug.info('event', event);
|
|
309
|
+
if (hash == (event as any).args.hash) {
|
|
310
|
+
fieldNames.push((event as any).args.value);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
// debug.info(`found field names:`);
|
|
315
|
+
// debug.info(fieldNames);
|
|
316
|
+
|
|
317
|
+
(tagType as DIDTag.TagParserBox).setFieldNames(fieldNames, 0);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
resolve(tagType);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
export const fetchTagValue = (tag: Tag, contract: ethers.Contract) =>
|
|
324
|
+
new Promise<any>(async (resolve, reject) => {
|
|
325
|
+
try {
|
|
326
|
+
debug.info('fetchTagValue', tag.from, tag.to, tag.name);
|
|
327
|
+
|
|
328
|
+
const tagRawType = await contract.getTagABIType(
|
|
329
|
+
tag.from,
|
|
330
|
+
tag.name
|
|
331
|
+
);
|
|
332
|
+
// debug.info(`tag type raw bytes:`);
|
|
333
|
+
// debug.info(tagRawType);
|
|
334
|
+
|
|
335
|
+
const tagType = DIDTag.doFormatType(tagRawType);
|
|
336
|
+
// debug.info(`tag type:`);
|
|
337
|
+
// debug.info(tagType);
|
|
338
|
+
|
|
339
|
+
const rawData = await contract.getTagElem(
|
|
340
|
+
tag.from,
|
|
341
|
+
tag.to,
|
|
342
|
+
tag.name,
|
|
343
|
+
[]
|
|
344
|
+
);
|
|
345
|
+
// debug.info(`tag raw value:`);
|
|
346
|
+
// debug.info(rawData);
|
|
347
|
+
tag.valueSource = rawData;
|
|
348
|
+
|
|
349
|
+
let data = tagType.parseValue(rawData);
|
|
350
|
+
data = data[0];
|
|
351
|
+
debug.info('tag value:');
|
|
352
|
+
debug.info(data);
|
|
353
|
+
|
|
354
|
+
if (
|
|
355
|
+
(tagType as DIDTag.TagParserBase)
|
|
356
|
+
.getAbiTypeString()
|
|
357
|
+
.includes('tuple')
|
|
358
|
+
) {
|
|
359
|
+
let [_, fieldNamesHashs] = await contract.getTagType(
|
|
360
|
+
tag.from,
|
|
361
|
+
tag.name
|
|
362
|
+
);
|
|
363
|
+
let fieldNames: any[] = [];
|
|
364
|
+
for (let hash of fieldNamesHashs) {
|
|
365
|
+
let blockNum = await contract.getFieldNamesEventBlock(
|
|
366
|
+
hash
|
|
367
|
+
);
|
|
368
|
+
// debug.info(`field name hash: ${hash}`);
|
|
369
|
+
// debug.info(`block num: ${blockNum}`);
|
|
370
|
+
const events = await contract.queryFilter(
|
|
371
|
+
'OffchainStringArray',
|
|
372
|
+
Number(blockNum),
|
|
373
|
+
Number(blockNum)
|
|
374
|
+
);
|
|
375
|
+
for (let event of events) {
|
|
376
|
+
// debug.info('event', event);
|
|
377
|
+
if (hash == (event as any).args.hash) {
|
|
378
|
+
fieldNames.push((event as any).args.value);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
// debug.info(`found field names:`);
|
|
383
|
+
// debug.info(fieldNames);
|
|
384
|
+
|
|
385
|
+
(tagType as DIDTag.TagParserBox).setFieldNames(
|
|
386
|
+
fieldNames,
|
|
387
|
+
0
|
|
388
|
+
);
|
|
389
|
+
|
|
390
|
+
data = (tagType as DIDTag.TagParserBase).hydration(rawData);
|
|
391
|
+
|
|
392
|
+
// data = data[0];
|
|
393
|
+
// debug.info('tag value with field names:');
|
|
394
|
+
// console.dir(data, { depth: null });
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
tag.valueFormated = data;
|
|
398
|
+
debug.info(
|
|
399
|
+
`✅ Successfully fetched value for tag: ${tag.name}`
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
resolve(tag);
|
|
403
|
+
} catch (error) {
|
|
404
|
+
debug.error(
|
|
405
|
+
`❌ Failed to fetch value for tag "${tag.name}" (${
|
|
406
|
+
tag.from
|
|
407
|
+
} -> ${tag.to}): ${
|
|
408
|
+
error instanceof Error ? error.message : String(error)
|
|
409
|
+
}`
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
tag.valueFormated = null;
|
|
413
|
+
tag.valueSource = null;
|
|
414
|
+
|
|
415
|
+
resolve(tag);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
export const fetchAllTagValue = (
|
|
420
|
+
domain: Domain,
|
|
421
|
+
contract: ethers.Contract
|
|
422
|
+
) =>
|
|
423
|
+
new Promise<Tag[]>(async (resolve, reject) => {
|
|
424
|
+
for (const tag of domain.tags) {
|
|
425
|
+
await fetchTagValue(tag, contract);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
resolve(domain.tags);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
export const defineTag = async (
|
|
432
|
+
domain: Domain | undefined,
|
|
433
|
+
tagName: string,
|
|
434
|
+
value: DIDTag.TagParserBase,
|
|
435
|
+
contract: any,
|
|
436
|
+
providerHolder: DIDTag.ABITypeProviderHolder
|
|
437
|
+
) => {
|
|
438
|
+
debug.info('abitype', await value.getAbiType(providerHolder));
|
|
439
|
+
debug.info('field', await value.getFieldNames());
|
|
440
|
+
|
|
441
|
+
await contract.defineTag(
|
|
442
|
+
domain?.name,
|
|
443
|
+
tagName,
|
|
444
|
+
await value.getAbiType(providerHolder),
|
|
445
|
+
await value.getFieldNames()
|
|
446
|
+
);
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
export const getAllSubdoamin = (domain: Domain) => {
|
|
450
|
+
let arr: Domain[] = [];
|
|
451
|
+
for (const d of domain.subdomains) {
|
|
452
|
+
arr.push(...getAllSubdoamin(d));
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
arr.push(...domain.subdomains);
|
|
456
|
+
|
|
457
|
+
return arr;
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
export const fetchOfficialExtendedInformation = (
|
|
461
|
+
domain: Domain,
|
|
462
|
+
contract: ethers.Contract
|
|
463
|
+
) =>
|
|
464
|
+
new Promise<OfficialExtendedInformation>(async (resolve, reject) => {
|
|
465
|
+
let ip: string | undefined = undefined;
|
|
466
|
+
let ipStr = '';
|
|
467
|
+
try {
|
|
468
|
+
ip = (await contract.getDnsARecord(domain.name)) as string;
|
|
469
|
+
ipStr = `${parseInt(ip.substring(2, 4), 16)}.${parseInt(
|
|
470
|
+
ip.substring(4, 6),
|
|
471
|
+
16
|
|
472
|
+
)}.${parseInt(ip.substring(6, 8), 16)}.${parseInt(
|
|
473
|
+
ip.substring(8, 10),
|
|
474
|
+
16
|
|
475
|
+
)}`;
|
|
476
|
+
} catch (error) {
|
|
477
|
+
debug.info(
|
|
478
|
+
error instanceof Error ? error.message : String(error)
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
let rsaKey: any | undefined = undefined;
|
|
483
|
+
try {
|
|
484
|
+
rsaKey = (await contract.getRsaPubKey(domain.name)) as string;
|
|
485
|
+
} catch (error) {
|
|
486
|
+
debug.info(
|
|
487
|
+
error instanceof Error ? error.message : String(error)
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
let pubKey = '';
|
|
492
|
+
try {
|
|
493
|
+
if (rsaKey != '0x' && rsaKey != undefined) {
|
|
494
|
+
// debug.info('rsaKey:');
|
|
495
|
+
const str = String.fromCharCode(
|
|
496
|
+
...rsaKey
|
|
497
|
+
.match(/.{1,2}/g)
|
|
498
|
+
.map((byte: any) => parseInt(byte, 16))
|
|
499
|
+
);
|
|
500
|
+
const encoded = btoa(str);
|
|
501
|
+
const base64 = getBase64(rsaKey.replaceAll('0x', ''));
|
|
502
|
+
|
|
503
|
+
// debug.info(encoded);
|
|
504
|
+
// debug.info(base64);
|
|
505
|
+
pubKey = `-----BEGIN RSA PUBLIC KEY-----
|
|
506
|
+
${base64}
|
|
507
|
+
-----END RSA PUBLIC KEY-----`;
|
|
508
|
+
}
|
|
509
|
+
} catch (error) {
|
|
510
|
+
debug.error(
|
|
511
|
+
error instanceof Error ? error.message : String(error)
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
let authenticatedAddress: any[] = [];
|
|
516
|
+
try {
|
|
517
|
+
authenticatedAddress =
|
|
518
|
+
await contract.getAuthenticationAddresses(domain.name);
|
|
519
|
+
} catch (error) {
|
|
520
|
+
debug.error(
|
|
521
|
+
error instanceof Error ? error.message : String(error)
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// debug.info('authenticatedAddress:');
|
|
526
|
+
// debug.info(authenticatedAddress);
|
|
527
|
+
|
|
528
|
+
let latestDID = '';
|
|
529
|
+
try {
|
|
530
|
+
latestDID = await contract.getLatestDID(domain.name);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
debug.info(
|
|
533
|
+
error instanceof Error ? error.message : String(error)
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
// debug.info('latestDID:', latestDID);
|
|
537
|
+
|
|
538
|
+
const officialExt = {
|
|
539
|
+
ip: ipStr,
|
|
540
|
+
rsaKey: pubKey,
|
|
541
|
+
authenticateds: authenticatedAddress,
|
|
542
|
+
latestDID: latestDID
|
|
543
|
+
};
|
|
544
|
+
debug.info('officialExt: ', officialExt);
|
|
545
|
+
|
|
546
|
+
resolve(officialExt);
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
export const updateIp = (
|
|
550
|
+
domain: Domain,
|
|
551
|
+
ipStr: string,
|
|
552
|
+
contract: ethers.Contract
|
|
553
|
+
) =>
|
|
554
|
+
new Promise<string>(async (resolve, reject) => {
|
|
555
|
+
debug.info('updateIp', ipStr);
|
|
556
|
+
|
|
557
|
+
const v2 =
|
|
558
|
+
'((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))';
|
|
559
|
+
const v3 =
|
|
560
|
+
'((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5])).((d{1,2})|(1d{2})|(2[0-4]d)|(25[0-5]))';
|
|
561
|
+
const reg = new RegExp(v3, 'g');
|
|
562
|
+
const reg2 = new RegExp(v2, 'g');
|
|
563
|
+
|
|
564
|
+
const isIp = ipStr.match(reg);
|
|
565
|
+
const isIp2 = ipStr.match(reg2);
|
|
566
|
+
|
|
567
|
+
if (isIp != null || isIp2 != null) {
|
|
568
|
+
const ipArr = ipStr.split('.');
|
|
569
|
+
|
|
570
|
+
const add0 = (str: string) =>
|
|
571
|
+
str.length == 1 ? '0' + str : str;
|
|
572
|
+
const ipBytes = `0x${add0(
|
|
573
|
+
parseInt(ipArr[0]).toString(16)
|
|
574
|
+
)}${add0(parseInt(ipArr[1]).toString(16))}${add0(
|
|
575
|
+
parseInt(ipArr[2]).toString(16)
|
|
576
|
+
)}${add0(parseInt(ipArr[3]).toString(16))}`;
|
|
577
|
+
|
|
578
|
+
debug.info(ethers.getBytes(ipBytes));
|
|
579
|
+
debug.info(ipBytes);
|
|
580
|
+
debug.info(domain.name);
|
|
581
|
+
|
|
582
|
+
await contract
|
|
583
|
+
.setDnsARecord(domain.name, ipBytes)
|
|
584
|
+
.then((resp) => {
|
|
585
|
+
resolve('sumbit succeed');
|
|
586
|
+
})
|
|
587
|
+
.catch((error) => {
|
|
588
|
+
debug.info('error1:');
|
|
589
|
+
debug.info(error);
|
|
590
|
+
|
|
591
|
+
reject(error.message);
|
|
592
|
+
});
|
|
593
|
+
} else {
|
|
594
|
+
reject('ip format error');
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
export const updateRSAPublicKey = (
|
|
599
|
+
domain: Domain,
|
|
600
|
+
rsaPublicKey: string,
|
|
601
|
+
contract: ethers.Contract
|
|
602
|
+
) =>
|
|
603
|
+
new Promise<string>(async (resolve, reject) => {
|
|
604
|
+
const regex1 = new RegExp('-----BEGIN PUBLIC KEY-----', 'g');
|
|
605
|
+
const regex2 = new RegExp('-----END PUBLIC KEY-----', 'g');
|
|
606
|
+
const regex3 = new RegExp('\n', 'g');
|
|
607
|
+
|
|
608
|
+
const keyBase64 = rsaPublicKey
|
|
609
|
+
.replace(regex1, '')
|
|
610
|
+
.replace(regex2, '')
|
|
611
|
+
.replace(regex3, '');
|
|
612
|
+
|
|
613
|
+
const keyHex = getHex(atob(rsaPublicKey));
|
|
614
|
+
|
|
615
|
+
debug.info('keyHex:');
|
|
616
|
+
debug.info(keyHex);
|
|
617
|
+
debug.info(keyBase64);
|
|
618
|
+
debug.info(rsaPublicKey);
|
|
619
|
+
|
|
620
|
+
await contract
|
|
621
|
+
.setRsaPubKey(domain.name, `0x${keyHex}`)
|
|
622
|
+
.then((resp) => {
|
|
623
|
+
resolve('sumbit succeed');
|
|
624
|
+
})
|
|
625
|
+
.catch((error) => {
|
|
626
|
+
reject(error.message);
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
export const setValue = (tag: Tag, data: any, contract: ethers.Contract) =>
|
|
631
|
+
new Promise<string>(async (resolve, reject) => {
|
|
632
|
+
await contract
|
|
633
|
+
.addTag(tag.from, tag.to, tag.name, data)
|
|
634
|
+
.then((resp) => {
|
|
635
|
+
debug.info(resp);
|
|
636
|
+
resolve('sumbit succeed');
|
|
637
|
+
})
|
|
638
|
+
.catch((error) => {
|
|
639
|
+
reject(error.message);
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
export const setTagger = (
|
|
644
|
+
tag: Tag,
|
|
645
|
+
tagger: string,
|
|
646
|
+
contract: ethers.Contract
|
|
647
|
+
) =>
|
|
648
|
+
new Promise<string>(async (resolve, reject) => {
|
|
649
|
+
await contract
|
|
650
|
+
.setTagger(tag.from, tag.name, tagger)
|
|
651
|
+
.then((resp) => {
|
|
652
|
+
debug.info(resp);
|
|
653
|
+
resolve('sumbit succeed');
|
|
654
|
+
})
|
|
655
|
+
.catch((error) => {
|
|
656
|
+
reject(error.message);
|
|
657
|
+
});
|
|
658
|
+
});
|
|
659
|
+
}
|