@dashevo/dapi-client 0.23.0-dev.8 → 0.24.0-dev.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/lib/dapiAddressProvider/DAPIAddress.js +26 -2
- package/lib/dapiAddressProvider/ListDAPIAddressProvider.js +1 -0
- package/lib/transport/GrpcTransport/GrpcTransport.js +5 -2
- package/lib/transport/GrpcTransport/createGrpcTransportError.js +21 -26
- package/lib/transport/JsonRpcTransport/JsonRpcTransport.js +2 -0
- package/lib/transport/JsonRpcTransport/requestJsonRpc.js +19 -4
- package/package.json +8 -8
|
@@ -10,13 +10,15 @@ class DAPIAddress {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
if (typeof address === 'string') {
|
|
13
|
-
const [host, httpPort, grpcPort] = address.split(':');
|
|
13
|
+
const [host, httpPort, grpcPort, ssl] = address.split(':');
|
|
14
14
|
|
|
15
15
|
// eslint-disable-next-line no-param-reassign
|
|
16
16
|
address = {
|
|
17
17
|
host,
|
|
18
18
|
httpPort: httpPort ? parseInt(httpPort, 10) : DAPIAddress.DEFAULT_HTTP_PORT,
|
|
19
19
|
grpcPort: grpcPort ? parseInt(grpcPort, 10) : DAPIAddress.DEFAULT_GRPC_PORT,
|
|
20
|
+
protocol: ssl === 'no-ssl' ? 'http' : 'https',
|
|
21
|
+
allowSelfSignedCertificate: ssl === 'self-signed',
|
|
20
22
|
};
|
|
21
23
|
}
|
|
22
24
|
|
|
@@ -24,15 +26,26 @@ class DAPIAddress {
|
|
|
24
26
|
throw new DAPIAddressHostMissingError();
|
|
25
27
|
}
|
|
26
28
|
|
|
29
|
+
this.protocol = address.protocol || 'https';
|
|
27
30
|
this.host = address.host;
|
|
28
31
|
this.httpPort = address.httpPort || DAPIAddress.DEFAULT_HTTP_PORT;
|
|
29
32
|
this.grpcPort = address.grpcPort || DAPIAddress.DEFAULT_GRPC_PORT;
|
|
30
33
|
this.proRegTxHash = address.proRegTxHash;
|
|
34
|
+
this.allowSelfSignedCertificate = address.allowSelfSignedCertificate || false;
|
|
31
35
|
|
|
32
36
|
this.banCount = 0;
|
|
33
37
|
this.banStartTime = undefined;
|
|
34
38
|
}
|
|
35
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Get protocol
|
|
42
|
+
*
|
|
43
|
+
* @returns {string}
|
|
44
|
+
*/
|
|
45
|
+
getProtocol() {
|
|
46
|
+
return this.protocol;
|
|
47
|
+
}
|
|
48
|
+
|
|
36
49
|
/**
|
|
37
50
|
* Get host
|
|
38
51
|
*
|
|
@@ -150,6 +163,13 @@ class DAPIAddress {
|
|
|
150
163
|
return this.banCount > 0;
|
|
151
164
|
}
|
|
152
165
|
|
|
166
|
+
/**
|
|
167
|
+
* @returns {boolean}
|
|
168
|
+
*/
|
|
169
|
+
isSelfSignedCertificateAllowed() {
|
|
170
|
+
return this.allowSelfSignedCertificate;
|
|
171
|
+
}
|
|
172
|
+
|
|
153
173
|
/**
|
|
154
174
|
* Return DAPIAddress as plain object
|
|
155
175
|
*
|
|
@@ -157,15 +177,17 @@ class DAPIAddress {
|
|
|
157
177
|
*/
|
|
158
178
|
toJSON() {
|
|
159
179
|
return {
|
|
180
|
+
protocol: this.getProtocol(),
|
|
160
181
|
host: this.getHost(),
|
|
161
182
|
httpPort: this.getHttpPort(),
|
|
162
183
|
grpcPort: this.getGrpcPort(),
|
|
163
184
|
proRegTxHash: this.getProRegTxHash(),
|
|
185
|
+
allowSelfSignedCertificate: this.isSelfSignedCertificateAllowed(),
|
|
164
186
|
};
|
|
165
187
|
}
|
|
166
188
|
|
|
167
189
|
toString() {
|
|
168
|
-
return `${this.getHost()}:${this.getHttpPort()}:${this.getGrpcPort()}`;
|
|
190
|
+
return `${this.getProtocol()}://${this.getHost()}:${this.getHttpPort()}:${this.getGrpcPort()}`;
|
|
169
191
|
}
|
|
170
192
|
}
|
|
171
193
|
|
|
@@ -174,10 +196,12 @@ DAPIAddress.DEFAULT_GRPC_PORT = 3010;
|
|
|
174
196
|
|
|
175
197
|
/**
|
|
176
198
|
* @typedef {object} RawDAPIAddress
|
|
199
|
+
* @property {string} protocol
|
|
177
200
|
* @property {string} host
|
|
178
201
|
* @property {number} [httpPort=3000]
|
|
179
202
|
* @property {number} [grpcPort=3010]
|
|
180
203
|
* @property {string} [proRegTxHash]
|
|
204
|
+
* @property {bool} [selfSigned]
|
|
181
205
|
*/
|
|
182
206
|
|
|
183
207
|
module.exports = DAPIAddress;
|
|
@@ -37,6 +37,7 @@ class ListDAPIAddressProvider {
|
|
|
37
37
|
const randomNodeIndex = Math.floor(Math.random() * liveAddresses.length);
|
|
38
38
|
|
|
39
39
|
liveAddress.host = '127.0.0.1';
|
|
40
|
+
liveAddress.allowSelfSignedCertificate = true;
|
|
40
41
|
liveAddress.httpPort = 3000 + randomNodeIndex * 100;
|
|
41
42
|
liveAddress.grpcPort = 3010 + randomNodeIndex * 100;
|
|
42
43
|
}
|
|
@@ -132,6 +132,7 @@ class GrpcTransport {
|
|
|
132
132
|
* @returns {string}
|
|
133
133
|
*/
|
|
134
134
|
makeGrpcUrlFromAddress(address) {
|
|
135
|
+
let protocol = address.getProtocol();
|
|
135
136
|
let port = address.getHttpPort();
|
|
136
137
|
|
|
137
138
|
// For NodeJS Client
|
|
@@ -139,9 +140,11 @@ class GrpcTransport {
|
|
|
139
140
|
&& process.versions != null
|
|
140
141
|
&& process.versions.node != null) {
|
|
141
142
|
port = address.getGrpcPort();
|
|
142
|
-
}
|
|
143
143
|
|
|
144
|
-
|
|
144
|
+
if (address.isSelfSignedCertificateAllowed()) {
|
|
145
|
+
protocol = 'http';
|
|
146
|
+
}
|
|
147
|
+
}
|
|
145
148
|
|
|
146
149
|
return `${protocol}://${address.getHost()}:${port}`;
|
|
147
150
|
}
|
|
@@ -50,32 +50,34 @@ function createGrpcTransportError(grpcError, dapiAddress) {
|
|
|
50
50
|
|
|
51
51
|
const message = grpcError.details || grpcError.message;
|
|
52
52
|
|
|
53
|
+
let metadata = {};
|
|
54
|
+
|
|
53
55
|
if (grpcError.metadata) {
|
|
54
56
|
// In cases of gRPC-Web client we get plain map instead of Metadata instance
|
|
55
|
-
|
|
57
|
+
metadata = grpcError.metadata;
|
|
56
58
|
if (grpcError.metadata instanceof Metadata) {
|
|
57
|
-
|
|
59
|
+
metadata = grpcError.metadata.getMap();
|
|
58
60
|
}
|
|
61
|
+
}
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
// Error data
|
|
64
|
+
const driveErrorData = metadata['drive-error-data-bin'];
|
|
65
|
+
if (driveErrorData) {
|
|
66
|
+
const encodedData = Buffer.from(driveErrorData, 'base64');
|
|
67
|
+
data = cbor.decode(encodedData);
|
|
68
|
+
}
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
// Error code
|
|
71
|
+
const driveErrorCode = metadata.code;
|
|
72
|
+
if (driveErrorCode) {
|
|
73
|
+
code = Number(driveErrorCode);
|
|
74
|
+
}
|
|
72
75
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
76
|
+
// Error stack
|
|
77
|
+
const driveErrorStack = metadata['stack-bin'];
|
|
78
|
+
if (driveErrorStack) {
|
|
79
|
+
const encodedStack = Buffer.from(driveErrorStack, 'base64');
|
|
80
|
+
data.stack = cbor.decode(encodedStack);
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
// Specialized classes
|
|
@@ -100,13 +102,6 @@ function createGrpcTransportError(grpcError, dapiAddress) {
|
|
|
100
102
|
}
|
|
101
103
|
|
|
102
104
|
if (code === GrpcErrorCodes.INTERNAL) {
|
|
103
|
-
if (grpcError.metadata) {
|
|
104
|
-
const metaStack = grpcError.metadata.get('stack-bin');
|
|
105
|
-
if (metaStack && metaStack.length > 0) {
|
|
106
|
-
data.stack = cbor.decode(metaStack[0]);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
105
|
return new InternalServerError(
|
|
111
106
|
code,
|
|
112
107
|
message,
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
|
+
const https = require('https');
|
|
2
3
|
|
|
3
4
|
const JsonRpcError = require('./errors/JsonRpcError');
|
|
4
5
|
const WrongHttpCodeError = require('./errors/WrongHttpCodeError');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* @typedef {requestJsonRpc}
|
|
9
|
+
* @param {string} protocol
|
|
8
10
|
* @param {string} host
|
|
9
11
|
* @param {number} port
|
|
12
|
+
* @param {boolean} selfSigned
|
|
10
13
|
* @param {string} method
|
|
11
14
|
* @param {object} params
|
|
12
15
|
* @param {object} [options]
|
|
13
16
|
* @returns {Promise<*>}
|
|
14
17
|
*/
|
|
15
|
-
async function requestJsonRpc(host, port, method, params, options = {}) {
|
|
16
|
-
const protocol = port === 443 ? 'https' : 'http';
|
|
17
|
-
|
|
18
|
+
async function requestJsonRpc(protocol, host, port, selfSigned, method, params, options = {}) {
|
|
18
19
|
const url = `${protocol}://${host}${port && port !== 443 ? `:${port}` : ''}`;
|
|
19
20
|
|
|
20
21
|
const payload = {
|
|
@@ -30,8 +31,10 @@ async function requestJsonRpc(host, port, method, params, options = {}) {
|
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
const requestInfo = {
|
|
34
|
+
protocol,
|
|
33
35
|
host,
|
|
34
36
|
port,
|
|
37
|
+
selfSigned,
|
|
35
38
|
method,
|
|
36
39
|
params,
|
|
37
40
|
options,
|
|
@@ -39,11 +42,23 @@ async function requestJsonRpc(host, port, method, params, options = {}) {
|
|
|
39
42
|
|
|
40
43
|
let response;
|
|
41
44
|
|
|
45
|
+
const config = { timeout: options.timeout };
|
|
46
|
+
// For NodeJS Client
|
|
47
|
+
if (typeof process !== 'undefined'
|
|
48
|
+
&& process.versions != null
|
|
49
|
+
&& process.versions.node != null
|
|
50
|
+
&& protocol === 'https'
|
|
51
|
+
&& selfSigned) {
|
|
52
|
+
config.httpsAgent = new https.Agent({
|
|
53
|
+
rejectUnauthorized: false,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
42
57
|
try {
|
|
43
58
|
response = await axios.post(
|
|
44
59
|
url,
|
|
45
60
|
payload,
|
|
46
|
-
|
|
61
|
+
config,
|
|
47
62
|
);
|
|
48
63
|
} catch (error) {
|
|
49
64
|
if (error.response && error.response.status >= 500) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dashevo/dapi-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.0-dev.1",
|
|
4
4
|
"description": "Client library used to access Dash DAPI endpoints",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"contributors": [
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
}
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@dashevo/dapi-grpc": "~0.
|
|
30
|
-
"@dashevo/dash-spv": "~
|
|
31
|
-
"@dashevo/dashcore-lib": "~0.19.
|
|
32
|
-
"@dashevo/dpp": "~0.
|
|
33
|
-
"@dashevo/grpc-common": "~0.
|
|
29
|
+
"@dashevo/dapi-grpc": "~0.24.0-dev.1",
|
|
30
|
+
"@dashevo/dash-spv": "~0.24.0-dev.1",
|
|
31
|
+
"@dashevo/dashcore-lib": "~0.19.44",
|
|
32
|
+
"@dashevo/dpp": "~0.24.0-dev.1",
|
|
33
|
+
"@dashevo/grpc-common": "~0.24.0-dev.1",
|
|
34
34
|
"@grpc/grpc-js": "^1.3.7",
|
|
35
|
-
"axios": "^0.
|
|
35
|
+
"axios": "^0.27.2",
|
|
36
36
|
"bs58": "^4.0.1",
|
|
37
37
|
"cbor": "^8.0.0",
|
|
38
38
|
"lodash.sample": "^4.2.1",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"eslint-plugin-import": "^2.24.2",
|
|
55
55
|
"eslint-plugin-jsdoc": "^27.0.0",
|
|
56
56
|
"events": "^3.3.0",
|
|
57
|
-
"karma": "^6.
|
|
57
|
+
"karma": "^6.4.1",
|
|
58
58
|
"karma-chai": "^0.1.0",
|
|
59
59
|
"karma-chrome-launcher": "^3.1.0",
|
|
60
60
|
"karma-firefox-launcher": "^2.1.1",
|