@tchain/tcabci-read-js-client 1.0.2 → 1.0.4
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 +2 -2
- package/client.js +33 -12
- package/client_test.js +40 -3
- package/constants.js +25 -1
- package/errors.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Read Node WebSocket Address: [wss://read-node-01.transferchain.io/ws](wss://read
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```shell
|
|
10
|
-
$ npm
|
|
10
|
+
$ npm i @tchain/tcabci-read-js-client
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
## Example
|
|
@@ -15,7 +15,7 @@ $ npm install tcabci-read-js-client
|
|
|
15
15
|
**Subscribe, Listen and Unsubscribe Example**
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
const tcAbciClient = require("tcabci-read-js-client")
|
|
18
|
+
const tcAbciClient = require("@tchain/tcabci-read-js-client")
|
|
19
19
|
|
|
20
20
|
const client = new tcAbciClient()
|
|
21
21
|
|
package/client.js
CHANGED
|
@@ -4,9 +4,11 @@ const {
|
|
|
4
4
|
INVALID_ARGUMENTS,
|
|
5
5
|
NOT_SUBSCRIBED,
|
|
6
6
|
ADDRESSES_IS_EMPTY,
|
|
7
|
-
BLOCK_NOT_FOUND
|
|
7
|
+
BLOCK_NOT_FOUND,
|
|
8
|
+
TRANSACTION_TYPE_NOT_VALID,
|
|
9
|
+
TRANSACTION_NOT_BROADCAST
|
|
8
10
|
} = require("./errors")
|
|
9
|
-
const {MESSAGE_TYPE, READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS} = require("./constants")
|
|
11
|
+
const {MESSAGE_TYPE, READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS, TX_TYPE} = require("./constants")
|
|
10
12
|
const Transaction = require("./transaction")
|
|
11
13
|
const Message = require("./message")
|
|
12
14
|
const {toJSON} = require("./util")
|
|
@@ -55,7 +57,15 @@ class TCAbciClient {
|
|
|
55
57
|
this.#setConnected(true)
|
|
56
58
|
resolve(event)
|
|
57
59
|
}
|
|
58
|
-
this.#ws.onmessage =
|
|
60
|
+
this.#ws.onmessage = (message) => {
|
|
61
|
+
if (message.data === "OK" && message.data.length < 10) {
|
|
62
|
+
return { status: message.data }
|
|
63
|
+
}
|
|
64
|
+
if (this.listenCb) {
|
|
65
|
+
this.listenCb(toJSON(message.data))
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
59
69
|
})
|
|
60
70
|
}
|
|
61
71
|
Stop() {
|
|
@@ -137,6 +147,26 @@ class TCAbciClient {
|
|
|
137
147
|
subscribed: this.#subscribed,
|
|
138
148
|
}
|
|
139
149
|
}
|
|
150
|
+
Broadcast({id, version, type, data, sender_addr, recipient_addr, sign, fee}) {
|
|
151
|
+
if(Object.values(TX_TYPE).indexOf(type) < 0) {
|
|
152
|
+
throw TRANSACTION_TYPE_NOT_VALID
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return this.#httpClient.post(
|
|
156
|
+
"/v1/broadcast",
|
|
157
|
+
{
|
|
158
|
+
id,
|
|
159
|
+
version,
|
|
160
|
+
type,
|
|
161
|
+
data,
|
|
162
|
+
sender_addr,
|
|
163
|
+
recipient_addr,
|
|
164
|
+
sign,
|
|
165
|
+
fee
|
|
166
|
+
}
|
|
167
|
+
).then(res => { return { data: res.data.data } })
|
|
168
|
+
.catch(e => { throw TRANSACTION_NOT_BROADCAST })
|
|
169
|
+
}
|
|
140
170
|
|
|
141
171
|
#wsClient() {
|
|
142
172
|
if (typeof window !== "undefined") {
|
|
@@ -145,15 +175,6 @@ class TCAbciClient {
|
|
|
145
175
|
const { WebSocket } = require("ws")
|
|
146
176
|
return WebSocket
|
|
147
177
|
}
|
|
148
|
-
#listen(message) {
|
|
149
|
-
if (message.data === "OK" && message.data.length < 10) {
|
|
150
|
-
return { status: message.data }
|
|
151
|
-
}
|
|
152
|
-
const txData = toJSON(message.data)
|
|
153
|
-
if (this.listenCb) {
|
|
154
|
-
this.listenCb(new Transaction(txData).ToJSONString())
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
178
|
#getConnected() {
|
|
158
179
|
return this.#connected
|
|
159
180
|
}
|
package/client_test.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const tcAbciClient = require("./client")
|
|
2
2
|
const unitJS = require("unit.js")
|
|
3
|
-
const {INVALID_ARGUMENTS, INVALID_URL} = require("./errors")
|
|
3
|
+
const {INVALID_ARGUMENTS, INVALID_URL, TRANSACTION_TYPE_NOT_VALID} = require("./errors")
|
|
4
|
+
const { TX_TYPE } = require("./constants")
|
|
4
5
|
const {randomString} = require("./util")
|
|
5
6
|
|
|
6
7
|
describe("TCAbciClient TESTS", () => {
|
|
@@ -70,13 +71,13 @@ describe("TCAbciClient TESTS", () => {
|
|
|
70
71
|
|
|
71
72
|
it('should return transaction search result',(done) => {
|
|
72
73
|
const client = new tcAbciClient()
|
|
73
|
-
client.TxSearch({
|
|
74
|
+
client.TxSearch({
|
|
74
75
|
heightOperator: ">=",
|
|
75
76
|
height: 0,
|
|
76
77
|
recipientAddrs: ["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"],
|
|
77
78
|
limit: 1,
|
|
78
79
|
offset: 0,
|
|
79
|
-
orderBy: "ASC"
|
|
80
|
+
orderBy: "ASC"
|
|
80
81
|
})
|
|
81
82
|
.then(data => {
|
|
82
83
|
unitJS.value(data.txs).hasLength(1)
|
|
@@ -88,4 +89,40 @@ describe("TCAbciClient TESTS", () => {
|
|
|
88
89
|
})
|
|
89
90
|
})
|
|
90
91
|
|
|
92
|
+
it('should not broadcast transaction if type is incorrect',(done) => {
|
|
93
|
+
const client = new tcAbciClient()
|
|
94
|
+
try {
|
|
95
|
+
client.Broadcast({
|
|
96
|
+
type: "dummy type",
|
|
97
|
+
data: "",
|
|
98
|
+
sender_addr: "",
|
|
99
|
+
recipient_addr: ""
|
|
100
|
+
})
|
|
101
|
+
} catch (err) {
|
|
102
|
+
unitJS.assert.equal(TRANSACTION_TYPE_NOT_VALID, err)
|
|
103
|
+
done()
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('should not broadcast transaction',(done) => {
|
|
108
|
+
const client = new tcAbciClient()
|
|
109
|
+
client.Broadcast({
|
|
110
|
+
id: "dummy id",
|
|
111
|
+
version: 0,
|
|
112
|
+
type: TX_TYPE.TX_TYPE_ADDRESS,
|
|
113
|
+
data: btoa(JSON.stringify({data: ""})),
|
|
114
|
+
sender_addr: "2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN",
|
|
115
|
+
recipient_addr: "2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN",
|
|
116
|
+
sign: btoa("dummy sign"),
|
|
117
|
+
fee: 0
|
|
118
|
+
})
|
|
119
|
+
.then(data => {
|
|
120
|
+
unitJS.assert.notEqual(data.data.hash, "")
|
|
121
|
+
done()
|
|
122
|
+
})
|
|
123
|
+
.catch(err => {
|
|
124
|
+
done(err)
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
91
128
|
})
|
package/constants.js
CHANGED
|
@@ -5,5 +5,29 @@ module.exports = {
|
|
|
5
5
|
},
|
|
6
6
|
CHARACTERS: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
|
|
7
7
|
READ_NODE_ADDRESS: "https://read-node-01.transferchain.io",
|
|
8
|
-
READ_NODE_WS_ADDRESS: "wss://read-node-01.transferchain.io/ws"
|
|
8
|
+
READ_NODE_WS_ADDRESS: "wss://read-node-01.transferchain.io/ws",
|
|
9
|
+
TX_TYPE: {
|
|
10
|
+
TX_TYPE_MASTER: "initial_storage",
|
|
11
|
+
TX_TYPE_ADDRESS: "interim_storage",
|
|
12
|
+
TX_TYPE_ADDRESSES: "interim_storages",
|
|
13
|
+
TX_TYPE_MESSAGE: "message",
|
|
14
|
+
TX_TYPE_MESSAGE_SENT: "inherit_message",
|
|
15
|
+
TX_TYPE_MESSAGE_THREAD_DELETE: "inherit_message_recv",
|
|
16
|
+
TX_TYPE_TRANSFER: "transfer",
|
|
17
|
+
TX_TYPE_TRANSFER_CANCEL: "transfer_Cancel",
|
|
18
|
+
TX_TYPE_TRANSFER_SENT: "transfer_sent",
|
|
19
|
+
TX_TYPE_TRANSFER_RECEIVE_DELETE: "transfer_receive_delete",
|
|
20
|
+
TX_TYPE_STORAGE: "storage",
|
|
21
|
+
TX_TYPE_STORAGE_DELETE: "storage_delete",
|
|
22
|
+
TX_TYPE_BACKUP: "backup",
|
|
23
|
+
TX_TYPE_CONTACT: "interim_message",
|
|
24
|
+
TX_TYPE_FILE_VIRTUAL: "fs_virt",
|
|
25
|
+
TX_TYPE_FILE_FS: "fs_real",
|
|
26
|
+
TX_TYPE_RFILE_VIRTUAL: "fs_rvirt",
|
|
27
|
+
TX_TYPE_RFILE_FS: "fs_rreal",
|
|
28
|
+
TX_TYPE_REQUEST: "request",
|
|
29
|
+
TX_TYPE_REQUEST_IN: "request_in",
|
|
30
|
+
TX_TYPE_REQUEST_UPLOAD: "request_upload",
|
|
31
|
+
TX_TYPE_REQUEST_CANCEL: "request_Cancel"
|
|
32
|
+
}
|
|
9
33
|
}
|
package/errors.js
CHANGED
|
@@ -8,5 +8,7 @@ module.exports = {
|
|
|
8
8
|
NOT_SUBSCRIBED: new Error("Not subscribed"),
|
|
9
9
|
ALREADY_SUBSCRIBED: new Error("Already subscribed"),
|
|
10
10
|
ADDRESSES_IS_EMPTY: new Error("Addresses is empty"),
|
|
11
|
-
BLOCK_NOT_FOUND: new Error("Block does not exist")
|
|
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")
|
|
12
14
|
}
|