UNPKG

@npmjs_tdsoftware/subidentity

Version:

This package provides functionality to fetch identities and search identities by address or any identity field from any Substrate chain implementing the identities pallet. It was developed for use in SubIdentity, a Substrate identity directory, and contai

112 lines (111 loc) 4.79 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getChainStatus = exports.getTokenDetails = exports.getChainName = exports.isArchiveNode = exports.connectToWsProvider = exports.apiPromises = void 0; const api_1 = require("@polkadot/api"); const identities_1 = require("./identities"); exports.apiPromises = {}; /** * connect to wsProvider * @param wsAddress Network endpoint URL * @returns ApiPromise instance using the supplied provider * @throws Error when the initial connection fails */ function connectToWsProvider(wsAddress) { return __awaiter(this, void 0, void 0, function* () { if (exports.apiPromises[wsAddress]) { if (yield exports.apiPromises[wsAddress].isConnected) return exports.apiPromises[wsAddress]; else { exports.apiPromises[wsAddress].disconnect(); delete exports.apiPromises[wsAddress]; } } const wsProvider = new api_1.WsProvider(wsAddress); const apiPromise = new api_1.ApiPromise({ provider: wsProvider }); try { yield apiPromise.isReadyOrError; exports.apiPromises[wsAddress] = apiPromise; return apiPromise; } catch (error) { //disconnect to prevent connection retries apiPromise.disconnect(); throw new Error("Could not connect to endpoint."); } }); } exports.connectToWsProvider = connectToWsProvider; /** * check if the node of provided wsProvider is running on archive mode * @param wsAddress Network end point URL * @returns true if node is running in archive mode */ const isArchiveNode = (wsAddress) => __awaiter(void 0, void 0, void 0, function* () { const api = yield connectToWsProvider(wsAddress); const historyBlockHash = yield api.rpc.chain.getBlockHash(1); //Hash of Block#1 //Archive nodes have all the historical data of the blockchain since the genesis block try { //Query state of a history block yield api.at(historyBlockHash); } catch (ex) { return false; } return true; }); exports.isArchiveNode = isArchiveNode; /** * fetch chain name from a selected substrate based chain * @param wsAddress Network end point URL * @returns name of the requested chain */ const getChainName = (wsAddress) => __awaiter(void 0, void 0, void 0, function* () { const api = yield connectToWsProvider(wsAddress); return (yield api.rpc.system.chain()).toString(); }); exports.getChainName = getChainName; /** * fetch Token details of a selected substrate based chain * @param wsAddress Network end point URL * @returns token symbol and decimals of the requested chain */ const getTokenDetails = (wsAddress) => __awaiter(void 0, void 0, void 0, function* () { const api = yield connectToWsProvider(wsAddress); let symbol, decimals; const properties = (yield api.rpc.system.properties()); if (properties) { const { tokenSymbol, tokenDecimals } = properties.toHuman(); if (tokenSymbol && Array.isArray(tokenSymbol) && tokenSymbol.length > 0) symbol = tokenSymbol.shift(); if (tokenDecimals && Array.isArray(tokenDecimals) && tokenDecimals.length > 0) decimals = Number(tokenDecimals.shift()); } return { symbol, decimals }; }); exports.getTokenDetails = getTokenDetails; //TODO add unit test for getChainStatus /** * fetch chain status from a selected substrate based chain * @param wsAddress Network end point URL * @returns chainStatus of the requested chain */ const getChainStatus = (wsAddress) => __awaiter(void 0, void 0, void 0, function* () { const api = yield connectToWsProvider(wsAddress); const token = yield (0, exports.getTokenDetails)(wsAddress); return { implementsIdentityPallet: yield (0, identities_1.implementsIdentityPallet)(wsAddress), chainName: yield (0, exports.getChainName)(wsAddress), tokenDecimals: token.decimals, tokenSymbol: token.symbol }; }); exports.getChainStatus = getChainStatus;