@ocap/util 1.27.16 → 1.28.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/esm/bn.d.ts +8 -6
- package/esm/bn.js +11 -7
- package/esm/constant.d.ts +4 -1
- package/esm/constant.js +5 -1
- package/esm/create-sorted-list.d.ts +4 -1
- package/esm/create-sorted-list.js +8 -3
- package/esm/curve.d.ts +85 -55
- package/esm/curve.js +132 -127
- package/esm/error.d.ts +12 -9
- package/esm/error.js +24 -17
- package/esm/get-list-field.d.ts +4 -1
- package/esm/get-list-field.js +7 -2
- package/esm/get-related-addr.d.ts +4 -1
- package/esm/get-related-addr.js +5 -1
- package/esm/index.d.ts +47 -44
- package/esm/index.js +364 -469
- package/esm/lodash.d.ts +12 -0
- package/esm/md5.d.ts +4 -1
- package/esm/md5.js +7 -3
- package/esm/ready.d.ts +13 -9
- package/esm/ready.js +33 -36
- package/esm/retry.d.ts +16 -9
- package/esm/retry.js +26 -27
- package/esm/tsfixme.d.ts +3 -0
- package/esm/url.d.ts +16 -12
- package/esm/url.js +67 -92
- package/lib/_virtual/rolldown_runtime.js +29 -0
- package/lib/bn.d.ts +8 -6
- package/lib/bn.js +12 -12
- package/lib/constant.d.ts +4 -1
- package/lib/constant.js +6 -4
- package/lib/create-sorted-list.d.ts +4 -1
- package/lib/create-sorted-list.js +11 -10
- package/lib/curve.d.ts +84 -55
- package/lib/curve.js +139 -137
- package/lib/error.d.ts +12 -9
- package/lib/error.js +25 -21
- package/lib/get-list-field.d.ts +4 -1
- package/lib/get-list-field.js +9 -9
- package/lib/get-related-addr.d.ts +4 -1
- package/lib/get-related-addr.js +5 -4
- package/lib/index.d.ts +47 -44
- package/lib/index.js +386 -506
- package/lib/lodash.d.ts +12 -0
- package/lib/md5.d.ts +4 -1
- package/lib/md5.js +9 -10
- package/lib/ready.d.ts +13 -9
- package/lib/ready.js +34 -42
- package/lib/retry.d.ts +16 -9
- package/lib/retry.js +27 -30
- package/lib/tsfixme.d.ts +3 -0
- package/lib/url.d.ts +16 -12
- package/lib/url.js +69 -118
- package/package.json +20 -9
package/esm/index.js
CHANGED
|
@@ -1,529 +1,424 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
|
|
1
|
+
import { BN } from "./bn.js";
|
|
2
|
+
import BaseBN from "bn.js";
|
|
3
|
+
import isBoolean from "lodash/isBoolean.js";
|
|
4
|
+
import isString from "lodash/isString.js";
|
|
5
|
+
import isNumber from "lodash/isNumber.js";
|
|
6
|
+
import isObject from "lodash/isObject.js";
|
|
7
|
+
import upperFirst from "lodash/upperFirst.js";
|
|
8
|
+
import camelCase from "lodash/camelCase.js";
|
|
9
|
+
import isNull from "lodash/isNull.js";
|
|
10
|
+
import rightPad from "lodash/padEnd.js";
|
|
11
|
+
import leftPad from "lodash/padStart.js";
|
|
12
|
+
import base58 from "bs58";
|
|
13
|
+
import base64 from "base64-url";
|
|
14
|
+
import * as utf8 from "utf8";
|
|
15
|
+
|
|
16
|
+
//#region src/index.ts
|
|
17
|
+
const DID_PREFIX = "did:abt:";
|
|
15
18
|
const zero = new BN(0);
|
|
16
19
|
const negative1 = new BN(-1);
|
|
17
20
|
const base58btc = {
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
encode: (v) => `z${base58.encode(v)}`,
|
|
22
|
+
decode: (v) => base58.decode(v.slice(1))
|
|
20
23
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
catch (err) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return false;
|
|
24
|
+
const isBase58btc = (data) => {
|
|
25
|
+
if (typeof data !== "string") return false;
|
|
26
|
+
if (data[0] === "z") try {
|
|
27
|
+
base58btc.decode(data);
|
|
28
|
+
return true;
|
|
29
|
+
} catch (err) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
36
33
|
};
|
|
37
|
-
export { BN, leftPad, rightPad };
|
|
38
34
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if ((stringArg.match(/^-?[0-9]+$/) || stringArg === '') && isHexPrefixed === false) {
|
|
61
|
-
return new BN(stringArg, 10).mul(multiplier);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (isBN(arg)) {
|
|
65
|
-
return new BN(arg.toString(10), 10);
|
|
66
|
-
}
|
|
67
|
-
throw new Error(`[number-to-bn] while converting number ${JSON.stringify(arg)} to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.`);
|
|
35
|
+
* Returns a BN object, converts a number value to a BN
|
|
36
|
+
* @param {string|number|BN} `arg` input a string number, hex string number, number, BigNumber or BN object
|
|
37
|
+
* @return {BN} `output` BN object of the number
|
|
38
|
+
* @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
|
|
39
|
+
*/
|
|
40
|
+
const numberToBN = (arg) => {
|
|
41
|
+
if (typeof arg === "string" || typeof arg === "number") {
|
|
42
|
+
let multiplier = new BN(1);
|
|
43
|
+
const formattedString = String(arg).toLowerCase().trim();
|
|
44
|
+
const isHexPrefixed$1 = formattedString.substr(0, 2) === "0x" || formattedString.substr(0, 3) === "-0x";
|
|
45
|
+
let stringArg = stripHexPrefix(formattedString);
|
|
46
|
+
if (stringArg.substr(0, 1) === "-") {
|
|
47
|
+
stringArg = stripHexPrefix(stringArg.slice(1));
|
|
48
|
+
multiplier = new BN(-1, 10);
|
|
49
|
+
}
|
|
50
|
+
stringArg = stringArg === "" ? "0" : stringArg;
|
|
51
|
+
if (!stringArg.match(/^-?[0-9]+$/) && stringArg.match(/^[0-9A-Fa-f]+$/) || stringArg.match(/^[a-fA-F]+$/) || isHexPrefixed$1 === true && stringArg.match(/^[0-9A-Fa-f]+$/)) return new BN(stringArg, 16).mul(multiplier);
|
|
52
|
+
if ((stringArg.match(/^-?[0-9]+$/) || stringArg === "") && isHexPrefixed$1 === false) return new BN(stringArg, 10).mul(multiplier);
|
|
53
|
+
}
|
|
54
|
+
if (isBN(arg)) return new BN(arg.toString(10), 10);
|
|
55
|
+
throw new Error(`[number-to-bn] while converting number ${JSON.stringify(arg)} to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.`);
|
|
68
56
|
};
|
|
69
57
|
/**
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
return str.slice(0, 2).toLowerCase() === '0x';
|
|
58
|
+
* Returns a `boolean` on whether or not the `string` starts with '0x'
|
|
59
|
+
*
|
|
60
|
+
* @public
|
|
61
|
+
* @static
|
|
62
|
+
* @param {String} str the string input value
|
|
63
|
+
* @return {Boolean} a boolean if it is or is not hex prefixed
|
|
64
|
+
* @throws if the str input is not a string
|
|
65
|
+
*/
|
|
66
|
+
const isHexPrefixed = (str) => {
|
|
67
|
+
if (typeof str !== "string") throw new Error("[is-hex-prefixed] value must be type string");
|
|
68
|
+
return str.slice(0, 2).toLowerCase() === "0x";
|
|
83
69
|
};
|
|
84
70
|
/**
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
return str;
|
|
71
|
+
* Removes '0x' from a given `String` if present
|
|
72
|
+
*
|
|
73
|
+
* @public
|
|
74
|
+
* @static
|
|
75
|
+
*/
|
|
76
|
+
const stripHexPrefix = (str) => {
|
|
77
|
+
if (typeof str === "string") return isHexPrefixed(str) ? str.slice(2) : str;
|
|
78
|
+
return str;
|
|
95
79
|
};
|
|
96
80
|
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
object instanceof BaseBN ||
|
|
107
|
-
// constructor validation only work for node cause browser bundle will obfuscated code
|
|
108
|
-
(object && object.constructor && object.constructor.name === 'BN');
|
|
81
|
+
* Returns true if object is BN, otherwise false
|
|
82
|
+
*
|
|
83
|
+
* @public
|
|
84
|
+
* @static
|
|
85
|
+
* @method isBN
|
|
86
|
+
* @param {Object} object
|
|
87
|
+
* @returns {Boolean}
|
|
88
|
+
*/
|
|
89
|
+
const isBN = (object) => object instanceof BN || object instanceof BaseBN || object && object.constructor && object.constructor.name === "BN";
|
|
109
90
|
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
91
|
+
* Returns true if object is BigNumber, otherwise false
|
|
92
|
+
*
|
|
93
|
+
* @public
|
|
94
|
+
* @static
|
|
95
|
+
* @method isBigNumber
|
|
96
|
+
* @param {Object} object
|
|
97
|
+
* @returns {Boolean}
|
|
98
|
+
*/
|
|
99
|
+
const isBigNumber = (object) => object && object.constructor && object.constructor.name === "BigNumber";
|
|
119
100
|
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
101
|
+
* Check if string is HEX, requires a 0x in front
|
|
102
|
+
*
|
|
103
|
+
* @public
|
|
104
|
+
* @static
|
|
105
|
+
* @method isHexStrict
|
|
106
|
+
* @param {String} hex to be checked
|
|
107
|
+
* @returns {Boolean}
|
|
108
|
+
*/
|
|
109
|
+
const isHexStrict = (hex) => (isString(hex) || isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex);
|
|
129
110
|
/**
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
111
|
+
* Check if string is HEX
|
|
112
|
+
*
|
|
113
|
+
* @public
|
|
114
|
+
* @static
|
|
115
|
+
* @method isHex
|
|
116
|
+
* @param {String} hex to be checked
|
|
117
|
+
* @returns {Boolean}
|
|
118
|
+
*/
|
|
119
|
+
const isHex = (hex) => (isString(hex) || isNumber(hex)) && /^(-0x|0x|0X|-0X)?[0-9a-f]*$/i.test(hex);
|
|
139
120
|
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
throw new Error(`${error} Given value: "${num}"`);
|
|
158
|
-
}
|
|
121
|
+
* Takes an input and transforms it into an BN
|
|
122
|
+
*
|
|
123
|
+
* @public
|
|
124
|
+
* @static
|
|
125
|
+
* @method toBN
|
|
126
|
+
* @param {Number|String|BN} num, string, HEX string or BN
|
|
127
|
+
* @returns {BN} BN
|
|
128
|
+
*/
|
|
129
|
+
const toBN = (num, base = 10) => {
|
|
130
|
+
try {
|
|
131
|
+
if (typeof num === "number" && num > 0) return new BN(Number(num).toLocaleString("fullwide", { useGrouping: false }), base);
|
|
132
|
+
return numberToBN(num);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
throw new Error(`${error} Given value: "${num}"`);
|
|
135
|
+
}
|
|
159
136
|
};
|
|
160
137
|
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const n = code.toString(16);
|
|
182
|
-
hex += n.length < 2 ? `0${n}` : n;
|
|
183
|
-
}
|
|
184
|
-
return `0x${hex}`;
|
|
138
|
+
* Should be called to get hex representation (prefixed by 0x) of utf8 string
|
|
139
|
+
*
|
|
140
|
+
* @public
|
|
141
|
+
* @static
|
|
142
|
+
* @method utf8ToHex
|
|
143
|
+
* @param {String} str
|
|
144
|
+
* @returns {String} hex representation of input string
|
|
145
|
+
*/
|
|
146
|
+
const utf8ToHex = (str) => {
|
|
147
|
+
str = utf8.encode(str);
|
|
148
|
+
let hex = "";
|
|
149
|
+
str = str.replace(/^(?:\u0000)*/, "");
|
|
150
|
+
str = str.split("").reverse().join("");
|
|
151
|
+
str = str.replace(/^(?:\u0000)*/, "");
|
|
152
|
+
str = str.split("").reverse().join("");
|
|
153
|
+
for (let i = 0; i < str.length; i++) {
|
|
154
|
+
const n = str.charCodeAt(i).toString(16);
|
|
155
|
+
hex += n.length < 2 ? `0${n}` : n;
|
|
156
|
+
}
|
|
157
|
+
return `0x${hex}`;
|
|
185
158
|
};
|
|
186
159
|
/**
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
str += String.fromCharCode(code);
|
|
211
|
-
// }
|
|
212
|
-
}
|
|
213
|
-
return utf8.decode(str);
|
|
160
|
+
* Should be called to get utf8 from it's hex representation
|
|
161
|
+
*
|
|
162
|
+
* @public
|
|
163
|
+
* @static
|
|
164
|
+
* @method hexToUtf8
|
|
165
|
+
* @param {String} hex
|
|
166
|
+
* @returns {String} ascii string representation of hex value
|
|
167
|
+
*/
|
|
168
|
+
const hexToUtf8 = (hex) => {
|
|
169
|
+
if (!isHexStrict(hex)) throw new Error(`The parameter "${hex}" must be a valid HEX string.`);
|
|
170
|
+
let str = "";
|
|
171
|
+
let code = 0;
|
|
172
|
+
hex = hex.replace(/^0x/i, "");
|
|
173
|
+
hex = hex.replace(/^(?:00)*/, "");
|
|
174
|
+
hex = hex.split("").reverse().join("");
|
|
175
|
+
hex = hex.replace(/^(?:00)*/, "");
|
|
176
|
+
hex = hex.split("").reverse().join("");
|
|
177
|
+
const l = hex.length;
|
|
178
|
+
for (let i = 0; i < l; i += 2) {
|
|
179
|
+
code = parseInt(hex.substr(i, 2), 16);
|
|
180
|
+
str += String.fromCharCode(code);
|
|
181
|
+
}
|
|
182
|
+
return utf8.decode(str);
|
|
214
183
|
};
|
|
215
184
|
/**
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
return toBN(value).toNumber();
|
|
185
|
+
* Converts value to number representation
|
|
186
|
+
*
|
|
187
|
+
* @public
|
|
188
|
+
* @static
|
|
189
|
+
* @method hexToNumber
|
|
190
|
+
* @param {String|Number|BN} value
|
|
191
|
+
* @returns {Number}
|
|
192
|
+
*/
|
|
193
|
+
const hexToNumber = (value) => {
|
|
194
|
+
if (!value) return 0;
|
|
195
|
+
return toBN(value).toNumber();
|
|
229
196
|
};
|
|
230
197
|
/**
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
throw new Error(`Given input "${value}" is not a number.`);
|
|
246
|
-
}
|
|
247
|
-
const num = toBN(value);
|
|
248
|
-
const result = num.toString(16);
|
|
249
|
-
return num.lt(new BN(0)) ? `-0x${result.substr(1)}` : `0x${result}`;
|
|
198
|
+
* Converts value to hex representation
|
|
199
|
+
*
|
|
200
|
+
* @public
|
|
201
|
+
* @static
|
|
202
|
+
* @method numberToHex
|
|
203
|
+
* @param {String|Number|BN} value
|
|
204
|
+
* @returns {String}
|
|
205
|
+
*/
|
|
206
|
+
const numberToHex = (value) => {
|
|
207
|
+
if (isNull(value) || typeof value === "undefined") return value;
|
|
208
|
+
if (!isFinite(value) && !isHex(value)) throw new Error(`Given input "${value}" is not a number.`);
|
|
209
|
+
const num = toBN(value);
|
|
210
|
+
const result = num.toString(16);
|
|
211
|
+
return num.lt(new BN(0)) ? `-0x${result.substr(1)}` : `0x${result}`;
|
|
250
212
|
};
|
|
251
213
|
/**
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
214
|
+
* Convert a byte array to a hex string
|
|
215
|
+
* Note: Implementation from crypto-js
|
|
216
|
+
*
|
|
217
|
+
* @public
|
|
218
|
+
* @static
|
|
219
|
+
* @method bytesToHex
|
|
220
|
+
* @param {Array} bytes
|
|
221
|
+
* @returns {String} the hex string
|
|
222
|
+
*/
|
|
223
|
+
const bytesToHex = (bytes) => {
|
|
224
|
+
const hex = [];
|
|
225
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
226
|
+
hex.push((bytes[i] >>> 4).toString(16));
|
|
227
|
+
hex.push((bytes[i] & 15).toString(16));
|
|
228
|
+
}
|
|
229
|
+
return `0x${hex.join("")}`;
|
|
268
230
|
};
|
|
269
231
|
/**
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
for (let c = 0; c < hex.length; c += 2) {
|
|
288
|
-
bytes.push(parseInt(hex.substr(c, 2), 16));
|
|
289
|
-
}
|
|
290
|
-
return bytes;
|
|
232
|
+
* Convert a hex string to a byte array
|
|
233
|
+
* Note: Implementation from crypto-js
|
|
234
|
+
*
|
|
235
|
+
* @public
|
|
236
|
+
* @static
|
|
237
|
+
* @method hexToBytes
|
|
238
|
+
* @param {String} hex
|
|
239
|
+
* @returns {Array} the byte array
|
|
240
|
+
*/
|
|
241
|
+
const hexToBytes = (hex) => {
|
|
242
|
+
hex = hex.toString(16);
|
|
243
|
+
if (!isHex(hex)) throw new Error(`Given value "${hex}" is not a valid hex string.`);
|
|
244
|
+
hex = hex.replace(/^0x/i, "");
|
|
245
|
+
hex = hex.length % 2 ? `0${hex}` : hex;
|
|
246
|
+
const bytes = [];
|
|
247
|
+
for (let c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16));
|
|
248
|
+
return bytes;
|
|
291
249
|
};
|
|
292
250
|
/**
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (isObject(value) && !isBigNumber(value) && !isBN(value)) {
|
|
315
|
-
return returnType ? 'string' : utf8ToHex(JSON.stringify(value));
|
|
316
|
-
}
|
|
317
|
-
// if its a negative number, pass it through numberToHex
|
|
318
|
-
if (typeof value === 'string') {
|
|
319
|
-
if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) {
|
|
320
|
-
return returnType ? 'int256' : numberToHex(value);
|
|
321
|
-
}
|
|
322
|
-
if (value.indexOf('0x') === 0 || value.indexOf('0X') === 0) {
|
|
323
|
-
return returnType ? 'bytes' : value;
|
|
324
|
-
}
|
|
325
|
-
// TODO: some edge case may be not properly handled here
|
|
326
|
-
return returnType ? 'string' : utf8ToHex(value);
|
|
327
|
-
}
|
|
328
|
-
// eslint-disable-next-line no-nested-ternary
|
|
329
|
-
return returnType ? (+value < 0 ? 'int256' : 'uint256') : numberToHex(value);
|
|
251
|
+
* Auto converts any given value into it's hex representation.
|
|
252
|
+
* And even stringify objects before.
|
|
253
|
+
*
|
|
254
|
+
* @public
|
|
255
|
+
* @static
|
|
256
|
+
* @method toHex
|
|
257
|
+
* @param {String|Number|BN|Object|TypedArray|Buffer} value
|
|
258
|
+
* @param {Boolean} returnType
|
|
259
|
+
* @returns {String}
|
|
260
|
+
*/
|
|
261
|
+
const toHex = (value, returnType = false) => {
|
|
262
|
+
if (isUint8Array(value) || Buffer.isBuffer(value)) return returnType ? "bytes" : bytesToHex(value);
|
|
263
|
+
if (isBase58btc(value)) return returnType ? "bytes" : bytesToHex(base58btc.decode(value));
|
|
264
|
+
if (isBoolean(value)) return returnType ? "bool" : value ? "0x01" : "0x00";
|
|
265
|
+
if (isObject(value) && !isBigNumber(value) && !isBN(value)) return returnType ? "string" : utf8ToHex(JSON.stringify(value));
|
|
266
|
+
if (typeof value === "string") {
|
|
267
|
+
if (value.indexOf("-0x") === 0 || value.indexOf("-0X") === 0) return returnType ? "int256" : numberToHex(value);
|
|
268
|
+
if (value.indexOf("0x") === 0 || value.indexOf("0X") === 0) return returnType ? "bytes" : value;
|
|
269
|
+
return returnType ? "string" : utf8ToHex(value);
|
|
270
|
+
}
|
|
271
|
+
return returnType ? +value < 0 ? "int256" : "uint256" : numberToHex(value);
|
|
330
272
|
};
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (arg.toPrecision) {
|
|
346
|
-
return String(arg.toPrecision());
|
|
347
|
-
}
|
|
348
|
-
return arg.toString(10);
|
|
349
|
-
}
|
|
350
|
-
throw new Error(`while converting number to string, invalid number value '${arg}' type ${typeof arg}.`);
|
|
273
|
+
const numberToString = (arg) => {
|
|
274
|
+
if (typeof arg === "string") {
|
|
275
|
+
if (!arg.match(/^-?[0-9.]+$/)) throw new Error(`Invalid value '${arg}' while converting to string, should be a number matching (^-?[0-9.]+).`);
|
|
276
|
+
return arg;
|
|
277
|
+
}
|
|
278
|
+
if (typeof arg === "number") {
|
|
279
|
+
if (Number.isInteger(arg) && arg > 0) return Number(arg).toLocaleString("fullwide", { useGrouping: false });
|
|
280
|
+
return String(arg);
|
|
281
|
+
}
|
|
282
|
+
if (typeof arg === "object" && arg.toString && (arg.toTwos || arg.dividedToIntegerBy)) {
|
|
283
|
+
if (arg.toPrecision) return String(arg.toPrecision());
|
|
284
|
+
return arg.toString(10);
|
|
285
|
+
}
|
|
286
|
+
throw new Error(`while converting number to string, invalid number value '${arg}' type ${typeof arg}.`);
|
|
351
287
|
};
|
|
352
288
|
/**
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
|
371
|
-
}
|
|
372
|
-
let whole = unit.div(base).toString(10);
|
|
373
|
-
if (options.commify) {
|
|
374
|
-
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
375
|
-
}
|
|
376
|
-
let value = `${whole}${fraction === '0' ? '' : `.${fraction}`}`;
|
|
377
|
-
if (negative) {
|
|
378
|
-
value = `-${value}`;
|
|
379
|
-
}
|
|
380
|
-
return value;
|
|
289
|
+
* Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token
|
|
290
|
+
*/
|
|
291
|
+
const fromUnitToToken = (input, decimal = 18, optionsInput = {}) => {
|
|
292
|
+
let unit = toBN(input);
|
|
293
|
+
const negative = unit.lt(zero);
|
|
294
|
+
const base = toBN(`1${"0".repeat(decimal)}`, 10);
|
|
295
|
+
const baseLength = base.toString(10).length - 1 || 1;
|
|
296
|
+
const options = optionsInput || {};
|
|
297
|
+
if (negative) unit = unit.mul(negative1);
|
|
298
|
+
let fraction = unit.mod(base).toString(10);
|
|
299
|
+
while (fraction.length < baseLength) fraction = `0${fraction}`;
|
|
300
|
+
if (!options.pad) fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1];
|
|
301
|
+
let whole = unit.div(base).toString(10);
|
|
302
|
+
if (options.commify) whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
303
|
+
let value = `${whole}${fraction === "0" ? "" : `.${fraction}`}`;
|
|
304
|
+
if (negative) value = `-${value}`;
|
|
305
|
+
return value;
|
|
381
306
|
};
|
|
382
307
|
/**
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
whole = '0';
|
|
406
|
-
}
|
|
407
|
-
if (!fraction) {
|
|
408
|
-
fraction = '0';
|
|
409
|
-
}
|
|
410
|
-
if (fraction.length > baseLength) {
|
|
411
|
-
throw new Error(`error converting token ${input} to unit, too many decimal places`);
|
|
412
|
-
}
|
|
413
|
-
while (fraction.length < baseLength) {
|
|
414
|
-
fraction += '0';
|
|
415
|
-
}
|
|
416
|
-
whole = new BN(whole);
|
|
417
|
-
fraction = new BN(fraction);
|
|
418
|
-
let unit = whole.mul(base).add(fraction);
|
|
419
|
-
if (negative) {
|
|
420
|
-
unit = unit.mul(negative1);
|
|
421
|
-
}
|
|
422
|
-
return new BN(unit.toString(10), 10);
|
|
308
|
+
* Convert human readable token number to big number instance
|
|
309
|
+
*/
|
|
310
|
+
const fromTokenToUnit = (input, decimal = 18) => {
|
|
311
|
+
let token = numberToString(input);
|
|
312
|
+
const base = toBN(`1${"0".repeat(decimal)}`, 10);
|
|
313
|
+
const baseLength = base.toString(10).length - 1 || 1;
|
|
314
|
+
const negative = token.substring(0, 1) === "-";
|
|
315
|
+
if (negative) token = token.substring(1);
|
|
316
|
+
if (token === ".") throw new Error(`error converting token ${input} to unit, invalid value`);
|
|
317
|
+
const comps = token.split(".");
|
|
318
|
+
if (comps.length > 2) throw new Error(`error converting token ${input} to unit, too many decimal points`);
|
|
319
|
+
let whole = comps[0];
|
|
320
|
+
let fraction = comps[1];
|
|
321
|
+
if (!whole) whole = "0";
|
|
322
|
+
if (!fraction) fraction = "0";
|
|
323
|
+
if (fraction.length > baseLength) throw new Error(`error converting token ${input} to unit, too many decimal places`);
|
|
324
|
+
while (fraction.length < baseLength) fraction += "0";
|
|
325
|
+
whole = new BN(whole);
|
|
326
|
+
fraction = new BN(fraction);
|
|
327
|
+
let unit = whole.mul(base).add(fraction);
|
|
328
|
+
if (negative) unit = unit.mul(negative1);
|
|
329
|
+
return new BN(unit.toString(10), 10);
|
|
423
330
|
};
|
|
424
331
|
/**
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
332
|
+
* Validates if a value is an Uint8Array.
|
|
333
|
+
*/
|
|
334
|
+
function isUint8Array(value) {
|
|
335
|
+
return Object.prototype.toString.call(value) === "[object Uint8Array]";
|
|
429
336
|
}
|
|
430
337
|
/**
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
});
|
|
338
|
+
* Generate a random UUID
|
|
339
|
+
*/
|
|
340
|
+
function UUID() {
|
|
341
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
342
|
+
const r = Math.random() * 16 | 0;
|
|
343
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
344
|
+
});
|
|
439
345
|
}
|
|
440
346
|
/**
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
347
|
+
* Check if a string is valid UUID
|
|
348
|
+
*/
|
|
349
|
+
function isUUID(str) {
|
|
350
|
+
return /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.test(str);
|
|
445
351
|
}
|
|
446
352
|
/**
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
else if (isBase58btc(v)) {
|
|
464
|
-
vb = new Uint8Array(base58btc.decode(v));
|
|
465
|
-
}
|
|
466
|
-
else if (typeof v === 'string') {
|
|
467
|
-
vb = new Uint8Array(hexToBytes(toHex(v)));
|
|
468
|
-
}
|
|
469
|
-
else {
|
|
470
|
-
throw new Error(`Unsupported input type ${typeof v} detected for toBuffer, only Uint8Array/Buffer/Hex/Base58 are supported`);
|
|
471
|
-
}
|
|
472
|
-
return vb;
|
|
353
|
+
* Convert input to Uint8Array on best effort, base64 node supported
|
|
354
|
+
*/
|
|
355
|
+
function toUint8Array(v) {
|
|
356
|
+
let vb = null;
|
|
357
|
+
if ([
|
|
358
|
+
null,
|
|
359
|
+
void 0,
|
|
360
|
+
""
|
|
361
|
+
].includes(v)) vb = new Uint8Array();
|
|
362
|
+
else if (Buffer.isBuffer(v)) vb = new Uint8Array(v);
|
|
363
|
+
else if (isHexStrict(v)) vb = new Uint8Array(hexToBytes(v));
|
|
364
|
+
else if (isUint8Array(v)) vb = new Uint8Array(v);
|
|
365
|
+
else if (isBase58btc(v)) vb = new Uint8Array(base58btc.decode(v));
|
|
366
|
+
else if (typeof v === "string") vb = new Uint8Array(hexToBytes(toHex(v)));
|
|
367
|
+
else throw new Error(`Unsupported input type ${typeof v} detected for toBuffer, only Uint8Array/Buffer/Hex/Base58 are supported`);
|
|
368
|
+
return vb;
|
|
473
369
|
}
|
|
474
370
|
/**
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
371
|
+
* Convert input to Buffer on best effort, base64 not supported
|
|
372
|
+
*/
|
|
373
|
+
function toBuffer(v) {
|
|
374
|
+
return Buffer.from(toUint8Array(v));
|
|
479
375
|
}
|
|
480
376
|
/**
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
377
|
+
* Convert input to base58btc format on best effort
|
|
378
|
+
*/
|
|
379
|
+
function toBase58(v) {
|
|
380
|
+
const buf = base58btc.encode(toUint8Array(v));
|
|
381
|
+
return Buffer.from(buf).toString("utf-8");
|
|
486
382
|
}
|
|
487
383
|
/**
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
}
|
|
494
|
-
return Buffer.from(base58btc.decode(v));
|
|
384
|
+
* Decode base58 string
|
|
385
|
+
*/
|
|
386
|
+
function fromBase58(v) {
|
|
387
|
+
if (isBase58btc(v) === false) throw new Error("fromBase58 expect strict base58 encoded string as input");
|
|
388
|
+
return Buffer.from(base58btc.decode(v));
|
|
495
389
|
}
|
|
496
390
|
/**
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
391
|
+
* Convert input to base64 format
|
|
392
|
+
*/
|
|
393
|
+
function toBase64(v, escape = true) {
|
|
394
|
+
const encoded = base64.encode(toBuffer(v));
|
|
395
|
+
return escape ? base64.escape(encoded) : encoded;
|
|
502
396
|
}
|
|
503
397
|
/**
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
}
|
|
510
|
-
return Buffer.from(base64.unescape(v), 'base64');
|
|
398
|
+
* Decode base64(base64_url) string to buffer
|
|
399
|
+
*/
|
|
400
|
+
function fromBase64(v) {
|
|
401
|
+
if (typeof v !== "string") throw new Error("fromBase64 requires input to be a string");
|
|
402
|
+
return Buffer.from(base64.unescape(v), "base64");
|
|
511
403
|
}
|
|
512
404
|
/**
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
405
|
+
* Convert did to address: remove `did:abt:` prefix
|
|
406
|
+
*/
|
|
407
|
+
function toAddress(did) {
|
|
408
|
+
return did.replace(DID_PREFIX, "");
|
|
517
409
|
}
|
|
518
410
|
/**
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
411
|
+
* Convert address to did: prepend `did:abt:` prefix
|
|
412
|
+
*/
|
|
413
|
+
function toDid(address) {
|
|
414
|
+
return `${DID_PREFIX}${toAddress(address)}`;
|
|
523
415
|
}
|
|
524
|
-
|
|
525
|
-
|
|
416
|
+
function isSameDid(a, b) {
|
|
417
|
+
return toAddress(a).toLowerCase() === toAddress(b).toLowerCase();
|
|
526
418
|
}
|
|
527
|
-
|
|
528
|
-
|
|
419
|
+
function formatTxType(type) {
|
|
420
|
+
return upperFirst(camelCase(type));
|
|
529
421
|
}
|
|
422
|
+
|
|
423
|
+
//#endregion
|
|
424
|
+
export { BN, UUID, bytesToHex, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|