/
cli-helpers.ts
81 lines (73 loc) · 2.17 KB
/
cli-helpers.ts
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
import Resolution from '../Resolution';
import * as fs from 'fs';
export async function tryInfo(
method,
response,
name: string,
): Promise<boolean> {
const field = name;
try {
const resolvedPromise = await method();
response[field] = resolvedPromise;
return true;
} catch (err) {
response[field] = err.code;
return false;
}
}
export function commaSeparatedList(value, dummyPrevious) {
return value.split(',').map((v: string) => v.toUpperCase());
}
export function signedInfuraLink(key: string): string {
return `https://mainnet.infura.io/v3/${key}`;
}
export function getEtheriumUrl(): string {
//try to get them from config files
const configObject = getConfig();
if (!configObject) {
if (process.env.UNSTOPPABLE_RESOLUTION_INFURA_PROJECTID)
return signedInfuraLink(
process.env.UNSTOPPABLE_RESOLUTION_INFURA_PROJECTID,
);
if (process.env.UNSTOPPABLE_RESOLUTION_URL)
return process.env.UNSTOPPABLE_RESOLUTION_URL;
} else {
if (configObject.type === 'infura')
return signedInfuraLink(configObject.value);
if (configObject.type === 'url') return configObject.value;
}
throw new Error(
"Couldn't find any configurations\n\tUse -C to configurate the library",
);
}
export function buildResolutionPackage() {
return new Resolution({
blockchain: {
ens: getEtheriumUrl(),
cns: getEtheriumUrl(),
},
});
}
export function parseConfig(value: string) {
const words = value.split(':');
return { type: words[0], value: words.slice(1).join(':')};
}
export function storeConfig(type: 'infura' | 'url', value: string) {
fs.writeFile('.resolution', `${type}=${value}`, () =>
console.log(`${type}=${value} record stored`),
);
}
export function getConfig() {
try {
const config = fs
.readFileSync(`.resolution`)
.toString()
.split('=');
if (config[0] === 'infura' || config[0] === 'url')
return { type: config[0], value: config[1] };
} catch (err) {
throw new Error(
'Resolution library is not configured. Please use resolution -C and configure it either with infura project id or node url for lookup. Resolution -h for more details',
);
}
}