|
| 1 | +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction |
| 2 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 3 | + |
| 4 | +import path from "path"; |
| 5 | +import { promises as fs } from "fs"; |
| 6 | +import { ChainInfo } from "@keplr-wallet/types"; |
| 7 | +import Cors from "cors"; |
| 8 | +import { |
| 9 | + nativeMainnetChainIdentifiers, |
| 10 | + nativeTestnetChainIdentifiers, |
| 11 | +} from "../../src/constants"; |
| 12 | +import { ChainIdHelper } from "@keplr-wallet/cosmos"; |
| 13 | + |
| 14 | +type Data = { |
| 15 | + mainnetChains: ChainInfo[]; |
| 16 | + testnetChains: ChainInfo[]; |
| 17 | +}; |
| 18 | + |
| 19 | +const cors = Cors({ |
| 20 | + methods: ["GET"], |
| 21 | +}); |
| 22 | + |
| 23 | +export default async function ( |
| 24 | + req: NextApiRequest, |
| 25 | + res: NextApiResponse<Data>, |
| 26 | +) { |
| 27 | + await new Promise((resolve, reject) => { |
| 28 | + cors(req, res, (result: any) => { |
| 29 | + if (result instanceof Error) { |
| 30 | + return reject(result); |
| 31 | + } |
| 32 | + |
| 33 | + return resolve(result); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + const cosmosChainsDirectory = path.join(process.cwd(), "cosmos"); |
| 38 | + const cosmosChains = (await fs.readdir(cosmosChainsDirectory)).map( |
| 39 | + (fileName) => fs.readFile(`${cosmosChainsDirectory}/${fileName}`, "utf8"), |
| 40 | + ); |
| 41 | + const cosmosChainInfos: ChainInfo[] = (await Promise.all(cosmosChains)).map( |
| 42 | + (chainInfo) => JSON.parse(chainInfo), |
| 43 | + ); |
| 44 | + |
| 45 | + const evmChainsDirectory = path.join(process.cwd(), "evm"); |
| 46 | + const evmChains = await Promise.all( |
| 47 | + ( |
| 48 | + await fs.readdir(evmChainsDirectory) |
| 49 | + ).map((fileName) => |
| 50 | + fs.readFile(`${evmChainsDirectory}/${fileName}`, "utf8"), |
| 51 | + ), |
| 52 | + ); |
| 53 | + const evmChainInfos: ChainInfo[] = (await Promise.all(evmChains)).map( |
| 54 | + (chainInfo) => { |
| 55 | + const evmChainInfo = JSON.parse(chainInfo); |
| 56 | + const evmChainId = parseInt(evmChainInfo.chainId.replace("eip155:", "")); |
| 57 | + const { websocket, features, ...restEVMChainInfo } = evmChainInfo; |
| 58 | + return { |
| 59 | + ...restEVMChainInfo, |
| 60 | + rest: evmChainInfo.rpc, |
| 61 | + evm: { |
| 62 | + chainId: evmChainId, |
| 63 | + rpc: evmChainInfo.rpc, |
| 64 | + websocket, |
| 65 | + }, |
| 66 | + features: ["eth-address-gen", "eth-key-sign"].concat(features ?? []), |
| 67 | + }; |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + const allChainInfos = cosmosChainInfos.concat(evmChainInfos); |
| 72 | + |
| 73 | + //Return the content of the data file in json format |
| 74 | + res.status(200).json({ |
| 75 | + mainnetChains: allChainInfos.filter((chainInfo) => |
| 76 | + nativeMainnetChainIdentifiers.includes( |
| 77 | + ChainIdHelper.parse(chainInfo.chainId).identifier, |
| 78 | + ), |
| 79 | + ), |
| 80 | + testnetChains: allChainInfos.filter((chainInfo) => |
| 81 | + nativeTestnetChainIdentifiers.includes( |
| 82 | + ChainIdHelper.parse(chainInfo.chainId).identifier, |
| 83 | + ), |
| 84 | + ), |
| 85 | + }); |
| 86 | +} |
0 commit comments