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