openchain-nodejs-ts-yxl 1.0.5 → 1.0.7
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/index.d.ts +0 -2
- package/lib/common/util.js +14 -15
- package/lib/util/index.js +119 -57
- package/package.json +2 -3
package/index.d.ts
CHANGED
package/lib/common/util.js
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
const
|
3
|
+
const axios = require('axios');
|
4
4
|
const is = require('is-type-of');
|
5
5
|
const long = require('long');
|
6
6
|
const JSONbig = require('json-bigint');
|
7
|
-
// const bigNumberToString = require('bignumber-to-string')
|
8
7
|
const BigNumber = require('bignumber.js');
|
9
8
|
const protobuf = require("protobufjs");
|
10
9
|
const tou8 = require('buffer-to-uint8array');
|
@@ -12,10 +11,8 @@ const humps = require('humps');
|
|
12
11
|
const { keypair, signature } = require('yxchain-encryption-nodejs');
|
13
12
|
const errors = require('../exception');
|
14
13
|
|
15
|
-
|
16
14
|
const proto = exports;
|
17
15
|
|
18
|
-
|
19
16
|
/**
|
20
17
|
* GET/POST request
|
21
18
|
*
|
@@ -24,7 +21,7 @@ const proto = exports;
|
|
24
21
|
* @param {Object} data
|
25
22
|
* @return {Object}
|
26
23
|
*/
|
27
|
-
proto._request = function
|
24
|
+
proto._request = async function (method, path, data = {}) {
|
28
25
|
try {
|
29
26
|
const protocol = this.options.secure ? 'https://' : 'http://';
|
30
27
|
const uri = `${protocol}${this.options.host}/${path}`;
|
@@ -37,7 +34,7 @@ proto._request = function* (method, path, data = {}) {
|
|
37
34
|
throw new Error('path must be a non-empty string');
|
38
35
|
}
|
39
36
|
|
40
|
-
const methods = [
|
37
|
+
const methods = ['get', 'post'];
|
41
38
|
|
42
39
|
if (!methods.includes(method.toLowerCase())) {
|
43
40
|
throw new Error(`${method} http method is not supported`);
|
@@ -45,21 +42,23 @@ proto._request = function* (method, path, data = {}) {
|
|
45
42
|
|
46
43
|
const options = {
|
47
44
|
method,
|
48
|
-
uri,
|
45
|
+
url: uri,
|
49
46
|
timeout: this.options.timeout,
|
50
47
|
};
|
51
48
|
|
52
|
-
if (method === 'get') {
|
53
|
-
options.
|
49
|
+
if (method.toLowerCase() === 'get') {
|
50
|
+
options.params = data;
|
54
51
|
}
|
55
52
|
|
56
|
-
if (method === 'post') {
|
57
|
-
options.
|
53
|
+
if (method.toLowerCase() === 'post') {
|
54
|
+
options.data = data;
|
58
55
|
}
|
59
|
-
|
60
|
-
const
|
56
|
+
|
57
|
+
const response = await axios(options);
|
58
|
+
const result = response.data;
|
59
|
+
const obj = typeof result === 'string' ? JSONbig.parse(result) : result;
|
61
60
|
const error_code = obj.error_code;
|
62
|
-
const final =
|
61
|
+
const final = this._bigNumberToString(obj);
|
63
62
|
final.error_code = error_code;
|
64
63
|
return final;
|
65
64
|
} catch (err) {
|
@@ -877,4 +876,4 @@ proto._isAvailableBu = function (str) {
|
|
877
876
|
long.fromValue(str).greaterThanOrEqual(0) &&
|
878
877
|
long.fromValue(str).lessThanOrEqual(long.MAX_VALUE.divide(Math.pow(10, 8)))
|
879
878
|
);
|
880
|
-
}
|
879
|
+
}
|
package/lib/util/index.js
CHANGED
@@ -1,86 +1,148 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
const wrap = require('co-wrap-all');
|
4
|
-
const merge = require('merge-descriptors');
|
5
|
-
const BigNumber = require('bignumber.js');
|
6
3
|
const is = require('is-type-of');
|
4
|
+
const BigNumber = require('bignumber.js');
|
7
5
|
const long = require('long');
|
6
|
+
const axios = require('axios');
|
7
|
+
const JSONbig = require('json-bigint');
|
8
|
+
const errors = require('../exception');
|
9
|
+
|
10
|
+
const util = exports;
|
11
|
+
|
12
|
+
/**
|
13
|
+
* check if the object is BigNumber
|
14
|
+
* @param {Object} object
|
15
|
+
* @return {Boolean}
|
16
|
+
*/
|
17
|
+
util.isBigNumber = function (object) {
|
18
|
+
return object instanceof BigNumber;
|
19
|
+
};
|
8
20
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
}
|
21
|
+
/**
|
22
|
+
* create BigNumber object
|
23
|
+
* @param {String | Number} value
|
24
|
+
* @return {BigNumber}
|
25
|
+
*/
|
26
|
+
util.toBigNumber = function (value) {
|
27
|
+
return new BigNumber(value);
|
28
|
+
};
|
18
29
|
|
19
|
-
|
30
|
+
/**
|
31
|
+
* encode string to hex string
|
32
|
+
* @param {String} str
|
33
|
+
* @return {String} hex string
|
34
|
+
*/
|
35
|
+
util.utfToHex = function (str) {
|
36
|
+
if (is.string(str) || is.number(str)) {
|
37
|
+
let hex = '';
|
20
38
|
|
21
|
-
|
39
|
+
for (let i = 0; i < str.length; i++) {
|
40
|
+
hex += str.charCodeAt(i).toString(16);
|
41
|
+
}
|
22
42
|
|
23
|
-
|
24
|
-
|
25
|
-
};
|
43
|
+
return hex;
|
44
|
+
}
|
26
45
|
|
27
|
-
|
28
|
-
return this._toBigNumber(data);
|
46
|
+
return str;
|
29
47
|
};
|
30
48
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
49
|
+
/**
|
50
|
+
* decode hex string to string
|
51
|
+
* @param {String} str hex string
|
52
|
+
* @return {String}
|
53
|
+
*/
|
54
|
+
util.hexToUtf = function (str) {
|
55
|
+
if (is.string(str) || is.number(str)) {
|
56
|
+
let utf = '';
|
35
57
|
|
36
|
-
|
37
|
-
|
58
|
+
for (let i = 0; i < str.length; i += 2) {
|
59
|
+
utf += String.fromCharCode(parseInt(str.substr(i, 2), 16));
|
60
|
+
}
|
38
61
|
|
39
|
-
|
40
|
-
if (!is.string(str) ||
|
41
|
-
str === '' ||
|
42
|
-
!this._isHexString(str)) {
|
43
|
-
return;
|
62
|
+
return utf;
|
44
63
|
}
|
45
64
|
|
46
|
-
return
|
65
|
+
return str;
|
47
66
|
};
|
48
67
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
68
|
+
/**
|
69
|
+
* convert bu to mo
|
70
|
+
* @param {String} str
|
71
|
+
* @return {String}
|
72
|
+
*/
|
73
|
+
util.buToMo = function (str) {
|
74
|
+
return new BigNumber(str).multipliedBy(100000000).toString(10);
|
75
|
+
};
|
53
76
|
|
54
|
-
|
55
|
-
|
56
|
-
|
77
|
+
/**
|
78
|
+
* convert mo to bu
|
79
|
+
* @param {String} str
|
80
|
+
* @return {String}
|
81
|
+
*/
|
82
|
+
util.moToBu = function (str) {
|
83
|
+
return new BigNumber(str).dividedBy(100000000).toString(10);
|
57
84
|
};
|
58
85
|
|
59
|
-
|
60
|
-
|
61
|
-
|
86
|
+
/**
|
87
|
+
* calculate amount with decimals
|
88
|
+
* @param {String} amount
|
89
|
+
* @param {String} decimals
|
90
|
+
* @return {String | false}
|
91
|
+
*/
|
92
|
+
util.unitWithDecimals = function (amount, decimals) {
|
93
|
+
if (!is.string(decimals) || !is.string(amount)) {
|
94
|
+
return false;
|
62
95
|
}
|
63
96
|
|
64
|
-
const
|
65
|
-
const
|
66
|
-
return bu.toString();
|
67
|
-
};
|
68
|
-
|
69
|
-
proto.unitWithDecimals = function(amount, decimals) {
|
70
|
-
const reg = /^[0-9]+$/;
|
97
|
+
const decimalsBN = new BigNumber(decimals);
|
98
|
+
const amountBN = new BigNumber(amount);
|
71
99
|
|
72
|
-
if (
|
100
|
+
if (decimalsBN.isNaN() || amountBN.isNaN()) {
|
73
101
|
return false;
|
74
102
|
}
|
75
103
|
|
76
|
-
|
77
|
-
|
78
|
-
const amountWithDecimals = amount.times(decimals);
|
104
|
+
return amountBN.multipliedBy(Math.pow(10, decimals)).toString(10);
|
105
|
+
};
|
79
106
|
|
80
|
-
|
81
|
-
|
107
|
+
/**
|
108
|
+
* http request
|
109
|
+
* @param {String} method
|
110
|
+
* @param {String} url
|
111
|
+
* @param {Object} data
|
112
|
+
* @return {Object}
|
113
|
+
*/
|
114
|
+
util.httpRequest = async function (method, url, data = {}) {
|
115
|
+
try {
|
116
|
+
if (!is.string(method) || !is.string(url)) {
|
117
|
+
throw new Error('method and url must be string');
|
118
|
+
}
|
119
|
+
|
120
|
+
const methods = ['get', 'post'];
|
121
|
+
|
122
|
+
if (!methods.includes(method.toLowerCase())) {
|
123
|
+
throw new Error(`${method} http method is not supported`);
|
124
|
+
}
|
125
|
+
|
126
|
+
const options = {
|
127
|
+
method,
|
128
|
+
url,
|
129
|
+
timeout: 3000,
|
130
|
+
};
|
131
|
+
|
132
|
+
if (method.toLowerCase() === 'get') {
|
133
|
+
options.params = data;
|
134
|
+
}
|
135
|
+
|
136
|
+
if (method.toLowerCase() === 'post') {
|
137
|
+
options.data = data;
|
138
|
+
}
|
139
|
+
|
140
|
+
const response = await axios(options);
|
141
|
+
const result = response.data;
|
142
|
+
return typeof result === 'string' ? JSONbig.parse(result) : result;
|
143
|
+
} catch (err) {
|
144
|
+
throw err;
|
82
145
|
}
|
83
|
-
|
84
|
-
}
|
146
|
+
};
|
85
147
|
|
86
|
-
|
148
|
+
module.exports = util;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "openchain-nodejs-ts-yxl",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.7",
|
4
4
|
"description": "openchain sdk",
|
5
5
|
"main": "index.js",
|
6
6
|
"types": "index.d.ts",
|
@@ -14,6 +14,7 @@
|
|
14
14
|
"author": "OpenChain",
|
15
15
|
"license": "ISC",
|
16
16
|
"dependencies": {
|
17
|
+
"axios": "^1.6.2",
|
17
18
|
"bignumber.js": "^7.2.1",
|
18
19
|
"buffer-to-uint8array": "^1.1.0",
|
19
20
|
"co-wrap-all": "^1.0.0",
|
@@ -23,8 +24,6 @@
|
|
23
24
|
"long": "^4.0.0",
|
24
25
|
"merge-descriptors": "^1.0.1",
|
25
26
|
"protobufjs": "^6.8.8",
|
26
|
-
"request": "^2.87.0",
|
27
|
-
"request-promise": "^4.2.2",
|
28
27
|
"yxchain-encryption-nodejs": "^1.0.2"
|
29
28
|
},
|
30
29
|
"devDependencies": {
|