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.

94 lines (93 loc) 4.56 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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.getBridges = exports.getConnector = exports.getSignatureByBridge = exports.formatLoginInfo = void 0; const isMobile_1 = require("./isMobile"); const toStr = (x, y) => '04' + (x === null || x === void 0 ? void 0 : x.padStart(64, '0')) + (y === null || y === void 0 ? void 0 : y.padStart(64, '0')); const formatPubKey = (publicKey) => { try { const { x, y } = (typeof publicKey === 'object' ? publicKey : JSON.parse(publicKey)) || {}; return toStr(x, y); } catch (e) { return publicKey; } }; const formatLoginInfo = (loginInfo) => { const detail = JSON.parse(loginInfo); const account = detail.address; const pubKey = formatPubKey(detail.publicKey); delete detail.address; return Object.assign(Object.assign({}, detail), { account, pubKey }); }; exports.formatLoginInfo = formatLoginInfo; const getSignatureByBridge = (aelfBridge, account, hexToBeSign) => __awaiter(void 0, void 0, void 0, function* () { if (aelfBridge.connected !== undefined) { const sign = yield aelfBridge.sendMessage('keyPairUtils', { method: 'sign', arguments: [hexToBeSign] }); if (sign === null || sign === void 0 ? void 0 : sign.error) throw sign; return [sign.r.toString(16, 64), sign.s.toString(16, 64), `0${sign.recoveryParam.toString()}`].join(''); } else { const sign = yield aelfBridge.getSignature({ address: account, hexToBeSign }); if (sign === null || sign === void 0 ? void 0 : sign.error) throw sign; return sign === null || sign === void 0 ? void 0 : sign.signature; } }); exports.getSignatureByBridge = getSignatureByBridge; const getConnector = () => __awaiter(void 0, void 0, void 0, function* () { // @ts-ignore const isAElfBridge = (0, isMobile_1.isMobileDevices)() && !(window === null || window === void 0 ? void 0 : window.NightElf); const connector = (yield (isAElfBridge ? Promise.resolve().then(() => __importStar(require('./NightElf/AelfBridgeCheck'))) : Promise.resolve().then(() => __importStar(require('./NightElf/NightElfCheck'))))) .default; return connector; }); exports.getConnector = getConnector; const getBridges = (nodes, appName) => __awaiter(void 0, void 0, void 0, function* () { const connector = yield (0, exports.getConnector)(); // check connector yield connector.getInstance().check(); let firstKey = ''; const bridges = {}; Object.entries(nodes).forEach(([k, v]) => { if (!firstKey) firstKey = k; bridges[k] = connector.initAelfInstanceByExtension(v.rpcUrl, appName); }); const node = nodes[firstKey]; const bridge = bridges[firstKey]; return { bridge, node, bridges }; }); exports.getBridges = getBridges;