@aeternity/aepp-sdk 10.0.0 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -14
- package/dist/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -27
- package/dist/aepp-sdk.browser-script.js.map +1 -1
- package/dist/aepp-sdk.browser.js +1 -1
- package/dist/aepp-sdk.browser.js.map +1 -1
- package/dist/aepp-sdk.js +1 -1
- package/dist/aepp-sdk.js.map +1 -1
- package/es/account/{base.js → base.mjs} +20 -10
- package/es/account/base.mjs.map +1 -0
- package/es/account/{memory.js → memory.mjs} +29 -14
- package/es/account/memory.mjs.map +1 -0
- package/es/account/{multiple.js → multiple.mjs} +10 -9
- package/es/account/multiple.mjs.map +1 -0
- package/es/ae/{aens.js → aens.mjs} +18 -22
- package/es/ae/aens.mjs.map +1 -0
- package/es/ae/{aepp.js → aepp.mjs} +9 -9
- package/es/ae/aepp.mjs.map +1 -0
- package/es/ae/contract.mjs +159 -0
- package/es/ae/contract.mjs.map +1 -0
- package/es/ae/{index.js → index.mjs} +8 -7
- package/es/ae/index.mjs.map +1 -0
- package/es/ae/{oracle.js → oracle.mjs} +33 -16
- package/es/ae/oracle.mjs.map +1 -0
- package/es/ae/{universal.js → universal.mjs} +10 -10
- package/es/ae/universal.mjs.map +1 -0
- package/es/ae/{wallet.js → wallet.mjs} +10 -10
- package/es/ae/wallet.mjs.map +1 -0
- package/es/aepp-wallet-communication/schema.d.ts +74 -0
- package/es/bignumber.d.ts +1 -7
- package/es/chain/{index.js → index.mjs} +20 -4
- package/es/chain/index.mjs.map +1 -0
- package/es/chain/{node.js → node.mjs} +121 -73
- package/es/chain/node.mjs.map +1 -0
- package/es/channel/{handlers.js → handlers.mjs} +18 -20
- package/es/channel/handlers.mjs.map +1 -0
- package/es/channel/{index.js → index.mjs} +7 -7
- package/es/channel/index.mjs.map +1 -0
- package/es/channel/{internal.js → internal.mjs} +49 -32
- package/es/channel/internal.mjs.map +1 -0
- package/es/contract/aci.mjs +477 -0
- package/es/contract/aci.mjs.map +1 -0
- package/es/contract/compiler.mjs +115 -0
- package/es/contract/compiler.mjs.map +1 -0
- package/es/contract/ga/helpers.mjs +30 -0
- package/es/contract/ga/helpers.mjs.map +1 -0
- package/es/contract/ga/{index.js → index.mjs} +36 -28
- package/es/contract/ga/index.mjs.map +1 -0
- package/es/errors.d.ts +284 -0
- package/es/index.mjs +57 -0
- package/es/index.mjs.map +1 -0
- package/es/node-pool/{index.js → index.mjs} +11 -10
- package/es/node-pool/index.mjs.map +1 -0
- package/es/{node.js → node.mjs} +9 -8
- package/es/node.mjs.map +1 -0
- package/es/tx/builder/field-types.mjs +74 -0
- package/es/tx/builder/field-types.mjs.map +1 -0
- package/es/tx/builder/{helpers.js → helpers.mjs} +114 -56
- package/es/tx/builder/helpers.mjs.map +1 -0
- package/es/tx/builder/{index.js → index.mjs} +79 -68
- package/es/tx/builder/index.mjs.map +1 -0
- package/es/tx/builder/{schema.js → schema.mjs} +11 -65
- package/es/tx/builder/schema.mjs.map +1 -0
- package/es/tx/{index.js → index.mjs} +5 -2
- package/es/tx/index.mjs.map +1 -0
- package/es/tx/{tx-object.js → tx-object.mjs} +22 -15
- package/es/tx/tx-object.mjs.map +1 -0
- package/es/tx/{tx.js → tx.mjs} +15 -14
- package/es/tx/tx.mjs.map +1 -0
- package/es/tx/{validator.js → validator.mjs} +72 -33
- package/es/tx/validator.mjs.map +1 -0
- package/es/typings/blakejs/index.d.mjs +2 -0
- package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
- package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +7 -6
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/{browser-window-message.js → browser-window-message.mjs} +10 -11
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +5 -2
- package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +6 -5
- package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{helpers.js → helpers.mjs} +3 -14
- package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{aepp-rpc.js → aepp-rpc.mjs} +113 -67
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{rpc-client.js → rpc-client.mjs} +14 -9
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{wallet-rpc.js → wallet-rpc.mjs} +71 -63
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
- package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{wallet-detector.js → wallet-detector.mjs} +6 -5
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
- package/es/utils/{amount-formatter.js → amount-formatter.mjs} +14 -7
- package/es/utils/amount-formatter.mjs.map +1 -0
- package/es/utils/{async-init.js → async-init.mjs} +8 -11
- package/es/utils/async-init.mjs.map +1 -0
- package/es/utils/{bignumber.js → bignumber.mjs} +2 -9
- package/es/utils/bignumber.mjs.map +1 -0
- package/es/utils/{bytes.js → bytes.mjs} +16 -8
- package/es/utils/bytes.mjs.map +1 -0
- package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
- package/es/utils/crypto-ts.mjs.map +1 -0
- package/es/utils/{crypto.js → crypto.mjs} +40 -105
- package/es/utils/crypto.mjs.map +1 -0
- package/es/utils/errors.mjs +689 -0
- package/es/utils/errors.mjs.map +1 -0
- package/es/utils/{hd-wallet.js → hd-wallet.mjs} +33 -34
- package/es/utils/hd-wallet.mjs.map +1 -0
- package/es/utils/{json-big.js → json-big.mjs} +1 -1
- package/es/utils/{json-big.js.map → json-big.mjs.map} +1 -1
- package/es/utils/{keystore.js → keystore.mjs} +63 -51
- package/es/utils/keystore.mjs.map +1 -0
- package/es/utils/{mptree.js → mptree.mjs} +21 -12
- package/es/utils/mptree.mjs.map +1 -0
- package/es/utils/{other.js → other.mjs} +2 -2
- package/es/utils/other.mjs.map +1 -0
- package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
- package/es/utils/{semver-satisfies.js.map → semver-satisfies.mjs.map} +1 -1
- package/es/utils/{string.js → string.mjs} +1 -1
- package/es/utils/{string.js.map → string.mjs.map} +1 -1
- package/es/utils/{swagger.js → swagger.mjs} +46 -14
- package/es/utils/swagger.mjs.map +1 -0
- package/package.json +46 -55
- package/src/account/memory.js +13 -8
- package/src/account/multiple.js +12 -4
- package/src/ae/aens.js +12 -17
- package/src/ae/aepp.js +2 -2
- package/src/ae/contract.js +15 -152
- package/src/ae/index.js +2 -1
- package/src/ae/oracle.js +25 -10
- package/src/ae/universal.js +2 -2
- package/src/ae/wallet.js +2 -2
- package/src/chain/index.js +21 -2
- package/src/chain/node.js +80 -53
- package/src/channel/handlers.js +20 -17
- package/src/channel/internal.js +30 -24
- package/src/contract/{aci/index.js → aci.js} +152 -93
- package/src/contract/compiler.js +22 -117
- package/src/contract/ga/helpers.js +7 -5
- package/src/contract/ga/index.js +27 -24
- package/src/index.js +7 -8
- package/src/node-pool/index.js +9 -8
- package/src/node.js +5 -7
- package/src/tx/builder/field-types.js +61 -0
- package/src/tx/builder/helpers.js +113 -58
- package/src/tx/builder/index.js +39 -34
- package/src/tx/builder/schema.js +11 -25
- package/src/tx/tx-object.js +10 -9
- package/src/tx/tx.js +17 -8
- package/src/tx/validator.js +25 -5
- package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +8 -3
- package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +10 -6
- package/src/utils/aepp-wallet-communication/content-script-bridge.js +5 -4
- package/src/utils/aepp-wallet-communication/helpers.js +2 -12
- package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +81 -55
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +15 -6
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +60 -49
- package/src/utils/aepp-wallet-communication/schema.ts +80 -0
- package/src/utils/aepp-wallet-communication/wallet-detector.js +3 -2
- package/src/utils/amount-formatter.ts +4 -3
- package/src/utils/async-init.js +10 -6
- package/src/utils/bignumber.ts +1 -9
- package/src/utils/bytes.ts +3 -2
- package/src/utils/crypto-ts.ts +2 -2
- package/src/utils/crypto.js +9 -92
- package/src/utils/errors.ts +683 -0
- package/src/utils/hd-wallet.js +11 -26
- package/src/utils/keystore.js +47 -51
- package/src/utils/mptree.ts +12 -6
- package/src/utils/swagger.js +67 -44
- package/CHANGELOG.md +0 -1603
- package/es/account/base.js.map +0 -1
- package/es/account/memory.js.map +0 -1
- package/es/account/multiple.js.map +0 -1
- package/es/ae/aens.js.map +0 -1
- package/es/ae/aepp.js.map +0 -1
- package/es/ae/contract.js +0 -312
- package/es/ae/contract.js.map +0 -1
- package/es/ae/index.js.map +0 -1
- package/es/ae/oracle.js.map +0 -1
- package/es/ae/universal.js.map +0 -1
- package/es/ae/wallet.js.map +0 -1
- package/es/chain/index.js.map +0 -1
- package/es/chain/node.js.map +0 -1
- package/es/channel/handlers.js.map +0 -1
- package/es/channel/index.js.map +0 -1
- package/es/channel/internal.js.map +0 -1
- package/es/contract/aci/helpers.js +0 -298
- package/es/contract/aci/helpers.js.map +0 -1
- package/es/contract/aci/index.js +0 -361
- package/es/contract/aci/index.js.map +0 -1
- package/es/contract/aci/transformation.js +0 -66
- package/es/contract/aci/transformation.js.map +0 -1
- package/es/contract/compiler.js +0 -244
- package/es/contract/compiler.js.map +0 -1
- package/es/contract/ga/helpers.js +0 -22
- package/es/contract/ga/helpers.js.map +0 -1
- package/es/contract/ga/index.js.map +0 -1
- package/es/contract/index.js +0 -162
- package/es/contract/index.js.map +0 -1
- package/es/index.js +0 -57
- package/es/index.js.map +0 -1
- package/es/node-pool/index.js.map +0 -1
- package/es/node.js.map +0 -1
- package/es/tx/builder/helpers.js.map +0 -1
- package/es/tx/builder/index.js.map +0 -1
- package/es/tx/builder/schema.js.map +0 -1
- package/es/tx/index.js.map +0 -1
- package/es/tx/tx-object.js.map +0 -1
- package/es/tx/tx.js.map +0 -1
- package/es/tx/validator.js.map +0 -1
- package/es/typings/blakejs/index.d.js +0 -2
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
- package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
- package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/schema.js +0 -107
- package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
- package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
- package/es/utils/amount-formatter.js.map +0 -1
- package/es/utils/async-init.js.map +0 -1
- package/es/utils/bignumber.js.map +0 -1
- package/es/utils/bytes.js.map +0 -1
- package/es/utils/crypto-ts.js.map +0 -1
- package/es/utils/crypto.js.map +0 -1
- package/es/utils/hd-wallet.js.map +0 -1
- package/es/utils/keystore.js.map +0 -1
- package/es/utils/mptree.js.map +0 -1
- package/es/utils/other.js.map +0 -1
- package/es/utils/swagger.js.map +0 -1
- package/src/contract/aci/transformation.js +0 -72
- package/src/contract/index.js +0 -163
- package/src/utils/aepp-wallet-communication/schema.js +0 -108
|
@@ -7,22 +7,32 @@
|
|
|
7
7
|
* import ContentScriptBridge
|
|
8
8
|
* from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc'
|
|
9
9
|
*/
|
|
10
|
-
import { v4 as uuid } from 'uuid';
|
|
11
|
-
import Ae from
|
|
12
|
-
import RpcClient from
|
|
13
|
-
import { getHandler, message
|
|
14
|
-
import { METHODS, RPC_STATUS, VERSION } from
|
|
10
|
+
import { v4 as uuid } from '@aeternity/uuid';
|
|
11
|
+
import Ae from "../../../ae/index.mjs";
|
|
12
|
+
import RpcClient from "./rpc-client.mjs";
|
|
13
|
+
import { getHandler, message } from "../helpers.mjs";
|
|
14
|
+
import { METHODS, RPC_STATUS, VERSION } from "../schema.mjs";
|
|
15
|
+
import { AlreadyConnectedError, NoWalletConnectedError, UnsubscribedAccountError, UnAuthorizedAccountError, ArgumentError, RpcConnectionError } from "../../errors.mjs";
|
|
16
|
+
import { Node } from "../../../index.mjs";
|
|
15
17
|
const NOTIFICATIONS = {
|
|
16
|
-
[METHODS.
|
|
18
|
+
[METHODS.updateAddress]: instance => _ref => {
|
|
17
19
|
let {
|
|
18
20
|
params
|
|
19
21
|
} = _ref;
|
|
20
22
|
instance.rpcClient.accounts = params;
|
|
21
23
|
instance.onAddressChange(params);
|
|
22
24
|
},
|
|
23
|
-
[METHODS.updateNetwork]: instance =>
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
[METHODS.updateNetwork]: instance => async _ref2 => {
|
|
26
|
+
let {
|
|
27
|
+
params
|
|
28
|
+
} = _ref2;
|
|
29
|
+
const {
|
|
30
|
+
networkId,
|
|
31
|
+
node
|
|
32
|
+
} = params;
|
|
33
|
+
instance.rpcClient.info.networkId = networkId;
|
|
34
|
+
node && instance.addNode(node.name, await Node(node), true);
|
|
35
|
+
instance.onNetworkChange(params);
|
|
26
36
|
},
|
|
27
37
|
[METHODS.closeConnection]: instance => msg => {
|
|
28
38
|
instance.disconnectWallet();
|
|
@@ -30,12 +40,12 @@ const NOTIFICATIONS = {
|
|
|
30
40
|
}
|
|
31
41
|
};
|
|
32
42
|
const RESPONSES = {
|
|
33
|
-
[METHODS.
|
|
34
|
-
[METHODS.
|
|
43
|
+
[METHODS.address]: instance => msg => instance.rpcClient.processResponse(msg),
|
|
44
|
+
[METHODS.connect]: instance => msg => {
|
|
35
45
|
if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED;
|
|
36
46
|
instance.rpcClient.processResponse(msg);
|
|
37
47
|
},
|
|
38
|
-
[METHODS.
|
|
48
|
+
[METHODS.subscribeAddress]: instance => msg => {
|
|
39
49
|
if (msg.result) {
|
|
40
50
|
if (msg.result.address) {
|
|
41
51
|
instance.rpcClient.accounts = msg.result.address;
|
|
@@ -46,29 +56,29 @@ const RESPONSES = {
|
|
|
46
56
|
}
|
|
47
57
|
}
|
|
48
58
|
|
|
49
|
-
instance.rpcClient.processResponse(msg,
|
|
59
|
+
instance.rpcClient.processResponse(msg, _ref3 => {
|
|
50
60
|
let {
|
|
51
61
|
id,
|
|
52
62
|
result
|
|
53
|
-
} =
|
|
63
|
+
} = _ref3;
|
|
54
64
|
return [result];
|
|
55
65
|
});
|
|
56
66
|
},
|
|
57
|
-
[METHODS.
|
|
58
|
-
instance.rpcClient.processResponse(msg,
|
|
67
|
+
[METHODS.sign]: instance => msg => {
|
|
68
|
+
instance.rpcClient.processResponse(msg, _ref4 => {
|
|
59
69
|
let {
|
|
60
70
|
id,
|
|
61
71
|
result
|
|
62
|
-
} =
|
|
72
|
+
} = _ref4;
|
|
63
73
|
return [result.signedTransaction || result.transactionHash];
|
|
64
74
|
});
|
|
65
75
|
},
|
|
66
|
-
[METHODS.
|
|
67
|
-
instance.rpcClient.processResponse(msg,
|
|
76
|
+
[METHODS.signMessage]: instance => msg => {
|
|
77
|
+
instance.rpcClient.processResponse(msg, _ref5 => {
|
|
68
78
|
let {
|
|
69
79
|
id,
|
|
70
80
|
result
|
|
71
|
-
} =
|
|
81
|
+
} = _ref5;
|
|
72
82
|
return [result.signature];
|
|
73
83
|
});
|
|
74
84
|
}
|
|
@@ -106,17 +116,20 @@ const handleMessage = instance => async msg => {
|
|
|
106
116
|
|
|
107
117
|
|
|
108
118
|
export default Ae.compose({
|
|
109
|
-
async init(
|
|
119
|
+
async init(_ref6) {
|
|
110
120
|
let {
|
|
111
121
|
name,
|
|
112
|
-
onAddressChange = voidFn,
|
|
113
|
-
onDisconnect = voidFn,
|
|
114
|
-
onNetworkChange = voidFn,
|
|
115
122
|
connection,
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
} =
|
|
119
|
-
|
|
123
|
+
debug = false,
|
|
124
|
+
...other
|
|
125
|
+
} = _ref6;
|
|
126
|
+
['onAddressChange', 'onDisconnect', 'onNetworkChange'].forEach(event => {
|
|
127
|
+
var _other$event;
|
|
128
|
+
|
|
129
|
+
const handler = (_other$event = other[event]) !== null && _other$event !== void 0 ? _other$event : () => {};
|
|
130
|
+
if (typeof handler !== 'function') throw new ArgumentError(event, 'a function', handler);
|
|
131
|
+
this[event] = handler;
|
|
132
|
+
});
|
|
120
133
|
this.connection = connection;
|
|
121
134
|
this.name = name;
|
|
122
135
|
this.debug = debug;
|
|
@@ -124,16 +137,7 @@ export default Ae.compose({
|
|
|
124
137
|
if (connection) {
|
|
125
138
|
// Init RPCClient
|
|
126
139
|
await this.connectToWallet(connection);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this.onDisconnect = onDisconnect;
|
|
131
|
-
this.onAddressChange = onAddressChange;
|
|
132
|
-
this.onNetworkChange = onNetworkChange; // validation
|
|
133
|
-
|
|
134
|
-
eventsHandlers.forEach(event => {
|
|
135
|
-
if (!forceValidation && typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`);
|
|
136
|
-
});
|
|
140
|
+
}
|
|
137
141
|
},
|
|
138
142
|
|
|
139
143
|
deepProps: {
|
|
@@ -147,7 +151,7 @@ export default Ae.compose({
|
|
|
147
151
|
sign() {},
|
|
148
152
|
|
|
149
153
|
addresses() {
|
|
150
|
-
if (!this.
|
|
154
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
|
|
151
155
|
return [this.rpcClient.currentAccount, ...Object.keys(this.rpcClient.accounts.connected)];
|
|
152
156
|
},
|
|
153
157
|
|
|
@@ -157,10 +161,19 @@ export default Ae.compose({
|
|
|
157
161
|
* @instance
|
|
158
162
|
* @rtype (connection: Object) => void
|
|
159
163
|
* @param {Object} connection Wallet connection object
|
|
160
|
-
* @
|
|
164
|
+
* @param {Object} [options={}]
|
|
165
|
+
* @param {Boolean} [options.connectNode=true] - Request wallet to bind node
|
|
166
|
+
* @param {String} [options.name=wallet-node] - Node name
|
|
167
|
+
* @param {Boolean} [options.select=false] - Select this node as current
|
|
168
|
+
* @return {Object}
|
|
161
169
|
*/
|
|
162
170
|
async connectToWallet(connection) {
|
|
163
|
-
|
|
171
|
+
let {
|
|
172
|
+
connectNode = false,
|
|
173
|
+
name = 'wallet-node',
|
|
174
|
+
select = false
|
|
175
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
176
|
+
if (this.rpcClient && this.rpcClient.isConnected()) throw new AlreadyConnectedError('You are already connected to wallet ' + this.rpcClient);
|
|
164
177
|
this.rpcClient = RpcClient({
|
|
165
178
|
connection,
|
|
166
179
|
networkId: this.getNetworkId({
|
|
@@ -170,7 +183,14 @@ export default Ae.compose({
|
|
|
170
183
|
id: uuid(),
|
|
171
184
|
handlers: [handleMessage(this), this.onDisconnect]
|
|
172
185
|
});
|
|
173
|
-
|
|
186
|
+
const walletInfo = await this.sendConnectRequest(connectNode);
|
|
187
|
+
|
|
188
|
+
if (connectNode && !Object.prototype.hasOwnProperty.call(walletInfo, 'node')) {
|
|
189
|
+
throw new RpcConnectionError('Missing URLs of the Node');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
connectNode && this.addNode(name, await Node(walletInfo.node), select);
|
|
193
|
+
return walletInfo;
|
|
174
194
|
},
|
|
175
195
|
|
|
176
196
|
/**
|
|
@@ -183,7 +203,7 @@ export default Ae.compose({
|
|
|
183
203
|
*/
|
|
184
204
|
async disconnectWallet() {
|
|
185
205
|
let sendDisconnect = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
186
|
-
if (!this.
|
|
206
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
|
|
187
207
|
sendDisconnect && this.rpcClient.sendMessage(message(METHODS.closeConnection, {
|
|
188
208
|
reason: 'bye'
|
|
189
209
|
}), true);
|
|
@@ -195,10 +215,14 @@ export default Ae.compose({
|
|
|
195
215
|
let {
|
|
196
216
|
onAccount
|
|
197
217
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
198
|
-
if (!this.
|
|
218
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
|
|
199
219
|
const current = this.rpcClient.currentAccount;
|
|
200
|
-
if (!current) throw new
|
|
201
|
-
|
|
220
|
+
if (!current) throw new UnsubscribedAccountError();
|
|
221
|
+
|
|
222
|
+
if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) {
|
|
223
|
+
throw new UnAuthorizedAccountError(onAccount);
|
|
224
|
+
}
|
|
225
|
+
|
|
202
226
|
return onAccount || current;
|
|
203
227
|
},
|
|
204
228
|
|
|
@@ -210,9 +234,9 @@ export default Ae.compose({
|
|
|
210
234
|
* @return {Promise} Address from wallet
|
|
211
235
|
*/
|
|
212
236
|
async askAddresses() {
|
|
213
|
-
if (!this.
|
|
214
|
-
if (!this.
|
|
215
|
-
return this.rpcClient.request(METHODS.
|
|
237
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
|
|
238
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
|
|
239
|
+
return this.rpcClient.request(METHODS.address);
|
|
216
240
|
},
|
|
217
241
|
|
|
218
242
|
/**
|
|
@@ -225,8 +249,8 @@ export default Ae.compose({
|
|
|
225
249
|
* @return {Promise} Address from wallet
|
|
226
250
|
*/
|
|
227
251
|
async subscribeAddress(type, value) {
|
|
228
|
-
if (!this.
|
|
229
|
-
return this.rpcClient.request(METHODS.
|
|
252
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
|
|
253
|
+
return this.rpcClient.request(METHODS.subscribeAddress, {
|
|
230
254
|
type,
|
|
231
255
|
value
|
|
232
256
|
});
|
|
@@ -242,10 +266,14 @@ export default Ae.compose({
|
|
|
242
266
|
*/
|
|
243
267
|
async signTransaction(tx) {
|
|
244
268
|
let opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
245
|
-
if (!this.
|
|
246
|
-
if (!this.
|
|
247
|
-
|
|
248
|
-
|
|
269
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
|
|
270
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
|
|
271
|
+
|
|
272
|
+
if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
|
|
273
|
+
throw new UnAuthorizedAccountError(opt.onAccount);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return this.rpcClient.request(METHODS.sign, { ...opt,
|
|
249
277
|
tx,
|
|
250
278
|
returnSigned: true,
|
|
251
279
|
networkId: this.getNetworkId()
|
|
@@ -262,10 +290,14 @@ export default Ae.compose({
|
|
|
262
290
|
*/
|
|
263
291
|
async signMessage(msg) {
|
|
264
292
|
let opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
265
|
-
if (!this.
|
|
266
|
-
if (!this.
|
|
267
|
-
|
|
268
|
-
|
|
293
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
|
|
294
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
|
|
295
|
+
|
|
296
|
+
if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
|
|
297
|
+
throw new UnAuthorizedAccountError(opt.onAccount);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return this.rpcClient.request(METHODS.signMessage, { ...opt,
|
|
269
301
|
message: msg
|
|
270
302
|
});
|
|
271
303
|
},
|
|
@@ -274,16 +306,18 @@ export default Ae.compose({
|
|
|
274
306
|
* Send connection request to wallet
|
|
275
307
|
* @function sendConnectRequest
|
|
276
308
|
* @instance
|
|
309
|
+
* @param {Boolean} connectNode - Request wallet to bind node
|
|
277
310
|
* @rtype () => Promise
|
|
278
311
|
* @return {Promise} Connection response
|
|
279
312
|
*/
|
|
280
|
-
async sendConnectRequest() {
|
|
281
|
-
return this.rpcClient.request(METHODS.
|
|
313
|
+
async sendConnectRequest(connectNode) {
|
|
314
|
+
return this.rpcClient.request(METHODS.connect, {
|
|
282
315
|
name: this.name,
|
|
283
316
|
version: VERSION,
|
|
284
317
|
networkId: this.getNetworkId({
|
|
285
318
|
force: true
|
|
286
|
-
})
|
|
319
|
+
}),
|
|
320
|
+
connectNode
|
|
287
321
|
});
|
|
288
322
|
},
|
|
289
323
|
|
|
@@ -302,9 +336,13 @@ export default Ae.compose({
|
|
|
302
336
|
*/
|
|
303
337
|
async send(tx) {
|
|
304
338
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
305
|
-
if (!this.
|
|
306
|
-
if (!this.
|
|
307
|
-
|
|
339
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet');
|
|
340
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError();
|
|
341
|
+
|
|
342
|
+
if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) {
|
|
343
|
+
throw new UnAuthorizedAccountError(options.onAccount);
|
|
344
|
+
}
|
|
345
|
+
|
|
308
346
|
const opt = { ...this.Ae.defaults,
|
|
309
347
|
...options
|
|
310
348
|
};
|
|
@@ -316,14 +354,22 @@ export default Ae.compose({
|
|
|
316
354
|
return this.sendTransaction(signed, opt);
|
|
317
355
|
}
|
|
318
356
|
|
|
319
|
-
return this.rpcClient.request(METHODS.
|
|
357
|
+
return this.rpcClient.request(METHODS.sign, {
|
|
320
358
|
onAccount: opt.onAccount,
|
|
321
359
|
tx,
|
|
322
360
|
returnSigned: false,
|
|
323
361
|
networkId: this.getNetworkId()
|
|
324
362
|
});
|
|
363
|
+
},
|
|
364
|
+
|
|
365
|
+
isConnected() {
|
|
366
|
+
return this.rpcClient && this.rpcClient.isConnected();
|
|
367
|
+
},
|
|
368
|
+
|
|
369
|
+
isSubscribedAccount() {
|
|
370
|
+
return typeof this.rpcClient.currentAccount !== 'undefined';
|
|
325
371
|
}
|
|
326
372
|
|
|
327
373
|
}
|
|
328
374
|
});
|
|
329
|
-
//# sourceMappingURL=aepp-rpc.
|
|
375
|
+
//# sourceMappingURL=aepp-rpc.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/aepp-wallet-communication/rpc/aepp-rpc.js"],"names":["v4","uuid","Ae","RpcClient","getHandler","message","METHODS","RPC_STATUS","VERSION","AlreadyConnectedError","NoWalletConnectedError","UnsubscribedAccountError","UnAuthorizedAccountError","ArgumentError","RpcConnectionError","Node","NOTIFICATIONS","updateAddress","instance","params","rpcClient","accounts","onAddressChange","updateNetwork","networkId","node","info","addNode","name","onNetworkChange","closeConnection","msg","disconnectWallet","onDisconnect","RESPONSES","address","processResponse","connect","result","status","CONNECTED","subscribeAddress","subscription","addressSubscription","id","sign","signedTransaction","transactionHash","signMessage","signature","REQUESTS","handleMessage","debug","Object","prototype","hasOwnProperty","call","callbacks","compose","init","connection","other","forEach","event","handler","connectToWallet","deepProps","defaults","walletBroadcast","methods","addresses","isSubscribedAccount","currentAccount","keys","connected","connectNode","select","isConnected","getNetworkId","force","connectionInfo","handlers","walletInfo","sendConnectRequest","sendDisconnect","sendMessage","reason","disconnect","onAccount","current","hasAccessToAccount","askAddresses","request","type","value","signTransaction","tx","opt","returnSigned","version","send","options","signed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AAAA,SAAS,EAAES,GAAG,CAACT;AAAjB,SAAzB,CAArB;AACA,eAAO,KAAKe,eAAL,CAAqBD,MAArB,EAA6BL,GAA7B,CAAP;AACD;;AACD,aAAO,KAAK7E,SAAL,CAAewE,OAAf,CACLtF,OAAO,CAACuC,IADH,EAEL;AAAE2C,QAAAA,SAAS,EAAES,GAAG,CAACT,SAAjB;AAA4BQ,QAAAA,EAA5B;AAAgCE,QAAAA,YAAY,EAAE,KAA9C;AAAqD1E,QAAAA,SAAS,EAAE,KAAKsD,YAAL;AAAhE,OAFK,CAAP;AAID,KApKM;;AAqKPD,IAAAA,WAAW,GAAI;AAAE,aAAQ,KAAKzD,SAAL,IAAkB,KAAKA,SAAL,CAAeyD,WAAf,EAA1B;AAAyD,KArKnE;;AAsKPN,IAAAA,mBAAmB,GAAI;AAAE,aAAO,OAAO,KAAKnD,SAAL,CAAeoD,cAAtB,KAAyC,WAAhD;AAA6D;;AAtK/E;AAvBe,CAAX,CAAf","sourcesContent":["/**\n * RPC handler for AEPP side\n *\n * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc\n * @export AeppRpc\n * @example\n * import ContentScriptBridge\n * from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc'\n */\nimport { v4 as uuid } from '@aeternity/uuid'\nimport Ae from '../../../ae'\nimport RpcClient from './rpc-client'\nimport { getHandler, message } from '../helpers'\nimport { METHODS, RPC_STATUS, VERSION } from '../schema'\nimport {\n AlreadyConnectedError,\n NoWalletConnectedError,\n UnsubscribedAccountError,\n UnAuthorizedAccountError,\n ArgumentError,\n RpcConnectionError\n} from '../../errors'\nimport { Node } from '../../../index'\n\nconst NOTIFICATIONS = {\n [METHODS.updateAddress]: (instance) =>\n ({ params }) => {\n instance.rpcClient.accounts = params\n instance.onAddressChange(params)\n },\n [METHODS.updateNetwork]: (instance) =>\n async ({ params }) => {\n const { networkId, node } = params\n instance.rpcClient.info.networkId = networkId\n node && instance.addNode(node.name, await Node(node), true)\n instance.onNetworkChange(params)\n },\n [METHODS.closeConnection]: (instance) =>\n (msg) => {\n instance.disconnectWallet()\n instance.onDisconnect(msg.params)\n }\n}\n\nconst RESPONSES = {\n [METHODS.address]: (instance) =>\n (msg) => instance.rpcClient.processResponse(msg),\n [METHODS.connect]: (instance) =>\n (msg) => {\n if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED\n instance.rpcClient.processResponse(msg)\n },\n [METHODS.subscribeAddress]: (instance) =>\n (msg) => {\n if (msg.result) {\n if (msg.result.address) {\n instance.rpcClient.accounts = msg.result.address\n }\n if (msg.result.subscription) {\n instance.rpcClient.addressSubscription = msg.result.subscription\n }\n }\n\n instance.rpcClient.processResponse(msg, ({ id, result }) => [result])\n },\n [METHODS.sign]: (instance) =>\n (msg) => {\n instance.rpcClient.processResponse(\n msg, ({ id, result }) => [result.signedTransaction || result.transactionHash]\n )\n },\n [METHODS.signMessage]: (instance) =>\n (msg) => {\n instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signature])\n }\n}\n\nconst REQUESTS = {}\n\nconst handleMessage = (instance) => async (msg) => {\n if (!msg.id) {\n return getHandler(NOTIFICATIONS, msg, { debug: instance.debug })(instance)(msg)\n } else if (Object.prototype.hasOwnProperty.call(instance.rpcClient.callbacks, msg.id)) {\n return getHandler(RESPONSES, msg, { debug: instance.debug })(instance)(msg)\n } else {\n return getHandler(REQUESTS, msg, { debug: instance.debug })(instance)(msg)\n }\n}\n\n/**\n * Contain functionality for wallet interaction and connect it to sdk\n * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc\n * @function\n * @rtype Stamp\n * @param {Object} param Init params object\n * @param {String=} [param.name] Aepp name\n * @param {Function} onAddressChange Call-back function for update address event\n * @param {Function} onDisconnect Call-back function for disconnect event\n * @param {Function} onNetworkChange Call-back function for update network event\n * @param {Object} connection Wallet connection object\n * @return {Object}\n */\nexport default Ae.compose({\n async init ({\n name,\n connection,\n debug = false,\n ...other\n }) {\n ['onAddressChange', 'onDisconnect', 'onNetworkChange'].forEach(event => {\n const handler = other[event] ?? (() => {})\n if (typeof handler !== 'function') throw new ArgumentError(event, 'a function', handler)\n this[event] = handler\n })\n\n this.connection = connection\n this.name = name\n this.debug = debug\n\n if (connection) {\n // Init RPCClient\n await this.connectToWallet(connection)\n }\n },\n deepProps: { Ae: { defaults: { walletBroadcast: true } } },\n methods: {\n sign () {\n },\n addresses () {\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n return [this.rpcClient.currentAccount, ...Object.keys(this.rpcClient.accounts.connected)]\n },\n /**\n * Connect to wallet\n * @function connectToWallet\n * @instance\n * @rtype (connection: Object) => void\n * @param {Object} connection Wallet connection object\n * @param {Object} [options={}]\n * @param {Boolean} [options.connectNode=true] - Request wallet to bind node\n * @param {String} [options.name=wallet-node] - Node name\n * @param {Boolean} [options.select=false] - Select this node as current\n * @return {Object}\n */\n async connectToWallet (connection, { connectNode = false, name = 'wallet-node', select = false } = {}) {\n if (this.rpcClient && this.rpcClient.isConnected()) throw new AlreadyConnectedError('You are already connected to wallet ' + this.rpcClient)\n this.rpcClient = RpcClient({\n connection,\n networkId: this.getNetworkId({ force: true }),\n ...connection.connectionInfo,\n id: uuid(),\n handlers: [handleMessage(this), this.onDisconnect]\n })\n const walletInfo = await this.sendConnectRequest(connectNode)\n if (connectNode && !Object.prototype.hasOwnProperty.call(walletInfo, 'node')) {\n throw new RpcConnectionError('Missing URLs of the Node')\n }\n connectNode && this.addNode(name, await Node(walletInfo.node), select)\n return walletInfo\n },\n /**\n * Disconnect from wallet\n * @function disconnectWallet\n * @instance\n * @rtype (force: Boolean = false) => void\n * @param {Boolean} sendDisconnect=false Force sending close connection message\n * @return {void}\n */\n async disconnectWallet (sendDisconnect = true) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n sendDisconnect && this.rpcClient.sendMessage(message(METHODS.closeConnection, { reason: 'bye' }), true)\n this.rpcClient.disconnect()\n this.rpcClient = null\n },\n async address ({ onAccount } = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n const current = this.rpcClient.currentAccount\n if (!current) throw new UnsubscribedAccountError()\n if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) {\n throw new UnAuthorizedAccountError(onAccount)\n }\n return onAccount || current\n },\n /**\n * Ask address from wallet\n * @function askAddresses\n * @instance\n * @rtype () => Promise\n * @return {Promise} Address from wallet\n */\n async askAddresses () {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n return this.rpcClient.request(METHODS.address)\n },\n /**\n * Subscribe for addresses from wallet\n * @function subscribeAddress\n * @instance\n * @rtype (type: String, value: String) => Promise\n * @param {String} type Should be one of 'current' (the selected account), 'connected' (all)\n * @param {String} value Subscription action('subscribe'|'unsubscribe')\n * @return {Promise} Address from wallet\n */\n async subscribeAddress (type, value) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n return this.rpcClient.request(METHODS.subscribeAddress, { type, value })\n },\n /**\n * Overwriting of `signTransaction` AE method\n * All sdk API which use it will be send notification to wallet and wait for callBack\n * @function signTransaction\n * @instance\n * @rtype (tx: String, options = {}) => Promise\n * @return {Promise<String>} Signed transaction\n */\n async signTransaction (tx, opt = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {\n throw new UnAuthorizedAccountError(opt.onAccount)\n }\n return this.rpcClient.request(\n METHODS.sign,\n { ...opt, tx, returnSigned: true, networkId: this.getNetworkId() }\n )\n },\n /**\n * Overwriting of `signMessage` AE method\n * All sdk API which use it will be send notification to wallet and wait for callBack\n * @function signMessage\n * @instance\n * @rtype (msg: String, options = {}) => Promise\n * @return {Promise<String>} Signed transaction\n */\n async signMessage (msg, opt = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {\n throw new UnAuthorizedAccountError(opt.onAccount)\n }\n return this.rpcClient.request(METHODS.signMessage, { ...opt, message: msg })\n },\n /**\n * Send connection request to wallet\n * @function sendConnectRequest\n * @instance\n * @param {Boolean} connectNode - Request wallet to bind node\n * @rtype () => Promise\n * @return {Promise} Connection response\n */\n async sendConnectRequest (connectNode) {\n return this.rpcClient.request(\n METHODS.connect, {\n name: this.name,\n version: VERSION,\n networkId: this.getNetworkId({ force: true }),\n connectNode\n }\n )\n },\n /**\n * Overwriting of `send` AE method\n * All sdk API which use it will be send notification to wallet and wait for callBack\n * This method will sign, broadcast and wait until transaction will be accepted using rpc\n * communication with wallet\n * @function send\n * @instance\n * @rtype (tx: String, options = {}) => Promise\n * @param {String} tx\n * @param {Object} [options={}]\n * @param {Object} [options.walletBroadcast=true]\n * @return {Promise<Object>} Transaction broadcast result\n */\n async send (tx, options = {}) {\n if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')\n if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()\n if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) {\n throw new UnAuthorizedAccountError(options.onAccount)\n }\n const opt = { ...this.Ae.defaults, ...options }\n if (!opt.walletBroadcast) {\n const signed = await this.signTransaction(tx, { onAccount: opt.onAccount })\n return this.sendTransaction(signed, opt)\n }\n return this.rpcClient.request(\n METHODS.sign,\n { onAccount: opt.onAccount, tx, returnSigned: false, networkId: this.getNetworkId() }\n )\n },\n isConnected () { return (this.rpcClient && this.rpcClient.isConnected()) },\n isSubscribedAccount () { return typeof this.rpcClient.currentAccount !== 'undefined' }\n }\n})\n"],"file":"aepp-rpc.mjs"}
|
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
* import RpcClient from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client'
|
|
8
8
|
*/
|
|
9
9
|
import stampit from '@stamp/it';
|
|
10
|
-
import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from
|
|
11
|
-
import { sendMessage, message, isValidAccounts } from
|
|
10
|
+
import { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from "../schema.mjs";
|
|
11
|
+
import { sendMessage, message, isValidAccounts } from "../helpers.mjs";
|
|
12
|
+
import { InvalidRpcMessageError, TypeError, DuplicateCallbackError, MissingCallbackError } from "../../errors.mjs";
|
|
12
13
|
/**
|
|
13
14
|
* Contain functionality for using RPC conection
|
|
14
15
|
* @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client
|
|
@@ -55,7 +56,7 @@ export default stampit({
|
|
|
55
56
|
|
|
56
57
|
const handleMessage = (msg, origin) => {
|
|
57
58
|
if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {
|
|
58
|
-
throw new
|
|
59
|
+
throw new InvalidRpcMessageError(msg);
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
onMessage(msg, origin);
|
|
@@ -139,7 +140,7 @@ export default stampit({
|
|
|
139
140
|
* @return {Boolean} is connected
|
|
140
141
|
*/
|
|
141
142
|
isConnected() {
|
|
142
|
-
return this.connection.isConnected() && this.info.status === RPC_STATUS.CONNECTED;
|
|
143
|
+
return this.connection.isConnected() && (this.info.status === RPC_STATUS.CONNECTED || this.info.status === RPC_STATUS.NODE_BINDED);
|
|
143
144
|
},
|
|
144
145
|
|
|
145
146
|
/**
|
|
@@ -184,14 +185,14 @@ export default stampit({
|
|
|
184
185
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
185
186
|
|
|
186
187
|
if (!isValidAccounts(accounts)) {
|
|
187
|
-
throw new
|
|
188
|
+
throw new TypeError('Invalid accounts object. Should be object like: `{ connected: {}, selected: {} }`');
|
|
188
189
|
}
|
|
189
190
|
|
|
190
191
|
this.accounts = accounts;
|
|
191
192
|
|
|
192
193
|
if (!forceNotification) {
|
|
193
194
|
// Sent notification about account updates
|
|
194
|
-
this.sendMessage(message(METHODS.
|
|
195
|
+
this.sendMessage(message(METHODS.updateAddress, this.accounts), true);
|
|
195
196
|
}
|
|
196
197
|
},
|
|
197
198
|
|
|
@@ -227,7 +228,11 @@ export default stampit({
|
|
|
227
228
|
*/
|
|
228
229
|
request(name, params) {
|
|
229
230
|
const msgId = this.sendMessage(message(name, params));
|
|
230
|
-
|
|
231
|
+
|
|
232
|
+
if (Object.prototype.hasOwnProperty.call(this.callbacks, msgId)) {
|
|
233
|
+
throw new DuplicateCallbackError();
|
|
234
|
+
}
|
|
235
|
+
|
|
231
236
|
return new Promise((resolve, reject) => {
|
|
232
237
|
this.callbacks[msgId] = {
|
|
233
238
|
resolve,
|
|
@@ -251,7 +256,7 @@ export default stampit({
|
|
|
251
256
|
error,
|
|
252
257
|
result
|
|
253
258
|
} = _ref2;
|
|
254
|
-
if (!this.callbacks[id]) throw new
|
|
259
|
+
if (!this.callbacks[id]) throw new MissingCallbackError(id);
|
|
255
260
|
|
|
256
261
|
if (result) {
|
|
257
262
|
this.callbacks[id].resolve(...(typeof transformResult === 'function' ? transformResult({
|
|
@@ -267,4 +272,4 @@ export default stampit({
|
|
|
267
272
|
|
|
268
273
|
}
|
|
269
274
|
});
|
|
270
|
-
//# sourceMappingURL=rpc-client.
|
|
275
|
+
//# sourceMappingURL=rpc-client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/utils/aepp-wallet-communication/rpc/rpc-client.js"],"names":["stampit","METHODS","RPC_STATUS","SUBSCRIPTION_TYPES","sendMessage","message","isValidAccounts","InvalidRpcMessageError","TypeError","DuplicateCallbackError","MissingCallbackError","init","id","name","networkId","icons","connection","handlers","onMessage","onDisconnect","info","callbacks","addressSubscription","accounts","handleMessage","msg","origin","jsonrpc","method","disconnect","aepp","connect","propertyDescriptors","currentAccount","enumerable","configurable","get","isHasAccounts","Object","keys","current","undefined","addresses","connected","methods","updateInfo","assign","isSubscribed","length","hasAccessToAccount","address","find","a","isConnected","status","CONNECTED","NODE_BINDED","getCurrentAccount","onAccount","forceConnectionClose","DISCONNECTED","setAccounts","forceNotification","updateAddress","updateSubscription","type","value","subscribe","includes","push","unsubscribe","filter","s","request","params","msgId","prototype","hasOwnProperty","call","Promise","resolve","reject","processResponse","transformResult","error","result"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,OAAP,MAAoB,WAApB;SAESC,O,EAASC,U,EAAYC,kB;SACrBC,W,EAAaC,O,EAASC,e;SAE7BC,sB,EACAC,S,EACAC,sB,EACAC,oB;AAGF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAeV,OAAO,CAAC;AACrBW,EAAAA,IAAI,OAAmF;AAAA,QAAjF;AAAEC,MAAAA,EAAF;AAAMC,MAAAA,IAAN;AAAYC,MAAAA,SAAZ;AAAuBC,MAAAA,KAAvB;AAA8BC,MAAAA,UAA9B;AAA0CC,MAAAA,QAAQ,EAAE,CAACC,SAAD,EAAYC,YAAZ;AAApD,KAAiF;AACrF,SAAKP,EAAL,GAAUA,EAAV;AACA,SAAKI,UAAL,GAAkBA,UAAlB;AACA,SAAKI,IAAL,GAAY;AAAEP,MAAAA,IAAF;AAAQC,MAAAA,SAAR;AAAmBC,MAAAA;AAAnB,KAAZ,CAHqF,CAIrF;AACA;AACA;;AACA,SAAKM,SAAL,GAAiB,EAAjB,CAPqF,CAQrF;;AACA,SAAKC,mBAAL,GAA2B,EAA3B,CATqF,CAUrF;AACA;AACA;AACA;;AACA,SAAKC,QAAL,GAAgB,EAAhB;AAEA,SAAKnB,WAAL,GAAmBA,WAAW,CAAC,KAAKY,UAAN,CAA9B;;AAEA,UAAMQ,aAAa,GAAG,CAACC,GAAD,EAAMC,MAAN,KAAiB;A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B,CAA0BG,KAA1B;AACD;;AACD,aAAO,KAAK/D,SAAL,CAAeT,EAAf,CAAP;AACD;;AAzIM;AA5DY,CAAD,CAAtB","sourcesContent":["/**\n * RpcClient module\n *\n * @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client\n * @export { RpcClient, RpcClients }\n * @example\n * import RpcClient from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client'\n */\nimport stampit from '@stamp/it'\n\nimport { METHODS, RPC_STATUS, SUBSCRIPTION_TYPES } from '../schema'\nimport { sendMessage, message, isValidAccounts } from '../helpers'\nimport {\n InvalidRpcMessageError,\n TypeError,\n DuplicateCallbackError,\n MissingCallbackError\n} from '../../errors'\n\n/**\n * Contain functionality for using RPC conection\n * @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/rpc-client\n * @function\n * @rtype Stamp\n * @param {Object} param Init params object\n * @param {String} param.name Client name\n * @param {Object} param.connection Connection object\n * @param {Function[]} param.handlers Array with two function for message handling\n * @param {Function} param.handlers[0] Message handler\n * @param {Function} param.handlers[1] Disconnect callback\n * @return {Object}\n */\nexport default stampit({\n init ({ id, name, networkId, icons, connection, handlers: [onMessage, onDisconnect] }) {\n this.id = id\n this.connection = connection\n this.info = { name, networkId, icons }\n // {\n // [msg.id]: { resolve, reject }\n // }\n this.callbacks = {}\n // ['connected', 'current']\n this.addressSubscription = []\n // {\n // connected: { [pub]: {...meta} },\n // current: { [pub]: {...meta} }\n // }\n this.accounts = {}\n\n this.sendMessage = sendMessage(this.connection)\n\n const handleMessage = (msg, origin) => {\n if (!msg || !msg.jsonrpc || msg.jsonrpc !== '2.0' || !msg.method) {\n throw new InvalidRpcMessageError(msg)\n }\n onMessage(msg, origin)\n }\n\n const disconnect = (aepp, connection) => {\n this.disconnect(true)\n typeof onDisconnect === 'function' && onDisconnect(connection, this)\n }\n\n connection.connect(handleMessage, disconnect)\n },\n propertyDescriptors: {\n currentAccount: {\n enumerable: true,\n configurable: false,\n get () {\n return this.isHasAccounts()\n ? Object.keys(this.accounts.current)[0]\n : undefined\n }\n },\n addresses: {\n enumerable: true,\n configurable: false,\n get () {\n return this.isHasAccounts()\n ? [...Object.keys(this.accounts.current), ...Object.keys(this.accounts.connected)]\n : []\n }\n },\n origin: {\n enumerable: true,\n configurable: false,\n get () {\n return this.connection\n }\n }\n },\n methods: {\n /**\n * Update info\n * @function updateInfo\n * @instance\n * @rtype (info: Object) => void\n * @param {Object} info Info to update (will be merged with current info object)\n * @return {void}\n */\n updateInfo (info) {\n Object.assign(this.info, info)\n },\n isHasAccounts () {\n return typeof this.accounts === 'object' &&\n typeof this.accounts.connected === 'object' &&\n typeof this.accounts.current === 'object'\n },\n isSubscribed () {\n return this.addressSubscription.length && this.isHasAccounts()\n },\n /**\n * Check if aepp has access to account\n * @function hasAccessToAccount\n * @instance\n * @rtype (address: String) => Boolean\n * @param {String} address Account address\n * @return {Boolean} is connected\n */\n hasAccessToAccount (address) {\n return !!address && this.addresses.find(a => a === address)\n },\n /**\n * Check if is connected\n * @function isConnected\n * @instance\n * @rtype () => Boolean\n * @return {Boolean} is connected\n */\n isConnected () {\n return this.connection.isConnected() &&\n (this.info.status === RPC_STATUS.CONNECTED || this.info.status === RPC_STATUS.NODE_BINDED)\n },\n /**\n * Get selected account\n * @function getCurrentAccount\n * @instance\n * @rtype ({ onAccount } = {}) => String\n * @param {Object} options Options\n * @return {String}\n */\n getCurrentAccount ({ onAccount } = {}) {\n return onAccount || Object.keys(this.accounts.current)[0]\n },\n /**\n * Disconnect\n * @function disconnect\n * @instance\n * @rtype () => void\n * @return {void}\n */\n disconnect (forceConnectionClose = false) {\n this.info.status = RPC_STATUS.DISCONNECTED\n this.addressSubscription = []\n this.accounts = {}\n forceConnectionClose || this.connection.disconnect()\n },\n /**\n * Update accounts and sent `update.address` notification to AEPP\n * @param {{ current: Object, connected: Object }} accounts Current and connected accounts\n * @param {Object} [options]\n * @param {Boolean} [options.forceNotification] Don't sent update notification to AEPP\n */\n setAccounts (accounts, { forceNotification } = {}) {\n if (!isValidAccounts(accounts)) {\n throw new TypeError('Invalid accounts object. Should be object like: `{ connected: {}, selected: {} }`')\n }\n this.accounts = accounts\n if (!forceNotification) {\n // Sent notification about account updates\n this.sendMessage(message(METHODS.updateAddress, this.accounts), true)\n }\n },\n /**\n * Update subscription\n * @function updateSubscription\n * @instance\n * @rtype (type: String, value: String) => void\n * @param {String} type Subscription type\n * @param {String} value Subscription value\n * @return {String[]}\n */\n updateSubscription (type, value) {\n if (type === SUBSCRIPTION_TYPES.subscribe && !this.addressSubscription.includes(value)) {\n this.addressSubscription.push(value)\n }\n if (type === SUBSCRIPTION_TYPES.unsubscribe && this.addressSubscription.includes(value)) {\n this.addressSubscription = this.addressSubscription.filter(s => s !== value)\n }\n return this.addressSubscription\n },\n /**\n * Make a request\n * @function request\n * @instance\n * @rtype (name: String, params: Object) => Promise\n * @param {String} name Method name\n * @param {Object} params Method params\n * @return {Promise} Promise which will be resolved after receiving response message\n */\n request (name, params) {\n const msgId = this.sendMessage(message(name, params))\n if (Object.prototype.hasOwnProperty.call(this.callbacks, msgId)) {\n throw new DuplicateCallbackError()\n }\n return new Promise((resolve, reject) => {\n this.callbacks[msgId] = { resolve, reject }\n })\n },\n /**\n * Process response message\n * @function processResponse\n * @instance\n * @rtype (msg: Object, transformResult: Function) => void\n * @param {Object} msg Message object\n * @param {Function=} transformResult Optional parser function for message\n * @return {void}\n */\n processResponse ({ id, error, result }, transformResult) {\n if (!this.callbacks[id]) throw new MissingCallbackError(id)\n if (result) {\n this.callbacks[id].resolve(...typeof transformResult === 'function'\n ? transformResult({ id, result })\n : [result])\n } else {\n this.callbacks[id].reject(error)\n }\n delete this.callbacks[id]\n }\n }\n})\n"],"file":"rpc-client.mjs"}
|