UNPKG

2.08 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ApiBase = void 0;
4const util_1 = require("@polkadot/util");
5const Getters_js_1 = require("./Getters.js");
6class ApiBase extends Getters_js_1.Getters {
7 /**
8 * @description Create an instance of the class
9 *
10 * @param options Options object to create API instance or a Provider instance
11 *
12 * @example
13 * <BR>
14 *
15 * ```javascript
16 * import Api from '@polkadot/api/promise';
17 *
18 * const api = new Api().isReady();
19 *
20 * api.rpc.subscribeNewHeads((header) => {
21 * console.log(`new block #${header.number.toNumber()}`);
22 * });
23 * ```
24 */
25 constructor(options = {}, type, decorateMethod) {
26 super(options, type, decorateMethod);
27 }
28 /**
29 * @description Connect from the underlying provider, halting all network traffic
30 */
31 connect() {
32 return this._rpcCore.connect();
33 }
34 /**
35 * @description Disconnect from the underlying provider, halting all network traffic
36 */
37 disconnect() {
38 this._unsubscribe();
39 return this._rpcCore.disconnect();
40 }
41 /**
42 * @description Set an external signer which will be used to sign extrinsic when account passed in is not KeyringPair
43 */
44 setSigner(signer) {
45 this._rx.signer = signer;
46 }
47 /**
48 * @description Signs a raw signer payload, string or Uint8Array
49 */
50 async sign(address, data, { signer } = {}) {
51 if ((0, util_1.isString)(address)) {
52 const _signer = signer || this._rx.signer;
53 if (!_signer?.signRaw) {
54 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.');
55 }
56 return (await _signer.signRaw((0, util_1.objectSpread)({ type: 'bytes' }, data, { address }))).signature;
57 }
58 return (0, util_1.u8aToHex)(address.sign((0, util_1.u8aToU8a)(data.data)));
59 }
60}
61exports.ApiBase = ApiBase;