@tchain/tcabci-read-js-client 2.3.0 → 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/client.js +107 -31
- package/client_test.js +2 -2
- package/errors.js +11 -11
- package/message.js +43 -28
- package/package.json +1 -1
- package/transaction.js +66 -66
- package/util.js +12 -2
package/client.js
CHANGED
|
@@ -18,7 +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'
|
|
21
|
+
import { isJSON, toJSON } from './util.js'
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* @callback successCallback
|
|
@@ -43,10 +43,11 @@ import { toJSON } from './util.js'
|
|
|
43
43
|
export default class TCaBCIClient {
|
|
44
44
|
subscribed = false
|
|
45
45
|
subscribedAddresses = []
|
|
46
|
+
subscribedSignedData = {}
|
|
46
47
|
connected = false
|
|
47
48
|
chainName = 'transferchain'
|
|
48
49
|
chainVersion = 'v1'
|
|
49
|
-
version = 'v2.
|
|
50
|
+
version = 'v2.4.0'
|
|
50
51
|
/**
|
|
51
52
|
* @type {?successCallback}
|
|
52
53
|
*/
|
|
@@ -80,15 +81,17 @@ export default class TCaBCIClient {
|
|
|
80
81
|
chainName = null,
|
|
81
82
|
chainVersion = null,
|
|
82
83
|
) {
|
|
83
|
-
if (!wsLibrary) throw INVALID_ARGUMENTS
|
|
84
|
+
if (!wsLibrary) throw new Error(INVALID_ARGUMENTS)
|
|
84
85
|
this.wsLibrary = wsLibrary
|
|
85
86
|
|
|
86
87
|
if (Array.isArray(readNodeAddresses) && readNodeAddresses.length === 2) {
|
|
87
88
|
this.readNodeAddress = readNodeAddresses[0]
|
|
88
89
|
this.readNodeWSAddress = readNodeAddresses[1]
|
|
89
90
|
|
|
90
|
-
if (!readNodeAddresses[0].startsWith('http'))
|
|
91
|
-
|
|
91
|
+
if (!readNodeAddresses[0].startsWith('http'))
|
|
92
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
93
|
+
if (!readNodeAddresses[1].startsWith('ws'))
|
|
94
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
92
95
|
}
|
|
93
96
|
|
|
94
97
|
if (chainName) this.chainName = chainName
|
|
@@ -165,9 +168,13 @@ export default class TCaBCIClient {
|
|
|
165
168
|
return this.connect()
|
|
166
169
|
}
|
|
167
170
|
|
|
171
|
+
/**
|
|
172
|
+
* @return {TCaBCIClient}
|
|
173
|
+
* @throws {Error}
|
|
174
|
+
*/
|
|
168
175
|
Stop() {
|
|
169
176
|
if (!this.getConnected()) {
|
|
170
|
-
throw NOT_CONNECTED
|
|
177
|
+
throw new Error(NOT_CONNECTED)
|
|
171
178
|
}
|
|
172
179
|
this.Disconnect(1000)
|
|
173
180
|
this.setConnected(false)
|
|
@@ -176,30 +183,42 @@ export default class TCaBCIClient {
|
|
|
176
183
|
return this
|
|
177
184
|
}
|
|
178
185
|
|
|
179
|
-
|
|
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) {
|
|
180
193
|
if (!Array.isArray(addresses)) {
|
|
181
|
-
throw INVALID_ARGUMENTS
|
|
194
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (typeof signedData !== 'object') {
|
|
198
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
182
199
|
}
|
|
183
200
|
|
|
184
201
|
if (txTypes && !Array.isArray(addresses)) {
|
|
185
|
-
throw INVALID_ARGUMENTS
|
|
202
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
186
203
|
}
|
|
187
204
|
|
|
188
|
-
if (txTypes
|
|
189
|
-
throw INVALID_ARGUMENTS
|
|
205
|
+
if (txTypes && txTypes.length > Object.values(TX_TYPE).length) {
|
|
206
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
190
207
|
}
|
|
191
208
|
|
|
192
209
|
if (!this.getConnected()) {
|
|
193
|
-
throw NOT_CONNECTED
|
|
210
|
+
throw new Error(NOT_CONNECTED)
|
|
194
211
|
}
|
|
195
212
|
|
|
196
|
-
let addrs = []
|
|
213
|
+
let addrs = [],
|
|
214
|
+
sdata = {}
|
|
197
215
|
|
|
198
216
|
if (addresses.length === 0) {
|
|
199
|
-
throw ADDRESSES_IS_EMPTY
|
|
217
|
+
throw new Error(ADDRESSES_IS_EMPTY)
|
|
200
218
|
}
|
|
201
219
|
|
|
202
220
|
addrs = addresses
|
|
221
|
+
sdata = signedData
|
|
203
222
|
|
|
204
223
|
if (this.getSubscribeAddresses().length > 0) {
|
|
205
224
|
let newAddress = []
|
|
@@ -212,26 +231,40 @@ export default class TCaBCIClient {
|
|
|
212
231
|
addrs = newAddress
|
|
213
232
|
}
|
|
214
233
|
|
|
215
|
-
|
|
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
|
+
)
|
|
216
245
|
|
|
217
|
-
this.ws.send(message.
|
|
246
|
+
this.ws.send(message.ToJSON())
|
|
218
247
|
this.setSubscribeAddresses(addrs, true)
|
|
219
|
-
|
|
248
|
+
.setSubscribeSignedData(signedData)
|
|
249
|
+
.setSubscribed(true)
|
|
250
|
+
|
|
251
|
+
return this
|
|
220
252
|
}
|
|
221
253
|
|
|
222
254
|
Unsubscribe() {
|
|
223
255
|
if (!this.getSubscribed()) {
|
|
224
|
-
throw NOT_SUBSCRIBED
|
|
256
|
+
throw new Error(NOT_SUBSCRIBED)
|
|
225
257
|
}
|
|
226
258
|
this.ws.send(
|
|
227
259
|
new Message(
|
|
228
260
|
true,
|
|
229
261
|
MESSAGE_TYPE.UNSUBSCRIBE,
|
|
230
262
|
this.getSubscribeAddresses(),
|
|
231
|
-
).
|
|
263
|
+
).ToJSON(),
|
|
232
264
|
)
|
|
233
265
|
this.setSubscribed(false)
|
|
234
266
|
this.setSubscribeAddresses([])
|
|
267
|
+
this.setSubscribeSignedData({})
|
|
235
268
|
}
|
|
236
269
|
|
|
237
270
|
/**
|
|
@@ -249,15 +282,23 @@ export default class TCaBCIClient {
|
|
|
249
282
|
.then((res) => {
|
|
250
283
|
return { blocks: res.data, total_count: res.total_count }
|
|
251
284
|
})
|
|
252
|
-
.catch((e) => this.handleRestError(e, BLOCK_NOT_FOUND))
|
|
285
|
+
.catch((e) => this.handleRestError(e, new Error(BLOCK_NOT_FOUND)))
|
|
253
286
|
}
|
|
254
287
|
|
|
255
|
-
|
|
288
|
+
/**
|
|
289
|
+
* @param {string} id
|
|
290
|
+
* @param {string} signature
|
|
291
|
+
* @return {Promise<*>}
|
|
292
|
+
*/
|
|
293
|
+
Tx(id, signature) {
|
|
256
294
|
if (!id || typeof id !== 'string') {
|
|
257
|
-
return Promise.reject(INVALID_ARGUMENTS)
|
|
295
|
+
return Promise.reject(new Error(INVALID_ARGUMENTS))
|
|
258
296
|
}
|
|
259
297
|
|
|
260
|
-
return this.httpClient(`/v1/tx/${id}`, {
|
|
298
|
+
return this.httpClient(`/v1/tx/${id}`, {
|
|
299
|
+
method: 'GET',
|
|
300
|
+
headers: { 'X-Signature': signature },
|
|
301
|
+
})
|
|
261
302
|
.then((res) => {
|
|
262
303
|
return { tx: res.data }
|
|
263
304
|
})
|
|
@@ -267,6 +308,7 @@ export default class TCaBCIClient {
|
|
|
267
308
|
/**
|
|
268
309
|
* @param {?Array<string>} recipientAddrs
|
|
269
310
|
* @param {?Array<string>} senderAddrs
|
|
311
|
+
* @param {?Object} signedData
|
|
270
312
|
* @param {?string} typ
|
|
271
313
|
* @param {?Array<string>} types
|
|
272
314
|
* @param {?string} chainName
|
|
@@ -276,13 +318,14 @@ export default class TCaBCIClient {
|
|
|
276
318
|
TxSummary({
|
|
277
319
|
recipientAddrs,
|
|
278
320
|
senderAddrs,
|
|
321
|
+
signedData,
|
|
279
322
|
typ,
|
|
280
323
|
types = null,
|
|
281
324
|
chainName = null,
|
|
282
325
|
chainVersion = null,
|
|
283
326
|
}) {
|
|
284
327
|
if (!recipientAddrs && !senderAddrs) {
|
|
285
|
-
return Promise.reject(INVALID_ARGUMENTS)
|
|
328
|
+
return Promise.reject(new Error(INVALID_ARGUMENTS))
|
|
286
329
|
}
|
|
287
330
|
|
|
288
331
|
return this.httpClient('/v1/tx_summary', {
|
|
@@ -292,6 +335,7 @@ export default class TCaBCIClient {
|
|
|
292
335
|
chain_version: chainVersion ?? this.chainVersion,
|
|
293
336
|
recipient_addrs: recipientAddrs,
|
|
294
337
|
sender_addrs: senderAddrs,
|
|
338
|
+
signed_addrs: signedData,
|
|
295
339
|
...(types ? { types: types } : { typ: typ }),
|
|
296
340
|
}),
|
|
297
341
|
})
|
|
@@ -316,6 +360,7 @@ export default class TCaBCIClient {
|
|
|
316
360
|
* @param {?number} lastOrder
|
|
317
361
|
* @param {?Array<string>} recipientAddrs
|
|
318
362
|
* @param {?Array<string>} senderAddrs
|
|
363
|
+
* @param {?Object} signedData
|
|
319
364
|
* @param {?Array<string>} hashes
|
|
320
365
|
* @param {string} typ
|
|
321
366
|
* @param {?Array<string>} types
|
|
@@ -334,6 +379,7 @@ export default class TCaBCIClient {
|
|
|
334
379
|
lastOrder,
|
|
335
380
|
recipientAddrs,
|
|
336
381
|
senderAddrs,
|
|
382
|
+
signedData,
|
|
337
383
|
hashes,
|
|
338
384
|
typ,
|
|
339
385
|
types,
|
|
@@ -354,6 +400,7 @@ export default class TCaBCIClient {
|
|
|
354
400
|
...(lastOrder ? { last_order: lastOrder } : {}),
|
|
355
401
|
...(recipientAddrs ? { recipient_addrs: recipientAddrs } : {}),
|
|
356
402
|
...(senderAddrs ? { sender_addrs: senderAddrs } : {}),
|
|
403
|
+
...(signedData ? { signed_addrs: signedData } : {}),
|
|
357
404
|
...(hashes ? { hashes: hashes } : {}),
|
|
358
405
|
...(limit ? { limit: limit } : {}),
|
|
359
406
|
...(offset ? { offset: offset } : {}),
|
|
@@ -497,7 +544,7 @@ export default class TCaBCIClient {
|
|
|
497
544
|
commit = false,
|
|
498
545
|
) {
|
|
499
546
|
if (Object.values(TX_TYPE).indexOf(type) < 0) {
|
|
500
|
-
throw TRANSACTION_TYPE_NOT_VALID
|
|
547
|
+
throw new Error(TRANSACTION_TYPE_NOT_VALID)
|
|
501
548
|
}
|
|
502
549
|
|
|
503
550
|
return this.httpClient(
|
|
@@ -519,11 +566,14 @@ export default class TCaBCIClient {
|
|
|
519
566
|
.then((res) => {
|
|
520
567
|
return { data: res.data }
|
|
521
568
|
})
|
|
522
|
-
.catch((e) =>
|
|
569
|
+
.catch((e) =>
|
|
570
|
+
this.handleRestError(e, { 400: new Error(TRANSACTION_NOT_BROADCAST) }),
|
|
571
|
+
)
|
|
523
572
|
}
|
|
524
573
|
|
|
525
574
|
/**
|
|
526
575
|
* @param {Array<string>} addresses
|
|
576
|
+
* @param {Object} signedData
|
|
527
577
|
* @param {?number} maxHeight
|
|
528
578
|
* @param {?string} chainName
|
|
529
579
|
* @param {?string} chainVersion
|
|
@@ -532,6 +582,7 @@ export default class TCaBCIClient {
|
|
|
532
582
|
*/
|
|
533
583
|
Bulk(
|
|
534
584
|
addresses = [],
|
|
585
|
+
signedData = {},
|
|
535
586
|
maxHeight = null,
|
|
536
587
|
chainName = null,
|
|
537
588
|
chainVersion = null,
|
|
@@ -542,6 +593,7 @@ export default class TCaBCIClient {
|
|
|
542
593
|
chain_name: chainName ?? this.chainName,
|
|
543
594
|
chain_version: chainVersion ?? this.chainVersion,
|
|
544
595
|
addresses: addresses,
|
|
596
|
+
signed_addrs: signedData,
|
|
545
597
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
546
598
|
}),
|
|
547
599
|
})
|
|
@@ -582,7 +634,7 @@ export default class TCaBCIClient {
|
|
|
582
634
|
*/
|
|
583
635
|
async connect() {
|
|
584
636
|
if (this.getConnected()) {
|
|
585
|
-
return Promise.reject(ALREADY_CONNECTED)
|
|
637
|
+
return Promise.reject(new Error(ALREADY_CONNECTED))
|
|
586
638
|
}
|
|
587
639
|
|
|
588
640
|
const options = {
|
|
@@ -598,6 +650,7 @@ export default class TCaBCIClient {
|
|
|
598
650
|
this.setConnected(false)
|
|
599
651
|
this.setSubscribed(false)
|
|
600
652
|
this.setSubscribeAddresses([], false)
|
|
653
|
+
this.setSubscribeSignedData({})
|
|
601
654
|
this.callErrorCallback(event)
|
|
602
655
|
|
|
603
656
|
reject(event)
|
|
@@ -613,12 +666,13 @@ export default class TCaBCIClient {
|
|
|
613
666
|
return { status: message.data }
|
|
614
667
|
}
|
|
615
668
|
|
|
616
|
-
this.callListenCallback(toJSON(message.data))
|
|
669
|
+
this.callListenCallback(!isJSON(message.data) ? message.data : toJSON(message.data))
|
|
617
670
|
}
|
|
618
671
|
this.ws.onclose = (event) => {
|
|
619
672
|
this.setConnected(false)
|
|
620
673
|
this.setSubscribed(false)
|
|
621
674
|
this.setSubscribeAddresses([], false)
|
|
675
|
+
this.setSubscribeSignedData({})
|
|
622
676
|
this.callCloseCallback(event)
|
|
623
677
|
|
|
624
678
|
resolve(event)
|
|
@@ -646,13 +700,34 @@ export default class TCaBCIClient {
|
|
|
646
700
|
return this.subscribedAddresses
|
|
647
701
|
}
|
|
648
702
|
|
|
703
|
+
getSubscribedSignedData() {
|
|
704
|
+
return this.subscribedSignedData
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* @param {Array<string>} addresses
|
|
709
|
+
* @param {?boolean} push
|
|
710
|
+
* @return {TCaBCIClient}
|
|
711
|
+
*/
|
|
649
712
|
setSubscribeAddresses(addresses, push = false) {
|
|
650
713
|
if (push) {
|
|
651
714
|
this.subscribedAddresses.push(...addresses)
|
|
652
715
|
|
|
653
|
-
return
|
|
716
|
+
return this
|
|
654
717
|
}
|
|
655
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
|
|
656
731
|
}
|
|
657
732
|
|
|
658
733
|
/**
|
|
@@ -680,6 +755,7 @@ export default class TCaBCIClient {
|
|
|
680
755
|
/**
|
|
681
756
|
* @param {Error} err
|
|
682
757
|
* @param {?Object|Error} custom
|
|
758
|
+
* @throws Error
|
|
683
759
|
*/
|
|
684
760
|
handleRestError(err, custom = null) {
|
|
685
761
|
const code = err.code
|
|
@@ -692,9 +768,9 @@ export default class TCaBCIClient {
|
|
|
692
768
|
|
|
693
769
|
switch (code) {
|
|
694
770
|
case 400:
|
|
695
|
-
throw INVALID_ARGUMENTS
|
|
771
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
696
772
|
case 'ERR_NETWORK':
|
|
697
|
-
throw ERR_NETWORK
|
|
773
|
+
throw new Error(ERR_NETWORK)
|
|
698
774
|
default:
|
|
699
775
|
throw err
|
|
700
776
|
}
|
package/client_test.js
CHANGED
|
@@ -242,7 +242,7 @@ describe('TCaBCIClient', () => {
|
|
|
242
242
|
|
|
243
243
|
done(new Error('invalid'))
|
|
244
244
|
} catch (err) {
|
|
245
|
-
unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err)
|
|
245
|
+
unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err.message)
|
|
246
246
|
done()
|
|
247
247
|
}
|
|
248
248
|
})
|
|
@@ -268,7 +268,7 @@ describe('TCaBCIClient', () => {
|
|
|
268
268
|
done()
|
|
269
269
|
})
|
|
270
270
|
.catch((err) => {
|
|
271
|
-
unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err)
|
|
271
|
+
unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err.message)
|
|
272
272
|
done()
|
|
273
273
|
})
|
|
274
274
|
})
|
package/errors.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export const ALREADY_CONNECTED =
|
|
2
|
-
NOT_CONNECTED =
|
|
3
|
-
ERR_NETWORK =
|
|
4
|
-
INVALID_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
|
|
6
|
+
return INVALID_ARGUMENTS + ' ' + val
|
|
7
7
|
},
|
|
8
|
-
NOT_SUBSCRIBED =
|
|
9
|
-
ALREADY_SUBSCRIBED =
|
|
10
|
-
ADDRESSES_IS_EMPTY =
|
|
11
|
-
BLOCK_NOT_FOUND =
|
|
12
|
-
TRANSACTION_TYPE_NOT_VALID =
|
|
13
|
-
TRANSACTION_NOT_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/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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
17
|
-
this
|
|
26
|
+
ToJSON() {
|
|
27
|
+
this._validate()
|
|
18
28
|
|
|
19
29
|
return JSON.stringify({
|
|
20
|
-
is_web: this
|
|
21
|
-
type: this
|
|
22
|
-
addrs: this
|
|
23
|
-
|
|
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
|
-
|
|
28
|
-
if (typeof this
|
|
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 (
|
|
33
|
-
throw INVALID_ARGUMENT_WITH_CS('
|
|
47
|
+
if (!Array.isArray(this._addrs)) {
|
|
48
|
+
throw new Error(INVALID_ARGUMENT_WITH_CS('addrs'))
|
|
34
49
|
}
|
|
35
50
|
|
|
36
|
-
if (
|
|
37
|
-
throw INVALID_ARGUMENT_WITH_CS('
|
|
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
|
|
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
|
|
62
|
+
if (this._txTypes.length) {
|
|
48
63
|
let vals = Object.values(TX_TYPE)
|
|
49
64
|
|
|
50
|
-
for (let i = 0; i < this
|
|
51
|
-
if (vals.indexOf(this
|
|
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
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
34
|
-
this
|
|
35
|
-
this
|
|
36
|
-
this
|
|
37
|
-
this
|
|
38
|
-
this
|
|
39
|
-
this
|
|
40
|
-
this
|
|
41
|
-
this
|
|
42
|
-
this
|
|
43
|
-
this
|
|
44
|
-
this
|
|
45
|
-
this
|
|
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
|
-
|
|
49
|
-
this
|
|
48
|
+
ToJSON() {
|
|
49
|
+
this._validate()
|
|
50
50
|
|
|
51
51
|
return JSON.stringify({
|
|
52
|
-
id: this
|
|
53
|
-
block_id: this
|
|
54
|
-
height: this
|
|
55
|
-
identifier: this
|
|
56
|
-
version: this
|
|
57
|
-
typ: this
|
|
58
|
-
sender_addr: this
|
|
59
|
-
recipient_addr: this
|
|
60
|
-
data: this
|
|
61
|
-
sign: this
|
|
62
|
-
fee: this
|
|
63
|
-
hash: this
|
|
64
|
-
inserted_at: this
|
|
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
|
-
|
|
69
|
-
if (typeof this
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
+
}
|