Skip to content

Commit

Permalink
Merge pull request #415 from GridPlus/dl/chain-json
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance authored Jun 22, 2022
2 parents bf36e80 + 706bc72 commit 58b8de5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,22 +528,29 @@ function getFwVersionConst (v) {
// eslint-disable-next-line no-control-regex
const ASCII_REGEX = /^[\x00-\x7F]+$/;

const EXTERNAL_NETWORKS_BY_CHAIN_ID_URL =
'https://gridplus.github.io/chains/chains.json';

const NETWORKS_BY_CHAIN_ID = {
1: {
name: 'ethereum',
baseUrl: 'https://api.etherscan.io',
apiRoute: 'api?module=contract&action=getabi',
},
137: {
name: 'polygon',
baseUrl: 'https://api.polygonscan.com',
apiRoute: 'api?module=contract&action=getabi',
},
56: {
name: 'binance',
baseUrl: 'https://api.bscscan.com',
apiRoute: 'api?module=contract&action=getabi',
},
43114: {
name: 'avalanche',
baseUrl: 'https://api.snowtrace.io',
apiRoute: 'api?module=contract&action=getabi',
},
};

Expand All @@ -555,6 +562,7 @@ export {
BIP_CONSTANTS,
BASE_URL,
NETWORKS_BY_CHAIN_ID,
EXTERNAL_NETWORKS_BY_CHAIN_ID_URL,
ENC_MSG_LEN,
addressSizes,
decResLengths,
Expand Down
50 changes: 46 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
HARDENED_OFFSET,
responseCodes,
responseMsgs,
VERSION_BYTE
VERSION_BYTE,
EXTERNAL_NETWORKS_BY_CHAIN_ID_URL,
} from './constants';
const { COINS, PURPOSES } = BIP_CONSTANTS;
const EC = elliptic.ec;
Expand Down Expand Up @@ -409,6 +410,45 @@ export const getV = function (tx, resp) {
return chainId.muln(2).addn(35).addn(recovery);
}

/**
* Fetches an external JSON file containing networks indexed by chain id from a GridPlus repo, and
* returns the parsed JSON.
*/
async function fetchExternalNetworkForChainId (
chainId: number | string,
): Promise<{
[key: string]: {
name: string;
baseUrl: string;
apiRoute: string;
};
}> {
try {
const response = await superagent.get(EXTERNAL_NETWORKS_BY_CHAIN_ID_URL);
if (response && response.body) {
return response.body[chainId];
} else {
return undefined;
}
} catch (err) {
console.warn('Fetching external networks failed.\n', err);
}
}

/**
* Builds a URL for fetching calldata from block explorers for any supported chains
* */
function buildUrlForSupportedChainAndAddress ({ supportedChain, address }) {
const baseUrl = supportedChain.baseUrl;
const apiRoute = supportedChain.apiRoute;
const urlWithRoute = `${baseUrl}/${apiRoute}&address=${address}`;

const apiKey = process.env.ETHERSCAN_KEY;
const apiKeyParam = apiKey ? `&apiKey=${process.env.ETHERSCAN_KEY}` : '';

return urlWithRoute + apiKeyParam
}

/**
* Fetches calldata from a remote scanner based on the transaction's `chainId`
*/
Expand All @@ -432,10 +472,12 @@ export async function fetchCalldataDecoder (_data: Uint8Array | string, to: stri
// Convert the chainId to a number and use it to determine if we can call out to
// an etherscan-like explorer for richer data.
const chainId = Number(_chainId);
const supportedChain = NETWORKS_BY_CHAIN_ID[chainId];
const cachedNetwork = NETWORKS_BY_CHAIN_ID[chainId];
const supportedChain = cachedNetwork
? cachedNetwork
: await fetchExternalNetworkForChainId(chainId);
if (supportedChain) {
const baseUrl = supportedChain.baseUrl;
const url = `${baseUrl}/api?module=contract&action=getabi&address=${to}&apiKey=${process.env.ETHERSCAN_KEY}`
const url = buildUrlForSupportedChainAndAddress({ supportedChain, address: to })
const response = await superagent
.get(url)
.catch(err => {
Expand Down

0 comments on commit 58b8de5

Please sign in to comment.