@leofcoin/chain 1.3.11 → 1.4.0
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/block-worker.js +1 -1
- package/demo/chain.browser.js +4 -0
- package/dist/browser/workers/block-worker.js +1977 -5692
- package/dist/browser/workers/machine-worker.js +1955 -5670
- package/dist/browser/workers/pool-worker.js +1636 -5531
- package/dist/browser/workers/transaction-worker.js +1626 -5521
- package/dist/chain.js +9865 -1192
- package/dist/client-80bc8156.js +491 -0
- package/dist/commonjs-7fe3c381.js +270 -0
- package/dist/generate-account-445db122.js +46 -0
- package/dist/index-57f93805.js +718 -0
- package/dist/{messages.browser.js → messages-bce1b91d-81af3b00.js} +26 -39
- package/dist/module/chain.js +9820 -1174
- package/dist/module/client-8031ec88.js +489 -0
- package/dist/module/commonjs-9005d5c0.js +268 -0
- package/dist/module/generate-account-489552b6.js +44 -0
- package/dist/module/index-ac2285c4.js +688 -0
- package/dist/module/messages-bce1b91d-eaf75d83.js +302 -0
- package/dist/module/node.js +6833 -2
- package/dist/module/workers/block-worker.js +3 -287
- package/dist/module/workers/machine-worker.js +3 -287
- package/dist/node.js +6845 -8
- package/dist/workers/machine-worker.js +1 -1
- package/package.json +11 -20
- package/src/chain.js +9 -12
- package/src/contract.js +2 -2
- package/src/machine.js +10 -7
- package/src/node.js +2 -2
- package/src/transaction.js +5 -5
- package/test/chain.js +5 -7
- package/test/index.js +1 -3
- package/tsconfig.js +15 -0
- package/workers/block-worker.js +40 -0
- package/workers/machine-worker.js +219 -0
- package/workers/pool-worker.js +28 -0
- package/workers/transaction-worker.js +20 -0
- package/workers/workers.js +9 -0
- package/dist/865.browser.js +0 -10
- package/dist/chain.browser.js +0 -66838
- package/dist/generate-account.browser.js +0 -50
- package/dist/multi-wallet.browser.js +0 -15
- package/dist/node.browser.js +0 -9858
- package/dist/pako.browser.js +0 -6900
- package/dist/peernet-swarm.browser.js +0 -839
- package/dist/storage.browser.js +0 -3724
- package/dist/wrtc.browser.js +0 -28
- package/rollup.config.js +0 -229
- package/src/standards/initializer.js +0 -10
- package/webpack.config.js +0 -109
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import '@ethersproject/bignumber';
|
|
2
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
3
|
+
import '@ethersproject/units';
|
|
4
|
+
import 'randombytes';
|
|
6
5
|
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
7
6
|
|
|
8
7
|
var proto = `
|
|
@@ -47,289 +46,6 @@ class BlockMessage extends FormatInterface {
|
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
const version$1 = "bignumber/5.6.2";
|
|
51
|
-
|
|
52
|
-
var BN = _BN.BN;
|
|
53
|
-
const logger = new Logger(version$1);
|
|
54
|
-
const _constructorGuard = {};
|
|
55
|
-
const MAX_SAFE = 0x1fffffffffffff;
|
|
56
|
-
// Only warn about passing 10 into radix once
|
|
57
|
-
let _warnedToStringRadix = false;
|
|
58
|
-
class BigNumber {
|
|
59
|
-
constructor(constructorGuard, hex) {
|
|
60
|
-
if (constructorGuard !== _constructorGuard) {
|
|
61
|
-
logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
62
|
-
operation: "new (BigNumber)"
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
this._hex = hex;
|
|
66
|
-
this._isBigNumber = true;
|
|
67
|
-
Object.freeze(this);
|
|
68
|
-
}
|
|
69
|
-
fromTwos(value) {
|
|
70
|
-
return toBigNumber(toBN(this).fromTwos(value));
|
|
71
|
-
}
|
|
72
|
-
toTwos(value) {
|
|
73
|
-
return toBigNumber(toBN(this).toTwos(value));
|
|
74
|
-
}
|
|
75
|
-
abs() {
|
|
76
|
-
if (this._hex[0] === "-") {
|
|
77
|
-
return BigNumber.from(this._hex.substring(1));
|
|
78
|
-
}
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
add(other) {
|
|
82
|
-
return toBigNumber(toBN(this).add(toBN(other)));
|
|
83
|
-
}
|
|
84
|
-
sub(other) {
|
|
85
|
-
return toBigNumber(toBN(this).sub(toBN(other)));
|
|
86
|
-
}
|
|
87
|
-
div(other) {
|
|
88
|
-
const o = BigNumber.from(other);
|
|
89
|
-
if (o.isZero()) {
|
|
90
|
-
throwFault("division-by-zero", "div");
|
|
91
|
-
}
|
|
92
|
-
return toBigNumber(toBN(this).div(toBN(other)));
|
|
93
|
-
}
|
|
94
|
-
mul(other) {
|
|
95
|
-
return toBigNumber(toBN(this).mul(toBN(other)));
|
|
96
|
-
}
|
|
97
|
-
mod(other) {
|
|
98
|
-
const value = toBN(other);
|
|
99
|
-
if (value.isNeg()) {
|
|
100
|
-
throwFault("division-by-zero", "mod");
|
|
101
|
-
}
|
|
102
|
-
return toBigNumber(toBN(this).umod(value));
|
|
103
|
-
}
|
|
104
|
-
pow(other) {
|
|
105
|
-
const value = toBN(other);
|
|
106
|
-
if (value.isNeg()) {
|
|
107
|
-
throwFault("negative-power", "pow");
|
|
108
|
-
}
|
|
109
|
-
return toBigNumber(toBN(this).pow(value));
|
|
110
|
-
}
|
|
111
|
-
and(other) {
|
|
112
|
-
const value = toBN(other);
|
|
113
|
-
if (this.isNegative() || value.isNeg()) {
|
|
114
|
-
throwFault("unbound-bitwise-result", "and");
|
|
115
|
-
}
|
|
116
|
-
return toBigNumber(toBN(this).and(value));
|
|
117
|
-
}
|
|
118
|
-
or(other) {
|
|
119
|
-
const value = toBN(other);
|
|
120
|
-
if (this.isNegative() || value.isNeg()) {
|
|
121
|
-
throwFault("unbound-bitwise-result", "or");
|
|
122
|
-
}
|
|
123
|
-
return toBigNumber(toBN(this).or(value));
|
|
124
|
-
}
|
|
125
|
-
xor(other) {
|
|
126
|
-
const value = toBN(other);
|
|
127
|
-
if (this.isNegative() || value.isNeg()) {
|
|
128
|
-
throwFault("unbound-bitwise-result", "xor");
|
|
129
|
-
}
|
|
130
|
-
return toBigNumber(toBN(this).xor(value));
|
|
131
|
-
}
|
|
132
|
-
mask(value) {
|
|
133
|
-
if (this.isNegative() || value < 0) {
|
|
134
|
-
throwFault("negative-width", "mask");
|
|
135
|
-
}
|
|
136
|
-
return toBigNumber(toBN(this).maskn(value));
|
|
137
|
-
}
|
|
138
|
-
shl(value) {
|
|
139
|
-
if (this.isNegative() || value < 0) {
|
|
140
|
-
throwFault("negative-width", "shl");
|
|
141
|
-
}
|
|
142
|
-
return toBigNumber(toBN(this).shln(value));
|
|
143
|
-
}
|
|
144
|
-
shr(value) {
|
|
145
|
-
if (this.isNegative() || value < 0) {
|
|
146
|
-
throwFault("negative-width", "shr");
|
|
147
|
-
}
|
|
148
|
-
return toBigNumber(toBN(this).shrn(value));
|
|
149
|
-
}
|
|
150
|
-
eq(other) {
|
|
151
|
-
return toBN(this).eq(toBN(other));
|
|
152
|
-
}
|
|
153
|
-
lt(other) {
|
|
154
|
-
return toBN(this).lt(toBN(other));
|
|
155
|
-
}
|
|
156
|
-
lte(other) {
|
|
157
|
-
return toBN(this).lte(toBN(other));
|
|
158
|
-
}
|
|
159
|
-
gt(other) {
|
|
160
|
-
return toBN(this).gt(toBN(other));
|
|
161
|
-
}
|
|
162
|
-
gte(other) {
|
|
163
|
-
return toBN(this).gte(toBN(other));
|
|
164
|
-
}
|
|
165
|
-
isNegative() {
|
|
166
|
-
return (this._hex[0] === "-");
|
|
167
|
-
}
|
|
168
|
-
isZero() {
|
|
169
|
-
return toBN(this).isZero();
|
|
170
|
-
}
|
|
171
|
-
toNumber() {
|
|
172
|
-
try {
|
|
173
|
-
return toBN(this).toNumber();
|
|
174
|
-
}
|
|
175
|
-
catch (error) {
|
|
176
|
-
throwFault("overflow", "toNumber", this.toString());
|
|
177
|
-
}
|
|
178
|
-
return null;
|
|
179
|
-
}
|
|
180
|
-
toBigInt() {
|
|
181
|
-
try {
|
|
182
|
-
return BigInt(this.toString());
|
|
183
|
-
}
|
|
184
|
-
catch (e) { }
|
|
185
|
-
return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
186
|
-
value: this.toString()
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
toString() {
|
|
190
|
-
// Lots of people expect this, which we do not support, so check (See: #889)
|
|
191
|
-
if (arguments.length > 0) {
|
|
192
|
-
if (arguments[0] === 10) {
|
|
193
|
-
if (!_warnedToStringRadix) {
|
|
194
|
-
_warnedToStringRadix = true;
|
|
195
|
-
logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
else if (arguments[0] === 16) {
|
|
199
|
-
logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {});
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
logger.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {});
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return toBN(this).toString(10);
|
|
206
|
-
}
|
|
207
|
-
toHexString() {
|
|
208
|
-
return this._hex;
|
|
209
|
-
}
|
|
210
|
-
toJSON(key) {
|
|
211
|
-
return { type: "BigNumber", hex: this.toHexString() };
|
|
212
|
-
}
|
|
213
|
-
static from(value) {
|
|
214
|
-
if (value instanceof BigNumber) {
|
|
215
|
-
return value;
|
|
216
|
-
}
|
|
217
|
-
if (typeof (value) === "string") {
|
|
218
|
-
if (value.match(/^-?0x[0-9a-f]+$/i)) {
|
|
219
|
-
return new BigNumber(_constructorGuard, toHex(value));
|
|
220
|
-
}
|
|
221
|
-
if (value.match(/^-?[0-9]+$/)) {
|
|
222
|
-
return new BigNumber(_constructorGuard, toHex(new BN(value)));
|
|
223
|
-
}
|
|
224
|
-
return logger.throwArgumentError("invalid BigNumber string", "value", value);
|
|
225
|
-
}
|
|
226
|
-
if (typeof (value) === "number") {
|
|
227
|
-
if (value % 1) {
|
|
228
|
-
throwFault("underflow", "BigNumber.from", value);
|
|
229
|
-
}
|
|
230
|
-
if (value >= MAX_SAFE || value <= -MAX_SAFE) {
|
|
231
|
-
throwFault("overflow", "BigNumber.from", value);
|
|
232
|
-
}
|
|
233
|
-
return BigNumber.from(String(value));
|
|
234
|
-
}
|
|
235
|
-
const anyValue = value;
|
|
236
|
-
if (typeof (anyValue) === "bigint") {
|
|
237
|
-
return BigNumber.from(anyValue.toString());
|
|
238
|
-
}
|
|
239
|
-
if (isBytes(anyValue)) {
|
|
240
|
-
return BigNumber.from(hexlify(anyValue));
|
|
241
|
-
}
|
|
242
|
-
if (anyValue) {
|
|
243
|
-
// Hexable interface (takes priority)
|
|
244
|
-
if (anyValue.toHexString) {
|
|
245
|
-
const hex = anyValue.toHexString();
|
|
246
|
-
if (typeof (hex) === "string") {
|
|
247
|
-
return BigNumber.from(hex);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
// For now, handle legacy JSON-ified values (goes away in v6)
|
|
252
|
-
let hex = anyValue._hex;
|
|
253
|
-
// New-form JSON
|
|
254
|
-
if (hex == null && anyValue.type === "BigNumber") {
|
|
255
|
-
hex = anyValue.hex;
|
|
256
|
-
}
|
|
257
|
-
if (typeof (hex) === "string") {
|
|
258
|
-
if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) {
|
|
259
|
-
return BigNumber.from(hex);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
return logger.throwArgumentError("invalid BigNumber value", "value", value);
|
|
265
|
-
}
|
|
266
|
-
static isBigNumber(value) {
|
|
267
|
-
return !!(value && value._isBigNumber);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
// Normalize the hex string
|
|
271
|
-
function toHex(value) {
|
|
272
|
-
// For BN, call on the hex string
|
|
273
|
-
if (typeof (value) !== "string") {
|
|
274
|
-
return toHex(value.toString(16));
|
|
275
|
-
}
|
|
276
|
-
// If negative, prepend the negative sign to the normalized positive value
|
|
277
|
-
if (value[0] === "-") {
|
|
278
|
-
// Strip off the negative sign
|
|
279
|
-
value = value.substring(1);
|
|
280
|
-
// Cannot have multiple negative signs (e.g. "--0x04")
|
|
281
|
-
if (value[0] === "-") {
|
|
282
|
-
logger.throwArgumentError("invalid hex", "value", value);
|
|
283
|
-
}
|
|
284
|
-
// Call toHex on the positive component
|
|
285
|
-
value = toHex(value);
|
|
286
|
-
// Do not allow "-0x00"
|
|
287
|
-
if (value === "0x00") {
|
|
288
|
-
return value;
|
|
289
|
-
}
|
|
290
|
-
// Negate the value
|
|
291
|
-
return "-" + value;
|
|
292
|
-
}
|
|
293
|
-
// Add a "0x" prefix if missing
|
|
294
|
-
if (value.substring(0, 2) !== "0x") {
|
|
295
|
-
value = "0x" + value;
|
|
296
|
-
}
|
|
297
|
-
// Normalize zero
|
|
298
|
-
if (value === "0x") {
|
|
299
|
-
return "0x00";
|
|
300
|
-
}
|
|
301
|
-
// Make the string even length
|
|
302
|
-
if (value.length % 2) {
|
|
303
|
-
value = "0x0" + value.substring(2);
|
|
304
|
-
}
|
|
305
|
-
// Trim to smallest even-length string
|
|
306
|
-
while (value.length > 4 && value.substring(0, 4) === "0x00") {
|
|
307
|
-
value = "0x" + value.substring(4);
|
|
308
|
-
}
|
|
309
|
-
return value;
|
|
310
|
-
}
|
|
311
|
-
function toBigNumber(value) {
|
|
312
|
-
return BigNumber.from(toHex(value));
|
|
313
|
-
}
|
|
314
|
-
function toBN(value) {
|
|
315
|
-
const hex = BigNumber.from(value).toHexString();
|
|
316
|
-
if (hex[0] === "-") {
|
|
317
|
-
return (new BN("-" + hex.substring(3), 16));
|
|
318
|
-
}
|
|
319
|
-
return new BN(hex.substring(2), 16);
|
|
320
|
-
}
|
|
321
|
-
function throwFault(fault, operation, value) {
|
|
322
|
-
const params = { fault: fault, operation: operation };
|
|
323
|
-
if (value != null) {
|
|
324
|
-
params.value = value;
|
|
325
|
-
}
|
|
326
|
-
return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
const version = "units/5.6.1";
|
|
330
|
-
|
|
331
|
-
new Logger(version);
|
|
332
|
-
|
|
333
49
|
const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
334
50
|
|
|
335
51
|
const formatBytes = (bytes, decimals = 2) => {
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { FormatInterface } from '@leofcoin/codec-format-interface';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import '@ethersproject/bignumber';
|
|
2
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
3
|
+
import '@ethersproject/units';
|
|
4
|
+
import 'randombytes';
|
|
6
5
|
import EasyWorker from '@vandeurenglenn/easy-worker';
|
|
7
6
|
|
|
8
7
|
var proto$1 = `
|
|
@@ -69,289 +68,6 @@ class BlockMessage extends FormatInterface {
|
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
const version$1 = "bignumber/5.6.2";
|
|
73
|
-
|
|
74
|
-
var BN = _BN.BN;
|
|
75
|
-
const logger = new Logger(version$1);
|
|
76
|
-
const _constructorGuard = {};
|
|
77
|
-
const MAX_SAFE = 0x1fffffffffffff;
|
|
78
|
-
// Only warn about passing 10 into radix once
|
|
79
|
-
let _warnedToStringRadix = false;
|
|
80
|
-
class BigNumber {
|
|
81
|
-
constructor(constructorGuard, hex) {
|
|
82
|
-
if (constructorGuard !== _constructorGuard) {
|
|
83
|
-
logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
84
|
-
operation: "new (BigNumber)"
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
this._hex = hex;
|
|
88
|
-
this._isBigNumber = true;
|
|
89
|
-
Object.freeze(this);
|
|
90
|
-
}
|
|
91
|
-
fromTwos(value) {
|
|
92
|
-
return toBigNumber(toBN(this).fromTwos(value));
|
|
93
|
-
}
|
|
94
|
-
toTwos(value) {
|
|
95
|
-
return toBigNumber(toBN(this).toTwos(value));
|
|
96
|
-
}
|
|
97
|
-
abs() {
|
|
98
|
-
if (this._hex[0] === "-") {
|
|
99
|
-
return BigNumber.from(this._hex.substring(1));
|
|
100
|
-
}
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
add(other) {
|
|
104
|
-
return toBigNumber(toBN(this).add(toBN(other)));
|
|
105
|
-
}
|
|
106
|
-
sub(other) {
|
|
107
|
-
return toBigNumber(toBN(this).sub(toBN(other)));
|
|
108
|
-
}
|
|
109
|
-
div(other) {
|
|
110
|
-
const o = BigNumber.from(other);
|
|
111
|
-
if (o.isZero()) {
|
|
112
|
-
throwFault("division-by-zero", "div");
|
|
113
|
-
}
|
|
114
|
-
return toBigNumber(toBN(this).div(toBN(other)));
|
|
115
|
-
}
|
|
116
|
-
mul(other) {
|
|
117
|
-
return toBigNumber(toBN(this).mul(toBN(other)));
|
|
118
|
-
}
|
|
119
|
-
mod(other) {
|
|
120
|
-
const value = toBN(other);
|
|
121
|
-
if (value.isNeg()) {
|
|
122
|
-
throwFault("division-by-zero", "mod");
|
|
123
|
-
}
|
|
124
|
-
return toBigNumber(toBN(this).umod(value));
|
|
125
|
-
}
|
|
126
|
-
pow(other) {
|
|
127
|
-
const value = toBN(other);
|
|
128
|
-
if (value.isNeg()) {
|
|
129
|
-
throwFault("negative-power", "pow");
|
|
130
|
-
}
|
|
131
|
-
return toBigNumber(toBN(this).pow(value));
|
|
132
|
-
}
|
|
133
|
-
and(other) {
|
|
134
|
-
const value = toBN(other);
|
|
135
|
-
if (this.isNegative() || value.isNeg()) {
|
|
136
|
-
throwFault("unbound-bitwise-result", "and");
|
|
137
|
-
}
|
|
138
|
-
return toBigNumber(toBN(this).and(value));
|
|
139
|
-
}
|
|
140
|
-
or(other) {
|
|
141
|
-
const value = toBN(other);
|
|
142
|
-
if (this.isNegative() || value.isNeg()) {
|
|
143
|
-
throwFault("unbound-bitwise-result", "or");
|
|
144
|
-
}
|
|
145
|
-
return toBigNumber(toBN(this).or(value));
|
|
146
|
-
}
|
|
147
|
-
xor(other) {
|
|
148
|
-
const value = toBN(other);
|
|
149
|
-
if (this.isNegative() || value.isNeg()) {
|
|
150
|
-
throwFault("unbound-bitwise-result", "xor");
|
|
151
|
-
}
|
|
152
|
-
return toBigNumber(toBN(this).xor(value));
|
|
153
|
-
}
|
|
154
|
-
mask(value) {
|
|
155
|
-
if (this.isNegative() || value < 0) {
|
|
156
|
-
throwFault("negative-width", "mask");
|
|
157
|
-
}
|
|
158
|
-
return toBigNumber(toBN(this).maskn(value));
|
|
159
|
-
}
|
|
160
|
-
shl(value) {
|
|
161
|
-
if (this.isNegative() || value < 0) {
|
|
162
|
-
throwFault("negative-width", "shl");
|
|
163
|
-
}
|
|
164
|
-
return toBigNumber(toBN(this).shln(value));
|
|
165
|
-
}
|
|
166
|
-
shr(value) {
|
|
167
|
-
if (this.isNegative() || value < 0) {
|
|
168
|
-
throwFault("negative-width", "shr");
|
|
169
|
-
}
|
|
170
|
-
return toBigNumber(toBN(this).shrn(value));
|
|
171
|
-
}
|
|
172
|
-
eq(other) {
|
|
173
|
-
return toBN(this).eq(toBN(other));
|
|
174
|
-
}
|
|
175
|
-
lt(other) {
|
|
176
|
-
return toBN(this).lt(toBN(other));
|
|
177
|
-
}
|
|
178
|
-
lte(other) {
|
|
179
|
-
return toBN(this).lte(toBN(other));
|
|
180
|
-
}
|
|
181
|
-
gt(other) {
|
|
182
|
-
return toBN(this).gt(toBN(other));
|
|
183
|
-
}
|
|
184
|
-
gte(other) {
|
|
185
|
-
return toBN(this).gte(toBN(other));
|
|
186
|
-
}
|
|
187
|
-
isNegative() {
|
|
188
|
-
return (this._hex[0] === "-");
|
|
189
|
-
}
|
|
190
|
-
isZero() {
|
|
191
|
-
return toBN(this).isZero();
|
|
192
|
-
}
|
|
193
|
-
toNumber() {
|
|
194
|
-
try {
|
|
195
|
-
return toBN(this).toNumber();
|
|
196
|
-
}
|
|
197
|
-
catch (error) {
|
|
198
|
-
throwFault("overflow", "toNumber", this.toString());
|
|
199
|
-
}
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
toBigInt() {
|
|
203
|
-
try {
|
|
204
|
-
return BigInt(this.toString());
|
|
205
|
-
}
|
|
206
|
-
catch (e) { }
|
|
207
|
-
return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
208
|
-
value: this.toString()
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
toString() {
|
|
212
|
-
// Lots of people expect this, which we do not support, so check (See: #889)
|
|
213
|
-
if (arguments.length > 0) {
|
|
214
|
-
if (arguments[0] === 10) {
|
|
215
|
-
if (!_warnedToStringRadix) {
|
|
216
|
-
_warnedToStringRadix = true;
|
|
217
|
-
logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
else if (arguments[0] === 16) {
|
|
221
|
-
logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {});
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
logger.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {});
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return toBN(this).toString(10);
|
|
228
|
-
}
|
|
229
|
-
toHexString() {
|
|
230
|
-
return this._hex;
|
|
231
|
-
}
|
|
232
|
-
toJSON(key) {
|
|
233
|
-
return { type: "BigNumber", hex: this.toHexString() };
|
|
234
|
-
}
|
|
235
|
-
static from(value) {
|
|
236
|
-
if (value instanceof BigNumber) {
|
|
237
|
-
return value;
|
|
238
|
-
}
|
|
239
|
-
if (typeof (value) === "string") {
|
|
240
|
-
if (value.match(/^-?0x[0-9a-f]+$/i)) {
|
|
241
|
-
return new BigNumber(_constructorGuard, toHex(value));
|
|
242
|
-
}
|
|
243
|
-
if (value.match(/^-?[0-9]+$/)) {
|
|
244
|
-
return new BigNumber(_constructorGuard, toHex(new BN(value)));
|
|
245
|
-
}
|
|
246
|
-
return logger.throwArgumentError("invalid BigNumber string", "value", value);
|
|
247
|
-
}
|
|
248
|
-
if (typeof (value) === "number") {
|
|
249
|
-
if (value % 1) {
|
|
250
|
-
throwFault("underflow", "BigNumber.from", value);
|
|
251
|
-
}
|
|
252
|
-
if (value >= MAX_SAFE || value <= -MAX_SAFE) {
|
|
253
|
-
throwFault("overflow", "BigNumber.from", value);
|
|
254
|
-
}
|
|
255
|
-
return BigNumber.from(String(value));
|
|
256
|
-
}
|
|
257
|
-
const anyValue = value;
|
|
258
|
-
if (typeof (anyValue) === "bigint") {
|
|
259
|
-
return BigNumber.from(anyValue.toString());
|
|
260
|
-
}
|
|
261
|
-
if (isBytes(anyValue)) {
|
|
262
|
-
return BigNumber.from(hexlify(anyValue));
|
|
263
|
-
}
|
|
264
|
-
if (anyValue) {
|
|
265
|
-
// Hexable interface (takes priority)
|
|
266
|
-
if (anyValue.toHexString) {
|
|
267
|
-
const hex = anyValue.toHexString();
|
|
268
|
-
if (typeof (hex) === "string") {
|
|
269
|
-
return BigNumber.from(hex);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
else {
|
|
273
|
-
// For now, handle legacy JSON-ified values (goes away in v6)
|
|
274
|
-
let hex = anyValue._hex;
|
|
275
|
-
// New-form JSON
|
|
276
|
-
if (hex == null && anyValue.type === "BigNumber") {
|
|
277
|
-
hex = anyValue.hex;
|
|
278
|
-
}
|
|
279
|
-
if (typeof (hex) === "string") {
|
|
280
|
-
if (isHexString(hex) || (hex[0] === "-" && isHexString(hex.substring(1)))) {
|
|
281
|
-
return BigNumber.from(hex);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
return logger.throwArgumentError("invalid BigNumber value", "value", value);
|
|
287
|
-
}
|
|
288
|
-
static isBigNumber(value) {
|
|
289
|
-
return !!(value && value._isBigNumber);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
// Normalize the hex string
|
|
293
|
-
function toHex(value) {
|
|
294
|
-
// For BN, call on the hex string
|
|
295
|
-
if (typeof (value) !== "string") {
|
|
296
|
-
return toHex(value.toString(16));
|
|
297
|
-
}
|
|
298
|
-
// If negative, prepend the negative sign to the normalized positive value
|
|
299
|
-
if (value[0] === "-") {
|
|
300
|
-
// Strip off the negative sign
|
|
301
|
-
value = value.substring(1);
|
|
302
|
-
// Cannot have multiple negative signs (e.g. "--0x04")
|
|
303
|
-
if (value[0] === "-") {
|
|
304
|
-
logger.throwArgumentError("invalid hex", "value", value);
|
|
305
|
-
}
|
|
306
|
-
// Call toHex on the positive component
|
|
307
|
-
value = toHex(value);
|
|
308
|
-
// Do not allow "-0x00"
|
|
309
|
-
if (value === "0x00") {
|
|
310
|
-
return value;
|
|
311
|
-
}
|
|
312
|
-
// Negate the value
|
|
313
|
-
return "-" + value;
|
|
314
|
-
}
|
|
315
|
-
// Add a "0x" prefix if missing
|
|
316
|
-
if (value.substring(0, 2) !== "0x") {
|
|
317
|
-
value = "0x" + value;
|
|
318
|
-
}
|
|
319
|
-
// Normalize zero
|
|
320
|
-
if (value === "0x") {
|
|
321
|
-
return "0x00";
|
|
322
|
-
}
|
|
323
|
-
// Make the string even length
|
|
324
|
-
if (value.length % 2) {
|
|
325
|
-
value = "0x0" + value.substring(2);
|
|
326
|
-
}
|
|
327
|
-
// Trim to smallest even-length string
|
|
328
|
-
while (value.length > 4 && value.substring(0, 4) === "0x00") {
|
|
329
|
-
value = "0x" + value.substring(4);
|
|
330
|
-
}
|
|
331
|
-
return value;
|
|
332
|
-
}
|
|
333
|
-
function toBigNumber(value) {
|
|
334
|
-
return BigNumber.from(toHex(value));
|
|
335
|
-
}
|
|
336
|
-
function toBN(value) {
|
|
337
|
-
const hex = BigNumber.from(value).toHexString();
|
|
338
|
-
if (hex[0] === "-") {
|
|
339
|
-
return (new BN("-" + hex.substring(3), 16));
|
|
340
|
-
}
|
|
341
|
-
return new BN(hex.substring(2), 16);
|
|
342
|
-
}
|
|
343
|
-
function throwFault(fault, operation, value) {
|
|
344
|
-
const params = { fault: fault, operation: operation };
|
|
345
|
-
if (value != null) {
|
|
346
|
-
params.value = value;
|
|
347
|
-
}
|
|
348
|
-
return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
const version = "units/5.6.1";
|
|
352
|
-
|
|
353
|
-
new Logger(version);
|
|
354
|
-
|
|
355
71
|
const byteFormats = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
356
72
|
|
|
357
73
|
const formatBytes = (bytes, decimals = 2) => {
|