@tchain/tcabci-read-js-client 2.2.4 → 2.4.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 CHANGED
@@ -4,6 +4,11 @@ TransferChain Fastest Read Network WebSocket Client
4
4
  Read Node Address: [https://read-node-01.transferchain.io](https://read-node-01.transferchain.io)
5
5
  Read Node WebSocket Address: [wss://read-node-01.transferchain.io/ws](wss://read-node-01.transferchain.io/ws)
6
6
 
7
+ ## Networks
8
+
9
+ - transferchain - v1
10
+ - medusa - v2 (new blocks)
11
+
7
12
  ## Installation
8
13
 
9
14
  ```shell
@@ -23,6 +28,17 @@ const client = new TCaBCIClient([
23
28
  'https://read-node-01.transferchain.io',
24
29
  'wss://read-node-01.transferchain.io/ws',
25
30
  ])
31
+ // OR
32
+ // With addresses and WebSocket library and network name, version
33
+ const client = new TCaBCIClient(
34
+ [
35
+ 'https://read-node-01.transferchain.io',
36
+ 'wss://read-node-01.transferchain.io/ws',
37
+ ],
38
+ WebSocket,
39
+ 'transferchain',
40
+ 'v1',
41
+ )
26
42
 
27
43
  client.Start()
28
44
 
package/client.js CHANGED
@@ -18,8 +18,7 @@ import {
18
18
  TX_TYPE,
19
19
  } from './constants.js'
20
20
  import Message from './message.js'
21
- import { toJSON } from './util.js'
22
-
21
+ import { isJSON, toJSON } from './util.js'
23
22
 
24
23
  /**
25
24
  * @callback successCallback
@@ -44,8 +43,11 @@ import { toJSON } from './util.js'
44
43
  export default class TCaBCIClient {
45
44
  subscribed = false
46
45
  subscribedAddresses = []
46
+ subscribedSignedData = {}
47
47
  connected = false
48
- version = 'v2.2.4'
48
+ chainName = 'transferchain'
49
+ chainVersion = 'v1'
50
+ version = 'v2.4.0'
49
51
  /**
50
52
  * @type {?successCallback}
51
53
  */
@@ -70,18 +72,30 @@ export default class TCaBCIClient {
70
72
  /**
71
73
  * @param {Array<String>} readNodeAddresses
72
74
  * @param {WebSocket} wsLibrary
75
+ * @param {?string} chainName
76
+ * @param {?string} chainVersion
73
77
  */
74
- constructor(readNodeAddresses = [], wsLibrary) {
75
- if (!wsLibrary) throw INVALID_ARGUMENTS
78
+ constructor(
79
+ readNodeAddresses = [],
80
+ wsLibrary,
81
+ chainName = null,
82
+ chainVersion = null,
83
+ ) {
84
+ if (!wsLibrary) throw new Error(INVALID_ARGUMENTS)
76
85
  this.wsLibrary = wsLibrary
77
86
 
78
87
  if (Array.isArray(readNodeAddresses) && readNodeAddresses.length === 2) {
79
88
  this.readNodeAddress = readNodeAddresses[0]
80
89
  this.readNodeWSAddress = readNodeAddresses[1]
81
90
 
82
- if (!readNodeAddresses[0].startsWith('http')) throw INVALID_ARGUMENTS
83
- if (!readNodeAddresses[1].startsWith('ws')) throw INVALID_ARGUMENTS
91
+ if (!readNodeAddresses[0].startsWith('http'))
92
+ throw new Error(INVALID_ARGUMENTS)
93
+ if (!readNodeAddresses[1].startsWith('ws'))
94
+ throw new Error(INVALID_ARGUMENTS)
84
95
  }
96
+
97
+ if (chainName) this.chainName = chainName
98
+ if (chainVersion) this.chainVersion = chainVersion
85
99
  }
86
100
 
87
101
  /**
@@ -154,9 +168,13 @@ export default class TCaBCIClient {
154
168
  return this.connect()
155
169
  }
156
170
 
171
+ /**
172
+ * @return {TCaBCIClient}
173
+ * @throws {Error}
174
+ */
157
175
  Stop() {
158
176
  if (!this.getConnected()) {
159
- throw NOT_CONNECTED
177
+ throw new Error(NOT_CONNECTED)
160
178
  }
161
179
  this.Disconnect(1000)
162
180
  this.setConnected(false)
@@ -165,30 +183,42 @@ export default class TCaBCIClient {
165
183
  return this
166
184
  }
167
185
 
168
- Subscribe(addresses = [], txTypes = []) {
186
+ /**
187
+ * @param {Array<string>} addresses
188
+ * @param {Object} signedData
189
+ * @param {?Array<string>} txTypes
190
+ * @return {TCaBCIClient}
191
+ */
192
+ Subscribe(addresses, signedData, txTypes = null) {
169
193
  if (!Array.isArray(addresses)) {
170
- throw INVALID_ARGUMENTS
194
+ throw new Error(INVALID_ARGUMENTS)
195
+ }
196
+
197
+ if (typeof signedData !== 'object') {
198
+ throw new Error(INVALID_ARGUMENTS)
171
199
  }
172
200
 
173
201
  if (txTypes && !Array.isArray(addresses)) {
174
- throw INVALID_ARGUMENTS
202
+ throw new Error(INVALID_ARGUMENTS)
175
203
  }
176
204
 
177
- if (txTypes.length && txTypes.length > Object.values(TX_TYPE).length) {
178
- throw INVALID_ARGUMENTS
205
+ if (txTypes && txTypes.length > Object.values(TX_TYPE).length) {
206
+ throw new Error(INVALID_ARGUMENTS)
179
207
  }
180
208
 
181
209
  if (!this.getConnected()) {
182
- throw NOT_CONNECTED
210
+ throw new Error(NOT_CONNECTED)
183
211
  }
184
212
 
185
- let addrs = []
213
+ let addrs = [],
214
+ sdata = {}
186
215
 
187
216
  if (addresses.length === 0) {
188
- throw ADDRESSES_IS_EMPTY
217
+ throw new Error(ADDRESSES_IS_EMPTY)
189
218
  }
190
219
 
191
220
  addrs = addresses
221
+ sdata = signedData
192
222
 
193
223
  if (this.getSubscribeAddresses().length > 0) {
194
224
  let newAddress = []
@@ -201,47 +231,74 @@ export default class TCaBCIClient {
201
231
  addrs = newAddress
202
232
  }
203
233
 
204
- const message = new Message(true, MESSAGE_TYPE.SUBSCRIBE, addrs, txTypes)
234
+ if (this.getSubscribedSignedData().length > 0) {
235
+ sdata = this.getSubscribedSignedData()
236
+ }
237
+
238
+ const message = new Message(
239
+ true,
240
+ MESSAGE_TYPE.SUBSCRIBE,
241
+ addrs,
242
+ sdata,
243
+ txTypes,
244
+ )
205
245
 
206
- this.ws.send(message.ToJSONString())
246
+ this.ws.send(message.ToJSON())
207
247
  this.setSubscribeAddresses(addrs, true)
208
- this.setSubscribed(true)
248
+ .setSubscribeSignedData(signedData)
249
+ .setSubscribed(true)
250
+
251
+ return this
209
252
  }
210
253
 
211
254
  Unsubscribe() {
212
255
  if (!this.getSubscribed()) {
213
- throw NOT_SUBSCRIBED
256
+ throw new Error(NOT_SUBSCRIBED)
214
257
  }
215
258
  this.ws.send(
216
259
  new Message(
217
260
  true,
218
261
  MESSAGE_TYPE.UNSUBSCRIBE,
219
262
  this.getSubscribeAddresses(),
220
- ).ToJSONString(),
263
+ ).ToJSON(),
221
264
  )
222
265
  this.setSubscribed(false)
223
266
  this.setSubscribeAddresses([])
267
+ this.setSubscribeSignedData({})
224
268
  }
225
269
 
226
270
  /**
271
+ * @param {?string} chainName
272
+ * @param {?string} chainVersion
227
273
  * @return {Promise<any>}
228
274
  */
229
- LastBlock() {
230
- return this.httpClient('/v1/blocks?limit=1&offset=0', {
231
- method: 'GET',
232
- })
275
+ LastBlock(chainName = null, chainVersion = null) {
276
+ return this.httpClient(
277
+ `/v1/blocks?chain_name=${chainName ?? this.chainName}&chain_version=${chainVersion ?? this.chainVersion}&limit=1&offset=0`,
278
+ {
279
+ method: 'GET',
280
+ },
281
+ )
233
282
  .then((res) => {
234
283
  return { blocks: res.data, total_count: res.total_count }
235
284
  })
236
- .catch((e) => this.handleRestError(e, BLOCK_NOT_FOUND))
285
+ .catch((e) => this.handleRestError(e, new Error(BLOCK_NOT_FOUND)))
237
286
  }
238
287
 
239
- Tx(id) {
288
+ /**
289
+ * @param {string} id
290
+ * @param {string} signature
291
+ * @return {Promise<*>}
292
+ */
293
+ Tx(id, signature) {
240
294
  if (!id || typeof id !== 'string') {
241
- return Promise.reject(INVALID_ARGUMENTS)
295
+ return Promise.reject(new Error(INVALID_ARGUMENTS))
242
296
  }
243
297
 
244
- return this.httpClient(`/v1/tx/${id}`, { method: 'GET' })
298
+ return this.httpClient(`/v1/tx/${id}`, {
299
+ method: 'GET',
300
+ headers: { 'X-Signature': signature },
301
+ })
245
302
  .then((res) => {
246
303
  return { tx: res.data }
247
304
  })
@@ -251,25 +308,41 @@ export default class TCaBCIClient {
251
308
  /**
252
309
  * @param {?Array<string>} recipientAddrs
253
310
  * @param {?Array<string>} senderAddrs
311
+ * @param {?Object} signedData
254
312
  * @param {?string} typ
255
- * @param {?Array<string>}
313
+ * @param {?Array<string>} types
314
+ * @param {?string} chainName
315
+ * @param {?string} chainVersion
256
316
  * @return {Promise<any>}
257
317
  */
258
- TxSummary({ recipientAddrs, senderAddrs, typ, types }) {
318
+ TxSummary({
319
+ recipientAddrs,
320
+ senderAddrs,
321
+ signedData,
322
+ typ,
323
+ types = null,
324
+ chainName = null,
325
+ chainVersion = null,
326
+ }) {
259
327
  if (!recipientAddrs && !senderAddrs) {
260
- return Promise.reject(INVALID_ARGUMENTS)
328
+ return Promise.reject(new Error(INVALID_ARGUMENTS))
261
329
  }
262
330
 
263
331
  return this.httpClient('/v1/tx_summary', {
264
332
  method: 'POST',
265
333
  body: JSON.stringify({
334
+ chain_name: chainName ?? this.chainName,
335
+ chain_version: chainVersion ?? this.chainVersion,
266
336
  recipient_addrs: recipientAddrs,
267
337
  sender_addrs: senderAddrs,
338
+ signed_addrs: signedData,
268
339
  ...(types ? { types: types } : { typ: typ }),
269
340
  }),
270
341
  })
271
342
  .then((res) => {
272
343
  return {
344
+ chain_name: res.data.chain_name,
345
+ chain_version: res.data.chain_version,
273
346
  first_block_height: res.data.first_block_height,
274
347
  first_transaction: res.data.first_transaction,
275
348
  last_block_height: res.data.last_block_height,
@@ -287,6 +360,7 @@ export default class TCaBCIClient {
287
360
  * @param {?number} lastOrder
288
361
  * @param {?Array<string>} recipientAddrs
289
362
  * @param {?Array<string>} senderAddrs
363
+ * @param {?Object} signedData
290
364
  * @param {?Array<string>} hashes
291
365
  * @param {string} typ
292
366
  * @param {?Array<string>} types
@@ -294,6 +368,8 @@ export default class TCaBCIClient {
294
368
  * @param {number} offset
295
369
  * @param {string} orderField
296
370
  * @param {string} orderBy
371
+ * @param {?string} chainName
372
+ * @param {?string} chainVersion
297
373
  * @return {Promise<any>}
298
374
  */
299
375
  TxSearch({
@@ -303,6 +379,7 @@ export default class TCaBCIClient {
303
379
  lastOrder,
304
380
  recipientAddrs,
305
381
  senderAddrs,
382
+ signedData,
306
383
  hashes,
307
384
  typ,
308
385
  types,
@@ -310,15 +387,20 @@ export default class TCaBCIClient {
310
387
  offset,
311
388
  orderField,
312
389
  orderBy,
390
+ chainName = null,
391
+ chainVersion = null,
313
392
  }) {
314
393
  return this.httpClient('/v1/tx_search/p', {
315
394
  method: 'POST',
316
395
  body: JSON.stringify({
396
+ chain_name: chainName ?? this.chainName,
397
+ chain_version: chainVersion ?? this.chainVersion,
317
398
  height: `${heightOperator} ${height}`,
318
399
  ...(maxHeight ? { max_height: maxHeight } : {}),
319
400
  ...(lastOrder ? { last_order: lastOrder } : {}),
320
401
  ...(recipientAddrs ? { recipient_addrs: recipientAddrs } : {}),
321
402
  ...(senderAddrs ? { sender_addrs: senderAddrs } : {}),
403
+ ...(signedData ? { signed_addrs: signedData } : {}),
322
404
  ...(hashes ? { hashes: hashes } : {}),
323
405
  ...(limit ? { limit: limit } : {}),
324
406
  ...(offset ? { offset: offset } : {}),
@@ -338,6 +420,8 @@ export default class TCaBCIClient {
338
420
 
339
421
  Status() {
340
422
  return {
423
+ chain_name: this.chainName,
424
+ chain_version: this.chainVersion,
341
425
  connected: this.connected,
342
426
  subscribed: this.subscribed,
343
427
  }
@@ -354,7 +438,7 @@ export default class TCaBCIClient {
354
438
  * @param {number} fee
355
439
  * @return {Promise<any>}
356
440
  */
357
- Broadcast({
441
+ BroadcastCommit({
358
442
  id,
359
443
  version,
360
444
  type,
@@ -364,13 +448,45 @@ export default class TCaBCIClient {
364
448
  sign,
365
449
  fee,
366
450
  }) {
367
- if (Object.values(TX_TYPE).indexOf(type) < 0) {
368
- throw TRANSACTION_TYPE_NOT_VALID
369
- }
451
+ return this.broadcast(
452
+ {
453
+ id,
454
+ version,
455
+ type,
456
+ data,
457
+ sender_addr,
458
+ recipient_addr,
459
+ sign,
460
+ fee,
461
+ },
462
+ false,
463
+ true,
464
+ )
465
+ }
370
466
 
371
- return this.httpClient('/v1/tx', {
372
- method: 'POST',
373
- body: JSON.stringify({
467
+ /**
468
+ * @param {string} id
469
+ * @param {number} version
470
+ * @param {string} type
471
+ * @param {string} data
472
+ * @param {string} sender_addr
473
+ * @param {string} recipient_addr
474
+ * @param {string} sign
475
+ * @param {number} fee
476
+ * @return {Promise<any>}
477
+ */
478
+ BroadcastSync({
479
+ id,
480
+ version,
481
+ type,
482
+ data,
483
+ sender_addr,
484
+ recipient_addr,
485
+ sign,
486
+ fee,
487
+ }) {
488
+ return this.broadcast(
489
+ {
374
490
  id,
375
491
  version,
376
492
  type,
@@ -379,25 +495,105 @@ export default class TCaBCIClient {
379
495
  recipient_addr,
380
496
  sign,
381
497
  fee,
382
- }),
383
- })
498
+ },
499
+ true,
500
+ false,
501
+ )
502
+ }
503
+
504
+ /**
505
+ * @param {string} id
506
+ * @param {number} version
507
+ * @param {string} type
508
+ * @param {string} data
509
+ * @param {string} sender_addr
510
+ * @param {string} recipient_addr
511
+ * @param {string} sign
512
+ * @param {number} fee
513
+ * @return {Promise<any>}
514
+ */
515
+ Broadcast({
516
+ id,
517
+ version,
518
+ type,
519
+ data,
520
+ sender_addr,
521
+ recipient_addr,
522
+ sign,
523
+ fee,
524
+ }) {
525
+ return this.broadcast(
526
+ {
527
+ id,
528
+ version,
529
+ type,
530
+ data,
531
+ sender_addr,
532
+ recipient_addr,
533
+ sign,
534
+ fee,
535
+ },
536
+ false,
537
+ false,
538
+ )
539
+ }
540
+
541
+ broadcast(
542
+ { id, version, type, data, sender_addr, recipient_addr, sign, fee },
543
+ sync = false,
544
+ commit = false,
545
+ ) {
546
+ if (Object.values(TX_TYPE).indexOf(type) < 0) {
547
+ throw new Error(TRANSACTION_TYPE_NOT_VALID)
548
+ }
549
+
550
+ return this.httpClient(
551
+ commit ? '/v1/tx/commit' : sync ? '/v1/tx/sync' : '/v1/tx',
552
+ {
553
+ method: 'POST',
554
+ body: JSON.stringify({
555
+ id,
556
+ version,
557
+ type,
558
+ data,
559
+ sender_addr,
560
+ recipient_addr,
561
+ sign,
562
+ fee,
563
+ }),
564
+ },
565
+ )
384
566
  .then((res) => {
385
567
  return { data: res.data }
386
568
  })
387
- .catch((e) => this.handleRestError(e, { 400: TRANSACTION_NOT_BROADCAST }))
569
+ .catch((e) =>
570
+ this.handleRestError(e, { 400: new Error(TRANSACTION_NOT_BROADCAST) }),
571
+ )
388
572
  }
389
573
 
390
574
  /**
391
575
  * @param {Array<string>} addresses
576
+ * @param {Object} signedData
392
577
  * @param {?number} maxHeight
578
+ * @param {?string} chainName
579
+ * @param {?string} chainVersion
393
580
  * @return {Promise<*>}
394
581
  * @constructor
395
582
  */
396
- Bulk(addresses = [], maxHeight = null) {
583
+ Bulk(
584
+ addresses = [],
585
+ signedData = {},
586
+ maxHeight = null,
587
+ chainName = null,
588
+ chainVersion = null,
589
+ ) {
397
590
  return this.httpClient('/v1/bulk_tx', {
398
591
  method: 'POST',
399
592
  body: JSON.stringify({
593
+ chain_name: chainName ?? this.chainName,
594
+ chain_version: chainVersion ?? this.chainVersion,
400
595
  addresses: addresses,
596
+ signed_addrs: signedData,
401
597
  ...(maxHeight ? { max_height: maxHeight } : {}),
402
598
  }),
403
599
  })
@@ -433,13 +629,12 @@ export default class TCaBCIClient {
433
629
  if (this.listenCb) this.listenCb(message)
434
630
  }
435
631
 
436
-
437
632
  /**
438
633
  * @return {Promise<Event>}
439
634
  */
440
635
  async connect() {
441
636
  if (this.getConnected()) {
442
- return Promise.reject(ALREADY_CONNECTED)
637
+ return Promise.reject(new Error(ALREADY_CONNECTED))
443
638
  }
444
639
 
445
640
  const options = {
@@ -455,6 +650,7 @@ export default class TCaBCIClient {
455
650
  this.setConnected(false)
456
651
  this.setSubscribed(false)
457
652
  this.setSubscribeAddresses([], false)
653
+ this.setSubscribeSignedData({})
458
654
  this.callErrorCallback(event)
459
655
 
460
656
  reject(event)
@@ -470,12 +666,13 @@ export default class TCaBCIClient {
470
666
  return { status: message.data }
471
667
  }
472
668
 
473
- this.callListenCallback(toJSON(message.data))
669
+ this.callListenCallback(!isJSON(message.data) ? message.data : toJSON(message.data))
474
670
  }
475
671
  this.ws.onclose = (event) => {
476
672
  this.setConnected(false)
477
673
  this.setSubscribed(false)
478
674
  this.setSubscribeAddresses([], false)
675
+ this.setSubscribeSignedData({})
479
676
  this.callCloseCallback(event)
480
677
 
481
678
  resolve(event)
@@ -503,13 +700,34 @@ export default class TCaBCIClient {
503
700
  return this.subscribedAddresses
504
701
  }
505
702
 
703
+ getSubscribedSignedData() {
704
+ return this.subscribedSignedData
705
+ }
706
+
707
+ /**
708
+ * @param {Array<string>} addresses
709
+ * @param {?boolean} push
710
+ * @return {TCaBCIClient}
711
+ */
506
712
  setSubscribeAddresses(addresses, push = false) {
507
713
  if (push) {
508
714
  this.subscribedAddresses.push(...addresses)
509
715
 
510
- return
716
+ return this
511
717
  }
512
718
  this.subscribedAddresses = addresses
719
+
720
+ return this
721
+ }
722
+
723
+ /**
724
+ * @param {Object} signedData
725
+ * @return {TCaBCIClient}
726
+ */
727
+ setSubscribeSignedData(signedData) {
728
+ this.subscribedSignedData = signedData
729
+
730
+ return this
513
731
  }
514
732
 
515
733
  /**
@@ -537,6 +755,7 @@ export default class TCaBCIClient {
537
755
  /**
538
756
  * @param {Error} err
539
757
  * @param {?Object|Error} custom
758
+ * @throws Error
540
759
  */
541
760
  handleRestError(err, custom = null) {
542
761
  const code = err.code
@@ -549,9 +768,9 @@ export default class TCaBCIClient {
549
768
 
550
769
  switch (code) {
551
770
  case 400:
552
- throw INVALID_ARGUMENTS
771
+ throw new Error(INVALID_ARGUMENTS)
553
772
  case 'ERR_NETWORK':
554
- throw ERR_NETWORK
773
+ throw new Error(ERR_NETWORK)
555
774
  default:
556
775
  throw err
557
776
  }
package/client_test.js CHANGED
@@ -21,7 +21,7 @@ import {
21
21
 
22
22
  describe('TCaBCIClient', () => {
23
23
  it('start with valid parameters', (done) => {
24
- const client = new TCaBCIClient([], WebSocket)
24
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
25
25
 
26
26
  client
27
27
  .Start()
@@ -38,7 +38,12 @@ describe('TCaBCIClient', () => {
38
38
  })
39
39
 
40
40
  it('start with valid parameters and read node addresses', (done) => {
41
- const client = new TCaBCIClient([READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS], WebSocket)
41
+ const client = new TCaBCIClient(
42
+ [READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS],
43
+ WebSocket,
44
+ 'transferchain',
45
+ 'v1',
46
+ )
42
47
 
43
48
  client
44
49
  .Start()
@@ -55,7 +60,7 @@ describe('TCaBCIClient', () => {
55
60
  })
56
61
 
57
62
  it('reconnect with start and valid parameters', (done) => {
58
- const client = new TCaBCIClient([], WebSocket)
63
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
59
64
 
60
65
  client
61
66
  .Start()
@@ -71,13 +76,18 @@ describe('TCaBCIClient', () => {
71
76
  unitJS.assert.equal(c2, false)
72
77
  unitJS.assert.equal(b2, false)
73
78
 
74
- setTimeout((client, done) => {
75
- const { connected, subscribed } = client.Status()
76
-
77
- unitJS.assert.equal(connected, true)
78
- unitJS.assert.equal(subscribed, false)
79
- done()
80
- }, 3100, client, done)
79
+ setTimeout(
80
+ (client, done) => {
81
+ const { connected, subscribed } = client.Status()
82
+
83
+ unitJS.assert.equal(connected, true)
84
+ unitJS.assert.equal(subscribed, false)
85
+ done()
86
+ },
87
+ 3100,
88
+ client,
89
+ done,
90
+ )
81
91
  })
82
92
  .catch((err) => {
83
93
  done(err)
@@ -85,7 +95,7 @@ describe('TCaBCIClient', () => {
85
95
  })
86
96
 
87
97
  it('subscribe with valid parameters', (done) => {
88
- const client = new TCaBCIClient([], WebSocket)
98
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
89
99
 
90
100
  client
91
101
  .Start()
@@ -110,7 +120,7 @@ describe('TCaBCIClient', () => {
110
120
  })
111
121
 
112
122
  it('should error subscribe with invalid tx type parameter', (done) => {
113
- const client = new TCaBCIClient([], WebSocket)
123
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
114
124
 
115
125
  client
116
126
  .Start()
@@ -129,7 +139,7 @@ describe('TCaBCIClient', () => {
129
139
  })
130
140
 
131
141
  it('unsubscribe with valid parameters', (done) => {
132
- const client = new TCaBCIClient([], WebSocket)
142
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
133
143
 
134
144
  client
135
145
  .Start()
@@ -158,7 +168,7 @@ describe('TCaBCIClient', () => {
158
168
  })
159
169
 
160
170
  it('last block', (done) => {
161
- const client = new TCaBCIClient([], WebSocket)
171
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
162
172
 
163
173
  client
164
174
  .LastBlock()
@@ -173,7 +183,7 @@ describe('TCaBCIClient', () => {
173
183
  })
174
184
 
175
185
  it('transaction search result', (done) => {
176
- const client = new TCaBCIClient([], WebSocket)
186
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
177
187
 
178
188
  client
179
189
  .TxSearch({
@@ -197,7 +207,7 @@ describe('TCaBCIClient', () => {
197
207
  })
198
208
  //
199
209
  it('transaction summary result', (done) => {
200
- const client = new TCaBCIClient([], WebSocket)
210
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
201
211
 
202
212
  client
203
213
  .TxSummary({
@@ -220,7 +230,7 @@ describe('TCaBCIClient', () => {
220
230
  })
221
231
 
222
232
  it('should error not broadcast transaction if type is incorrect', (done) => {
223
- const client = new TCaBCIClient([], WebSocket)
233
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
224
234
 
225
235
  try {
226
236
  client.Broadcast({
@@ -232,13 +242,13 @@ describe('TCaBCIClient', () => {
232
242
 
233
243
  done(new Error('invalid'))
234
244
  } catch (err) {
235
- unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err)
245
+ unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err.message)
236
246
  done()
237
247
  }
238
248
  })
239
249
 
240
250
  it('should error not broadcast transaction', (done) => {
241
- const client = new TCaBCIClient([], WebSocket)
251
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
242
252
 
243
253
  client
244
254
  .Broadcast({
@@ -258,13 +268,13 @@ describe('TCaBCIClient', () => {
258
268
  done()
259
269
  })
260
270
  .catch((err) => {
261
- unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err)
271
+ unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err.message)
262
272
  done()
263
273
  })
264
274
  })
265
275
 
266
276
  it('should error in bulk tx if addresses count is zero', (done) => {
267
- const client = new TCaBCIClient([], WebSocket)
277
+ const client = new TCaBCIClient([], WebSocket, 'transferchain', 'v1')
268
278
 
269
279
  client
270
280
  .Bulk([])
@@ -286,7 +296,7 @@ describe('TCaBCIClient', () => {
286
296
  )
287
297
  }
288
298
 
289
- const client = new TCaBCIClient(null, WebSocket)
299
+ const client = new TCaBCIClient(null, WebSocket, 'transferchain', 'v1')
290
300
 
291
301
  client
292
302
  .Bulk(addresses)
package/errors.js CHANGED
@@ -1,16 +1,16 @@
1
- export const ALREADY_CONNECTED = new Error('Already connected'),
2
- NOT_CONNECTED = new Error('Not connected'),
3
- ERR_NETWORK = new Error('Network Error'),
4
- INVALID_ARGUMENTS = new Error('Check your arguments'),
1
+ export const ALREADY_CONNECTED = 'Already connected',
2
+ NOT_CONNECTED = 'Not connected',
3
+ ERR_NETWORK = 'Network Error',
4
+ INVALID_ARGUMENTS = 'Check your arguments',
5
5
  INVALID_ARGUMENT_WITH_CS = (val) => {
6
- return new Error(INVALID_ARGUMENTS + ' ' + val)
6
+ return INVALID_ARGUMENTS + ' ' + val
7
7
  },
8
- NOT_SUBSCRIBED = new Error('Not subscribed'),
9
- ALREADY_SUBSCRIBED = new Error('Already subscribed'),
10
- ADDRESSES_IS_EMPTY = new Error('Addresses is empty'),
11
- BLOCK_NOT_FOUND = new Error('Block does not exist'),
12
- TRANSACTION_TYPE_NOT_VALID = new Error('Transaction type is not valid'),
13
- TRANSACTION_NOT_BROADCAST = new Error('Transaction can not be broadcast')
8
+ NOT_SUBSCRIBED = 'Not subscribed',
9
+ ALREADY_SUBSCRIBED = 'Already subscribed',
10
+ ADDRESSES_IS_EMPTY = 'Addresses is empty',
11
+ BLOCK_NOT_FOUND = 'Block does not exist',
12
+ TRANSACTION_TYPE_NOT_VALID = 'Transaction type is not valid',
13
+ TRANSACTION_NOT_BROADCAST = 'Transaction can not be broadcast'
14
14
 
15
15
  export class FetchError extends Error {
16
16
  code = -1
package/errors_test.js CHANGED
@@ -2,7 +2,7 @@ import { FetchError } from './errors.js'
2
2
  import unitJS from 'unit.js'
3
3
 
4
4
  describe('Errors', () => {
5
- it('FetchError', () => {
5
+ it('FetchError', () => {
6
6
  const err = new FetchError('err1').setCode(1)
7
7
 
8
8
  unitJS.assert.equal(err.message, 'err1')
package/message.js CHANGED
@@ -2,54 +2,69 @@ import { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } from './errors.js'
2
2
  import { MESSAGE_TYPE, TX_TYPE } from './constants.js'
3
3
 
4
4
  export default class Message {
5
- #is_web = false
6
- #type = ''
7
- #addrs = []
8
- #txTypes = []
9
- constructor(isWeb, type, addrs, txTypes = []) {
10
- this.#is_web = isWeb
11
- this.#type = type
12
- this.#addrs = addrs
13
- this.#txTypes = txTypes
5
+ _is_web = false
6
+ _type = ''
7
+ _addrs = []
8
+ _signedData = {}
9
+ _txTypes = []
10
+
11
+ /**
12
+ * @param {boolean} isWeb
13
+ * @param {string} type
14
+ * @param {Array<string>} addrs
15
+ * @param {?Object} signedData
16
+ * @param {?Array<string>} txTypes
17
+ */
18
+ constructor(isWeb, type, addrs, signedData = null, txTypes = null) {
19
+ this._is_web = isWeb
20
+ this._type = type
21
+ if (addrs && Array.isArray(addrs)) this._addrs = addrs
22
+ if (typeof signedData === 'object') this._signedData = signedData
23
+ if (txTypes && Array.isArray(txTypes)) this._txTypes = txTypes
14
24
  }
15
25
 
16
- ToJSONString() {
17
- this.#validate()
26
+ ToJSON() {
27
+ this._validate()
18
28
 
19
29
  return JSON.stringify({
20
- is_web: this.#is_web,
21
- type: this.#type,
22
- addrs: this.#addrs,
23
- ...(this.#txTypes.length > 0 ? { tx_types: this.#txTypes } : {}),
30
+ is_web: this._is_web,
31
+ type: this._type,
32
+ addrs: this._addrs,
33
+ signed_addrs: this._signedData,
34
+ ...(this._txTypes.length > 0 ? { tx_types: this._txTypes } : {}),
24
35
  })
25
36
  }
26
37
 
27
- #validate() {
28
- if (typeof this.#is_web !== 'boolean') {
29
- throw INVALID_ARGUMENT_WITH_CS('is_web')
38
+ _validate() {
39
+ if (typeof this._is_web !== 'boolean') {
40
+ throw new Error(INVALID_ARGUMENT_WITH_CS('is_web'))
41
+ }
42
+
43
+ if (typeof this._type !== 'string') {
44
+ throw new Error(INVALID_ARGUMENT_WITH_CS('type'))
30
45
  }
31
46
 
32
- if (typeof this.#type !== 'string') {
33
- throw INVALID_ARGUMENT_WITH_CS('type')
47
+ if (!Array.isArray(this._addrs)) {
48
+ throw new Error(INVALID_ARGUMENT_WITH_CS('addrs'))
34
49
  }
35
50
 
36
- if (!Array.isArray(this.#addrs)) {
37
- throw INVALID_ARGUMENT_WITH_CS('addrs')
51
+ if (typeof this._signedData !== 'object') {
52
+ throw new Error(INVALID_ARGUMENT_WITH_CS('signedData'))
38
53
  }
39
54
 
40
55
  if (
41
- [MESSAGE_TYPE.SUBSCRIBE, MESSAGE_TYPE.UNSUBSCRIBE].indexOf(this.#type) ===
56
+ [MESSAGE_TYPE.SUBSCRIBE, MESSAGE_TYPE.UNSUBSCRIBE].indexOf(this._type) ===
42
57
  -1
43
58
  ) {
44
- throw INVALID_ARGUMENTS
59
+ throw new Error(INVALID_ARGUMENTS)
45
60
  }
46
61
 
47
- if (this.#txTypes.length) {
62
+ if (this._txTypes.length) {
48
63
  let vals = Object.values(TX_TYPE)
49
64
 
50
- for (let i = 0; i < this.#txTypes.length; i++) {
51
- if (vals.indexOf(this.#txTypes[i]) === -1) {
52
- throw INVALID_ARGUMENT_WITH_CS('tx_types')
65
+ for (let i = 0; i < this._txTypes.length; i++) {
66
+ if (vals.indexOf(this._txTypes[i]) === -1) {
67
+ throw new Error(INVALID_ARGUMENT_WITH_CS('tx_types'))
53
68
  }
54
69
  }
55
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tchain/tcabci-read-js-client",
3
- "version": "2.2.4",
3
+ "version": "2.4.0",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
6
  "type": "module",
package/transaction.js CHANGED
@@ -1,19 +1,19 @@
1
1
  import { INVALID_ARGUMENT_WITH_CS } from './errors.js'
2
2
 
3
3
  export default class Transaction {
4
- #id
5
- #block_id
6
- #height
7
- #identifier
8
- #version
9
- #typ
10
- #sender_addr
11
- #recipient_addr
12
- #data
13
- #sign
14
- #fee
15
- #hash
16
- #inserted_at
4
+ _id
5
+ _block_id
6
+ _height
7
+ _identifier
8
+ _version
9
+ _typ
10
+ _sender_addr
11
+ _recipient_addr
12
+ _data
13
+ _sign
14
+ _fee
15
+ _hash
16
+ _inserted_at
17
17
 
18
18
  constructor({
19
19
  id,
@@ -30,77 +30,77 @@ export default class Transaction {
30
30
  hash,
31
31
  inserted_at,
32
32
  }) {
33
- this.#id = id
34
- this.#block_id = block_id
35
- this.#height = height
36
- this.#identifier = identifier
37
- this.#version = version
38
- this.#typ = typ
39
- this.#sender_addr = sender_addr
40
- this.#recipient_addr = recipient_addr
41
- this.#data = data
42
- this.#sign = sign
43
- this.#fee = fee
44
- this.#hash = hash
45
- this.#inserted_at = inserted_at
33
+ this._id = id
34
+ this._block_id = block_id
35
+ this._height = height
36
+ this._identifier = identifier
37
+ this._version = version
38
+ this._typ = typ
39
+ this._sender_addr = sender_addr
40
+ this._recipient_addr = recipient_addr
41
+ this._data = data
42
+ this._sign = sign
43
+ this._fee = fee
44
+ this._hash = hash
45
+ this._inserted_at = inserted_at
46
46
  }
47
47
 
48
- ToJSONString() {
49
- this.#validate()
48
+ ToJSON() {
49
+ this._validate()
50
50
 
51
51
  return JSON.stringify({
52
- id: this.#id,
53
- block_id: this.#block_id,
54
- height: this.#height,
55
- identifier: this.#identifier,
56
- version: this.#version,
57
- typ: this.#typ,
58
- sender_addr: this.#sender_addr,
59
- recipient_addr: this.#recipient_addr,
60
- data: this.#data,
61
- sign: this.#sign,
62
- fee: this.#fee,
63
- hash: this.#hash,
64
- inserted_at: this.#inserted_at,
52
+ id: this._id,
53
+ block_id: this._block_id,
54
+ height: this._height,
55
+ identifier: this._identifier,
56
+ version: this._version,
57
+ typ: this._typ,
58
+ sender_addr: this._sender_addr,
59
+ recipient_addr: this._recipient_addr,
60
+ data: this._data,
61
+ sign: this._sign,
62
+ fee: this._fee,
63
+ hash: this._hash,
64
+ inserted_at: this._inserted_at,
65
65
  })
66
66
  }
67
67
 
68
- #validate() {
69
- if (typeof this.#id !== 'number') {
70
- throw INVALID_ARGUMENT_WITH_CS('id')
68
+ _validate() {
69
+ if (typeof this._id !== 'number') {
70
+ throw new Error(INVALID_ARGUMENT_WITH_CS('id'))
71
71
  }
72
- if (typeof this.#block_id !== 'number') {
73
- throw INVALID_ARGUMENT_WITH_CS('blockID')
72
+ if (typeof this._block_id !== 'number') {
73
+ throw new Error(INVALID_ARGUMENT_WITH_CS('blockID'))
74
74
  }
75
- if (typeof this.#height !== 'number') {
76
- throw INVALID_ARGUMENT_WITH_CS('height')
75
+ if (typeof this._height !== 'number') {
76
+ throw new Error(INVALID_ARGUMENT_WITH_CS('height'))
77
77
  }
78
- if (typeof this.#identifier !== 'string') {
79
- throw INVALID_ARGUMENT_WITH_CS('identifier')
78
+ if (typeof this._identifier !== 'string') {
79
+ throw new Error(INVALID_ARGUMENT_WITH_CS('identifier'))
80
80
  }
81
- if (typeof this.#version !== 'number') {
82
- throw INVALID_ARGUMENT_WITH_CS('version')
81
+ if (typeof this._version !== 'number') {
82
+ throw new Error(INVALID_ARGUMENT_WITH_CS('version'))
83
83
  }
84
- if (typeof this.#typ !== 'string') {
85
- throw INVALID_ARGUMENT_WITH_CS('typ')
84
+ if (typeof this._typ !== 'string') {
85
+ throw new Error(INVALID_ARGUMENT_WITH_CS('typ'))
86
86
  }
87
- if (typeof this.#sender_addr !== 'string') {
88
- throw INVALID_ARGUMENT_WITH_CS('senderAddr')
87
+ if (typeof this._sender_addr !== 'string') {
88
+ throw new Error(INVALID_ARGUMENT_WITH_CS('senderAddr'))
89
89
  }
90
- if (typeof this.#recipient_addr !== 'string') {
91
- throw INVALID_ARGUMENT_WITH_CS('recipientAddr')
90
+ if (typeof this._recipient_addr !== 'string') {
91
+ throw new Error(INVALID_ARGUMENT_WITH_CS('recipientAddr'))
92
92
  }
93
- if (typeof this.#data !== 'object') {
94
- throw INVALID_ARGUMENT_WITH_CS('data')
93
+ if (typeof this._data !== 'object') {
94
+ throw new Error(INVALID_ARGUMENT_WITH_CS('data'))
95
95
  }
96
- if (typeof this.#sign !== 'object') {
97
- throw INVALID_ARGUMENT_WITH_CS('sign')
96
+ if (typeof this._sign !== 'object') {
97
+ throw new Error(INVALID_ARGUMENT_WITH_CS('sign'))
98
98
  }
99
- if (typeof this.#fee !== 'number') {
100
- throw INVALID_ARGUMENT_WITH_CS('fee')
99
+ if (typeof this._fee !== 'number') {
100
+ throw new Error(INVALID_ARGUMENT_WITH_CS('fee'))
101
101
  }
102
- if (typeof this.#inserted_at !== 'string') {
103
- throw INVALID_ARGUMENT_WITH_CS('insertedAt')
102
+ if (typeof this._inserted_at !== 'string') {
103
+ throw new Error(INVALID_ARGUMENT_WITH_CS('insertedAt'))
104
104
  }
105
105
  }
106
106
  }
package/util.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CHARACTERS } from './constants.js'
2
2
 
3
- export const randomString = (length) => {
3
+ export function randomString(length) {
4
4
  let result = ''
5
5
 
6
6
  const charactersLength = CHARACTERS.length
@@ -15,6 +15,16 @@ export const randomString = (length) => {
15
15
  return result
16
16
  }
17
17
 
18
- export const toJSON = (data) => {
18
+ export function toJSON(data) {
19
19
  return JSON.parse(data)
20
20
  }
21
+
22
+ export function isJSON(str) {
23
+ try {
24
+ JSON.parse(str)
25
+ } catch (e) {
26
+ return false
27
+ }
28
+
29
+ return true
30
+ }