@tchain/tcabci-read-js-client 1.3.12 → 2.0.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/.editorconfig +9 -0
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +85 -0
- package/.prettierrc +16 -0
- package/README.md +12 -9
- package/bytea.js +14 -15
- package/client.js +402 -254
- package/client_test.js +279 -213
- package/constants.js +54 -56
- package/errors.js +26 -23
- package/message.js +48 -44
- package/package.json +16 -6
- package/transaction.js +99 -101
- package/util.js +16 -17
package/errors.js
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
const ALREADY_CONNECTED = new Error(
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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'),
|
|
5
|
+
INVALID_ARGUMENT_WITH_CS = (val) => {
|
|
6
|
+
return new Error(INVALID_ARGUMENTS + ' ' + val)
|
|
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')
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
export class FetchError extends Error {
|
|
16
|
+
message = ''
|
|
17
|
+
code = -1
|
|
18
|
+
response = {}
|
|
19
|
+
|
|
20
|
+
constructor(props, code = -1) {
|
|
21
|
+
super(props)
|
|
22
|
+
this.code = code
|
|
23
|
+
this.response = {
|
|
24
|
+
status: code,
|
|
25
|
+
statusText: Array.isArray(props) ? props[0] : props,
|
|
26
|
+
}
|
|
27
|
+
}
|
|
25
28
|
}
|
package/message.js
CHANGED
|
@@ -1,55 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } from './errors.js'
|
|
2
|
+
import { MESSAGE_TYPE, TX_TYPE } from './constants.js'
|
|
3
|
+
|
|
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
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
ToJSONString() {
|
|
17
|
+
this.#validate()
|
|
18
|
+
|
|
19
|
+
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 } : {}),
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
#validate() {
|
|
28
|
+
if (typeof this.#is_web !== 'boolean') {
|
|
29
|
+
throw INVALID_ARGUMENT_WITH_CS('is_web')
|
|
13
30
|
}
|
|
14
31
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return JSON.stringify({
|
|
18
|
-
"is_web": this.#is_web,
|
|
19
|
-
"type": this.#type,
|
|
20
|
-
"addrs": this.#addrs,
|
|
21
|
-
...(this.#txTypes.length > 0 ? { "tx_types": this.#txTypes } : {})
|
|
22
|
-
})
|
|
32
|
+
if (typeof this.#type !== 'string') {
|
|
33
|
+
throw INVALID_ARGUMENT_WITH_CS('type')
|
|
23
34
|
}
|
|
24
35
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
36
|
+
if (!Array.isArray(this.#addrs)) {
|
|
37
|
+
throw INVALID_ARGUMENT_WITH_CS('addrs')
|
|
38
|
+
}
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
40
|
+
if (
|
|
41
|
+
[MESSAGE_TYPE.SUBSCRIBE, MESSAGE_TYPE.UNSUBSCRIBE].indexOf(this.#type) ===
|
|
42
|
+
-1
|
|
43
|
+
) {
|
|
44
|
+
throw INVALID_ARGUMENTS
|
|
45
|
+
}
|
|
33
46
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
47
|
+
if (this.#txTypes.length) {
|
|
48
|
+
let vals = Object.values(TX_TYPE)
|
|
37
49
|
|
|
38
|
-
|
|
39
|
-
|
|
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')
|
|
40
53
|
}
|
|
54
|
+
}
|
|
41
55
|
|
|
42
|
-
|
|
43
|
-
let vals = Object.values(TX_TYPE)
|
|
44
|
-
for (let i = 0; i < this.#txTypes.length; i++) {
|
|
45
|
-
if (vals.indexOf(this.#txTypes[i]) === -1) {
|
|
46
|
-
throw INVALID_ARGUMENT_WITH_CS('tx_types')
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
vals = []
|
|
51
|
-
}
|
|
56
|
+
vals = []
|
|
52
57
|
}
|
|
58
|
+
}
|
|
53
59
|
}
|
|
54
|
-
|
|
55
|
-
module.exports = Message
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tchain/tcabci-read-js-client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "TransferChain Fastest Read Network WebSocket Client",
|
|
5
5
|
"main": "client.js",
|
|
6
|
-
"type": "
|
|
6
|
+
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha --timeout 5000",
|
|
9
|
-
"test:coverage": "export NODE_ENV=test || SET \"NODE_ENV=test\" && nyc --reporter=text mocha --timeout 5000"
|
|
9
|
+
"test:coverage": "export NODE_ENV=test || SET \"NODE_ENV=test\" && nyc --reporter=text mocha --timeout 5000",
|
|
10
|
+
"lint": "eslint --ext .js ./",
|
|
11
|
+
"lint-fix": "eslint --fix --ext .js ./",
|
|
12
|
+
"lint-fix-file": "eslint --fix --ext .js",
|
|
13
|
+
"format": "prettier --write \"**/*.{js,md,json}\" --ignore-path .gitignore"
|
|
10
14
|
},
|
|
11
15
|
"repository": {
|
|
12
16
|
"type": "git",
|
|
@@ -20,11 +24,17 @@
|
|
|
20
24
|
"homepage": "https://github.com/TransferChain/tcabci-read-js-client#readme",
|
|
21
25
|
"devDependencies": {
|
|
22
26
|
"mocha": "^10.2.0",
|
|
23
|
-
"nyc": "^
|
|
24
|
-
"unit.js": "^2.1.1"
|
|
27
|
+
"nyc": "^17.0.0",
|
|
28
|
+
"unit.js": "^2.1.1",
|
|
29
|
+
"eslint": "^8.56.0",
|
|
30
|
+
"eslint-config-prettier": "^9.1.0",
|
|
31
|
+
"eslint-plugin-import": "^2.29.1",
|
|
32
|
+
"eslint-plugin-node": "^11.0.0",
|
|
33
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
34
|
+
"prettier": "^3.2.4"
|
|
25
35
|
},
|
|
26
36
|
"dependencies": {
|
|
27
|
-
"
|
|
37
|
+
"node-fetch": "^3.3.2",
|
|
28
38
|
"reconnecting-websocket": "^4.4.0",
|
|
29
39
|
"ws": "^8.18.0"
|
|
30
40
|
}
|
package/transaction.js
CHANGED
|
@@ -1,108 +1,106 @@
|
|
|
1
|
-
|
|
1
|
+
import { INVALID_ARGUMENT_WITH_CS } from './errors.js'
|
|
2
2
|
|
|
3
|
-
class Transaction {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
18
|
+
constructor({
|
|
19
|
+
id,
|
|
20
|
+
block_id,
|
|
21
|
+
height,
|
|
22
|
+
identifier,
|
|
23
|
+
version,
|
|
24
|
+
typ,
|
|
25
|
+
sender_addr,
|
|
26
|
+
recipient_addr,
|
|
27
|
+
data,
|
|
28
|
+
sign,
|
|
29
|
+
fee,
|
|
30
|
+
hash,
|
|
31
|
+
inserted_at,
|
|
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
|
|
46
|
+
}
|
|
33
47
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.#height = height
|
|
37
|
-
this.#identifier = identifier
|
|
38
|
-
this.#version = version
|
|
39
|
-
this.#typ = typ
|
|
40
|
-
this.#sender_addr = sender_addr
|
|
41
|
-
this.#recipient_addr = recipient_addr
|
|
42
|
-
this.#data = data
|
|
43
|
-
this.#sign = sign
|
|
44
|
-
this.#fee = fee
|
|
45
|
-
this.#hash = hash
|
|
46
|
-
this.#inserted_at = inserted_at
|
|
47
|
-
}
|
|
48
|
+
ToJSONString() {
|
|
49
|
+
this.#validate()
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
66
|
-
}
|
|
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,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
if (typeof this.#sender_addr !== "string") {
|
|
88
|
-
throw INVALID_ARGUMENT_WITH_CS("senderAddr")
|
|
89
|
-
}
|
|
90
|
-
if (typeof this.#recipient_addr !== "string") {
|
|
91
|
-
throw INVALID_ARGUMENT_WITH_CS("recipientAddr")
|
|
92
|
-
}
|
|
93
|
-
if (typeof this.#data !== "object") {
|
|
94
|
-
throw INVALID_ARGUMENT_WITH_CS("data")
|
|
95
|
-
}
|
|
96
|
-
if (typeof this.#sign !== "object") {
|
|
97
|
-
throw INVALID_ARGUMENT_WITH_CS("sign")
|
|
98
|
-
}
|
|
99
|
-
if (typeof this.#fee !== "number") {
|
|
100
|
-
throw INVALID_ARGUMENT_WITH_CS("fee")
|
|
101
|
-
}
|
|
102
|
-
if (typeof this.#inserted_at !== "string") {
|
|
103
|
-
throw INVALID_ARGUMENT_WITH_CS("insertedAt")
|
|
104
|
-
}
|
|
68
|
+
#validate() {
|
|
69
|
+
if (typeof this.#id !== 'number') {
|
|
70
|
+
throw INVALID_ARGUMENT_WITH_CS('id')
|
|
71
|
+
}
|
|
72
|
+
if (typeof this.#block_id !== 'number') {
|
|
73
|
+
throw INVALID_ARGUMENT_WITH_CS('blockID')
|
|
74
|
+
}
|
|
75
|
+
if (typeof this.#height !== 'number') {
|
|
76
|
+
throw INVALID_ARGUMENT_WITH_CS('height')
|
|
77
|
+
}
|
|
78
|
+
if (typeof this.#identifier !== 'string') {
|
|
79
|
+
throw INVALID_ARGUMENT_WITH_CS('identifier')
|
|
80
|
+
}
|
|
81
|
+
if (typeof this.#version !== 'number') {
|
|
82
|
+
throw INVALID_ARGUMENT_WITH_CS('version')
|
|
83
|
+
}
|
|
84
|
+
if (typeof this.#typ !== 'string') {
|
|
85
|
+
throw INVALID_ARGUMENT_WITH_CS('typ')
|
|
105
86
|
}
|
|
87
|
+
if (typeof this.#sender_addr !== 'string') {
|
|
88
|
+
throw INVALID_ARGUMENT_WITH_CS('senderAddr')
|
|
89
|
+
}
|
|
90
|
+
if (typeof this.#recipient_addr !== 'string') {
|
|
91
|
+
throw INVALID_ARGUMENT_WITH_CS('recipientAddr')
|
|
92
|
+
}
|
|
93
|
+
if (typeof this.#data !== 'object') {
|
|
94
|
+
throw INVALID_ARGUMENT_WITH_CS('data')
|
|
95
|
+
}
|
|
96
|
+
if (typeof this.#sign !== 'object') {
|
|
97
|
+
throw INVALID_ARGUMENT_WITH_CS('sign')
|
|
98
|
+
}
|
|
99
|
+
if (typeof this.#fee !== 'number') {
|
|
100
|
+
throw INVALID_ARGUMENT_WITH_CS('fee')
|
|
101
|
+
}
|
|
102
|
+
if (typeof this.#inserted_at !== 'string') {
|
|
103
|
+
throw INVALID_ARGUMENT_WITH_CS('insertedAt')
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
106
|
}
|
|
107
|
-
|
|
108
|
-
module.exports = Transaction
|
package/util.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
import { CHARACTERS } from './constants.js'
|
|
2
2
|
|
|
3
|
-
const randomString = (length) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
counter += 1;
|
|
10
|
-
}
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
3
|
+
export const randomString = (length) => {
|
|
4
|
+
let result = ''
|
|
5
|
+
|
|
6
|
+
const charactersLength = CHARACTERS.length
|
|
7
|
+
|
|
8
|
+
let counter = 0
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
while (counter < length) {
|
|
11
|
+
result += CHARACTERS.charAt(Math.floor(Math.random() * charactersLength))
|
|
12
|
+
counter += 1
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return result
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
18
|
+
export const toJSON = (data) => {
|
|
19
|
+
return JSON.parse(data)
|
|
20
|
+
}
|