UNPKG

7.26 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __generator = (this && this.__generator) || function (thisArg, body) {
12 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14 function verb(n) { return function (v) { return step([n, v]); }; }
15 function step(op) {
16 if (f) throw new TypeError("Generator is already executing.");
17 while (_) try {
18 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19 if (y = 0, t) op = [op[0] & 2, t.value];
20 switch (op[0]) {
21 case 0: case 1: t = op; break;
22 case 4: _.label++; return { value: op[1], done: false };
23 case 5: _.label++; y = op[1]; op = [0]; continue;
24 case 7: op = _.ops.pop(); _.trys.pop(); continue;
25 default:
26 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30 if (t[2]) _.ops.pop();
31 _.trys.pop(); continue;
32 }
33 op = body.call(thisArg, _);
34 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36 }
37};
38var __importDefault = (this && this.__importDefault) || function (mod) {
39 return (mod && mod.__esModule) ? mod : { "default": mod };
40};
41exports.__esModule = true;
42exports.list = exports.byContractAddressAndChainId = exports.findERC20SignaturesInfo = void 0;
43var erc20_signatures_1 = __importDefault(require("@ledgerhq/cryptoassets/data/erc20-signatures"));
44var logs_1 = require("@ledgerhq/logs");
45var axios_1 = __importDefault(require("axios"));
46var loadConfig_1 = require("./loadConfig");
47var findERC20SignaturesInfo = function (userLoadConfig) { return __awaiter(void 0, void 0, void 0, function () {
48 var cryptoassetsBaseURL, url, blob;
49 return __generator(this, function (_a) {
50 switch (_a.label) {
51 case 0:
52 cryptoassetsBaseURL = (0, loadConfig_1.getLoadConfig)(userLoadConfig).cryptoassetsBaseURL;
53 if (!cryptoassetsBaseURL)
54 return [2 /*return*/, null];
55 url = "".concat(cryptoassetsBaseURL, "/erc20-signatures.json");
56 return [4 /*yield*/, axios_1["default"]
57 .get(url)
58 .then(function (_a) {
59 var data = _a.data;
60 if (!data || typeof data !== "string") {
61 throw new Error("ERC20 signatures file is malformed ".concat(url));
62 }
63 return data;
64 })["catch"](function (e) {
65 (0, logs_1.log)("error", "could not fetch from " + url + ": " + String(e));
66 return null;
67 })];
68 case 1:
69 blob = _a.sent();
70 return [2 /*return*/, blob];
71 }
72 });
73}); };
74exports.findERC20SignaturesInfo = findERC20SignaturesInfo;
75/**
76 * Retrieve the token information by a given contract address if any
77 */
78var byContractAddressAndChainId = function (contract, chainId, erc20SignaturesBlob) {
79 // If we are able to fetch data from s3 bucket that contains dynamic CAL
80 if (erc20SignaturesBlob) {
81 try {
82 return parse(erc20SignaturesBlob).byContractAndChainId(asContractAddress(contract), chainId);
83 }
84 catch (e) {
85 return get().byContractAndChainId(asContractAddress(contract), chainId);
86 }
87 }
88 // the static fallback when dynamic cal is not provided
89 return get().byContractAndChainId(asContractAddress(contract), chainId);
90};
91exports.byContractAddressAndChainId = byContractAddressAndChainId;
92/**
93 * list all the ERC20 tokens informations
94 */
95var list = function (erc20SignaturesBlob) {
96 // If we are able to fetch data from s3 bucket that contains dynamic CAL
97 if (erc20SignaturesBlob) {
98 try {
99 return parse(erc20SignaturesBlob).list();
100 }
101 catch (e) {
102 return get().list();
103 }
104 }
105 // the static fallback when dynamic cal is not provided
106 return get().list();
107};
108exports.list = list;
109var asContractAddress = function (addr) {
110 var a = addr.toLowerCase();
111 return a.startsWith("0x") ? a : "0x" + a;
112};
113var parse = function (erc20SignaturesBlob) {
114 var buf = Buffer.from(erc20SignaturesBlob, "base64");
115 var map = {};
116 var entries = [];
117 var i = 0;
118 while (i < buf.length) {
119 var length_1 = buf.readUInt32BE(i);
120 i += 4;
121 var item = buf.slice(i, i + length_1);
122 var j = 0;
123 var tickerLength = item.readUInt8(j);
124 j += 1;
125 var ticker = item.slice(j, j + tickerLength).toString("ascii");
126 j += tickerLength;
127 var contractAddress = asContractAddress(item.slice(j, j + 20).toString("hex"));
128 j += 20;
129 var decimals = item.readUInt32BE(j);
130 j += 4;
131 var chainId = item.readUInt32BE(j);
132 j += 4;
133 var signature = item.slice(j);
134 var entry = {
135 ticker: ticker,
136 contractAddress: contractAddress,
137 decimals: decimals,
138 chainId: chainId,
139 signature: signature,
140 data: item
141 };
142 entries.push(entry);
143 map[String(chainId) + ":" + contractAddress] = entry;
144 i += length_1;
145 }
146 var api = {
147 list: function () { return entries; },
148 byContractAndChainId: function (contractAddress, chainId) {
149 return map[String(chainId) + ":" + contractAddress];
150 }
151 };
152 return api;
153};
154// this internal get() will lazy load and cache the data from the erc20 data blob
155var get = (function () {
156 var cache;
157 return function () {
158 if (cache)
159 return cache;
160 var api = parse(erc20_signatures_1["default"]);
161 cache = api;
162 return api;
163 };
164})();
165//# sourceMappingURL=erc20.js.map
\No newline at end of file