UNPKG

1.72 kBJavaScriptView Raw
1import blob from "@ledgerhq/cryptoassets/data/erc20-signatures";
2/**
3 * Retrieve the token information by a given contract address if any
4 */
5
6export const byContractAddress = contract => get().byContract(asContractAddress(contract));
7/**
8 * list all the ERC20 tokens informations
9 */
10
11export const list = () => get().list();
12
13const asContractAddress = addr => {
14 const a = addr.toLowerCase();
15 return a.startsWith("0x") ? a : "0x" + a;
16}; // this internal get() will lazy load and cache the data from the erc20 data blob
17
18
19const get = (() => {
20 let cache;
21 return () => {
22 if (cache) return cache;
23 const buf = Buffer.from(blob, "base64");
24 const byContract = {};
25 const entries = [];
26 let i = 0;
27
28 while (i < buf.length) {
29 const length = buf.readUInt32BE(i);
30 i += 4;
31 const item = buf.slice(i, i + length);
32 let j = 0;
33 const tickerLength = item.readUInt8(j);
34 j += 1;
35 const ticker = item.slice(j, j + tickerLength).toString("ascii");
36 j += tickerLength;
37 const contractAddress = asContractAddress(item.slice(j, j + 20).toString("hex"));
38 j += 20;
39 const decimals = item.readUInt32BE(j);
40 j += 4;
41 const chainId = item.readUInt32BE(j);
42 j += 4;
43 const signature = item.slice(j);
44 const entry = {
45 ticker,
46 contractAddress,
47 decimals,
48 chainId,
49 signature,
50 data: item
51 };
52 entries.push(entry);
53 byContract[contractAddress] = entry;
54 i += length;
55 }
56
57 const api = {
58 list: () => entries,
59 byContract: contractAddress => byContract[contractAddress]
60 };
61 cache = api;
62 return api;
63 };
64})();
65//# sourceMappingURL=erc20.js.map
\No newline at end of file