@tchain/tcabci-read-js-client 2.6.6 → 2.7.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/block.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { Transaction } from './transaction'
2
+
3
+ export declare class Block {
4
+ get Height(): number
5
+ get TXS(): number
6
+ get Hash(): string|null
7
+ get Transactions(): Transaction[]
8
+ get ChainName(): string
9
+ get ChainVersion(): string
10
+
11
+ static FromJSON(value: string): { block?: Block, error?: Error }
12
+ static FromObject(obj: Record<string, any>): { block?: Block, error?: Error }
13
+ }
package/block.js ADDED
@@ -0,0 +1,110 @@
1
+ import { Transaction } from './transaction.js'
2
+
3
+ export class Block {
4
+ _height
5
+ _txs
6
+ _hash
7
+ _transactions
8
+ _chainName
9
+ _chainVersion
10
+
11
+ get Height() {
12
+ return this._height
13
+ }
14
+
15
+ get TXS() {
16
+ return this._txs
17
+ }
18
+
19
+ get Hash() {
20
+ return this._hash
21
+ }
22
+
23
+ get Transactions() {
24
+ return this._transactions
25
+ }
26
+
27
+ get ChainName() {
28
+ return this._chainName
29
+ }
30
+
31
+ get ChainVersion() {
32
+ return this._chainVersion
33
+ }
34
+
35
+ /**
36
+ * @param {string} value
37
+ * @return {{block: ?Block, error: ?Error}}
38
+ * @constructor
39
+ */
40
+ static FromJSON(value) {
41
+ try {
42
+ const bl = new Block(),
43
+ parsed = JSON.parse(value)
44
+
45
+ bl._transactions = []
46
+
47
+ if (typeof parsed.height !== 'undefined') bl._height = parsed.height
48
+ if (typeof parsed.txs !== 'undefined') bl._txs = parsed.txs
49
+ if (typeof parsed.hash !== 'undefined') bl._hash = parsed.hash
50
+ if (typeof parsed.chain_name !== 'undefined')
51
+ bl._chainName = parsed.chain_name
52
+ if (typeof parsed.chain_version !== 'undefined')
53
+ bl._chainVersion = parsed.chain_version
54
+
55
+ if (typeof parsed.transactions !== 'undefined') {
56
+ for (let i = 0; i < parsed.transactions.length; i++) {
57
+ const { transaction, error } = Transaction.FromObject(
58
+ parsed.transactions[i],
59
+ )
60
+ if (error) {
61
+ return { block: null, error: error }
62
+ }
63
+
64
+ bl._transactions.push(transaction)
65
+ }
66
+ }
67
+
68
+ return { block: bl, error: null }
69
+ } catch (e) {
70
+ return { block: null, error: e }
71
+ }
72
+ }
73
+
74
+ /**
75
+ * @param {object} obj
76
+ * @return {{block: ?Block, error: ?Error}}
77
+ * @constructor
78
+ */
79
+ static FromObject(obj) {
80
+ try {
81
+ const bl = new Block()
82
+
83
+ bl._transactions = []
84
+
85
+ if (typeof obj.height !== 'undefined') bl._height = obj.height
86
+ if (typeof obj.txs !== 'undefined') bl._txs = obj.txs
87
+ if (typeof obj.hash !== 'undefined') bl._hash = obj.hash
88
+ if (typeof obj.chain_name !== 'undefined') bl._chainName = obj.chain_name
89
+ if (typeof obj.chain_version !== 'undefined')
90
+ bl._chainVersion = obj.chain_version
91
+
92
+ if (typeof obj.transactions !== 'undefined') {
93
+ for (let i = 0; i < obj.transactions.length; i++) {
94
+ const { transaction, error } = Transaction.FromObject(
95
+ obj.transactions[i],
96
+ )
97
+ if (error) {
98
+ return { block: null, error: error }
99
+ }
100
+
101
+ bl._transactions.push(transaction)
102
+ }
103
+ }
104
+
105
+ return { block: bl, error: null }
106
+ } catch (e) {
107
+ return { block: null, error: e }
108
+ }
109
+ }
110
+ }
package/callback.d.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  import { Transaction } from './transaction'
2
+ import { Block } from './block'
2
3
 
3
4
  export declare type SuccessCallback = (event: Event) => void
4
5
  export declare type ErrorCallback = (event: Event) => void
5
6
  export declare type CloseCallback = (event: Event) => void
6
- export declare type ListenCallback = (tx: Transaction) => void
7
+ export declare type ListenCallback = (
8
+ block: Block | null,
9
+ tx: Transaction | null,
10
+ msg: any | null,
11
+ ) => void
package/client.js CHANGED
@@ -11,10 +11,18 @@ import {
11
11
  TRANSACTION_TYPE_NOT_VALID,
12
12
  } from './errors.js'
13
13
  import { READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS } from './constants.js'
14
- import Message, { SUBSCRIBEMessage, UNSUBSCRIBEMessage } from './message.js'
14
+ import Message, {
15
+ Block as MBlock,
16
+ Transaction as MTransaction,
17
+ SUBSCRIBEMessage,
18
+ UNSUBSCRIBEMessage,
19
+ Subscription,
20
+ OK,
21
+ } from './message.js'
15
22
  import { Options } from './websocketOptions.js'
16
23
  import { TWebSocket } from './websocket.js'
17
24
  import { Transaction, TX_TYPE_LIST } from './transaction.js'
25
+ import { Block } from './block.js'
18
26
 
19
27
  /**
20
28
  * @callback SuccessCallback
@@ -36,7 +44,9 @@ import { Transaction, TX_TYPE_LIST } from './transaction.js'
36
44
 
37
45
  /**
38
46
  * @callback ListenCallback
39
- * @param {*} message
47
+ * @param {?Block} block
48
+ * @param {?Transaction} transaction
49
+ * @param {?*} msg
40
50
  * @return void
41
51
  */
42
52
 
@@ -47,7 +57,7 @@ export default class TCaBCIClient {
47
57
  _connected = false
48
58
  _chainName = 'transferchain'
49
59
  _chainVersion = 'v1'
50
- _version = `v2.6.6`
60
+ _version = `v2.7.0`
51
61
  /**
52
62
  * @type {?SuccessCallback}
53
63
  */
@@ -258,18 +268,16 @@ export default class TCaBCIClient {
258
268
  addrs.push(...newAddress)
259
269
  }
260
270
 
261
- const message = new Message(
262
- true,
263
- SUBSCRIBEMessage,
264
- addrs,
265
- _signedData,
266
- txTypes,
271
+ this._ws.send(
272
+ new Message({
273
+ isWeb: true,
274
+ type: SUBSCRIBEMessage,
275
+ addrs: addrs,
276
+ signedData: _signedData,
277
+ txTypes,
278
+ }).ToJSON(),
267
279
  )
268
-
269
- this._ws.send(message.ToJSON())
270
- this._setSubscribeAddresses(addrs, true)
271
- ._setSubscribeSignedData(signedData)
272
- ._setSubscribed(true)
280
+ this._setSubscribeAddresses(addrs, true)._setSubscribeSignedData(signedData)
273
281
 
274
282
  return this
275
283
  }
@@ -282,7 +290,11 @@ export default class TCaBCIClient {
282
290
  throw new Error(NOT_SUBSCRIBED)
283
291
  }
284
292
  this._ws.send(
285
- new Message(true, UNSUBSCRIBEMessage, this.SubscribeAddresses).ToJSON(),
293
+ new Message({
294
+ isWeb: true,
295
+ type: UNSUBSCRIBEMessage,
296
+ addrs: this.SubscribeAddresses,
297
+ }).ToJSON(),
286
298
  )
287
299
  this._setSubscribed(false)
288
300
  this._setSubscribeAddresses([])
@@ -708,10 +720,6 @@ export default class TCaBCIClient {
708
720
  })
709
721
 
710
722
  this._ws.addMessageListener((message) => {
711
- if (message.data === 'OK' && message.data.length < 10) {
712
- return { status: message.data }
713
- }
714
-
715
723
  this._callListenCallback(message.data)
716
724
  })
717
725
 
@@ -788,9 +796,49 @@ export default class TCaBCIClient {
788
796
  }
789
797
 
790
798
  _callListenCallback(message) {
791
- const { transaction, error } = Transaction.FromJSON(message)
792
- if (error) this._callErrorCallback(error)
793
- else if (this._listenCb) this._listenCb(transaction)
799
+ let msg
800
+
801
+ const { transaction, error: e1 } = Transaction.FromJSON(message)
802
+ if (e1) {
803
+ const { message: _msg, error: e2 } = Message.FromJSON(message)
804
+ if (e2) {
805
+ this._callErrorCallback(e2)
806
+ return
807
+ }
808
+
809
+ switch (_msg.type) {
810
+ case MBlock:
811
+ msg = Block.FromObject(_msg.data)
812
+ if (msg.error) {
813
+ this._callErrorCallback(msg.error)
814
+ return
815
+ }
816
+
817
+ if (this._listenCb) this._listenCb(msg.block, null, null)
818
+ break
819
+ case MTransaction:
820
+ msg = Transaction.FromObject(_msg.data)
821
+ if (msg.error) {
822
+ this._callErrorCallback(msg.error)
823
+ return
824
+ }
825
+
826
+ if (this._listenCb) this._listenCb(null, msg.transaction, null)
827
+ break
828
+ case Subscription:
829
+ if (message.state === OK) {
830
+ this._setSubscribed(true)
831
+ } else {
832
+ this._setSubscribed(false)
833
+ }
834
+ break
835
+ default:
836
+ if (this._listenCb) this._listenCb(null, null, _msg)
837
+ break
838
+ }
839
+ } else {
840
+ if (this._listenCb) this._listenCb(null, transaction, null)
841
+ }
794
842
  }
795
843
 
796
844
  /**
package/constants.d.ts CHANGED
@@ -1,8 +1,2 @@
1
- export declare type MessageType = string
2
-
3
- export declare const SUBSCRIBEMessage: MessageType,
4
- UNSUBSCRIBEMessage: MessageType
5
-
6
- export declare const CHARACTERS: string,
7
- READ_NODE_ADDRESS: string,
1
+ export declare const READ_NODE_ADDRESS: string,
8
2
  READ_NODE_WS_ADDRESS: string
package/message.d.ts CHANGED
@@ -1,13 +1,46 @@
1
- import { MessageType } from './constants'
2
1
  import { TXType } from './transaction'
3
2
 
3
+ export declare type MessageType = string
4
+
5
+ export declare const SUBSCRIBEMessage: MessageType,
6
+ UNSUBSCRIBEMessage: MessageType
7
+
8
+ export declare type MType = number
9
+
10
+ export declare const Block: MessageType,
11
+ Transaction: MessageType,
12
+ Subscription: MessageType,
13
+ Listen: MessageType,
14
+ MSG: MessageType
15
+
16
+ export declare type MState = number
17
+
18
+ export declare const OK: MState,
19
+ FAIL: MState
20
+
4
21
  export declare class Message {
5
- constructor(
6
- isWeb: boolean,
7
- type: MessageType,
8
- addrs: string[],
9
- signedData?: Record<string, string>,
10
- txTypes?: TXType[],
11
- )
22
+ constructor({
23
+ isWeb,
24
+ type,
25
+ addrs,
26
+ signedData,
27
+ txTypes,
28
+ }?: {
29
+ isWeb: boolean
30
+ type: MessageType
31
+ addrs: string[]
32
+ signedData?: Record<string, string>
33
+ txTypes?: TXType[]
34
+ })
35
+ get IsWeb(): boolean
36
+ get Type(): MessageType
37
+ get Addresses(): string[] | null
38
+ get SignedAddresses(): Record<string, string> | null
39
+ get TXTypes(): TXType[] | null
40
+ get Data(): any | null
41
+ get State(): MState | null
42
+
43
+ static FromJSON(value: string): { message?: Message; error?: Error }
44
+ static FromObject(obj: Record<string, any>): { message?: Message; error?: Error }
12
45
  ToJSON(): string
13
46
  }
package/message.js CHANGED
@@ -1,29 +1,115 @@
1
- import { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } from './errors.js'
1
+ import { INVALID_ARGUMENT_WITH_CS } from './errors.js'
2
2
  import { TX_TYPE_LIST } from './transaction.js'
3
3
 
4
4
  export const SUBSCRIBEMessage = 'subscribe',
5
5
  UNSUBSCRIBEMessage = 'unsubscribe'
6
6
 
7
+ export const Block = 0,
8
+ Transaction = 1,
9
+ Subscription = 2,
10
+ Listen = 3,
11
+ MSG = 4
12
+
13
+ export const OK = 1,
14
+ FAIL = 2
15
+
7
16
  export default class Message {
8
17
  _is_web = false
9
- _type = ''
10
- _addrs = []
11
- _signedData = {}
12
- _txTypes = []
18
+ _type
19
+ _addresses
20
+ _signedAddresses
21
+ _txTypes
22
+ _data
23
+ _state
13
24
 
14
25
  /**
15
26
  * @param {boolean} isWeb
16
- * @param {string} type
27
+ * @param {string|number} type
17
28
  * @param {Array<string>} addrs
18
29
  * @param {?Object} signedData
19
30
  * @param {?Array<string>} txTypes
31
+ * @param {*} data
20
32
  */
21
- constructor(isWeb, type, addrs, signedData = null, txTypes = null) {
33
+ constructor({
34
+ isWeb,
35
+ type,
36
+ addrs,
37
+ signedData = null,
38
+ txTypes = null,
39
+ data = null,
40
+ } = {}) {
22
41
  this._is_web = isWeb
23
42
  this._type = type
24
- if (addrs && Array.isArray(addrs)) this._addrs = addrs
25
- if (typeof signedData === 'object') this._signedData = signedData
43
+ if (addrs && Array.isArray(addrs)) this._addresses = addrs
44
+ if (typeof signedData === 'object') this._signedAddresses = signedData
26
45
  if (txTypes && Array.isArray(txTypes)) this._txTypes = txTypes
46
+ if (data) this._data = data
47
+ }
48
+
49
+ get IsWeb() {
50
+ return this._is_web
51
+ }
52
+
53
+ get Type() {
54
+ return this._type
55
+ }
56
+
57
+ get Addresses() {
58
+ return this._addresses
59
+ }
60
+
61
+ get SignedAddresses() {
62
+ return this._signedAddresses
63
+ }
64
+
65
+ get Data() {
66
+ return this._data
67
+ }
68
+
69
+ get State() {
70
+ return this._state
71
+ }
72
+
73
+ static FromJSON(value) {
74
+ try {
75
+ const msg = new Message(),
76
+ parsed = JSON.parse(value)
77
+
78
+ msg._type = parsed.type
79
+ if (typeof parsed.addresses !== 'undefined')
80
+ msg._addresses = parsed.addresses
81
+ if (typeof parsed.signed_addresses !== 'undefined')
82
+ msg._signedAddresses = parsed.signed_addresses
83
+ if (typeof parsed.data !== 'undefined') msg._data = parsed.data
84
+ if (typeof parsed.tx_types !== 'undefined') msg._txTypes = parsed.tx_types
85
+ if (typeof parsed.state !== 'undefined') msg._state = parsed.state
86
+
87
+ msg._validate()
88
+
89
+ return { message: this, error: null }
90
+ } catch (e) {
91
+ return { message: null, error: e }
92
+ }
93
+ }
94
+
95
+ static FromObject(obj) {
96
+ try {
97
+ const msg = new Message()
98
+
99
+ msg._type = obj.type
100
+ if (typeof obj.addresses !== 'undefined') msg._addresses = obj.addresses
101
+ if (typeof obj.signed_addresses !== 'undefined')
102
+ msg._signedAddresses = obj.signed_addresses
103
+ if (typeof obj.data !== 'undefined') msg._data = obj.data
104
+ if (typeof obj.tx_types !== 'undefined') msg._txTypes = obj.tx_types
105
+ if (typeof obj.state !== 'undefined') msg._state = obj.state
106
+
107
+ msg._validate()
108
+
109
+ return { message: this, error: null }
110
+ } catch (e) {
111
+ return { message: null, error: e }
112
+ }
27
113
  }
28
114
 
29
115
  ToJSON() {
@@ -32,39 +118,66 @@ export default class Message {
32
118
  return JSON.stringify({
33
119
  is_web: this._is_web,
34
120
  type: this._type,
35
- addrs: this._addrs,
36
- signed_addrs: this._signedData,
37
- ...(this._txTypes.length > 0 ? { tx_types: this._txTypes } : {}),
121
+ ...(this._addresses ? { addresses: this._addresses } : {}),
122
+ ...(this._signedAddresses
123
+ ? { signed_addresses: this._signedAddresses }
124
+ : {}),
125
+ ...(this._txTypes ? { tx_types: this._txTypes } : {}),
126
+ ...(this._data ? { data: this._data } : {}),
38
127
  })
39
128
  }
40
129
 
41
130
  _validate() {
42
- if (typeof this._is_web !== 'boolean') {
131
+ if (
132
+ typeof this._is_web !== 'undefined' &&
133
+ typeof this._is_web !== 'boolean'
134
+ ) {
43
135
  throw new Error(INVALID_ARGUMENT_WITH_CS('is_web'))
44
136
  }
45
137
 
46
- if (typeof this._type !== 'string') {
138
+ if (typeof this._type === 'undefined') {
47
139
  throw new Error(INVALID_ARGUMENT_WITH_CS('type'))
140
+ } else {
141
+ if (
142
+ typeof this._type === 'string' &&
143
+ ![SUBSCRIBEMessage, UNSUBSCRIBEMessage].includes(this._type)
144
+ ) {
145
+ throw new Error(INVALID_ARGUMENT_WITH_CS('type'))
146
+ } else if (
147
+ typeof this._type === 'number' &&
148
+ ![Block, Transaction, Subscription, Listen, MSG].includes(this._type)
149
+ ) {
150
+ throw new Error(INVALID_ARGUMENT_WITH_CS('type'))
151
+ }
48
152
  }
49
153
 
50
- if (!Array.isArray(this._addrs)) {
51
- throw new Error(INVALID_ARGUMENT_WITH_CS('addrs'))
52
- }
53
-
54
- if (typeof this._signedData !== 'object') {
55
- throw new Error(INVALID_ARGUMENT_WITH_CS('signedData'))
154
+ if (
155
+ typeof this._addresses !== 'undefined' &&
156
+ !Array.isArray(this._addresses)
157
+ ) {
158
+ throw new Error(INVALID_ARGUMENT_WITH_CS('addresses'))
56
159
  }
57
160
 
58
- if (![SUBSCRIBEMessage, UNSUBSCRIBEMessage].includes(this._type)) {
59
- throw new Error(INVALID_ARGUMENTS)
161
+ if (
162
+ typeof this._signedAddresses !== 'undefined' &&
163
+ typeof this._signedAddresses !== 'object'
164
+ ) {
165
+ throw new Error(INVALID_ARGUMENT_WITH_CS('signedAddresses'))
60
166
  }
61
167
 
62
- if (this._txTypes.length) {
168
+ if (typeof this._txTypes !== 'undefined' && this._txTypes.length) {
63
169
  for (let i = 0; i < this._txTypes.length; i++) {
64
170
  if (!TX_TYPE_LIST.includes(this._txTypes[i])) {
65
171
  throw new Error(INVALID_ARGUMENT_WITH_CS('tx_types'))
66
172
  }
67
173
  }
68
174
  }
175
+
176
+ if (
177
+ typeof this._state !== 'undefined' &&
178
+ ![OK, FAIL].includes(this._state)
179
+ ) {
180
+ throw new Error(INVALID_ARGUMENT_WITH_CS('state'))
181
+ }
69
182
  }
70
183
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tchain/tcabci-read-js-client",
3
- "version": "2.6.6",
3
+ "version": "2.7.0",
4
4
  "description": "TransferChain Fastest Read Network WebSocket Client",
5
5
  "main": "client.js",
6
6
  "type": "module",
@@ -11,6 +11,7 @@
11
11
  "files": [
12
12
  "*.d.ts",
13
13
  "constants.js",
14
+ "block.js",
14
15
  "client.js",
15
16
  "errors.js",
16
17
  "message.js",
package/transaction.js CHANGED
@@ -275,6 +275,8 @@ export class Transaction {
275
275
  if (typeof parsed.chain_version !== 'undefined')
276
276
  this._chainVersion = parsed.chain_version
277
277
 
278
+ tx.Validate()
279
+
278
280
  return { transaction: tx, error: null }
279
281
  } catch (e) {
280
282
  return { transaction: null, error: e }
@@ -309,10 +311,11 @@ export class Transaction {
309
311
  tx._hash = obj.hash
310
312
  if (typeof obj.inserted_at !== 'undefined')
311
313
  tx._inserted_at = new Date(obj.inserted_at)
312
- if (typeof obj.chain_name !== 'undefined')
313
- this._chainName = obj.chain_name
314
+ if (typeof obj.chain_name !== 'undefined') tx._chainName = obj.chain_name
314
315
  if (typeof obj.chain_version !== 'undefined')
315
- this._chainVersion = obj.chain_version
316
+ tx._chainVersion = obj.chain_version
317
+
318
+ tx.Validate()
316
319
 
317
320
  return { transaction: tx, error: null }
318
321
  } catch (e) {
@@ -13,7 +13,7 @@ export class Options {
13
13
  _protocols = []
14
14
  _customWS
15
15
  _endpoints = ['ws', 'longpoll']
16
- _version = `v2.6.6`
16
+ _version = `v2.7.0`
17
17
 
18
18
  _maxConnectionDelay = 10000
19
19
  _minReconnectionDelay = 1000 + Math.random() * 4000