UNPKG

@polkadot/api

Version:

Promise and RxJS wrappers around the Polkadot JS RPC

62 lines (61 loc) 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiBase = void 0; const util_1 = require("@polkadot/util"); const Getters_js_1 = require("./Getters.js"); class ApiBase extends Getters_js_1.Getters { /** * @description Create an instance of the class * * @param options Options object to create API instance or a Provider instance * * @example * <BR> * * ```javascript * import Api from '@polkadot/api/promise'; * * const api = new Api().isReady(); * * api.rpc.subscribeNewHeads((header) => { * console.log(`new block #${header.number.toNumber()}`); * }); * ``` */ constructor(options = {}, type, decorateMethod) { super(options, type, decorateMethod); } /** * @description Connect from the underlying provider, halting all network traffic */ connect() { return this._rpcCore.connect(); } /** * @description Disconnect from the underlying provider, halting all network traffic */ disconnect() { this._unsubscribe(); return this._rpcCore.disconnect(); } /** * @description Set an external signer which will be used to sign extrinsic when account passed in is not KeyringPair */ setSigner(signer) { this._rx.signer = signer; } /** * @description Signs a raw signer payload, string or Uint8Array */ async sign(address, data, { signer } = {}) { if ((0, util_1.isString)(address)) { const _signer = signer || this._rx.signer; if (!_signer?.signRaw) { throw new Error('No signer exists with a signRaw interface. You possibly need to pass through an explicit keypair for the origin so it can be used for signing.'); } return (await _signer.signRaw((0, util_1.objectSpread)({ type: 'bytes' }, data, { address }))).signature; } return (0, util_1.u8aToHex)(address.sign((0, util_1.u8aToU8a)(data.data))); } } exports.ApiBase = ApiBase;