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