@alephium/web3 0.2.0-rc.1 → 0.2.0-rc.12

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 (99) hide show
  1. package/.eslintignore +2 -2
  2. package/README.md +2 -135
  3. package/contracts/greeter/greeter.ral +2 -2
  4. package/contracts/greeter/greeter_interface.ral +1 -0
  5. package/contracts/greeter_main.ral +1 -3
  6. package/dist/alephium-web3.min.js +1 -1
  7. package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
  8. package/dist/alephium-web3.min.js.map +1 -1
  9. package/dist/scripts/create-project.js +3 -2
  10. package/dist/src/api/api-alephium.d.ts +44 -11
  11. package/dist/src/api/api-alephium.js +81 -74
  12. package/dist/src/api/api-explorer.d.ts +16 -16
  13. package/dist/src/api/api-explorer.js +24 -33
  14. package/dist/src/api/index.d.ts +3 -2
  15. package/dist/src/api/index.js +22 -3
  16. package/dist/src/api/types.d.ts +23 -0
  17. package/dist/src/api/types.js +240 -0
  18. package/dist/src/api/utils.d.ts +6 -0
  19. package/{src/utils/djb2.test.ts → dist/src/api/utils.js} +11 -17
  20. package/dist/src/contract/contract.d.ts +104 -64
  21. package/dist/src/contract/contract.js +370 -426
  22. package/dist/src/contract/events.d.ts +4 -4
  23. package/dist/src/contract/index.js +5 -1
  24. package/dist/src/contract/ralph.d.ts +4 -4
  25. package/dist/src/global.d.ts +4 -0
  26. package/dist/src/global.js +38 -0
  27. package/dist/src/index.d.ts +2 -0
  28. package/dist/src/index.js +20 -1
  29. package/dist/src/signer/index.d.ts +0 -1
  30. package/dist/src/signer/index.js +5 -2
  31. package/dist/src/signer/signer.d.ts +21 -11
  32. package/dist/src/signer/signer.js +46 -9
  33. package/dist/src/transaction/index.d.ts +0 -1
  34. package/dist/src/transaction/index.js +5 -2
  35. package/dist/src/transaction/status.d.ts +2 -1
  36. package/dist/src/utils/index.d.ts +0 -1
  37. package/dist/src/utils/index.js +5 -2
  38. package/dist/src/utils/subscription.d.ts +0 -1
  39. package/dist/src/utils/subscription.js +2 -1
  40. package/dist/src/utils/utils.d.ts +5 -11
  41. package/dist/src/utils/utils.js +17 -25
  42. package/jest-config.json +11 -0
  43. package/package.json +8 -36
  44. package/scripts/create-project.ts +3 -2
  45. package/src/api/api-alephium.ts +54 -16
  46. package/src/api/api-explorer.ts +1 -11
  47. package/src/api/index.ts +14 -3
  48. package/src/api/types.ts +233 -0
  49. package/src/{utils/password-crypto.test.ts → api/utils.ts} +7 -9
  50. package/src/contract/contract.ts +537 -526
  51. package/src/contract/events.ts +6 -6
  52. package/src/contract/ralph.ts +4 -4
  53. package/src/{transaction/sign-verify.ts → global.ts} +14 -15
  54. package/src/index.ts +3 -0
  55. package/src/signer/index.ts +0 -1
  56. package/src/signer/signer.ts +70 -19
  57. package/src/transaction/index.ts +0 -1
  58. package/src/transaction/status.ts +4 -2
  59. package/src/utils/index.ts +0 -1
  60. package/src/utils/subscription.ts +3 -3
  61. package/src/utils/utils.ts +10 -20
  62. package/templates/base/package.json +2 -2
  63. package/templates/base/src/greeter.ts +10 -9
  64. package/templates/react/package.json +2 -2
  65. package/templates/react/src/App.tsx +2 -2
  66. package/.eslintrc.json +0 -21
  67. package/LICENSE +0 -165
  68. package/contracts/add/add.ral +0 -16
  69. package/contracts/main.ral +0 -6
  70. package/contracts/sub/sub.ral +0 -9
  71. package/dist/src/signer/node-wallet.d.ts +0 -13
  72. package/dist/src/signer/node-wallet.js +0 -60
  73. package/dist/src/test/index.d.ts +0 -7
  74. package/dist/src/test/index.js +0 -41
  75. package/dist/src/test/privatekey-wallet.d.ts +0 -12
  76. package/dist/src/test/privatekey-wallet.js +0 -68
  77. package/dist/src/transaction/sign-verify.d.ts +0 -2
  78. package/dist/src/transaction/sign-verify.js +0 -58
  79. package/dist/src/utils/password-crypto.d.ts +0 -2
  80. package/dist/src/utils/password-crypto.js +0 -69
  81. package/gitignore +0 -10
  82. package/src/contract/ralph.test.ts +0 -178
  83. package/src/fixtures/address.json +0 -36
  84. package/src/fixtures/balance.json +0 -9
  85. package/src/fixtures/self-clique.json +0 -19
  86. package/src/fixtures/transaction.json +0 -13
  87. package/src/fixtures/transactions.json +0 -179
  88. package/src/signer/fixtures/genesis.json +0 -26
  89. package/src/signer/fixtures/wallets.json +0 -26
  90. package/src/signer/node-wallet.ts +0 -74
  91. package/src/test/index.ts +0 -32
  92. package/src/test/privatekey-wallet.ts +0 -58
  93. package/src/transaction/sign-verify.test.ts +0 -50
  94. package/src/utils/address.test.ts +0 -47
  95. package/src/utils/password-crypto.ts +0 -77
  96. package/src/utils/utils.test.ts +0 -161
  97. package/test/contract.test.ts +0 -178
  98. package/test/events.test.ts +0 -138
  99. package/test/transaction.test.ts +0 -72
@@ -12,14 +12,7 @@
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.Api = exports.HttpClient = exports.ContentType = void 0;
14
14
  require("cross-fetch/polyfill");
15
- function convertHttpResponse(response) {
16
- if (response.error) {
17
- throw new Error(response.error.detail);
18
- }
19
- else {
20
- return response.data;
21
- }
22
- }
15
+ const utils_1 = require("./utils");
23
16
  var ContentType;
24
17
  (function (ContentType) {
25
18
  ContentType["Json"] = "application/json";
@@ -113,8 +106,6 @@ class HttpClient {
113
106
  if (cancelToken) {
114
107
  this.abortControllers.delete(cancelToken);
115
108
  }
116
- if (!response.ok)
117
- throw data;
118
109
  return data;
119
110
  });
120
111
  };
@@ -177,7 +168,7 @@ class Api extends HttpClient {
177
168
  query: query,
178
169
  format: 'json',
179
170
  ...params
180
- }).then(convertHttpResponse),
171
+ }).then(utils_1.convertHttpResponse),
181
172
  /**
182
173
  * @description Get a block with hash
183
174
  *
@@ -190,7 +181,7 @@ class Api extends HttpClient {
190
181
  method: 'GET',
191
182
  format: 'json',
192
183
  ...params
193
- }).then(convertHttpResponse),
184
+ }).then(utils_1.convertHttpResponse),
194
185
  /**
195
186
  * @description Get block's transactions
196
187
  *
@@ -204,7 +195,7 @@ class Api extends HttpClient {
204
195
  query: query,
205
196
  format: 'json',
206
197
  ...params
207
- }).then(convertHttpResponse)
198
+ }).then(utils_1.convertHttpResponse)
208
199
  };
209
200
  this.transactions = {
210
201
  /**
@@ -219,7 +210,7 @@ class Api extends HttpClient {
219
210
  method: 'GET',
220
211
  format: 'json',
221
212
  ...params
222
- }).then(convertHttpResponse)
213
+ }).then(utils_1.convertHttpResponse)
223
214
  };
224
215
  this.addresses = {
225
216
  /**
@@ -234,7 +225,7 @@ class Api extends HttpClient {
234
225
  method: 'GET',
235
226
  format: 'json',
236
227
  ...params
237
- }).then(convertHttpResponse),
228
+ }).then(utils_1.convertHttpResponse),
238
229
  /**
239
230
  * @description List transactions of a given address
240
231
  *
@@ -248,7 +239,7 @@ class Api extends HttpClient {
248
239
  query: query,
249
240
  format: 'json',
250
241
  ...params
251
- }).then(convertHttpResponse),
242
+ }).then(utils_1.convertHttpResponse),
252
243
  /**
253
244
  * @description Get total transactions of a given address
254
245
  *
@@ -261,7 +252,7 @@ class Api extends HttpClient {
261
252
  method: 'GET',
262
253
  format: 'json',
263
254
  ...params
264
- }).then(convertHttpResponse),
255
+ }).then(utils_1.convertHttpResponse),
265
256
  /**
266
257
  * @description Get address balance
267
258
  *
@@ -274,7 +265,7 @@ class Api extends HttpClient {
274
265
  method: 'GET',
275
266
  format: 'json',
276
267
  ...params
277
- }).then(convertHttpResponse)
268
+ }).then(utils_1.convertHttpResponse)
278
269
  };
279
270
  this.infos = {
280
271
  /**
@@ -289,7 +280,7 @@ class Api extends HttpClient {
289
280
  method: 'GET',
290
281
  format: 'json',
291
282
  ...params
292
- }).then(convertHttpResponse),
283
+ }).then(utils_1.convertHttpResponse),
293
284
  /**
294
285
  * @description List latest height for each chain
295
286
  *
@@ -302,7 +293,7 @@ class Api extends HttpClient {
302
293
  method: 'GET',
303
294
  format: 'json',
304
295
  ...params
305
- }).then(convertHttpResponse),
296
+ }).then(utils_1.convertHttpResponse),
306
297
  /**
307
298
  * @description Get token supply list
308
299
  *
@@ -316,7 +307,7 @@ class Api extends HttpClient {
316
307
  query: query,
317
308
  format: 'json',
318
309
  ...params
319
- }).then(convertHttpResponse),
310
+ }).then(utils_1.convertHttpResponse),
320
311
  /**
321
312
  * @description Get the ALPH total supply
322
313
  *
@@ -328,7 +319,7 @@ class Api extends HttpClient {
328
319
  path: `/infos/supply/total-alph`,
329
320
  method: 'GET',
330
321
  ...params
331
- }).then(convertHttpResponse),
322
+ }).then(utils_1.convertHttpResponse),
332
323
  /**
333
324
  * @description Get the ALPH circulating supply
334
325
  *
@@ -340,7 +331,7 @@ class Api extends HttpClient {
340
331
  path: `/infos/supply/circulating-alph`,
341
332
  method: 'GET',
342
333
  ...params
343
- }).then(convertHttpResponse),
334
+ }).then(utils_1.convertHttpResponse),
344
335
  /**
345
336
  * @description Get the ALPH reserved supply
346
337
  *
@@ -352,7 +343,7 @@ class Api extends HttpClient {
352
343
  path: `/infos/supply/reserved-alph`,
353
344
  method: 'GET',
354
345
  ...params
355
- }).then(convertHttpResponse),
346
+ }).then(utils_1.convertHttpResponse),
356
347
  /**
357
348
  * @description Get the ALPH locked supply
358
349
  *
@@ -364,7 +355,7 @@ class Api extends HttpClient {
364
355
  path: `/infos/supply/locked-alph`,
365
356
  method: 'GET',
366
357
  ...params
367
- }).then(convertHttpResponse),
358
+ }).then(utils_1.convertHttpResponse),
368
359
  /**
369
360
  * @description Get the total number of transactions
370
361
  *
@@ -376,7 +367,7 @@ class Api extends HttpClient {
376
367
  path: `/infos/total-transactions`,
377
368
  method: 'GET',
378
369
  ...params
379
- }).then(convertHttpResponse),
370
+ }).then(utils_1.convertHttpResponse),
380
371
  /**
381
372
  * @description Get the average block time for each chain
382
373
  *
@@ -389,7 +380,7 @@ class Api extends HttpClient {
389
380
  method: 'GET',
390
381
  format: 'json',
391
382
  ...params
392
- }).then(convertHttpResponse)
383
+ }).then(utils_1.convertHttpResponse)
393
384
  };
394
385
  this.charts = {
395
386
  /**
@@ -406,7 +397,7 @@ class Api extends HttpClient {
406
397
  query: query,
407
398
  format: 'json',
408
399
  ...params
409
- }).then(convertHttpResponse),
400
+ }).then(utils_1.convertHttpResponse),
410
401
  /**
411
402
  * @description `interval-type` query param: hourly, daily
412
403
  *
@@ -421,7 +412,7 @@ class Api extends HttpClient {
421
412
  query: query,
422
413
  format: 'json',
423
414
  ...params
424
- }).then(convertHttpResponse),
415
+ }).then(utils_1.convertHttpResponse),
425
416
  /**
426
417
  * @description `interval-type` query param: hourly, daily
427
418
  *
@@ -436,7 +427,7 @@ class Api extends HttpClient {
436
427
  query: query,
437
428
  format: 'json',
438
429
  ...params
439
- }).then(convertHttpResponse)
430
+ }).then(utils_1.convertHttpResponse)
440
431
  };
441
432
  this.utils = {
442
433
  /**
@@ -450,7 +441,7 @@ class Api extends HttpClient {
450
441
  path: `/utils/sanity-check`,
451
442
  method: 'PUT',
452
443
  ...params
453
- }).then(convertHttpResponse),
444
+ }).then(utils_1.convertHttpResponse),
454
445
  /**
455
446
  * @description Update global log level, accepted: TRACE, DEBUG, INFO, WARN, ERROR
456
447
  *
@@ -463,7 +454,7 @@ class Api extends HttpClient {
463
454
  method: 'PUT',
464
455
  body: data,
465
456
  ...params
466
- }).then(convertHttpResponse),
457
+ }).then(utils_1.convertHttpResponse),
467
458
  /**
468
459
  * @description Update logging file, only logback.xml is accepted
469
460
  *
@@ -476,7 +467,7 @@ class Api extends HttpClient {
476
467
  method: 'PUT',
477
468
  body: data,
478
469
  ...params
479
- }).then(convertHttpResponse)
470
+ }).then(utils_1.convertHttpResponse)
480
471
  };
481
472
  }
482
473
  }
@@ -1,10 +1,11 @@
1
1
  import { Api as NodeApi } from './api-alephium';
2
2
  import { Api as ExplorerApi } from './api-explorer';
3
- export declare class NodeProvider extends NodeApi<null> {
4
- constructor(baseUrl: string);
3
+ export declare class NodeProvider extends NodeApi<string> {
4
+ constructor(baseUrl: string, apiKey?: string);
5
5
  }
6
6
  export declare class ExplorerProvider extends ExplorerApi<null> {
7
7
  constructor(baseUrl: string);
8
8
  }
9
9
  export * as node from './api-alephium';
10
10
  export * as explorer from './api-explorer';
11
+ export * from './types';
@@ -18,7 +18,11 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
20
  if (k2 === undefined) k2 = k;
21
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
21
+ var desc = Object.getOwnPropertyDescriptor(m, k);
22
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
+ desc = { enumerable: true, get: function() { return m[k]; } };
24
+ }
25
+ Object.defineProperty(o, k2, desc);
22
26
  }) : (function(o, m, k, k2) {
23
27
  if (k2 === undefined) k2 = k;
24
28
  o[k2] = m[k];
@@ -35,13 +39,27 @@ var __importStar = (this && this.__importStar) || function (mod) {
35
39
  __setModuleDefault(result, mod);
36
40
  return result;
37
41
  };
42
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
43
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
44
+ };
38
45
  Object.defineProperty(exports, "__esModule", { value: true });
39
46
  exports.explorer = exports.node = exports.ExplorerProvider = exports.NodeProvider = void 0;
40
47
  const api_alephium_1 = require("./api-alephium");
41
48
  const api_explorer_1 = require("./api-explorer");
42
49
  class NodeProvider extends api_alephium_1.Api {
43
- constructor(baseUrl) {
44
- super({ baseUrl: baseUrl });
50
+ constructor(baseUrl, apiKey) {
51
+ // eslint-disable-next-line security/detect-possible-timing-attacks
52
+ if (apiKey === undefined) {
53
+ super({ baseUrl: baseUrl });
54
+ }
55
+ else {
56
+ super({
57
+ baseUrl: baseUrl,
58
+ baseApiParams: { secure: true },
59
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
60
+ });
61
+ this.setSecurityData(apiKey);
62
+ }
45
63
  }
46
64
  }
47
65
  exports.NodeProvider = NodeProvider;
@@ -53,3 +71,4 @@ class ExplorerProvider extends api_explorer_1.Api {
53
71
  exports.ExplorerProvider = ExplorerProvider;
54
72
  exports.node = __importStar(require("./api-alephium"));
55
73
  exports.explorer = __importStar(require("./api-explorer"));
74
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,23 @@
1
+ import * as node from './api-alephium';
2
+ export declare type Number256 = number | bigint | string;
3
+ export declare type Val = Number256 | boolean | string | Val[];
4
+ export declare type NamedVals = Record<string, Val>;
5
+ export interface Token {
6
+ id: string;
7
+ amount: Number256;
8
+ }
9
+ export declare function toApiToken(token: Token): node.Token;
10
+ export declare function toApiTokens(tokens?: Token[]): node.Token[] | undefined;
11
+ export declare function fromApiToken(token: node.Token): Token;
12
+ export declare function fromApiTokens(tokens?: node.Token[]): Token[] | undefined;
13
+ export declare function toApiBoolean(v: Val): boolean;
14
+ export declare function toApiNumber256(v: Val): string;
15
+ export declare function toApiNumber256Optional(v?: Val): string | undefined;
16
+ export declare function fromApiNumber256(n: string): Number256;
17
+ export declare function toApiByteVec(v: Val): string;
18
+ export declare function toApiAddress(v: Val): string;
19
+ export declare function toApiArray(tpe: string, v: Val): node.Val;
20
+ export declare function toApiVal(v: Val, tpe: string): node.Val;
21
+ export declare function fromApiVals(vals: node.Val[], names: string[], types: string[]): NamedVals;
22
+ export declare function fromApiArray(vals: node.Val[], types: string[]): Val[];
23
+ export declare function fromApiVal(v: node.Val, tpe: string): Val;
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.fromApiVal = exports.fromApiArray = exports.fromApiVals = exports.toApiVal = exports.toApiArray = exports.toApiAddress = exports.toApiByteVec = exports.fromApiNumber256 = exports.toApiNumber256Optional = exports.toApiNumber256 = exports.toApiBoolean = exports.fromApiTokens = exports.fromApiToken = exports.toApiTokens = exports.toApiToken = void 0;
21
+ const utils_1 = require("../utils");
22
+ utils_1.assertType;
23
+ function toApiToken(token) {
24
+ return { id: token.id, amount: toApiNumber256(token.amount) };
25
+ }
26
+ exports.toApiToken = toApiToken;
27
+ function toApiTokens(tokens) {
28
+ return tokens?.map(toApiToken);
29
+ }
30
+ exports.toApiTokens = toApiTokens;
31
+ function fromApiToken(token) {
32
+ return { id: token.id, amount: fromApiNumber256(token.amount) };
33
+ }
34
+ exports.fromApiToken = fromApiToken;
35
+ function fromApiTokens(tokens) {
36
+ return tokens?.map(fromApiToken);
37
+ }
38
+ exports.fromApiTokens = fromApiTokens;
39
+ function toApiBoolean(v) {
40
+ if (typeof v === 'boolean') {
41
+ return v;
42
+ }
43
+ else {
44
+ throw new Error(`Invalid boolean value: ${v}`);
45
+ }
46
+ }
47
+ exports.toApiBoolean = toApiBoolean;
48
+ // TODO: check integer bounds
49
+ function toApiNumber256(v) {
50
+ if ((typeof v === 'number' && Number.isInteger(v)) || typeof v === 'bigint') {
51
+ return v.toString();
52
+ }
53
+ else if (typeof v === 'string') {
54
+ return v;
55
+ }
56
+ else {
57
+ throw new Error(`Invalid 256 bit number: ${v}`);
58
+ }
59
+ }
60
+ exports.toApiNumber256 = toApiNumber256;
61
+ function toApiNumber256Optional(v) {
62
+ return v === undefined ? undefined : toApiNumber256(v);
63
+ }
64
+ exports.toApiNumber256Optional = toApiNumber256Optional;
65
+ function fromApiNumber256(n) {
66
+ if (Number.isSafeInteger(Number.parseInt(n))) {
67
+ return Number(n);
68
+ }
69
+ else {
70
+ return BigInt(n);
71
+ }
72
+ }
73
+ exports.fromApiNumber256 = fromApiNumber256;
74
+ // TODO: check hex string
75
+ function toApiByteVec(v) {
76
+ if (typeof v === 'string') {
77
+ // try to convert from address to contract id
78
+ try {
79
+ const address = utils_1.bs58.decode(v);
80
+ if (address.length == 33 && address[0] == 3) {
81
+ return Buffer.from(address.slice(1)).toString('hex');
82
+ }
83
+ }
84
+ catch (_) {
85
+ return v;
86
+ }
87
+ return v;
88
+ }
89
+ else {
90
+ throw new Error(`Invalid string: ${v}`);
91
+ }
92
+ }
93
+ exports.toApiByteVec = toApiByteVec;
94
+ function toApiAddress(v) {
95
+ if (typeof v === 'string') {
96
+ try {
97
+ utils_1.bs58.decode(v);
98
+ return v;
99
+ }
100
+ catch (error) {
101
+ throw new Error(`Invalid base58 string: ${v}`);
102
+ }
103
+ }
104
+ else {
105
+ throw new Error(`Invalid string: ${v}`);
106
+ }
107
+ }
108
+ exports.toApiAddress = toApiAddress;
109
+ function toApiArray(tpe, v) {
110
+ if (!Array.isArray(v)) {
111
+ throw new Error(`Expected array, got ${v}`);
112
+ }
113
+ const semiColonIndex = tpe.lastIndexOf(';');
114
+ if (semiColonIndex == -1) {
115
+ throw new Error(`Invalid Val type: ${tpe}`);
116
+ }
117
+ const subType = tpe.slice(1, semiColonIndex);
118
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
119
+ if (v.length != dim) {
120
+ throw new Error(`Invalid val dimension: ${v}`);
121
+ }
122
+ else {
123
+ return { value: v.map((v) => toApiVal(v, subType)), type: 'Array' };
124
+ }
125
+ }
126
+ exports.toApiArray = toApiArray;
127
+ function toApiVal(v, tpe) {
128
+ if (tpe === 'Bool') {
129
+ return { value: toApiBoolean(v), type: tpe };
130
+ }
131
+ else if (tpe === 'U256' || tpe === 'I256') {
132
+ return { value: toApiNumber256(v), type: tpe };
133
+ }
134
+ else if (tpe === 'ByteVec') {
135
+ return { value: toApiByteVec(v), type: tpe };
136
+ }
137
+ else if (tpe === 'Address') {
138
+ return { value: toApiAddress(v), type: tpe };
139
+ }
140
+ else {
141
+ return toApiArray(tpe, v);
142
+ }
143
+ }
144
+ exports.toApiVal = toApiVal;
145
+ function _fromApiVal(vals, valIndex, tpe) {
146
+ if (vals.length === 0) {
147
+ throw new Error('Not enough Vals');
148
+ }
149
+ const firstVal = vals[`${valIndex}`];
150
+ if (tpe === 'Bool' && firstVal.type === tpe) {
151
+ return [firstVal.value, valIndex + 1];
152
+ }
153
+ else if ((tpe === 'U256' || tpe === 'I256') && firstVal.type === tpe) {
154
+ return [fromApiNumber256(firstVal.value), valIndex + 1];
155
+ }
156
+ else if ((tpe === 'ByteVec' || tpe === 'Address') && firstVal.type === tpe) {
157
+ return [firstVal.value, valIndex + 1];
158
+ }
159
+ else {
160
+ const [baseType, dims] = decodeArrayType(tpe);
161
+ const arraySize = dims.reduce((a, b) => a * b);
162
+ const nextIndex = valIndex + arraySize;
163
+ const valsToUse = vals.slice(valIndex, nextIndex);
164
+ if (valsToUse.length == arraySize && valsToUse.every((val) => val.type === baseType)) {
165
+ const localVals = valsToUse.map((val) => fromApiVal(val, baseType));
166
+ return [foldVals(localVals, dims), nextIndex];
167
+ }
168
+ else {
169
+ throw new Error(`Invalid array Val type: ${valsToUse}, ${tpe}`);
170
+ }
171
+ }
172
+ }
173
+ function fromApiVals(vals, names, types) {
174
+ let valIndex = 0;
175
+ const result = {};
176
+ types.forEach((currentType, index) => {
177
+ const currentName = names[`${index}`];
178
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType);
179
+ valIndex = nextIndex;
180
+ result[`${currentName}`] = val;
181
+ });
182
+ return result;
183
+ }
184
+ exports.fromApiVals = fromApiVals;
185
+ function fromApiArray(vals, types) {
186
+ let valIndex = 0;
187
+ const result = [];
188
+ for (const currentType of types) {
189
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType);
190
+ result.push(val);
191
+ valIndex = nextIndex;
192
+ }
193
+ return result;
194
+ }
195
+ exports.fromApiArray = fromApiArray;
196
+ function fromApiVal(v, tpe) {
197
+ if (v.type === 'Bool' && v.type === tpe) {
198
+ return v.value;
199
+ }
200
+ else if ((v.type === 'U256' || v.type === 'I256') && v.type === tpe) {
201
+ return fromApiNumber256(v.value);
202
+ }
203
+ else if ((v.type === 'ByteVec' || v.type === 'Address') && v.type === tpe) {
204
+ return v.value;
205
+ }
206
+ else {
207
+ throw new Error(`Invalid node.Val type: ${v}`);
208
+ }
209
+ }
210
+ exports.fromApiVal = fromApiVal;
211
+ function decodeArrayType(tpe) {
212
+ const semiColonIndex = tpe.lastIndexOf(';');
213
+ if (semiColonIndex === -1) {
214
+ throw new Error(`Invalid Val type: ${tpe}`);
215
+ }
216
+ const subType = tpe.slice(1, semiColonIndex);
217
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
218
+ if (subType[0] == '[') {
219
+ const [baseType, subDim] = decodeArrayType(subType);
220
+ return [baseType, (subDim.unshift(dim), subDim)];
221
+ }
222
+ else {
223
+ return [subType, [dim]];
224
+ }
225
+ }
226
+ function foldVals(vals, dims) {
227
+ if (dims.length == 1) {
228
+ return vals;
229
+ }
230
+ else {
231
+ const result = [];
232
+ const chunkSize = vals.length / dims[0];
233
+ const chunkDims = dims.slice(1);
234
+ for (let i = 0; i < vals.length; i += chunkSize) {
235
+ const chunk = vals.slice(i, i + chunkSize);
236
+ result.push(foldVals(chunk, chunkDims));
237
+ }
238
+ return result;
239
+ }
240
+ }
@@ -0,0 +1,6 @@
1
+ export declare function convertHttpResponse<T>(response: {
2
+ data: T;
3
+ error?: {
4
+ detail: string;
5
+ };
6
+ }): T;
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  /*
2
3
  Copyright 2018 - 2022 The Alephium Authors
3
4
  This file is part of the alephium project.
@@ -15,21 +16,14 @@ GNU Lesser General Public License for more details.
15
16
  You should have received a copy of the GNU Lesser General Public License
16
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
18
  */
18
-
19
- import { Buffer } from 'buffer/'
20
- import djb2 from './djb2'
21
-
22
- describe('djb2', function () {
23
- it('djb2', async () => {
24
- function check(str: string, expected: number) {
25
- const bytes = Buffer.from(str, 'utf8')
26
- expect(djb2(bytes)).toEqual(expected)
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.convertHttpResponse = void 0;
21
+ function convertHttpResponse(response) {
22
+ if (response.error) {
23
+ throw new Error(`[Node API Error] - ${response.error.detail}`);
27
24
  }
28
-
29
- check('', 5381)
30
- check('a', 177670)
31
- check('z', 177695)
32
- check('foo', 193491849)
33
- check('bar', 193487034)
34
- })
35
- })
25
+ else {
26
+ return response.data;
27
+ }
28
+ }
29
+ exports.convertHttpResponse = convertHttpResponse;