@xelis/sdk 0.6.1 → 0.8.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.
Files changed (56) hide show
  1. package/create_esm_pkg.js +13 -0
  2. package/dist/cjs/config.js +38 -0
  3. package/dist/cjs/daemon/rpc.js +143 -0
  4. package/dist/cjs/daemon/types.js +74 -0
  5. package/dist/cjs/daemon/websocket.js +236 -0
  6. package/dist/cjs/lib/rpc.js +89 -0
  7. package/dist/cjs/lib/types.js +2 -0
  8. package/dist/cjs/lib/websocket.js +283 -0
  9. package/dist/cjs/react/daemon.js +176 -0
  10. package/dist/cjs/wallet/rpc.js +131 -0
  11. package/dist/cjs/wallet/types.js +29 -0
  12. package/dist/cjs/wallet/websocket.js +92 -0
  13. package/dist/cjs/xswd/types.js +9 -0
  14. package/dist/cjs/xswd/websocket.js +38 -0
  15. package/dist/esm/config.js +35 -0
  16. package/dist/esm/daemon/rpc.js +140 -0
  17. package/{daemon → dist/esm/daemon}/types.js +13 -3
  18. package/dist/esm/daemon/websocket.js +233 -0
  19. package/dist/esm/lib/rpc.js +86 -0
  20. package/dist/esm/lib/websocket.js +277 -0
  21. package/dist/esm/package.json +1 -0
  22. package/dist/esm/react/daemon.js +144 -0
  23. package/dist/esm/wallet/rpc.js +128 -0
  24. package/{wallet → dist/esm/wallet}/types.js +1 -0
  25. package/dist/esm/wallet/websocket.js +89 -0
  26. package/dist/esm/xswd/websocket.js +35 -0
  27. package/{daemon → dist/types/daemon}/rpc.d.ts +17 -15
  28. package/{daemon → dist/types/daemon}/types.d.ts +127 -47
  29. package/{daemon → dist/types/daemon}/websocket.d.ts +20 -16
  30. package/{wallet → dist/types/wallet}/rpc.d.ts +9 -5
  31. package/{wallet → dist/types/wallet}/types.d.ts +26 -0
  32. package/{wallet → dist/types/wallet}/websocket.d.ts +7 -5
  33. package/jest.config.js +6 -0
  34. package/package.json +12 -3
  35. package/tsconfig.cjs.json +7 -0
  36. package/tsconfig.esm.json +7 -0
  37. package/tsconfig.json +17 -0
  38. package/tsconfig.types.json +8 -0
  39. package/config.js +0 -25
  40. package/daemon/rpc.js +0 -113
  41. package/daemon/websocket.js +0 -159
  42. package/lib/rpc.js +0 -38
  43. package/lib/websocket.js +0 -202
  44. package/react/daemon.js +0 -82
  45. package/wallet/rpc.js +0 -55
  46. package/wallet/websocket.js +0 -60
  47. package/xswd/websocket.js +0 -16
  48. /package/{lib → dist/esm/lib}/types.js +0 -0
  49. /package/{xswd → dist/esm/xswd}/types.js +0 -0
  50. /package/{config.d.ts → dist/types/config.d.ts} +0 -0
  51. /package/{lib → dist/types/lib}/rpc.d.ts +0 -0
  52. /package/{lib → dist/types/lib}/types.d.ts +0 -0
  53. /package/{lib → dist/types/lib}/websocket.d.ts +0 -0
  54. /package/{react → dist/types/react}/daemon.d.ts +0 -0
  55. /package/{xswd → dist/types/xswd}/types.d.ts +0 -0
  56. /package/{xswd → dist/types/xswd}/websocket.d.ts +0 -0
package/config.js DELETED
@@ -1,25 +0,0 @@
1
- export const DAEMON_RPC_PORT = `8080`;
2
- export const WALLET_RPC_PORT = `8081`;
3
- export const XSWD_PORT = `44325`;
4
- export const MAINNET_NODE_URL = `node.xelis.io`;
5
- export const TESTNET_NODE_URL = `testnet-node.xelis.io`;
6
- export const LOCAL_NODE_URL = `127.0.0.1:${DAEMON_RPC_PORT}`;
7
- export const MAINNET_NODE_RPC = `https://${MAINNET_NODE_URL}/json_rpc`;
8
- export const TESTNET_NODE_RPC = `https://${TESTNET_NODE_URL}/json_rpc`;
9
- export const LOCAL_NODE_RPC = `http://${LOCAL_NODE_URL}/json_rpc`;
10
- export const MAINNET_NODE_WS = `wss://${MAINNET_NODE_URL}/json_rpc`;
11
- export const TESTNET_NODE_WS = `wss://${TESTNET_NODE_URL}/json_rpc`;
12
- export const LOCAL_NODE_WS = `ws://${LOCAL_NODE_URL}/json_rpc`;
13
- export const LOCAL_WALLET_URL = `127.0.0.1:${WALLET_RPC_PORT}`;
14
- export const LOCAL_WALLET_RPC = `http://${LOCAL_WALLET_URL}/json_rpc`;
15
- export const LOCAL_WALLET_WS = `ws://${LOCAL_WALLET_URL}/json_rpc`;
16
- export const LOCAL_XSWD_URL = `127.0.0.1:${XSWD_PORT}`;
17
- export const LOCAL_XSWD_WS = `ws://${LOCAL_XSWD_URL}/xswd`;
18
- export default {
19
- MAINNET_NODE_URL, TESTNET_NODE_URL,
20
- MAINNET_NODE_RPC, TESTNET_NODE_RPC, LOCAL_NODE_RPC,
21
- MAINNET_NODE_WS, TESTNET_NODE_WS, LOCAL_NODE_WS,
22
- DAEMON_RPC_PORT, WALLET_RPC_PORT, XSWD_PORT,
23
- LOCAL_WALLET_URL, LOCAL_WALLET_RPC, LOCAL_WALLET_WS,
24
- LOCAL_XSWD_URL, LOCAL_XSWD_WS
25
- };
package/daemon/rpc.js DELETED
@@ -1,113 +0,0 @@
1
- import { RPCMethod } from './types';
2
- import { RPC as BaseRPC } from '../lib/rpc';
3
- export class RPC extends BaseRPC {
4
- getVersion() {
5
- return this.post(RPCMethod.GetVersion);
6
- }
7
- getInfo() {
8
- return this.post(RPCMethod.GetInfo);
9
- }
10
- getHeight() {
11
- return this.post(RPCMethod.GetHeight);
12
- }
13
- getTopoHeight() {
14
- return this.post(RPCMethod.GetTopoHeight);
15
- }
16
- getStableHeight() {
17
- return this.post(RPCMethod.GetStableHeight);
18
- }
19
- getBlockTemplate(address) {
20
- return this.post(RPCMethod.GetBlockTemplate, { address });
21
- }
22
- getBlockAtTopoHeight(params) {
23
- return this.post(RPCMethod.GetBlockAtTopoHeight, params);
24
- }
25
- getBlocksAtHeight(params) {
26
- return this.post(RPCMethod.GetBlocksAtHeight, params);
27
- }
28
- getBlockByHash(params) {
29
- return this.post(RPCMethod.GetBlockByHash, params);
30
- }
31
- getTopBlock(params) {
32
- return this.post(RPCMethod.GetTopBlock, params);
33
- }
34
- getNonce(params) {
35
- return this.post(RPCMethod.GetNonce, params);
36
- }
37
- getBalance(params) {
38
- return this.post(RPCMethod.GetBalance, params);
39
- }
40
- hasBalance(params) {
41
- return this.post(RPCMethod.HasBalance, params);
42
- }
43
- getBalanceAtTopoHeight(params) {
44
- return this.post(RPCMethod.GetBalanceAtTopoHeight, params);
45
- }
46
- getAsset(params) {
47
- return this.post(RPCMethod.GetAsset, params);
48
- }
49
- getAssets() {
50
- return this.post(RPCMethod.GetAssets);
51
- }
52
- countTransactions() {
53
- return this.post(RPCMethod.CountTransactions);
54
- }
55
- countAssets() {
56
- return this.post(RPCMethod.CountAssets);
57
- }
58
- countAccounts() {
59
- return this.post(RPCMethod.CountAccounts);
60
- }
61
- getTips() {
62
- return this.post(RPCMethod.GetTips);
63
- }
64
- p2pStatus() {
65
- return this.post(RPCMethod.P2PStatus);
66
- }
67
- getDAGOrder(params) {
68
- return this.post(RPCMethod.GetDAGOrder, params);
69
- }
70
- getMemPool() {
71
- return this.post(RPCMethod.GetMempool);
72
- }
73
- getTransaction(hash) {
74
- return this.post(RPCMethod.GetTransaction, { hash });
75
- }
76
- getTransactions(txHashes) {
77
- return this.post(RPCMethod.GetTransactions, { tx_hashes: txHashes });
78
- }
79
- getBlocksRangeByTopoheight(params) {
80
- return this.post(RPCMethod.GetBlocksRangeByTopoheight, params);
81
- }
82
- getBlocksRangeByHeight(params) {
83
- return this.post(RPCMethod.GetBlocksRangeByHeight, params);
84
- }
85
- getAccounts(params) {
86
- return this.post(RPCMethod.GetAccounts, params);
87
- }
88
- submitBlock(blockTemplate) {
89
- return this.post(RPCMethod.SubmitBlock, { block_template: blockTemplate });
90
- }
91
- submitTransaction(hexData) {
92
- return this.post(RPCMethod.SubmitTransaction, { data: hexData });
93
- }
94
- getAccountHistory(params) {
95
- return this.post(RPCMethod.GetAccountHistory, params);
96
- }
97
- getAccountAssets(address) {
98
- return this.post(RPCMethod.GetAccountAssets, { address });
99
- }
100
- getPeers() {
101
- return this.post(RPCMethod.GetPeers);
102
- }
103
- getDevFeeThresholds() {
104
- return this.post(RPCMethod.GetDevFeeThresholds);
105
- }
106
- getSizeOnDisk() {
107
- return this.post(RPCMethod.GetSizeOnDisk);
108
- }
109
- isTxExecutedInBlock(params) {
110
- return this.post(RPCMethod.IsTxExecutedInBlock, params);
111
- }
112
- }
113
- export default RPC;
@@ -1,159 +0,0 @@
1
- import { RPCEvent, RPCMethod } from './types';
2
- import { WS as BaseWS } from '../lib/websocket';
3
- export class DaemonMethods {
4
- constructor(ws, prefix = "") {
5
- this.ws = ws;
6
- this.prefix = prefix;
7
- }
8
- async listenEvent(event, onData) {
9
- return this.ws.listenEvent(this.prefix + event, onData);
10
- }
11
- dataCall(method, params) {
12
- return this.ws.dataCall(this.prefix + method, params);
13
- }
14
- onNewBlock(onData) {
15
- return this.listenEvent(RPCEvent.NewBlock, onData);
16
- }
17
- onTransactionAddedInMempool(onData) {
18
- return this.listenEvent(RPCEvent.TransactionAddedInMempool, onData);
19
- }
20
- onTransactionExecuted(onData) {
21
- return this.listenEvent(RPCEvent.TransactionExecuted, onData);
22
- }
23
- onBlockOrdered(onData) {
24
- return this.listenEvent(RPCEvent.BlockOrdered, onData);
25
- }
26
- onPeerConnected(onData) {
27
- return this.listenEvent(RPCEvent.PeerConnected, onData);
28
- }
29
- onPeerDisconnected(onData) {
30
- return this.listenEvent(RPCEvent.PeerDisconnected, onData);
31
- }
32
- onPeerPeerListUpdated(onData) {
33
- return this.listenEvent(RPCEvent.PeerPeerListUpdated, onData);
34
- }
35
- onPeerPeerDisconnected(onData) {
36
- return this.listenEvent(RPCEvent.PeerPeerDisconnected, onData);
37
- }
38
- onPeerStateUpdated(onData) {
39
- return this.listenEvent(RPCEvent.PeerStateUpdated, onData);
40
- }
41
- onNewAsset(onData) {
42
- return this.listenEvent(RPCEvent.NewAsset, onData);
43
- }
44
- onBlockOrphaned(onData) {
45
- return this.listenEvent(RPCEvent.BlockOrphaned, onData);
46
- }
47
- getVersion() {
48
- return this.dataCall(RPCMethod.GetVersion);
49
- }
50
- getInfo() {
51
- return this.dataCall(RPCMethod.GetInfo);
52
- }
53
- getHeight() {
54
- return this.dataCall(RPCMethod.GetHeight);
55
- }
56
- getTopoHeight() {
57
- return this.dataCall(RPCMethod.GetTopoHeight);
58
- }
59
- getStableHeight() {
60
- return this.dataCall(RPCMethod.GetStableHeight);
61
- }
62
- getBlockTemplate(address) {
63
- return this.dataCall(RPCMethod.GetBlockTemplate, { address });
64
- }
65
- getBlockAtTopoHeight(params) {
66
- return this.dataCall(RPCMethod.GetBlockAtTopoHeight, params);
67
- }
68
- getBlocksAtHeight(params) {
69
- return this.dataCall(RPCMethod.GetBlocksAtHeight, params);
70
- }
71
- getBlockByHash(params) {
72
- return this.dataCall(RPCMethod.GetBlockByHash, params);
73
- }
74
- getTopBlock(params) {
75
- return this.dataCall(RPCMethod.GetTopBlock, params);
76
- }
77
- getNonce(params) {
78
- return this.dataCall(RPCMethod.GetNonce, params);
79
- }
80
- getBalance(params) {
81
- return this.dataCall(RPCMethod.GetBalance, params);
82
- }
83
- hasBalance(params) {
84
- return this.dataCall(RPCMethod.HasBalance, params);
85
- }
86
- getBalanceAtTopoHeight(params) {
87
- return this.dataCall(RPCMethod.GetBalanceAtTopoHeight, params);
88
- }
89
- getAsset(params) {
90
- return this.dataCall(RPCMethod.GetAsset, params);
91
- }
92
- getAssets() {
93
- return this.dataCall(RPCMethod.GetAssets);
94
- }
95
- countTransactions() {
96
- return this.dataCall(RPCMethod.CountTransactions);
97
- }
98
- getTips() {
99
- return this.dataCall(RPCMethod.GetTips);
100
- }
101
- p2pStatus() {
102
- return this.dataCall(RPCMethod.P2PStatus);
103
- }
104
- getDAGOrder(params) {
105
- return this.dataCall(RPCMethod.GetDAGOrder, params);
106
- }
107
- getMemPool() {
108
- return this.dataCall(RPCMethod.GetMempool);
109
- }
110
- getTransaction(hash) {
111
- return this.dataCall(RPCMethod.GetTransaction, { hash });
112
- }
113
- getTransactions(txHashes) {
114
- return this.dataCall(RPCMethod.GetTransactions, { tx_hashes: txHashes });
115
- }
116
- getBlocksRangeByTopoheight(params) {
117
- return this.dataCall(RPCMethod.GetBlocksRangeByTopoheight, params);
118
- }
119
- getBlocksRangeByHeight(params) {
120
- return this.dataCall(RPCMethod.GetBlocksRangeByHeight, params);
121
- }
122
- getAccounts(params) {
123
- return this.dataCall(RPCMethod.GetAccounts, params);
124
- }
125
- submitBlock(blockTemplate) {
126
- return this.dataCall(RPCMethod.SubmitBlock, { block_template: blockTemplate });
127
- }
128
- submitTransaction(hexData) {
129
- return this.dataCall(RPCMethod.SubmitTransaction, { data: hexData });
130
- }
131
- countAccounts() {
132
- return this.dataCall(RPCMethod.CountAccounts);
133
- }
134
- getAccountHistory(params) {
135
- return this.dataCall(RPCMethod.GetAccountHistory, params);
136
- }
137
- getAccountAssets(address) {
138
- return this.dataCall(RPCMethod.GetAccountAssets, { address });
139
- }
140
- getPeers() {
141
- return this.dataCall(RPCMethod.GetPeers);
142
- }
143
- getDevFeeThresholds() {
144
- return this.dataCall(RPCMethod.GetDevFeeThresholds);
145
- }
146
- getSizeOnDisk() {
147
- return this.dataCall(RPCMethod.GetSizeOnDisk);
148
- }
149
- isTxExecutedInBlock(params) {
150
- return this.dataCall(RPCMethod.IsTxExecutedInBlock, params);
151
- }
152
- }
153
- export class WS extends BaseWS {
154
- constructor() {
155
- super();
156
- this.methods = new DaemonMethods(this);
157
- }
158
- }
159
- export default WS;
package/lib/rpc.js DELETED
@@ -1,38 +0,0 @@
1
- export class RPC {
2
- constructor(endpoint) {
3
- this.endpoint = endpoint;
4
- this.timeout = 3000;
5
- }
6
- async post(method, params, headers) {
7
- try {
8
- const controller = new AbortController();
9
- const body = JSON.stringify({ id: 1, jsonrpc: '2.0', method: method, params });
10
- const timeoutId = setTimeout(() => {
11
- controller.abort();
12
- }, this.timeout);
13
- headers = headers || new Headers();
14
- headers.set(`Content-Type`, `application/json`);
15
- const res = await fetch(this.endpoint, {
16
- headers,
17
- method: `POST`,
18
- body,
19
- signal: controller.signal
20
- });
21
- clearTimeout(timeoutId);
22
- if (res.ok) {
23
- const data = await res.json();
24
- if (data.error) {
25
- return Promise.reject(new Error(data.error.message));
26
- }
27
- return Promise.resolve(data);
28
- }
29
- else {
30
- return Promise.reject(new Error(`${res.status} - ${res.statusText}`));
31
- }
32
- }
33
- catch (err) {
34
- console.log(err);
35
- return Promise.reject(err);
36
- }
37
- }
38
- }
package/lib/websocket.js DELETED
@@ -1,202 +0,0 @@
1
- import WebSocket from 'isomorphic-ws';
2
- import to from 'await-to-js';
3
- export class WS {
4
- constructor(options) {
5
- this.connectionTries = 0;
6
- this.methodIdIncrement = 0;
7
- this.endpoint = "";
8
- this.timeout = 15000; // default to 15s
9
- this.events = {};
10
- this.unsubscribeSuspense = 1000;
11
- this.maxConnectionTries = 3;
12
- this.reconnectOnConnectionLoss = true;
13
- this.options = options;
14
- }
15
- connect(endpoint) {
16
- // force disconnect if already connected
17
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
18
- this.socket.close();
19
- }
20
- this.events = {};
21
- this.connectionTries = 0;
22
- return new Promise((resolve, reject) => {
23
- this.socket = new WebSocket(endpoint, this.options);
24
- this.endpoint = endpoint;
25
- this.socket.addEventListener(`open`, (event) => {
26
- resolve(event);
27
- });
28
- this.socket.addEventListener(`close`, (event) => {
29
- if (this.reconnectOnConnectionLoss && !event.wasClean) {
30
- this.tryReconnect();
31
- reject(new Error(`Unhandled close. Reconnecting...`));
32
- }
33
- else {
34
- reject(event);
35
- }
36
- });
37
- this.socket.addEventListener(`error`, (err) => {
38
- reject(err);
39
- });
40
- });
41
- }
42
- tryReconnect() {
43
- this.connectionTries++;
44
- if (this.connectionTries > this.maxConnectionTries) {
45
- return;
46
- }
47
- this.socket = new WebSocket(this.endpoint, this.options);
48
- this.socket.addEventListener(`open`, () => {
49
- this.connectionTries = 0;
50
- });
51
- this.socket.addEventListener(`close`, (event) => {
52
- this.tryReconnect();
53
- });
54
- }
55
- close() {
56
- if (!this.socket)
57
- return;
58
- this.socket.close();
59
- }
60
- clearEvent(event) {
61
- this.events[event].listeners.forEach(listener => {
62
- this.socket && this.socket.removeEventListener(`message`, listener);
63
- });
64
- Reflect.deleteProperty(this.events, event);
65
- }
66
- async closeAllListens(event) {
67
- if (this.events[event]) {
68
- const [err, _] = await to(this.call(`unsubscribe`, { notify: event }));
69
- if (err)
70
- return Promise.reject(err);
71
- this.clearEvent(event);
72
- }
73
- return Promise.resolve();
74
- }
75
- async listenEvent(event, onData) {
76
- const onMessage = (msgEvent) => {
77
- if (this.events[event]) {
78
- const { id } = this.events[event];
79
- if (typeof msgEvent.data === `string`) {
80
- try {
81
- const data = JSON.parse(msgEvent.data);
82
- if (data.id === id) {
83
- if (data.error) {
84
- onData(msgEvent, undefined, new Error(data.error.message));
85
- }
86
- else {
87
- onData(msgEvent, data.result, undefined);
88
- }
89
- }
90
- }
91
- catch (_a) {
92
- // can't parse json -- do nothing
93
- }
94
- }
95
- }
96
- };
97
- if (this.events[event]) {
98
- const { unsubscribeTimeoutId } = this.events[event];
99
- if (unsubscribeTimeoutId) {
100
- // clear timeout to unsubscribe
101
- // because we got a new registered event and want to cancel the pending unsubscribe grace period
102
- clearTimeout(unsubscribeTimeoutId);
103
- }
104
- this.events[event].listeners.push(onMessage);
105
- }
106
- else {
107
- // important if multiple listenEvent are called without await at least we store listener before getting id
108
- this.events[event] = { listeners: [onMessage] };
109
- const [err, res] = await to(this.call(`subscribe`, { notify: event }));
110
- if (err) {
111
- this.clearEvent(event);
112
- return Promise.reject(err);
113
- }
114
- this.events[event].id = res.id;
115
- }
116
- this.socket && this.socket.addEventListener(`message`, onMessage);
117
- const closeListen = () => {
118
- const eventData = this.events[event];
119
- if (eventData) {
120
- const listeners = eventData.listeners;
121
- for (let i = 0; i < listeners.length; i++) {
122
- if (listeners[i] === onMessage) {
123
- listeners.splice(i, 1);
124
- break;
125
- }
126
- }
127
- // no more listener so we unsubscribe from daemon websocket if socket still open
128
- if (listeners.length === 0) {
129
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
130
- // we use a grace period to unsubscribe (mostly because of react useEffect and avoid unecessary subscribe)
131
- this.events[event].unsubscribeTimeoutId = setTimeout(async () => {
132
- this.call(`unsubscribe`, { notify: event });
133
- Reflect.deleteProperty(this.events, event);
134
- }, this.unsubscribeSuspense);
135
- }
136
- else {
137
- // socket is closed so we don't send unsubscribe and no grace period delete right away
138
- Reflect.deleteProperty(this.events, event);
139
- }
140
- }
141
- }
142
- this.socket && this.socket.removeEventListener(`message`, onMessage);
143
- return Promise.resolve();
144
- };
145
- return Promise.resolve(closeListen);
146
- }
147
- call(method, params, overwriteData) {
148
- return new Promise((resolve, reject) => {
149
- if (!this.socket)
150
- return reject(new Error(`Socket is not initialized.`));
151
- if (this.socket.readyState !== WebSocket.OPEN)
152
- return reject(new Error(`Can't send msg. Socket is not opened.`));
153
- let requestMethod = this.createRequestMethod(method, params);
154
- // for XSWD we want to send the application data without request method wrapping
155
- if (overwriteData) {
156
- requestMethod.id = null;
157
- requestMethod.data = overwriteData;
158
- }
159
- let timeoutId = null;
160
- const onMessage = (msgEvent) => {
161
- if (typeof msgEvent.data === `string`) {
162
- const data = JSON.parse(msgEvent.data);
163
- if (data.id === requestMethod.id) {
164
- clearTimeout(timeoutId);
165
- this.socket && this.socket.removeEventListener(`message`, onMessage);
166
- if (data.error)
167
- return reject(new Error(data.error.message));
168
- else
169
- resolve(data);
170
- }
171
- }
172
- };
173
- // make sure you listen before sending data
174
- this.socket && this.socket.addEventListener(`message`, onMessage); // we don't use { once: true } option because of timeout feature
175
- if (this.timeout > 0) {
176
- timeoutId = setTimeout(() => {
177
- this.socket && this.socket.removeEventListener(`message`, onMessage);
178
- reject(new Error(`timeout`));
179
- }, this.timeout);
180
- }
181
- if (this.socket && this.socket.readyState === WebSocket.OPEN) {
182
- this.socket.send(requestMethod.data);
183
- }
184
- });
185
- }
186
- dataCall(method, params) {
187
- return new Promise(async (resolve, reject) => {
188
- const [err, res] = await to(this.call(method, params));
189
- if (err)
190
- return reject(err);
191
- return resolve(res.result);
192
- });
193
- }
194
- createRequestMethod(method, params) {
195
- const id = this.methodIdIncrement++;
196
- const request = { id: id, jsonrpc: `2.0`, method };
197
- if (params)
198
- request.params = params;
199
- const data = JSON.stringify(request);
200
- return { data, id };
201
- }
202
- }
package/react/daemon.js DELETED
@@ -1,82 +0,0 @@
1
- import React, { createContext, useContext, useEffect, useState } from 'react';
2
- import to from 'await-to-js';
3
- import DaemonWS from '../daemon/websocket';
4
- export const INITIATING = -1;
5
- const daemon = new DaemonWS();
6
- const Context = createContext({
7
- err: undefined,
8
- daemon,
9
- readyState: INITIATING
10
- });
11
- export const NodeSocketProvider = (props) => {
12
- const { children, endpoint, timeout } = props;
13
- const [readyState, setReadyState] = useState(INITIATING);
14
- const [err, setErr] = useState();
15
- useEffect(() => {
16
- const connect = async () => {
17
- setErr(undefined);
18
- const [err, _] = await to(daemon.connect(endpoint));
19
- if (err)
20
- setErr(err);
21
- };
22
- connect();
23
- }, [endpoint]);
24
- useEffect(() => {
25
- if (!timeout)
26
- return;
27
- daemon.timeout = timeout;
28
- }, [timeout]);
29
- useEffect(() => {
30
- if (!daemon.socket)
31
- return;
32
- setReadyState(daemon.socket.readyState);
33
- const onOpen = () => {
34
- if (!daemon.socket)
35
- return;
36
- setReadyState(daemon.socket.readyState);
37
- setErr(undefined);
38
- };
39
- const onClose = (event) => {
40
- if (!daemon.socket)
41
- return;
42
- setReadyState(daemon.socket.readyState);
43
- setErr(new Error(event.reason));
44
- };
45
- const onError = (err) => {
46
- if (!daemon.socket)
47
- return;
48
- setReadyState(daemon.socket.readyState);
49
- setErr(new Error(err.message));
50
- };
51
- daemon.socket.addEventListener(`open`, onOpen);
52
- daemon.socket.addEventListener(`close`, onClose);
53
- daemon.socket.addEventListener(`error`, onError);
54
- return () => {
55
- if (!daemon.socket)
56
- return;
57
- daemon.socket.removeEventListener(`open`, onOpen);
58
- daemon.socket.removeEventListener(`close`, onClose);
59
- daemon.socket.removeEventListener(`error`, onError);
60
- };
61
- }, [daemon.socket]);
62
- return React.createElement(Context.Provider, { value: { daemon, err, readyState } }, children);
63
- };
64
- export const useNodeSocketSubscribe = ({ event, onLoad, onData }, dependencies) => {
65
- const nodeSocket = useNodeSocket();
66
- useEffect(() => {
67
- if (nodeSocket.readyState !== WebSocket.OPEN)
68
- return;
69
- if (typeof onLoad === `function`)
70
- onLoad();
71
- let closeEvent;
72
- const listen = async () => {
73
- closeEvent = await nodeSocket.daemon.listenEvent(event, onData);
74
- };
75
- listen();
76
- return () => {
77
- closeEvent && closeEvent();
78
- };
79
- }, [nodeSocket, ...dependencies]);
80
- };
81
- export const useNodeSocket = () => useContext(Context);
82
- export default useNodeSocket;
package/wallet/rpc.js DELETED
@@ -1,55 +0,0 @@
1
- import { Base64 } from 'js-base64';
2
- import { RPCMethod } from './types';
3
- import { RPC as BaseRPC } from '../lib/rpc';
4
- export class RPC extends BaseRPC {
5
- constructor(endpoint, username, password) {
6
- super(endpoint);
7
- const authValue = Base64.encode(`${username}:${password}`);
8
- this.auth = `Basic ${authValue}`;
9
- }
10
- async post(method, params) {
11
- const headers = new Headers();
12
- headers.set(`Authorization`, this.auth);
13
- return super.post(method, params, headers);
14
- }
15
- getVersion() {
16
- return this.post(RPCMethod.GetVersion);
17
- }
18
- getNonce() {
19
- return this.post(RPCMethod.GetNonce);
20
- }
21
- getTopoheight() {
22
- return this.post(RPCMethod.GetTopoheight);
23
- }
24
- getAddress(params = {}) {
25
- return this.post(RPCMethod.GetAddress, params);
26
- }
27
- rescan() {
28
- return this.post(RPCMethod.Rescan);
29
- }
30
- splitAddress(params) {
31
- return this.post(RPCMethod.SplitAddress, params);
32
- }
33
- getBalance(asset) {
34
- return this.post(RPCMethod.GetBalance, { asset });
35
- }
36
- getTrackedAssets() {
37
- return this.post(RPCMethod.GetTrackedAssets);
38
- }
39
- getAssetPrecision(params) {
40
- return this.post(RPCMethod.GetAssetPrecision, params);
41
- }
42
- getTransaction(hash) {
43
- return this.post(RPCMethod.GetTransaction, { hash });
44
- }
45
- buildTransaction(params) {
46
- return this.post(RPCMethod.BuildTransaction, params);
47
- }
48
- listTransactions(params) {
49
- return this.post(RPCMethod.ListTransactions, params);
50
- }
51
- isOnline() {
52
- return this.post(RPCMethod.IsOnline);
53
- }
54
- }
55
- export default RPC;