openchain-nodejs-ts-yxl 1.0.8 → 1.1.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/util/index.js CHANGED
@@ -1,148 +1,86 @@
1
1
  'use strict';
2
2
 
3
- const is = require('is-type-of');
3
+ const wrap = require('co-wrap-all');
4
+ const merge = require('merge-descriptors');
4
5
  const BigNumber = require('bignumber.js');
6
+ const is = require('is-type-of');
5
7
  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
- };
20
8
 
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
- };
9
+ module.exports = Util;
29
10
 
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 = '';
11
+ function Util(options) {
12
+ if (!(this instanceof Util)) {
13
+ return new Util(options);
14
+ }
38
15
 
39
- for (let i = 0; i < str.length; i++) {
40
- hex += str.charCodeAt(i).toString(16);
41
- }
16
+ this.options = options;
17
+ }
42
18
 
43
- return hex;
44
- }
19
+ const proto = Util.prototype;
45
20
 
46
- return str;
47
- };
21
+ merge(proto, require('../common/util'));
48
22
 
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 = '';
23
+ proto.isBigNumber = function (object) {
24
+ return this._isBigNumber(object);
25
+ };
57
26
 
58
- for (let i = 0; i < str.length; i += 2) {
59
- utf += String.fromCharCode(parseInt(str.substr(i, 2), 16));
60
- }
27
+ proto.toBigNumber = function(data) {
28
+ return this._toBigNumber(data);
29
+ };
61
30
 
62
- return utf;
31
+ proto.utfToHex = function(str) {
32
+ if (!is.string(str)) {
33
+ return;
63
34
  }
64
35
 
65
- return str;
36
+ return Buffer.from(str, 'utf8').toString('hex');
66
37
  };
67
38
 
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);
39
+ proto.hexToUtf = function(str) {
40
+ if (!is.string(str) ||
41
+ str === '' ||
42
+ !this._isHexString(str)) {
43
+ return;
44
+ }
45
+
46
+ return Buffer.from(str, 'hex').toString('utf8');
75
47
  };
76
48
 
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);
49
+ proto.buToMo = function(bu) {
50
+ if (!this._isAvailableBu(bu)) {
51
+ return '';
52
+ }
53
+
54
+ const oneMo = Math.pow(10, 8);
55
+ const mo = new BigNumber(bu).times(oneMo);
56
+ return mo.toString();
84
57
  };
85
58
 
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;
59
+ proto.moToBu = function(mo) {
60
+ if (!this._isAvailableValue(mo)) {
61
+ return '';
95
62
  }
96
63
 
97
- const decimalsBN = new BigNumber(decimals);
98
- const amountBN = new BigNumber(amount);
64
+ const oneMo = Math.pow(10, 8);
65
+ const bu = new BigNumber(mo).dividedBy(oneMo);
66
+ return bu.toString();
67
+ };
68
+
69
+ proto.unitWithDecimals = function(amount, decimals) {
70
+ const reg = /^[0-9]+$/;
99
71
 
100
- if (decimalsBN.isNaN() || amountBN.isNaN()) {
72
+ if (!reg.test(amount) || !reg.test(decimals)) {
101
73
  return false;
102
74
  }
103
75
 
104
- return amountBN.multipliedBy(Math.pow(10, decimals)).toString(10);
105
- };
76
+ amount = new BigNumber(amount);
77
+ decimals = new BigNumber(Math.pow(10, decimals));
78
+ const amountWithDecimals = amount.times(decimals);
106
79
 
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;
80
+ if (amountWithDecimals.isGreaterThanOrEqualTo(0) && amountWithDecimals.isLessThanOrEqualTo(long.MAX_VALUE.toString())) {
81
+ return amountWithDecimals.toString();
145
82
  }
146
- };
83
+ return false;
84
+ }
147
85
 
148
- module.exports = util;
86
+ wrap(proto);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openchain-nodejs-ts-yxl",
3
- "version": "1.0.8",
3
+ "version": "1.1.1",
4
4
  "description": "openchain sdk",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",