UNPKG

@aeternity/aepp-sdk

Version:
244 lines (211 loc) 8.62 kB
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator"; import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat"; import _Object$assign from "@babel/runtime-corejs3/core-js-stable/object/assign"; import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce"; import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter"; /* * ISC License (ISC) * Copyright (c) 2021 aeternity developers * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ /** * Node module * @module @aeternity/aepp-sdk/es/node * @export Node * @example import { Node } from '@aeternity/aepp-sdk' */ import AsyncInit from './utils/async-init'; import genSwaggerClient from './utils/swagger'; import semverSatisfies from './utils/semver-satisfies'; /** * Obtain networkId from account or node * @instance * @category async * @rtype () => networkId: String * @return {String} NetworkId */ export function getNetworkId() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, networkId = _ref.networkId, _ref$force = _ref.force, force = _ref$force === void 0 ? false : _ref$force; if (!force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) throw new Error('networkId is not provided'); if (force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) return null; return networkId || this.networkId || this.selectedNode.networkId; } /** * get consensus protocol version * @param {Array} protocols Array of protocols * @param {Number} height Height * @return {Number} version Protocol version */ function getConsensusProtocolVersion() { return _getConsensusProtocolVersion.apply(this, arguments); } /** * {@link genSwaggerClient} based Node remote API Stamp * @function * @alias module:@aeternity/aepp-sdk/es/node * @rtype Stamp * @param {Object} [options={}] - Options * @param {String} options.url - Base URL for Node * @param {String} options.internalUrl - Base URL for internal requests * @param {String} options.axiosConfig - Object with axios configuration. Example { config: {}, errorHandler: (err) => throw err } * @return {Object} Node client * @example Node({url: 'https://testnet.aeternity.io'}) */ function _getConsensusProtocolVersion() { _getConsensusProtocolVersion = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { var _context3; var protocols, height, _args2 = arguments; return _regeneratorRuntime.wrap(function _callee2$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: protocols = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : []; height = _args2.length > 1 ? _args2[1] : undefined; if (protocols) { _context4.next = 4; break; } throw new Error('Protocols must be an array'); case 4: if (height) { _context4.next = 8; break; } _context4.next = 7; return this.api.getCurrentKeyBlock(); case 7: height = _context4.sent.height; case 8: if (!(height < 0)) { _context4.next = 10; break; } throw new Error('height must be a number >= 0'); case 10: return _context4.abrupt("return", _reduceInstanceProperty(_context3 = _filterInstanceProperty(protocols).call(protocols, function (_ref3) { var effectiveAtHeight = _ref3.effectiveAtHeight; return height >= effectiveAtHeight; })).call(_context3, function (acc, p) { return p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc; }, { effectiveAtHeight: -1, version: 0 }).version); case 11: case "end": return _context4.stop(); } } }, _callee2, this); })); return _getConsensusProtocolVersion.apply(this, arguments); } var Node = AsyncInit.compose({ init: function init(_ref2) { var _this = this; return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var url, internalUrl, ignoreVersion, client, _context, _yield$_this$api$getS, revision, genesisHash, networkId, protocols; return _regeneratorRuntime.wrap(function _callee$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: url = _ref2.url, internalUrl = _ref2.internalUrl, ignoreVersion = _ref2.ignoreVersion; if (url) { _context2.next = 3; break; } throw new Error('"url" required'); case 3: _this.url = url.replace(/\/$/, ''); _this.internalUrl = internalUrl ? internalUrl.replace(/\/$/, '') : _this.url; _context2.next = 7; return genSwaggerClient("".concat(_this.url, "/api"), { internalUrl: _this.internalUrl }); case 7: client = _context2.sent; _this.version = client.spec.info.version; if (!(!semverSatisfies(_this.version, NODE_GE_VERSION, NODE_LT_VERSION) && !ignoreVersion)) { _context2.next = 11; break; } throw new Error("Unsupported node version ".concat(_this.version, ". ") + _concatInstanceProperty(_context = "Supported: >= ".concat(NODE_GE_VERSION, " < ")).call(_context, NODE_LT_VERSION)); case 11: if (!semverSatisfies(_this.version, IRIS_NODE_GE_VERSION, NODE_LT_VERSION)) { _context2.next = 16; break; } _context2.next = 14; return genSwaggerClient("".concat(_this.url, "/api?oas3"), { internalUrl: _this.internalUrl }); case 14: client = _context2.sent; _this._isIrisNode = true; case 16: _this.api = client.api; _context2.next = 19; return _this.api.getStatus(); case 19: _yield$_this$api$getS = _context2.sent; revision = _yield$_this$api$getS.nodeRevision; genesisHash = _yield$_this$api$getS.genesisKeyBlockHash; networkId = _yield$_this$api$getS.networkId; protocols = _yield$_this$api$getS.protocols; _context2.next = 26; return _this.getConsensusProtocolVersion(protocols); case 26: _this.consensusProtocolVersion = _context2.sent; _this.nodeNetworkId = networkId; return _context2.abrupt("return", _Object$assign(_this, { revision: revision, genesisHash: genesisHash })); case 29: case "end": return _context2.stop(); } } }, _callee); }))(); }, methods: { getNodeInfo: function getNodeInfo() { return { url: this.url, internalUrl: this.internalUrl, nodeNetworkId: this.nodeNetworkId, version: this.version, consensusProtocolVersion: this.consensusProtocolVersion }; }, getConsensusProtocolVersion: getConsensusProtocolVersion }, props: { version: null, consensusProtocolVersion: null, nodeNetworkId: null } }); var NODE_GE_VERSION = '5.2.0'; var IRIS_NODE_GE_VERSION = '6.0.0'; var NODE_LT_VERSION = '7.0.0'; export default Node; //# sourceMappingURL=node.js.map