@gnolang/tm2-js-client 1.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.
Files changed (77) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +78 -0
  3. package/bin/index.d.ts +4 -0
  4. package/bin/index.js +20 -0
  5. package/bin/proto/google/protobuf/any.d.ts +151 -0
  6. package/bin/proto/google/protobuf/any.js +124 -0
  7. package/bin/proto/index.d.ts +2 -0
  8. package/bin/proto/index.js +20 -0
  9. package/bin/proto/tm2/tx.d.ts +540 -0
  10. package/bin/proto/tm2/tx.js +365 -0
  11. package/bin/provider/endpoints.d.ts +28 -0
  12. package/bin/provider/endpoints.js +36 -0
  13. package/bin/provider/index.d.ts +6 -0
  14. package/bin/provider/index.js +22 -0
  15. package/bin/provider/jsonrpc/index.d.ts +1 -0
  16. package/bin/provider/jsonrpc/index.js +17 -0
  17. package/bin/provider/jsonrpc/jsonrpc.d.ts +27 -0
  18. package/bin/provider/jsonrpc/jsonrpc.js +219 -0
  19. package/bin/provider/jsonrpc/jsonrpc.test.d.ts +1 -0
  20. package/bin/provider/jsonrpc/jsonrpc.test.js +374 -0
  21. package/bin/provider/provider.d.ts +86 -0
  22. package/bin/provider/provider.js +2 -0
  23. package/bin/provider/types/abci.d.ts +39 -0
  24. package/bin/provider/types/abci.js +4 -0
  25. package/bin/provider/types/common.d.ts +154 -0
  26. package/bin/provider/types/common.js +2 -0
  27. package/bin/provider/types/index.d.ts +3 -0
  28. package/bin/provider/types/index.js +19 -0
  29. package/bin/provider/types/jsonrpc.d.ts +26 -0
  30. package/bin/provider/types/jsonrpc.js +2 -0
  31. package/bin/provider/utility/index.d.ts +2 -0
  32. package/bin/provider/utility/index.js +18 -0
  33. package/bin/provider/utility/provider.utility.d.ts +28 -0
  34. package/bin/provider/utility/provider.utility.js +194 -0
  35. package/bin/provider/utility/requests.utility.d.ts +33 -0
  36. package/bin/provider/utility/requests.utility.js +74 -0
  37. package/bin/provider/websocket/index.d.ts +1 -0
  38. package/bin/provider/websocket/index.js +17 -0
  39. package/bin/provider/websocket/ws.d.ts +55 -0
  40. package/bin/provider/websocket/ws.js +303 -0
  41. package/bin/provider/websocket/ws.test.d.ts +1 -0
  42. package/bin/provider/websocket/ws.test.js +535 -0
  43. package/bin/services/index.d.ts +2 -0
  44. package/bin/services/index.js +18 -0
  45. package/bin/services/rest/restService.d.ts +4 -0
  46. package/bin/services/rest/restService.js +72 -0
  47. package/bin/services/rest/restService.types.d.ts +6 -0
  48. package/bin/services/rest/restService.types.js +2 -0
  49. package/bin/wallet/index.d.ts +5 -0
  50. package/bin/wallet/index.js +21 -0
  51. package/bin/wallet/key/index.d.ts +1 -0
  52. package/bin/wallet/key/index.js +17 -0
  53. package/bin/wallet/key/key.d.ts +21 -0
  54. package/bin/wallet/key/key.js +102 -0
  55. package/bin/wallet/key/key.test.d.ts +1 -0
  56. package/bin/wallet/key/key.test.js +121 -0
  57. package/bin/wallet/ledger/index.d.ts +1 -0
  58. package/bin/wallet/ledger/index.js +17 -0
  59. package/bin/wallet/ledger/ledger.d.ts +22 -0
  60. package/bin/wallet/ledger/ledger.js +109 -0
  61. package/bin/wallet/signer.d.ts +29 -0
  62. package/bin/wallet/signer.js +2 -0
  63. package/bin/wallet/types/index.d.ts +2 -0
  64. package/bin/wallet/types/index.js +18 -0
  65. package/bin/wallet/types/sign.d.ts +17 -0
  66. package/bin/wallet/types/sign.js +4 -0
  67. package/bin/wallet/types/wallet.d.ts +5 -0
  68. package/bin/wallet/types/wallet.js +2 -0
  69. package/bin/wallet/utility/index.d.ts +1 -0
  70. package/bin/wallet/utility/index.js +17 -0
  71. package/bin/wallet/utility/utility.d.ts +30 -0
  72. package/bin/wallet/utility/utility.js +105 -0
  73. package/bin/wallet/wallet.d.ts +94 -0
  74. package/bin/wallet/wallet.js +340 -0
  75. package/bin/wallet/wallet.test.d.ts +1 -0
  76. package/bin/wallet/wallet.test.js +345 -0
  77. package/package.json +64 -0
@@ -0,0 +1,55 @@
1
+ /// <reference types="node" />
2
+ import { Provider } from '../provider';
3
+ import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, RPCRequest, RPCResponse, Status } from '../types';
4
+ import { Tx } from '../../proto';
5
+ /**
6
+ * Provider based on WS JSON-RPC HTTP requests
7
+ */
8
+ export declare class WSProvider implements Provider {
9
+ protected ws: WebSocket;
10
+ protected readonly requestMap: Map<number | string, {
11
+ resolve: (response: RPCResponse<any>) => void;
12
+ reject: (reason: Error) => void;
13
+ timeout: NodeJS.Timeout;
14
+ }>;
15
+ protected requestTimeout: number;
16
+ /**
17
+ * Creates a new instance of the {@link WSProvider}
18
+ * @param {string} baseURL the WS URL of the node
19
+ * @param {number} requestTimeout the timeout for the WS request (in MS)
20
+ */
21
+ constructor(baseURL: string, requestTimeout?: number);
22
+ /**
23
+ * Closes the WS connection. Required when done working
24
+ * with the WS provider
25
+ */
26
+ closeConnection(): void;
27
+ /**
28
+ * Sends a request to the WS connection, and resolves
29
+ * upon receiving the response
30
+ * @param {RPCRequest} request the RPC request
31
+ */
32
+ sendRequest<Result>(request: RPCRequest): Promise<RPCResponse<Result>>;
33
+ /**
34
+ * Parses the result from the response
35
+ * @param {RPCResponse<Result>} response the response to be parsed
36
+ */
37
+ parseResponse<Result>(response: RPCResponse<Result>): Result;
38
+ /**
39
+ * Waits for the WS connection to be established
40
+ */
41
+ waitForOpenConnection: () => Promise<null>;
42
+ estimateGas(tx: Tx): Promise<number>;
43
+ getBalance(address: string, denomination?: string, height?: number): Promise<number>;
44
+ getBlock(height: number): Promise<BlockInfo>;
45
+ getBlockResult(height: number): Promise<BlockResult>;
46
+ getBlockNumber(): Promise<number>;
47
+ getConsensusParams(height: number): Promise<ConsensusParams>;
48
+ getGasPrice(): Promise<number>;
49
+ getNetwork(): Promise<NetworkInfo>;
50
+ getAccountSequence(address: string, height?: number): Promise<number>;
51
+ getAccountNumber(address: string, height?: number): Promise<number>;
52
+ getStatus(): Promise<Status>;
53
+ sendTransaction(tx: string): Promise<string>;
54
+ waitForTransaction(hash: string, fromHeight?: number, timeout?: number): Promise<Tx>;
55
+ }
@@ -0,0 +1,303 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.WSProvider = void 0;
40
+ var utility_1 = require("../utility");
41
+ var endpoints_1 = require("../endpoints");
42
+ /**
43
+ * Provider based on WS JSON-RPC HTTP requests
44
+ */
45
+ var WSProvider = /** @class */ (function () {
46
+ /**
47
+ * Creates a new instance of the {@link WSProvider}
48
+ * @param {string} baseURL the WS URL of the node
49
+ * @param {number} requestTimeout the timeout for the WS request (in MS)
50
+ */
51
+ function WSProvider(baseURL, requestTimeout) {
52
+ var _this = this;
53
+ this.requestMap = new Map(); // callback method map for the individual endpoints
54
+ this.requestTimeout = 15000; // 15s
55
+ /**
56
+ * Waits for the WS connection to be established
57
+ */
58
+ this.waitForOpenConnection = function () {
59
+ return new Promise(function (resolve, reject) {
60
+ var maxNumberOfAttempts = 10;
61
+ var intervalTime = 200; //ms
62
+ var currentAttempt = 0;
63
+ var interval = setInterval(function () {
64
+ if (_this.ws.readyState === WebSocket.OPEN) {
65
+ clearInterval(interval);
66
+ resolve(null);
67
+ }
68
+ currentAttempt++;
69
+ if (currentAttempt > maxNumberOfAttempts - 1) {
70
+ clearInterval(interval);
71
+ reject(new Error('Maximum number of attempts exceeded'));
72
+ }
73
+ }, intervalTime);
74
+ });
75
+ };
76
+ this.ws = new WebSocket(baseURL);
77
+ this.ws.addEventListener('message', function (event) {
78
+ var response = JSON.parse(event.data);
79
+ var request = _this.requestMap.get(response.id);
80
+ if (request) {
81
+ _this.requestMap.delete(response.id);
82
+ clearTimeout(request.timeout);
83
+ request.resolve(response);
84
+ }
85
+ // Set the default timeout
86
+ _this.requestTimeout = requestTimeout ? requestTimeout : 15000;
87
+ });
88
+ }
89
+ /**
90
+ * Closes the WS connection. Required when done working
91
+ * with the WS provider
92
+ */
93
+ WSProvider.prototype.closeConnection = function () {
94
+ this.ws.close();
95
+ };
96
+ /**
97
+ * Sends a request to the WS connection, and resolves
98
+ * upon receiving the response
99
+ * @param {RPCRequest} request the RPC request
100
+ */
101
+ WSProvider.prototype.sendRequest = function (request) {
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ var promise;
104
+ var _this = this;
105
+ return __generator(this, function (_a) {
106
+ switch (_a.label) {
107
+ case 0:
108
+ if (!(this.ws.readyState != WebSocket.OPEN)) return [3 /*break*/, 2];
109
+ return [4 /*yield*/, this.waitForOpenConnection()];
110
+ case 1:
111
+ _a.sent();
112
+ _a.label = 2;
113
+ case 2:
114
+ promise = new Promise(function (resolve, reject) {
115
+ var timeout = setTimeout(function () {
116
+ _this.requestMap.delete(request.id);
117
+ reject(new Error('Request timed out'));
118
+ }, _this.requestTimeout);
119
+ _this.requestMap.set(request.id, { resolve: resolve, reject: reject, timeout: timeout });
120
+ });
121
+ this.ws.send(JSON.stringify(request));
122
+ return [2 /*return*/, promise];
123
+ }
124
+ });
125
+ });
126
+ };
127
+ /**
128
+ * Parses the result from the response
129
+ * @param {RPCResponse<Result>} response the response to be parsed
130
+ */
131
+ WSProvider.prototype.parseResponse = function (response) {
132
+ var _a;
133
+ if (!response) {
134
+ throw new Error('invalid response');
135
+ }
136
+ if (response.error) {
137
+ throw new Error((_a = response.error) === null || _a === void 0 ? void 0 : _a.message);
138
+ }
139
+ if (!response.result) {
140
+ throw new Error('invalid response returned');
141
+ }
142
+ return response.result;
143
+ };
144
+ WSProvider.prototype.estimateGas = function (tx) {
145
+ return Promise.reject('implement me');
146
+ };
147
+ WSProvider.prototype.getBalance = function (address, denomination, height) {
148
+ return __awaiter(this, void 0, void 0, function () {
149
+ var response, abciResponse;
150
+ return __generator(this, function (_a) {
151
+ switch (_a.label) {
152
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
153
+ "auth/accounts/".concat(address),
154
+ '',
155
+ '0',
156
+ false,
157
+ ]))];
158
+ case 1:
159
+ response = _a.sent();
160
+ abciResponse = this.parseResponse(response);
161
+ return [2 /*return*/, (0, utility_1.extractBalanceFromResponse)(abciResponse.response.ResponseBase.Data, denomination ? denomination : 'ugnot')];
162
+ }
163
+ });
164
+ });
165
+ };
166
+ WSProvider.prototype.getBlock = function (height) {
167
+ return __awaiter(this, void 0, void 0, function () {
168
+ var response;
169
+ return __generator(this, function (_a) {
170
+ switch (_a.label) {
171
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.BlockEndpoint.BLOCK, [height.toString()]))];
172
+ case 1:
173
+ response = _a.sent();
174
+ return [2 /*return*/, this.parseResponse(response)];
175
+ }
176
+ });
177
+ });
178
+ };
179
+ WSProvider.prototype.getBlockResult = function (height) {
180
+ return __awaiter(this, void 0, void 0, function () {
181
+ var response;
182
+ return __generator(this, function (_a) {
183
+ switch (_a.label) {
184
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.BlockEndpoint.BLOCK_RESULTS, [height.toString()]))];
185
+ case 1:
186
+ response = _a.sent();
187
+ return [2 /*return*/, this.parseResponse(response)];
188
+ }
189
+ });
190
+ });
191
+ };
192
+ WSProvider.prototype.getBlockNumber = function () {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ var status;
195
+ return __generator(this, function (_a) {
196
+ switch (_a.label) {
197
+ case 0: return [4 /*yield*/, this.getStatus()];
198
+ case 1:
199
+ status = _a.sent();
200
+ return [2 /*return*/, parseInt(status.sync_info.latest_block_height)];
201
+ }
202
+ });
203
+ });
204
+ };
205
+ WSProvider.prototype.getConsensusParams = function (height) {
206
+ return __awaiter(this, void 0, void 0, function () {
207
+ var response;
208
+ return __generator(this, function (_a) {
209
+ switch (_a.label) {
210
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.ConsensusEndpoint.CONSENSUS_PARAMS, [height.toString()]))];
211
+ case 1:
212
+ response = _a.sent();
213
+ return [2 /*return*/, this.parseResponse(response)];
214
+ }
215
+ });
216
+ });
217
+ };
218
+ WSProvider.prototype.getGasPrice = function () {
219
+ return Promise.reject('implement me');
220
+ };
221
+ WSProvider.prototype.getNetwork = function () {
222
+ return __awaiter(this, void 0, void 0, function () {
223
+ var response;
224
+ return __generator(this, function (_a) {
225
+ switch (_a.label) {
226
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.ConsensusEndpoint.NET_INFO))];
227
+ case 1:
228
+ response = _a.sent();
229
+ return [2 /*return*/, this.parseResponse(response)];
230
+ }
231
+ });
232
+ });
233
+ };
234
+ WSProvider.prototype.getAccountSequence = function (address, height) {
235
+ return __awaiter(this, void 0, void 0, function () {
236
+ var response, abciResponse;
237
+ return __generator(this, function (_a) {
238
+ switch (_a.label) {
239
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
240
+ "auth/accounts/".concat(address),
241
+ '',
242
+ '0',
243
+ false,
244
+ ]))];
245
+ case 1:
246
+ response = _a.sent();
247
+ abciResponse = this.parseResponse(response);
248
+ return [2 /*return*/, (0, utility_1.extractSequenceFromResponse)(abciResponse.response.ResponseBase.Data)];
249
+ }
250
+ });
251
+ });
252
+ };
253
+ WSProvider.prototype.getAccountNumber = function (address, height) {
254
+ return __awaiter(this, void 0, void 0, function () {
255
+ var response, abciResponse;
256
+ return __generator(this, function (_a) {
257
+ switch (_a.label) {
258
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.ABCIEndpoint.ABCI_QUERY, [
259
+ "auth/accounts/".concat(address),
260
+ '',
261
+ '0',
262
+ false,
263
+ ]))];
264
+ case 1:
265
+ response = _a.sent();
266
+ abciResponse = this.parseResponse(response);
267
+ return [2 /*return*/, (0, utility_1.extractAccountNumberFromResponse)(abciResponse.response.ResponseBase.Data)];
268
+ }
269
+ });
270
+ });
271
+ };
272
+ WSProvider.prototype.getStatus = function () {
273
+ return __awaiter(this, void 0, void 0, function () {
274
+ var response;
275
+ return __generator(this, function (_a) {
276
+ switch (_a.label) {
277
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.CommonEndpoint.STATUS))];
278
+ case 1:
279
+ response = _a.sent();
280
+ return [2 /*return*/, this.parseResponse(response)];
281
+ }
282
+ });
283
+ });
284
+ };
285
+ WSProvider.prototype.sendTransaction = function (tx) {
286
+ return __awaiter(this, void 0, void 0, function () {
287
+ var response;
288
+ return __generator(this, function (_a) {
289
+ switch (_a.label) {
290
+ case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC, [tx]))];
291
+ case 1:
292
+ response = _a.sent();
293
+ return [2 /*return*/, this.parseResponse(response).hash];
294
+ }
295
+ });
296
+ });
297
+ };
298
+ WSProvider.prototype.waitForTransaction = function (hash, fromHeight, timeout) {
299
+ return (0, utility_1.waitForTransaction)(this, hash, fromHeight, timeout);
300
+ };
301
+ return WSProvider;
302
+ }());
303
+ exports.WSProvider = WSProvider;
@@ -0,0 +1 @@
1
+ export {};