UNPKG

2.37 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.ApiBase = void 0;
7
8var _util = require("@polkadot/util");
9
10var _Getters = require("./Getters");
11
12// Copyright 2017-2022 @polkadot/api authors & contributors
13// SPDX-License-Identifier: Apache-2.0
14class ApiBase extends _Getters.Getters {
15 /**
16 * @description Create an instance of the class
17 *
18 * @param options Options object to create API instance or a Provider instance
19 *
20 * @example
21 * <BR>
22 *
23 * ```javascript
24 * import Api from '@polkadot/api/promise';
25 *
26 * const api = new Api().isReady();
27 *
28 * api.rpc.subscribeNewHeads((header) => {
29 * console.log(`new block #${header.number.toNumber()}`);
30 * });
31 * ```
32 */
33 constructor() {
34 let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
35 let type = arguments.length > 1 ? arguments[1] : undefined;
36 let decorateMethod = arguments.length > 2 ? arguments[2] : undefined;
37 super(options, type, decorateMethod);
38 }
39 /**
40 * @description Connect from the underlying provider, halting all network traffic
41 */
42
43
44 connect() {
45 return this._rpcCore.connect();
46 }
47 /**
48 * @description Disconnect from the underlying provider, halting all network traffic
49 */
50
51
52 disconnect() {
53 this._unsubscribe();
54
55 return this._rpcCore.disconnect();
56 }
57 /**
58 * @description Set an external signer which will be used to sign extrinsic when account passed in is not KeyringPair
59 */
60
61
62 setSigner(signer) {
63 this._rx.signer = signer;
64 }
65 /**
66 * @description Signs a raw signer payload, string or Uint8Array
67 */
68
69
70 async sign(address, data) {
71 let {
72 signer
73 } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
74
75 if ((0, _util.isString)(address)) {
76 const _signer = signer || this._rx.signer;
77
78 (0, _util.assert)(_signer === null || _signer === void 0 ? void 0 : _signer.signRaw, '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.');
79 return (await _signer.signRaw((0, _util.objectSpread)({
80 type: 'bytes'
81 }, data, {
82 address
83 }))).signature;
84 }
85
86 return (0, _util.u8aToHex)(address.sign((0, _util.u8aToU8a)(data.data)));
87 }
88
89}
90
91exports.ApiBase = ApiBase;
\No newline at end of file