-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathasset.js
executable file
·97 lines (89 loc) · 2.65 KB
/
asset.js
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
const fetch = require("node-fetch");
const fs = require("fs");
const path = require("path");
const stringify = require("json-stable-stringify");
const prompt = async (display) => {
return new Promise((resolve) => {
process.stdout.write(display);
process.stdin.resume();
process.stdin.once("data", (data) => {
resolve(String(data).trim());
});
});
};
const main = async () => {
const reSymbol = new RegExp(/[A-Z]/);
const fetched = await fetch(
"https://raw.githubusercontent.com/trustwallet/wallet-core/master/coins.json",
).catch((e) => {
throw e;
});
const coins = await fetched.json().catch((e) => {
throw e;
});
while (true) {
const symbol = await prompt(
`Enter the symbol of the token or Ctrl-c to quit: `,
).catch((e) => {
throw e;
});
if (!symbol.match(reSymbol))
throw new Error(`Symbol should be mostly upper case, not '${symbol}'.`);
if (fs.existsSync(path.join("assets", symbol)))
throw new Error(`Asset for symbol ${symbol} already exists.`);
const lowercased = symbol.toLowerCase();
const coin = coins.find((coin) => coin.symbol == symbol);
const fileTrust = coin
? path.join(
"trustwallet",
"assets",
"blockchains",
coin.id,
"info",
"info.json",
)
: null; // HARD-CODED
const trusted = fs.existsSync(fileTrust)
? JSON.parse(fs.readFileSync(fileTrust, "utf-8"))
: null;
const name =
(trusted && trusted.name) ||
(await prompt(`Enter the name of the token: `).catch((e) => {
throw e;
}));
const fileAsset = path.join("assets", lowercased, "asset.json"); // HARD-CODED
const fileMetadata = path.join(
"assets",
lowercased,
"metadata",
"info.json",
); // HARD-CODED
const asset = {
"caip-19": null,
symbol: symbol,
"trustwallet-uid": coin ? `c${coin.coinId}` : null,
};
const metadata = {
"starname-uri": `asset:${lowercased}`,
"trustwallet-info": trusted ? `/${fileTrust}` : null,
};
if (!trusted) {
asset.logo = fileMetadata.replace("info.json", "logo.png"); // HARD-CODED
asset.name = name;
}
fs.mkdirSync(path.join("assets", lowercased, "metadata"), {
recursive: true,
});
fs.writeFileSync(fileAsset, stringify(asset, { space: " " }) + "\n");
fs.writeFileSync(fileMetadata, stringify(metadata, { space: " " }) + "\n");
console.log(`Wrote ${fileAsset} and ${fileMetadata}.`);
}
};
main()
.then(() => {
process.exit(0);
})
.catch((e) => {
console.error(e.stack || e.message || e);
process.exit(-1);
});