@tchain/tcabci-read-js-client 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,191 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ https://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2019 TransferChain A.G
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ https://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # TCABCI Read Node Javascript WebSocket Client
2
+
3
+ TransferChain Fastest Read Network WebSocket Client
4
+ Read Node Address: [https://read-node-01.transferchain.io](https://read-node-01.transferchain.io)
5
+ Read Node WebSocket Address: [wss://read-node-01.transferchain.io/ws](wss://read-node-01.transferchain.io/ws)
6
+
7
+ ## Installation
8
+
9
+ ```shell
10
+ $ npm install tcabci-read-js-client
11
+ ```
12
+
13
+ ## Example
14
+
15
+ **Subscribe, Listen and Unsubscribe Example**
16
+
17
+ ```js
18
+ const client = new tcAbciClient()
19
+
20
+ client.Start()
21
+
22
+ client.Subscribe(["<your-public-address-one>", "<your-public-address-two>",])
23
+
24
+ client.SetListen(() => {
25
+ // If a transaction has been sent to your addresses, the callback you set here will be called.
26
+ })
27
+
28
+ client.Unsubscribe()
29
+ client.Stop()
30
+ ```
31
+ ## License
32
+
33
+ tcabci-read-js-client is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license
34
+ text.
package/bytea.js ADDED
@@ -0,0 +1,16 @@
1
+ class Bytea {
2
+ #bytes
3
+ #status
4
+ constructor(bytes, status) {
5
+ this.#bytes = bytes
6
+ this.#status = status
7
+ }
8
+
9
+ ToJSON() {
10
+ return JSON.stringify({
11
+ "bytes": this.#bytes,
12
+ "status": this.#status
13
+ })
14
+ }
15
+
16
+ }
package/client.js ADDED
@@ -0,0 +1,181 @@
1
+ const {
2
+ NOT_CONNECTED,
3
+ ALREADY_CONNECTED,
4
+ INVALID_ARGUMENTS,
5
+ NOT_SUBSCRIBED,
6
+ ADDRESSES_IS_EMPTY,
7
+ BLOCK_NOT_FOUND
8
+ } = require("./errors")
9
+ const {MESSAGE_TYPE, READ_NODE_ADDRESS, READ_NODE_WS_ADDRESS} = require("./constants")
10
+ const Transaction = require("./transaction")
11
+ const Message = require("./message")
12
+ const {toJSON} = require("./util")
13
+ const axios = require("axios")
14
+
15
+ class TCAbciClient {
16
+ #subscribed = false
17
+ #subscribedAddresses = []
18
+ #connected = false
19
+ #version = "v0.1.0"
20
+ errorCb = null
21
+ listenCb = null
22
+ #ws = null
23
+ #httpClient = null
24
+
25
+ constructor() {
26
+ this.#httpClient = axios.create({
27
+ baseURL: READ_NODE_ADDRESS,
28
+ timeout: 10000,
29
+ headers: {'Client': `tcabaci-read-js-client${this.#version}`}
30
+ })
31
+ }
32
+
33
+ SetError(cb) {
34
+ this.errorCb = cb
35
+ }
36
+ SetListen(cb) {
37
+ this.listenCb = cb
38
+ }
39
+ Start() {
40
+ return new Promise((resolve, reject) => {
41
+ if (this.#getConnected()) {
42
+ reject(ALREADY_CONNECTED)
43
+ }
44
+ const wsClient = this.#wsClient()
45
+ this.#ws = new wsClient(READ_NODE_WS_ADDRESS)
46
+
47
+ this.#ws.onerror = (event) => {
48
+ this.#setConnected(false)
49
+ if (this.errorCb) {
50
+ this.errorCb(event.error)
51
+ }
52
+ reject(event)
53
+ }
54
+ this.#ws.onopen = (event) => {
55
+ this.#setConnected(true)
56
+ resolve(event)
57
+ }
58
+ this.#ws.onmessage = this.#listen
59
+ })
60
+ }
61
+ Stop() {
62
+ if (!this.#getConnected()) {
63
+ throw NOT_CONNECTED
64
+ }
65
+ this.#ws.close()
66
+ this.#setConnected(false)
67
+ this.#setSubscribed(false)
68
+ }
69
+ Subscribe(addresses) {
70
+ if (!Array.isArray(addresses)) {
71
+ throw INVALID_ARGUMENTS
72
+ }
73
+ if (!this.#getConnected()) {
74
+ throw NOT_CONNECTED
75
+ }
76
+ let addrs = []
77
+ if (addresses.length === 0) {
78
+ throw ADDRESSES_IS_EMPTY
79
+ }
80
+ addrs = addresses
81
+ if (this.#getSubscribeAddresses().length > 0) {
82
+ let newAddress = []
83
+ for (let i = 0; i < addresses.length; i++) {
84
+ if (this.#getSubscribeAddresses().indexOf(addresses[i]) === -1 ){
85
+ newAddress.push(addresses[i])
86
+ }
87
+ }
88
+ addrs = newAddress
89
+ }
90
+ const message = new Message(true, MESSAGE_TYPE.SUBSCRIBE, addrs)
91
+ this.#ws.send(message.ToJSONString())
92
+ this.#setSubscribeAddresses(addrs, true)
93
+ this.#setSubscribed(true)
94
+ }
95
+ Unsubscribe() {
96
+ if (!this.#getSubscribed()) {
97
+ throw NOT_SUBSCRIBED
98
+ }
99
+ this.#ws.send(new Message(true, MESSAGE_TYPE.UNSUBSCRIBE, this.#getSubscribeAddresses()).ToJSONString())
100
+ this.#setSubscribed(false)
101
+ this.#setSubscribeAddresses([])
102
+ }
103
+ LastBlock() {
104
+ return this.#httpClient.get("/v1/blocks?limit=1&offset=0")
105
+ .then(res => { return { blocks: res.data.data, total_count: res.data.total_count } } )
106
+ .catch(e => {
107
+ throw BLOCK_NOT_FOUND
108
+ })
109
+ }
110
+ TxSearch({heightOperator, height, recipientAddrs, senderAddrs, hashes, typ, limit, offset, orderField, orderBy}) {
111
+ return this.#httpClient.post(
112
+ "/v1/tx_search/p",
113
+ {
114
+ height: `${heightOperator} ${height}`,
115
+ recipient_addrs: recipientAddrs,
116
+ sender_addrs: senderAddrs,
117
+ hashes: hashes,
118
+ typ,
119
+ limit,
120
+ offset,
121
+ order_field: orderField,
122
+ order_by: orderBy
123
+ })
124
+ .then(res => { return {txs: res.data.data, total_count: res.data.data.length} })
125
+ .catch(e => {
126
+ switch (e.response.status) {
127
+ case 400:
128
+ throw INVALID_ARGUMENTS
129
+ default:
130
+ throw e
131
+ }
132
+ })
133
+ }
134
+ Status() {
135
+ return {
136
+ connected: this.#connected,
137
+ subscribed: this.#subscribed,
138
+ }
139
+ }
140
+
141
+ #wsClient() {
142
+ if (typeof window !== "undefined") {
143
+ return window.WebSocket
144
+ }
145
+ const { WebSocket } = require("ws")
146
+ return WebSocket
147
+ }
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
+ #getConnected() {
158
+ return this.#connected
159
+ }
160
+ #setConnected(value) {
161
+ this.#connected = value
162
+ }
163
+ #getSubscribed() {
164
+ return this.#subscribed
165
+ }
166
+ #setSubscribed(value) {
167
+ this.#subscribed = value
168
+ }
169
+ #getSubscribeAddresses() {
170
+ return this.#subscribedAddresses
171
+ }
172
+ #setSubscribeAddresses(addresses, push = false) {
173
+ if (push) {
174
+ this.#subscribedAddresses.push(...addresses)
175
+ return
176
+ }
177
+ this.#subscribedAddresses = addresses
178
+ }
179
+ }
180
+
181
+ module.exports = TCAbciClient
package/client_test.js ADDED
@@ -0,0 +1,91 @@
1
+ const tcAbciClient = require("./client")
2
+ const unitJS = require("unit.js")
3
+ const {INVALID_ARGUMENTS, INVALID_URL} = require("./errors")
4
+ const {randomString} = require("./util")
5
+
6
+ describe("TCAbciClient TESTS", () => {
7
+
8
+ it('should start with valid parameters',(done) => {
9
+ const client = new tcAbciClient()
10
+ client.Start()
11
+ .then(() => {
12
+ const { connected, subscribed } = client.Status()
13
+ unitJS.assert.equal(connected, true)
14
+ unitJS.assert.equal(subscribed, false)
15
+ done()
16
+ })
17
+ .catch(err => {
18
+ done(err)
19
+ })
20
+ })
21
+
22
+ it('should subscribe with valid parameters',(done) => {
23
+ const client = new tcAbciClient()
24
+ client.Start()
25
+ .then(() => {
26
+ client.Subscribe(["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"])
27
+ const { connected, subscribed } = client.Status()
28
+ unitJS.assert.equal(connected, true)
29
+ unitJS.assert.equal(subscribed, true)
30
+ done()
31
+ })
32
+ .catch(err => {
33
+ done(err)
34
+ })
35
+ })
36
+
37
+ it('should unsubscribe with valid parameters',(done) => {
38
+ const client = new tcAbciClient()
39
+ client.Start()
40
+ .then(() => {
41
+ client.Subscribe(["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"])
42
+ const { connected, subscribed } = client.Status()
43
+ unitJS.assert.equal(connected, true)
44
+ unitJS.assert.equal(subscribed, true)
45
+ })
46
+ .then(() => {
47
+ client.Unsubscribe()
48
+ const { connected, subscribed } = client.Status()
49
+ unitJS.assert.equal(connected, true)
50
+ unitJS.assert.equal(subscribed, false)
51
+ done()
52
+ })
53
+ .catch(err => {
54
+ done(err)
55
+ })
56
+ })
57
+
58
+ it('should return last block',(done) => {
59
+ const client = new tcAbciClient()
60
+ client.LastBlock()
61
+ .then(data => {
62
+ unitJS.value(data.blocks).hasLength(1)
63
+ unitJS.value(data.total_count).isGreaterThan(1)
64
+ done()
65
+ })
66
+ .catch(err => {
67
+ done(err)
68
+ })
69
+ })
70
+
71
+ it('should return transaction search result',(done) => {
72
+ const client = new tcAbciClient()
73
+ client.TxSearch({
74
+ heightOperator: ">=",
75
+ height: 0,
76
+ recipientAddrs: ["2mSCzresfg8Gwu7LZ9k9BTWkQAcQEkvYHFUSCZE2ubM4QV89PTeSYwQDqBas3ykq2emHEK6VRvxdgoe1vrhBbQGN"],
77
+ limit: 1,
78
+ offset: 0,
79
+ orderBy: "ASC"
80
+ })
81
+ .then(data => {
82
+ unitJS.value(data.txs).hasLength(1)
83
+ unitJS.assert.equal(data.total_count, 1)
84
+ done()
85
+ })
86
+ .catch(err => {
87
+ done(err)
88
+ })
89
+ })
90
+
91
+ })
package/constants.js ADDED
@@ -0,0 +1,9 @@
1
+ module.exports = {
2
+ MESSAGE_TYPE: {
3
+ SUBSCRIBE: "subscribe",
4
+ UNSUBSCRIBE: "unsubscribe"
5
+ },
6
+ CHARACTERS: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
7
+ READ_NODE_ADDRESS: "https://read-node-01.transferchain.io",
8
+ READ_NODE_WS_ADDRESS: "wss://read-node-01.transferchain.io/ws"
9
+ }
package/errors.js ADDED
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ ALREADY_CONNECTED: new Error("Already connected"),
3
+ NOT_CONNECTED: new Error("Not connected"),
4
+ INVALID_ARGUMENTS: new Error("Check your arguments"),
5
+ INVALID_ARGUMENT_WITH_CS: (val) => {
6
+ return new Error(this.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
+ }
package/message.js ADDED
@@ -0,0 +1,38 @@
1
+ const { INVALID_ARGUMENTS, INVALID_ARGUMENT_WITH_CS } = require("./errors")
2
+ const { MESSAGE_TYPE } = require("./constants")
3
+ class Message {
4
+ #is_web = false
5
+ #type = ""
6
+ #addrs = []
7
+ constructor(isWeb, type, addrs) {
8
+ this.#is_web = isWeb
9
+ this.#type = type
10
+ this.#addrs = addrs
11
+ }
12
+
13
+ ToJSONString() {
14
+ this.#validate()
15
+ return JSON.stringify({
16
+ "is_web": this.#is_web,
17
+ "type": this.#type,
18
+ "addrs": this.#addrs
19
+ })
20
+ }
21
+
22
+ #validate() {
23
+ if (typeof this.#is_web !== "boolean") {
24
+ throw INVALID_ARGUMENT_WITH_CS("is_web")
25
+ }
26
+ if (typeof this.#type !== "string") {
27
+ throw INVALID_ARGUMENT_WITH_CS("type")
28
+ }
29
+ if (!Array.isArray(this.#addrs)) {
30
+ throw INVALID_ARGUMENT_WITH_CS("addrs")
31
+ }
32
+ if ([MESSAGE_TYPE.SUBSCRIBE, MESSAGE_TYPE.UNSUBSCRIBE].indexOf(this.#type) === -1) {
33
+ throw INVALID_ARGUMENTS
34
+ }
35
+ }
36
+ }
37
+
38
+ module.exports = Message
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@tchain/tcabci-read-js-client",
3
+ "version": "1.0.1",
4
+ "description": "TransferChain Fastest Read Network WebSocket Client",
5
+ "main": "client.js",
6
+ "type": "commonjs",
7
+ "scripts": {
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"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/TransferChain/tcabci-read-js-client.git"
14
+ },
15
+ "author": "TransferChain A.G <info@transferchain.io>",
16
+ "license": "Apache-2.0",
17
+ "bugs": {
18
+ "url": "https://github.com/TransferChain/tcabci-read-js-client/issues"
19
+ },
20
+ "homepage": "https://github.com/TransferChain/tcabci-read-js-client#readme",
21
+ "devDependencies": {
22
+ "mocha": "^10.2.0",
23
+ "nyc": "^15.1.0",
24
+ "unit.js": "^2.1.1"
25
+ },
26
+ "dependencies": {
27
+ "axios": "^1.4.0",
28
+ "ws": "^8.13.0"
29
+ }
30
+ }
package/transaction.js ADDED
@@ -0,0 +1,109 @@
1
+ const {INVALID_ARGUMENT_WITH_CS, INVALID_ARGUMENTS} = require("./errors");
2
+ const {MESSAGE_TYPE} = require("./constants");
3
+
4
+ class Transaction {
5
+ #id
6
+ #block_id
7
+ #height
8
+ #identifier
9
+ #version
10
+ #typ
11
+ #sender_addr
12
+ #recipient_addr
13
+ #data
14
+ #sign
15
+ #fee
16
+ #hash
17
+ #inserted_at
18
+
19
+ constructor({
20
+ id,
21
+ block_id,
22
+ height,
23
+ identifier,
24
+ version,
25
+ typ,
26
+ sender_addr,
27
+ recipient_addr,
28
+ data,
29
+ sign,
30
+ fee,
31
+ hash,
32
+ inserted_at
33
+ }) {
34
+
35
+ this.#id = id
36
+ this.#block_id = block_id
37
+ this.#height = height
38
+ this.#identifier = identifier
39
+ this.#version = version
40
+ this.#typ = typ
41
+ this.#sender_addr = sender_addr
42
+ this.#recipient_addr = recipient_addr
43
+ this.#data = data
44
+ this.#sign = sign
45
+ this.#fee = fee
46
+ this.#hash = hash
47
+ this.#inserted_at = inserted_at
48
+ }
49
+
50
+ ToJSONString() {
51
+ this.#validate()
52
+ return JSON.stringify({
53
+ "id": this.#id,
54
+ "block_id": this.#block_id,
55
+ "height": this.#height,
56
+ "identifier": this.#identifier,
57
+ "version": this.#version,
58
+ "typ": this.#typ,
59
+ "sender_addr": this.#sender_addr,
60
+ "recipient_addr": this.#recipient_addr,
61
+ "data": this.#data,
62
+ "sign": this.#sign,
63
+ "fee": this.#fee,
64
+ "hash": this.#hash,
65
+ "inserted_at": this.#inserted_at
66
+ })
67
+ }
68
+
69
+ #validate() {
70
+ if (typeof this.#id !== "number") {
71
+ throw INVALID_ARGUMENT_WITH_CS("id")
72
+ }
73
+ if (typeof this.#block_id !== "number") {
74
+ throw INVALID_ARGUMENT_WITH_CS("blockID")
75
+ }
76
+ if (typeof this.#height !== "number") {
77
+ throw INVALID_ARGUMENT_WITH_CS("height")
78
+ }
79
+ if (typeof this.#identifier !== "string") {
80
+ throw INVALID_ARGUMENT_WITH_CS("identifier")
81
+ }
82
+ if (typeof this.#version !== "number") {
83
+ throw INVALID_ARGUMENT_WITH_CS("version")
84
+ }
85
+ if (typeof this.#typ !== "string") {
86
+ throw INVALID_ARGUMENT_WITH_CS("typ")
87
+ }
88
+ if (typeof this.#sender_addr !== "string") {
89
+ throw INVALID_ARGUMENT_WITH_CS("senderAddr")
90
+ }
91
+ if (typeof this.#recipient_addr !== "string") {
92
+ throw INVALID_ARGUMENT_WITH_CS("recipientAddr")
93
+ }
94
+ if (typeof this.#data !== "object") {
95
+ throw INVALID_ARGUMENT_WITH_CS("data")
96
+ }
97
+ if (typeof this.#sign !== "object") {
98
+ throw INVALID_ARGUMENT_WITH_CS("sign")
99
+ }
100
+ if (typeof this.#fee !== "number") {
101
+ throw INVALID_ARGUMENT_WITH_CS("fee")
102
+ }
103
+ if (typeof this.#inserted_at !== "string") {
104
+ throw INVALID_ARGUMENT_WITH_CS("insertedAt")
105
+ }
106
+ }
107
+ }
108
+
109
+ module.exports = Transaction
package/util.js ADDED
@@ -0,0 +1,21 @@
1
+ const {CHARACTERS} = require("./constants");
2
+
3
+ const randomString = (length) => {
4
+ let result = '';
5
+ const charactersLength = CHARACTERS.length;
6
+ let counter = 0;
7
+ while (counter < length) {
8
+ result += CHARACTERS.charAt(Math.floor(Math.random() * charactersLength));
9
+ counter += 1;
10
+ }
11
+ return result;
12
+ }
13
+
14
+ const toJSON = (data) => {
15
+ return JSON.parse(data)
16
+ }
17
+
18
+ module.exports = {
19
+ randomString,
20
+ toJSON
21
+ }