@tchain/tcabci-read-js-client 2.3.0 → 2.5.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 +1 -2
- package/client.js +198 -117
- package/client_test.js +99 -42
- package/errors.js +11 -11
- package/message.js +43 -28
- package/package.json +6 -5
- package/transaction.js +66 -66
- package/util.js +12 -2
- package/websocket.js +344 -0
- package/websocketOptions.js +277 -0
- package/websocketOptions_test.js +129 -0
- package/websocket_test.js +539 -0
package/client_test.js
CHANGED
|
@@ -10,26 +10,30 @@ import {
|
|
|
10
10
|
READ_NODE_WS_ADDRESS,
|
|
11
11
|
TX_TYPE,
|
|
12
12
|
} from './constants.js'
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// READ_NODE_WS_ADDRESS,
|
|
18
|
-
// } = require('./constants')
|
|
19
|
-
//
|
|
20
|
-
// const { randomString } = require('./util')
|
|
13
|
+
|
|
14
|
+
const waiter = async (timeout = 1000) => {
|
|
15
|
+
return new Promise((resolve) => setTimeout(resolve, timeout))
|
|
16
|
+
}
|
|
21
17
|
|
|
22
18
|
describe('TCaBCIClient', () => {
|
|
23
19
|
it('start with valid parameters', (done) => {
|
|
24
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
20
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
25
21
|
|
|
26
22
|
client
|
|
27
23
|
.Start()
|
|
24
|
+
.then(() => {
|
|
25
|
+
return waiter(500)
|
|
26
|
+
})
|
|
28
27
|
.then(() => {
|
|
29
28
|
const { connected, subscribed } = client.Status()
|
|
30
29
|
|
|
31
30
|
unitJS.assert.equal(connected, true)
|
|
32
31
|
unitJS.assert.equal(subscribed, false)
|
|
32
|
+
})
|
|
33
|
+
.then(() => {
|
|
34
|
+
return client.Disconnect()
|
|
35
|
+
})
|
|
36
|
+
.then(() => {
|
|
33
37
|
done()
|
|
34
38
|
})
|
|
35
39
|
.catch((err) => {
|
|
@@ -41,17 +45,25 @@ describe('TCaBCIClient', () => {
|
|
|
41
45
|
const client = new TCaBCIClient(
|
|
42
46
|
[READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS],
|
|
43
47
|
WebSocket,
|
|
44
|
-
'
|
|
45
|
-
'
|
|
48
|
+
'medusa',
|
|
49
|
+
'v2',
|
|
46
50
|
)
|
|
47
51
|
|
|
48
52
|
client
|
|
49
53
|
.Start()
|
|
54
|
+
.then(() => {
|
|
55
|
+
return waiter(500)
|
|
56
|
+
})
|
|
50
57
|
.then(() => {
|
|
51
58
|
const { connected, subscribed } = client.Status()
|
|
52
59
|
|
|
53
60
|
unitJS.assert.equal(connected, true)
|
|
54
61
|
unitJS.assert.equal(subscribed, false)
|
|
62
|
+
})
|
|
63
|
+
.then(() => {
|
|
64
|
+
return client.Disconnect()
|
|
65
|
+
})
|
|
66
|
+
.then(() => {
|
|
55
67
|
done()
|
|
56
68
|
})
|
|
57
69
|
.catch((err) => {
|
|
@@ -60,34 +72,49 @@ describe('TCaBCIClient', () => {
|
|
|
60
72
|
})
|
|
61
73
|
|
|
62
74
|
it('reconnect with start and valid parameters', (done) => {
|
|
63
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
75
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
64
76
|
|
|
65
77
|
client
|
|
66
78
|
.Start()
|
|
79
|
+
.then(() => {
|
|
80
|
+
return waiter(500)
|
|
81
|
+
})
|
|
67
82
|
.then(() => {
|
|
68
83
|
const { connected, subscribed } = client.Status()
|
|
69
84
|
|
|
70
85
|
unitJS.assert.equal(connected, true)
|
|
71
86
|
unitJS.assert.equal(subscribed, false)
|
|
72
87
|
|
|
73
|
-
client.Reconnect(
|
|
88
|
+
return client.Reconnect(1007)
|
|
89
|
+
})
|
|
90
|
+
.then(() => {
|
|
74
91
|
const { connected: c2, subscribed: b2 } = client.Status()
|
|
75
|
-
|
|
76
92
|
unitJS.assert.equal(c2, false)
|
|
77
93
|
unitJS.assert.equal(b2, false)
|
|
78
94
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
return waiter(500)
|
|
96
|
+
})
|
|
97
|
+
.then(() => {
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
setTimeout(
|
|
100
|
+
(client, res) => {
|
|
101
|
+
const { connected, subscribed } = client.Status()
|
|
102
|
+
|
|
103
|
+
unitJS.assert.equal(connected, true)
|
|
104
|
+
unitJS.assert.equal(subscribed, false)
|
|
105
|
+
res()
|
|
106
|
+
},
|
|
107
|
+
3100,
|
|
108
|
+
client,
|
|
109
|
+
resolve,
|
|
110
|
+
)
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
.then(() => {
|
|
114
|
+
return client.Disconnect()
|
|
115
|
+
})
|
|
116
|
+
.then(() => {
|
|
117
|
+
done()
|
|
91
118
|
})
|
|
92
119
|
.catch((err) => {
|
|
93
120
|
done(err)
|
|
@@ -95,16 +122,22 @@ describe('TCaBCIClient', () => {
|
|
|
95
122
|
})
|
|
96
123
|
|
|
97
124
|
it('subscribe with valid parameters', (done) => {
|
|
98
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
125
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
99
126
|
|
|
100
127
|
client
|
|
101
128
|
.Start()
|
|
129
|
+
.then(() => {
|
|
130
|
+
return waiter(750)
|
|
131
|
+
})
|
|
102
132
|
.then(() => {
|
|
103
133
|
client.Subscribe(
|
|
104
134
|
[
|
|
105
135
|
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
|
|
106
136
|
],
|
|
107
|
-
|
|
137
|
+
{
|
|
138
|
+
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN':
|
|
139
|
+
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
|
|
140
|
+
},
|
|
108
141
|
)
|
|
109
142
|
|
|
110
143
|
const { connected, subscribed } = client.Status()
|
|
@@ -112,6 +145,9 @@ describe('TCaBCIClient', () => {
|
|
|
112
145
|
unitJS.assert.equal(connected, true)
|
|
113
146
|
unitJS.assert.equal(subscribed, true)
|
|
114
147
|
|
|
148
|
+
return client.Disconnect()
|
|
149
|
+
})
|
|
150
|
+
.then(() => {
|
|
115
151
|
done()
|
|
116
152
|
})
|
|
117
153
|
.catch((err) => {
|
|
@@ -120,17 +156,25 @@ describe('TCaBCIClient', () => {
|
|
|
120
156
|
})
|
|
121
157
|
|
|
122
158
|
it('should error subscribe with invalid tx type parameter', (done) => {
|
|
123
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
159
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
124
160
|
|
|
125
161
|
client
|
|
126
162
|
.Start()
|
|
163
|
+
.then(() => {
|
|
164
|
+
return waiter(500)
|
|
165
|
+
})
|
|
127
166
|
.then(() => {
|
|
128
167
|
client.Subscribe(
|
|
129
168
|
[
|
|
130
169
|
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
|
|
170
|
+
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGU',
|
|
131
171
|
],
|
|
132
172
|
['invalid'],
|
|
133
173
|
)
|
|
174
|
+
|
|
175
|
+
return client.Disconnect()
|
|
176
|
+
})
|
|
177
|
+
.then(() => {
|
|
134
178
|
done(new Error('invalid'))
|
|
135
179
|
})
|
|
136
180
|
.catch(() => {
|
|
@@ -139,14 +183,23 @@ describe('TCaBCIClient', () => {
|
|
|
139
183
|
})
|
|
140
184
|
|
|
141
185
|
it('unsubscribe with valid parameters', (done) => {
|
|
142
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
186
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
143
187
|
|
|
144
188
|
client
|
|
145
189
|
.Start()
|
|
146
190
|
.then(() => {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
191
|
+
return waiter(500)
|
|
192
|
+
})
|
|
193
|
+
.then(() => {
|
|
194
|
+
client.Subscribe(
|
|
195
|
+
[
|
|
196
|
+
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
|
|
197
|
+
],
|
|
198
|
+
{
|
|
199
|
+
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN':
|
|
200
|
+
'2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN',
|
|
201
|
+
},
|
|
202
|
+
)
|
|
150
203
|
const { connected, subscribed } = client.Status()
|
|
151
204
|
|
|
152
205
|
unitJS.assert.equal(connected, true)
|
|
@@ -160,6 +213,10 @@ describe('TCaBCIClient', () => {
|
|
|
160
213
|
|
|
161
214
|
unitJS.assert.equal(connected, true)
|
|
162
215
|
unitJS.assert.equal(subscribed, false)
|
|
216
|
+
|
|
217
|
+
return client.Disconnect()
|
|
218
|
+
})
|
|
219
|
+
.then(() => {
|
|
163
220
|
done()
|
|
164
221
|
})
|
|
165
222
|
.catch((err) => {
|
|
@@ -168,7 +225,7 @@ describe('TCaBCIClient', () => {
|
|
|
168
225
|
})
|
|
169
226
|
|
|
170
227
|
it('last block', (done) => {
|
|
171
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
228
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
172
229
|
|
|
173
230
|
client
|
|
174
231
|
.LastBlock()
|
|
@@ -183,7 +240,7 @@ describe('TCaBCIClient', () => {
|
|
|
183
240
|
})
|
|
184
241
|
|
|
185
242
|
it('transaction search result', (done) => {
|
|
186
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
243
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
187
244
|
|
|
188
245
|
client
|
|
189
246
|
.TxSearch({
|
|
@@ -207,7 +264,7 @@ describe('TCaBCIClient', () => {
|
|
|
207
264
|
})
|
|
208
265
|
//
|
|
209
266
|
it('transaction summary result', (done) => {
|
|
210
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
267
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
211
268
|
|
|
212
269
|
client
|
|
213
270
|
.TxSummary({
|
|
@@ -230,7 +287,7 @@ describe('TCaBCIClient', () => {
|
|
|
230
287
|
})
|
|
231
288
|
|
|
232
289
|
it('should error not broadcast transaction if type is incorrect', (done) => {
|
|
233
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
290
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
234
291
|
|
|
235
292
|
try {
|
|
236
293
|
client.Broadcast({
|
|
@@ -242,13 +299,13 @@ describe('TCaBCIClient', () => {
|
|
|
242
299
|
|
|
243
300
|
done(new Error('invalid'))
|
|
244
301
|
} catch (err) {
|
|
245
|
-
unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err)
|
|
302
|
+
unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err.message)
|
|
246
303
|
done()
|
|
247
304
|
}
|
|
248
305
|
})
|
|
249
306
|
|
|
250
307
|
it('should error not broadcast transaction', (done) => {
|
|
251
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
308
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
252
309
|
|
|
253
310
|
client
|
|
254
311
|
.Broadcast({
|
|
@@ -268,13 +325,13 @@ describe('TCaBCIClient', () => {
|
|
|
268
325
|
done()
|
|
269
326
|
})
|
|
270
327
|
.catch((err) => {
|
|
271
|
-
unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err)
|
|
328
|
+
unitJS.assert.equal(TRANSACTION_NOT_BROADCAST, err.message)
|
|
272
329
|
done()
|
|
273
330
|
})
|
|
274
331
|
})
|
|
275
332
|
|
|
276
333
|
it('should error in bulk tx if addresses count is zero', (done) => {
|
|
277
|
-
const client = new TCaBCIClient([], WebSocket, '
|
|
334
|
+
const client = new TCaBCIClient([], WebSocket, 'medusa', 'v2')
|
|
278
335
|
|
|
279
336
|
client
|
|
280
337
|
.Bulk([])
|
|
@@ -296,7 +353,7 @@ describe('TCaBCIClient', () => {
|
|
|
296
353
|
)
|
|
297
354
|
}
|
|
298
355
|
|
|
299
|
-
const client = new TCaBCIClient(null, WebSocket, '
|
|
356
|
+
const client = new TCaBCIClient(null, WebSocket, 'medusa', 'v2')
|
|
300
357
|
|
|
301
358
|
client
|
|
302
359
|
.Bulk(addresses)
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tchain/tcabci-read-js-client",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "TransferChain Fastest Read Network WebSocket Client",
|
|
5
5
|
"main": "client.js",
|
|
6
6
|
"type": "module",
|
|
@@ -23,17 +23,18 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/TransferChain/tcabci-read-js-client#readme",
|
|
25
25
|
"devDependencies": {
|
|
26
|
+
"chai": "^5.2.1",
|
|
26
27
|
"eslint": "^9.15.0",
|
|
27
28
|
"eslint-config-prettier": "^9.1.0",
|
|
28
29
|
"eslint-plugin-import": "^2.31.0",
|
|
29
30
|
"eslint-plugin-node": "^11.1.0",
|
|
30
31
|
"eslint-plugin-promise": "^7.1.0",
|
|
31
|
-
"express": "^
|
|
32
|
-
"mocha": "^
|
|
32
|
+
"express": "^5.1.0",
|
|
33
|
+
"mocha": "^11.7.1",
|
|
33
34
|
"nyc": "^17.1.0",
|
|
34
|
-
"prettier": "^3.
|
|
35
|
+
"prettier": "^3.6.2",
|
|
35
36
|
"unit.js": "^2.1.1",
|
|
36
|
-
"ws": "^8.18.
|
|
37
|
+
"ws": "^8.18.3"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
40
|
"reconnecting-websocket": "^4.4.0"
|
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
|
+
}
|