UNPKG

@aelf-react/core

Version:

Please visit the [parent](https://github.com/mason-hz/aelf-react) [aelf-react](https://github.com/mason-hz/aelf-react) [repository](https://github.com/mason-hz/aelf-react) for documentation and details on this packge.

75 lines (74 loc) 3.07 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const aelf_bridge_1 = __importDefault(require("aelf-bridge")); let aelfBridgeInstance = null; let aelfInstanceByBridge = null; let accountInfo = null; class AelfBridgeCheck { constructor() { this.check = () => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const bridgeInstance = new aelf_bridge_1.default({ timeout: 3000, }); bridgeInstance.connect().then((isConnected) => { if (isConnected) { resolve(true); } else { reject({ error: 200001, message: 'timeout, please use AELF Wallet APP or open the page in PC', }); } }); setTimeout(() => { reject({ error: 200001, message: 'timeout, please use AELF Wallet APP or open the page in PC', }); }, 3000); }); }); } static getInstance() { if (aelfBridgeInstance) return aelfBridgeInstance; aelfBridgeInstance = new AelfBridgeCheck(); return aelfBridgeInstance; } // eslint-disable-next-line @typescript-eslint/no-unused-vars static initAelfInstanceByExtension(rpcUrl, appName) { aelfInstanceByBridge = new aelf_bridge_1.default({ endpoint: rpcUrl, }); aelfInstanceByBridge.login = () => __awaiter(this, void 0, void 0, function* () { if (accountInfo) { return accountInfo; } const account = yield aelfInstanceByBridge.account(); accountInfo = { detail: JSON.stringify(account.accounts[0]), }; return accountInfo; }); aelfInstanceByBridge.logout = (param, callback) => { accountInfo = null; callback(); }; return aelfInstanceByBridge; } } exports.default = AelfBridgeCheck;