quantum-coin-js-sdk 2.0.1 → 2.1.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/README.md +13 -0
- package/index.d.ts +9 -0
- package/index.js +37 -4
- package/package.json +12 -4
- package/wasmBase64.js +3 -2
- package/.idea/.name +0 -1
- package/.idea/modules.xml +0 -8
- package/.idea/quantum-coin-js-sdk.iml +0 -12
- package/.idea/vcs.xml +0 -6
- package/.vs/ProjectSettings.json +0 -3
- package/.vs/VSWorkspaceState.json +0 -8
- package/.vs/quantum-coin-js-sdk/FileContentIndex/a9d6de82-cfc3-4c22-ac38-06d706ff3673.vsidx +0 -0
- package/.vs/quantum-coin-js-sdk/FileContentIndex/fdc816df-1898-4153-99f2-c69ef4c2283b.vsidx +0 -0
- package/.vs/quantum-coin-js-sdk/config/applicationhost.config +0 -1011
- package/.vs/quantum-coin-js-sdk/v17/.wsuo +0 -0
- package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.backup.json +0 -39
- package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.json +0 -57
- package/.vs/slnx.sqlite +0 -0
- package/example/README.md +0 -14
- package/example/conversion-example.js +0 -19
- package/example/example-create-contract.js +0 -396
- package/example/example-encode-decode-rlp.js +0 -225
- package/example/example-event-pack-unpack.js +0 -391
- package/example/example-misc.js +0 -96
- package/example/example-token-pack-unpack.js +0 -960
- package/example/example-wallet-version4.js +0 -34
- package/example/example-wallet.js +0 -39
- package/example/example.js +0 -115
- package/example/node_modules/.bin/sdkgen +0 -16
- package/example/node_modules/.bin/sdkgen.cmd +0 -17
- package/example/node_modules/.bin/sdkgen.ps1 +0 -28
- package/example/node_modules/.package-lock.json +0 -51
- package/example/node_modules/quantumcoin/.github/workflows/publish-npmjs.yaml +0 -22
- package/example/node_modules/quantumcoin/LICENSE +0 -21
- package/example/node_modules/quantumcoin/README-SDK.md +0 -828
- package/example/node_modules/quantumcoin/README.md +0 -197
- package/example/node_modules/quantumcoin/config.d.ts +0 -50
- package/example/node_modules/quantumcoin/config.js +0 -123
- package/example/node_modules/quantumcoin/examples/AllSolidityTypes.sol +0 -184
- package/example/node_modules/quantumcoin/examples/SimpleIERC20.sol +0 -74
- package/example/node_modules/quantumcoin/examples/events.js +0 -41
- package/example/node_modules/quantumcoin/examples/events.ts +0 -35
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.js +0 -100
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.ts +0 -77
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.js +0 -100
- package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.ts +0 -77
- package/example/node_modules/quantumcoin/examples/example.js +0 -67
- package/example/node_modules/quantumcoin/examples/example.ts +0 -56
- package/example/node_modules/quantumcoin/examples/offline-signing.js +0 -77
- package/example/node_modules/quantumcoin/examples/offline-signing.ts +0 -65
- package/example/node_modules/quantumcoin/examples/package-lock.json +0 -596
- package/example/node_modules/quantumcoin/examples/package.json +0 -32
- package/example/node_modules/quantumcoin/examples/read-operations.js +0 -32
- package/example/node_modules/quantumcoin/examples/read-operations.ts +0 -31
- package/example/node_modules/quantumcoin/examples/sdk-generator-erc20.inline.json +0 -251
- package/example/node_modules/quantumcoin/examples/solidity-types.ts +0 -43
- package/example/node_modules/quantumcoin/examples/wallet-offline.js +0 -36
- package/example/node_modules/quantumcoin/examples/wallet-offline.ts +0 -26
- package/example/node_modules/quantumcoin/generate-sdk.js +0 -1846
- package/example/node_modules/quantumcoin/index.js +0 -12
- package/example/node_modules/quantumcoin/package.json +0 -95
- package/example/node_modules/quantumcoin/scripts/copy-declarations.js +0 -31
- package/example/node_modules/quantumcoin/scripts/run-all-one-by-one.js +0 -151
- package/example/node_modules/quantumcoin/src/abi/fragments.d.ts +0 -42
- package/example/node_modules/quantumcoin/src/abi/fragments.js +0 -63
- package/example/node_modules/quantumcoin/src/abi/index.d.ts +0 -13
- package/example/node_modules/quantumcoin/src/abi/index.js +0 -9
- package/example/node_modules/quantumcoin/src/abi/interface.d.ts +0 -128
- package/example/node_modules/quantumcoin/src/abi/interface.js +0 -605
- package/example/node_modules/quantumcoin/src/abi/js-abi-coder.d.ts +0 -8
- package/example/node_modules/quantumcoin/src/abi/js-abi-coder.js +0 -538
- package/example/node_modules/quantumcoin/src/constants.d.ts +0 -61
- package/example/node_modules/quantumcoin/src/constants.js +0 -94
- package/example/node_modules/quantumcoin/src/contract/contract-factory.d.ts +0 -28
- package/example/node_modules/quantumcoin/src/contract/contract-factory.js +0 -111
- package/example/node_modules/quantumcoin/src/contract/contract.d.ts +0 -113
- package/example/node_modules/quantumcoin/src/contract/contract.js +0 -408
- package/example/node_modules/quantumcoin/src/contract/index.d.ts +0 -9
- package/example/node_modules/quantumcoin/src/contract/index.js +0 -9
- package/example/node_modules/quantumcoin/src/errors/index.d.ts +0 -92
- package/example/node_modules/quantumcoin/src/errors/index.js +0 -200
- package/example/node_modules/quantumcoin/src/generator/index.d.ts +0 -81
- package/example/node_modules/quantumcoin/src/generator/index.js +0 -1571
- package/example/node_modules/quantumcoin/src/index.d.ts +0 -122
- package/example/node_modules/quantumcoin/src/index.js +0 -41
- package/example/node_modules/quantumcoin/src/internal/hex.d.ts +0 -68
- package/example/node_modules/quantumcoin/src/internal/hex.js +0 -180
- package/example/node_modules/quantumcoin/src/providers/extra-providers.d.ts +0 -139
- package/example/node_modules/quantumcoin/src/providers/extra-providers.js +0 -614
- package/example/node_modules/quantumcoin/src/providers/index.d.ts +0 -17
- package/example/node_modules/quantumcoin/src/providers/index.js +0 -10
- package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.d.ts +0 -12
- package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.js +0 -89
- package/example/node_modules/quantumcoin/src/providers/provider.d.ts +0 -206
- package/example/node_modules/quantumcoin/src/providers/provider.js +0 -524
- package/example/node_modules/quantumcoin/src/types/index.d.ts +0 -214
- package/example/node_modules/quantumcoin/src/types/index.js +0 -9
- package/example/node_modules/quantumcoin/src/utils/address.d.ts +0 -58
- package/example/node_modules/quantumcoin/src/utils/address.js +0 -144
- package/example/node_modules/quantumcoin/src/utils/encoding.d.ts +0 -120
- package/example/node_modules/quantumcoin/src/utils/encoding.js +0 -306
- package/example/node_modules/quantumcoin/src/utils/fixednumber.d.ts +0 -57
- package/example/node_modules/quantumcoin/src/utils/fixednumber.js +0 -366
- package/example/node_modules/quantumcoin/src/utils/hashing.d.ts +0 -76
- package/example/node_modules/quantumcoin/src/utils/hashing.js +0 -298
- package/example/node_modules/quantumcoin/src/utils/index.d.ts +0 -63
- package/example/node_modules/quantumcoin/src/utils/index.js +0 -14
- package/example/node_modules/quantumcoin/src/utils/result.d.ts +0 -57
- package/example/node_modules/quantumcoin/src/utils/result.js +0 -128
- package/example/node_modules/quantumcoin/src/utils/rlp.d.ts +0 -12
- package/example/node_modules/quantumcoin/src/utils/rlp.js +0 -215
- package/example/node_modules/quantumcoin/src/utils/units.d.ts +0 -29
- package/example/node_modules/quantumcoin/src/utils/units.js +0 -107
- package/example/node_modules/quantumcoin/src/wallet/index.d.ts +0 -10
- package/example/node_modules/quantumcoin/src/wallet/index.js +0 -8
- package/example/node_modules/quantumcoin/src/wallet/wallet.d.ts +0 -192
- package/example/node_modules/quantumcoin/src/wallet/wallet.js +0 -713
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.js +0 -207
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.ts +0 -191
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.fixtures.js +0 -231
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +0 -387
- package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +0 -350
- package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.js +0 -165
- package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.ts +0 -160
- package/example/node_modules/quantumcoin/test/e2e/helpers.js +0 -59
- package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.js +0 -141
- package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.ts +0 -128
- package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.js +0 -168
- package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +0 -141
- package/example/node_modules/quantumcoin/test/e2e/transactional.test.js +0 -245
- package/example/node_modules/quantumcoin/test/e2e/transactional.test.ts +0 -208
- package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.js +0 -407
- package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.ts +0 -337
- package/example/node_modules/quantumcoin/test/fixtures/ConstructorParam.sol +0 -23
- package/example/node_modules/quantumcoin/test/fixtures/MultiContracts.sol +0 -37
- package/example/node_modules/quantumcoin/test/fixtures/SimpleStorage.sol +0 -18
- package/example/node_modules/quantumcoin/test/fixtures/StakingContract.abi.json +0 -1
- package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.js +0 -49
- package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.ts +0 -44
- package/example/node_modules/quantumcoin/test/integration/provider.test.js +0 -88
- package/example/node_modules/quantumcoin/test/integration/provider.test.ts +0 -85
- package/example/node_modules/quantumcoin/test/integration/ws-provider.test.js +0 -41
- package/example/node_modules/quantumcoin/test/integration/ws-provider.test.ts +0 -38
- package/example/node_modules/quantumcoin/test/security/abi-decoder-bounds.test.js +0 -122
- package/example/node_modules/quantumcoin/test/security/contract-overrides.test.js +0 -112
- package/example/node_modules/quantumcoin/test/security/generator-injection.test.js +0 -195
- package/example/node_modules/quantumcoin/test/security/malformed-input.test.js +0 -330
- package/example/node_modules/quantumcoin/test/security/malformed-input.test.ts +0 -35
- package/example/node_modules/quantumcoin/test/security/rpc-numeric-bounds.test.js +0 -81
- package/example/node_modules/quantumcoin/test/security/rpc-trust.test.js +0 -202
- package/example/node_modules/quantumcoin/test/unit/_encrypted-output.txt +0 -6
- package/example/node_modules/quantumcoin/test/unit/_log-encrypted-jsons.js +0 -45
- package/example/node_modules/quantumcoin/test/unit/_write-keystore-fixture.js +0 -16
- package/example/node_modules/quantumcoin/test/unit/abi-interface.test.js +0 -110
- package/example/node_modules/quantumcoin/test/unit/abi-interface.test.ts +0 -109
- package/example/node_modules/quantumcoin/test/unit/address-wallet.test.js +0 -923
- package/example/node_modules/quantumcoin/test/unit/address-wallet.test.ts +0 -877
- package/example/node_modules/quantumcoin/test/unit/browser-provider.test.js +0 -85
- package/example/node_modules/quantumcoin/test/unit/browser-provider.test.ts +0 -79
- package/example/node_modules/quantumcoin/test/unit/contract.test.js +0 -85
- package/example/node_modules/quantumcoin/test/unit/contract.test.ts +0 -83
- package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.js +0 -127
- package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.ts +0 -91
- package/example/node_modules/quantumcoin/test/unit/errors.test.js +0 -77
- package/example/node_modules/quantumcoin/test/unit/errors.test.ts +0 -76
- package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.js +0 -55
- package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.ts +0 -54
- package/example/node_modules/quantumcoin/test/unit/fixednumber.test.js +0 -656
- package/example/node_modules/quantumcoin/test/unit/fixednumber.test.ts +0 -660
- package/example/node_modules/quantumcoin/test/unit/fixtures/encrypted-keystores-48-32-36.js +0 -9
- package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.js +0 -42
- package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.ts +0 -41
- package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.js +0 -113
- package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.ts +0 -110
- package/example/node_modules/quantumcoin/test/unit/generator.test.js +0 -149
- package/example/node_modules/quantumcoin/test/unit/generator.test.ts +0 -148
- package/example/node_modules/quantumcoin/test/unit/hashing.test.js +0 -64
- package/example/node_modules/quantumcoin/test/unit/hashing.test.ts +0 -63
- package/example/node_modules/quantumcoin/test/unit/init.test.js +0 -39
- package/example/node_modules/quantumcoin/test/unit/init.test.ts +0 -38
- package/example/node_modules/quantumcoin/test/unit/interface.test.js +0 -56
- package/example/node_modules/quantumcoin/test/unit/interface.test.ts +0 -54
- package/example/node_modules/quantumcoin/test/unit/internal-hex.test.js +0 -81
- package/example/node_modules/quantumcoin/test/unit/internal-hex.test.ts +0 -80
- package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.js +0 -98
- package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.ts +0 -64
- package/example/node_modules/quantumcoin/test/unit/providers.test.js +0 -252
- package/example/node_modules/quantumcoin/test/unit/providers.test.ts +0 -251
- package/example/node_modules/quantumcoin/test/unit/result.test.js +0 -80
- package/example/node_modules/quantumcoin/test/unit/result.test.ts +0 -79
- package/example/node_modules/quantumcoin/test/unit/solidity-types.test.js +0 -49
- package/example/node_modules/quantumcoin/test/unit/solidity-types.test.ts +0 -39
- package/example/node_modules/quantumcoin/test/unit/utils.test.js +0 -57
- package/example/node_modules/quantumcoin/test/unit/utils.test.ts +0 -56
- package/example/node_modules/quantumcoin/test/verbose-logger.js +0 -74
- package/example/node_modules/quantumcoin/tsconfig.build.json +0 -14
- package/example/node_modules/seed-words/.github/workflows/publish-npmjs.yaml +0 -22
- package/example/node_modules/seed-words/BUILD.md +0 -7
- package/example/node_modules/seed-words/LICENSE +0 -121
- package/example/node_modules/seed-words/README.md +0 -67
- package/example/node_modules/seed-words/dist/seedwords.d.ts +0 -39
- package/example/node_modules/seed-words/package.json +0 -27
- package/example/node_modules/seed-words/seedwords.js +0 -315
- package/example/node_modules/seed-words/seedwords.txt +0 -65536
- package/example/node_modules/seed-words/tsconfig.json +0 -21
- package/example/package-lock.json +0 -60
- package/example/package.json +0 -15
- package/scripts/build-browser-tests.js +0 -29
- package/scripts/build-wasm.js +0 -85
- package/tests/browser/bundle.js +0 -97643
- package/tests/browser/entry.js +0 -57
- package/tests/browser/harness.html +0 -20
- package/tests/browser/run-browser.js +0 -50
- package/tests/encrypted-32.json +0 -1
- package/tests/encrypted-36.json +0 -1
- package/tests/encrypted-48.json +0 -1
- package/tests/generate-verify-vectors.js +0 -88
- package/tests/get-gas-price.test.js +0 -59
- package/tests/hashing.test.js +0 -34
- package/tests/non-transactional.preinit.test.js +0 -33
- package/tests/non-transactional.test.js +0 -1318
- package/tests/sign-raw-keytype5-context-null.test.js +0 -105
- package/tests/sign-raw-transaction.test.js +0 -194
- package/tests/sign-verify.test.js +0 -308
- package/tests/specs/assert.js +0 -74
- package/tests/specs/crypto.spec.js +0 -104
- package/tests/specs/hex.js +0 -31
- package/tests/specs/preinit.spec.js +0 -19
- package/tests/specs/sign.spec.js +0 -90
- package/tests/specs/wallet.spec.js +0 -98
- package/tests/transactional.rpc.test.js +0 -102
- package/tests/verify-vectors.json +0 -95035
|
@@ -1,366 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Fixed-point arithmetic (ethers.js v5/v6 compatible).
|
|
3
|
-
*
|
|
4
|
-
* FixedNumber stores values as a bigint multiplied by 10**decimals,
|
|
5
|
-
* inside a fixed bit-width field. All instances are immutable.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { makeError, assertArgument, assert } = require("../errors");
|
|
9
|
-
const { arrayify, hexlify, isBytesLike, zeroPad } = require("./encoding");
|
|
10
|
-
|
|
11
|
-
const BN_N1 = -1n;
|
|
12
|
-
const BN_0 = 0n;
|
|
13
|
-
const BN_1 = 1n;
|
|
14
|
-
const BN_5 = 5n;
|
|
15
|
-
|
|
16
|
-
const _guard = {};
|
|
17
|
-
|
|
18
|
-
let _zeros = "0000";
|
|
19
|
-
while (_zeros.length < 80) { _zeros += _zeros; }
|
|
20
|
-
|
|
21
|
-
function _getTens(decimals) {
|
|
22
|
-
let z = _zeros;
|
|
23
|
-
while (z.length < decimals) { z += z; }
|
|
24
|
-
return BigInt("1" + z.substring(0, decimals));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function _fromTwos(value, width) {
|
|
28
|
-
const limit = BN_1 << BigInt(width);
|
|
29
|
-
if (value & (limit >> BN_1)) {
|
|
30
|
-
return value - limit;
|
|
31
|
-
}
|
|
32
|
-
return value;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function _mask(value, width) {
|
|
36
|
-
return value & ((BN_1 << BigInt(width)) - BN_1);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function _checkValue(val, format, safeOp) {
|
|
40
|
-
const width = BigInt(format.width);
|
|
41
|
-
if (format.signed) {
|
|
42
|
-
const limit = BN_1 << (width - BN_1);
|
|
43
|
-
assert(safeOp == null || (val >= -limit && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
44
|
-
operation: safeOp, fault: "overflow", value: val,
|
|
45
|
-
});
|
|
46
|
-
if (val > BN_0) {
|
|
47
|
-
val = _fromTwos(_mask(val, width), format.width);
|
|
48
|
-
} else if (val < BN_0) {
|
|
49
|
-
val = -_fromTwos(_mask(-val, width), format.width);
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
const limit = BN_1 << width;
|
|
53
|
-
assert(safeOp == null || (val >= BN_0 && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
54
|
-
operation: safeOp, fault: "overflow", value: val,
|
|
55
|
-
});
|
|
56
|
-
val = ((val % limit) + limit) % limit & (limit - BN_1);
|
|
57
|
-
}
|
|
58
|
-
return val;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @param {import("./units").BigNumberish} value
|
|
63
|
-
* @returns {bigint}
|
|
64
|
-
*/
|
|
65
|
-
function _getBigInt(value) {
|
|
66
|
-
if (typeof value === "bigint") return value;
|
|
67
|
-
if (typeof value === "number") {
|
|
68
|
-
assertArgument(Number.isInteger(value), "invalid numeric value", "value", value);
|
|
69
|
-
return BigInt(value);
|
|
70
|
-
}
|
|
71
|
-
if (typeof value === "string") {
|
|
72
|
-
const v = value.trim();
|
|
73
|
-
assertArgument(v.length > 0 && /^-?(?:0x[0-9a-fA-F]+|[0-9]+)$/.test(v), "invalid BigNumberish string", "value", value);
|
|
74
|
-
return BigInt(v);
|
|
75
|
-
}
|
|
76
|
-
assertArgument(false, "invalid BigNumberish", "value", value);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function _getFormat(value) {
|
|
80
|
-
if (typeof value === "number") { value = `fixed128x${value}`; }
|
|
81
|
-
|
|
82
|
-
let signed = true;
|
|
83
|
-
let width = 128;
|
|
84
|
-
let decimals = 18;
|
|
85
|
-
|
|
86
|
-
if (typeof value === "string") {
|
|
87
|
-
if (value === "fixed") {
|
|
88
|
-
// defaults
|
|
89
|
-
} else if (value === "ufixed") {
|
|
90
|
-
signed = false;
|
|
91
|
-
} else {
|
|
92
|
-
const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
|
|
93
|
-
assertArgument(match, "invalid fixed format", "format", value);
|
|
94
|
-
signed = match[1] !== "u";
|
|
95
|
-
width = parseInt(match[2]);
|
|
96
|
-
decimals = parseInt(match[3]);
|
|
97
|
-
}
|
|
98
|
-
} else if (value != null && typeof value === "object") {
|
|
99
|
-
const check = (key, type, defaultValue) => {
|
|
100
|
-
if (value[key] == null) return defaultValue;
|
|
101
|
-
assertArgument(typeof value[key] === type,
|
|
102
|
-
`invalid fixed format (${key} not ${type})`, `format.${key}`, value[key]);
|
|
103
|
-
return value[key];
|
|
104
|
-
};
|
|
105
|
-
signed = check("signed", "boolean", signed);
|
|
106
|
-
width = check("width", "number", width);
|
|
107
|
-
decimals = check("decimals", "number", decimals);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
assertArgument(width % 8 === 0, "invalid FixedNumber width (not byte aligned)", "format.width", width);
|
|
111
|
-
assertArgument(decimals <= 80, "invalid FixedNumber decimals (too large)", "format.decimals", decimals);
|
|
112
|
-
|
|
113
|
-
const name = (signed ? "" : "u") + "fixed" + String(width) + "x" + String(decimals);
|
|
114
|
-
return { signed, width, decimals, name };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function _toString(val, decimals) {
|
|
118
|
-
let negative = "";
|
|
119
|
-
if (val < BN_0) {
|
|
120
|
-
negative = "-";
|
|
121
|
-
val *= BN_N1;
|
|
122
|
-
}
|
|
123
|
-
let str = val.toString();
|
|
124
|
-
if (decimals === 0) return negative + str;
|
|
125
|
-
|
|
126
|
-
while (str.length <= decimals) { str = _zeros + str; }
|
|
127
|
-
const index = str.length - decimals;
|
|
128
|
-
str = str.substring(0, index) + "." + str.substring(index);
|
|
129
|
-
|
|
130
|
-
while (str[0] === "0" && str[1] !== ".") { str = str.substring(1); }
|
|
131
|
-
while (str[str.length - 1] === "0" && str[str.length - 2] !== ".") { str = str.substring(0, str.length - 1); }
|
|
132
|
-
|
|
133
|
-
return negative + str;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
class FixedNumber {
|
|
137
|
-
/**
|
|
138
|
-
* @param {any} guard
|
|
139
|
-
* @param {bigint} value
|
|
140
|
-
* @param {any} format
|
|
141
|
-
*/
|
|
142
|
-
constructor(guard, value, format) {
|
|
143
|
-
assertArgument(guard === _guard, "cannot use FixedNumber constructor; use FixedNumber.from", "guard", "INVALID");
|
|
144
|
-
|
|
145
|
-
/** @type {bigint} */
|
|
146
|
-
this._val = value;
|
|
147
|
-
/** @type {{ signed: boolean, width: number, decimals: number, name: string }} */
|
|
148
|
-
this._format = format;
|
|
149
|
-
/** @type {string} */
|
|
150
|
-
this.format = format.name;
|
|
151
|
-
/** @type {string} */
|
|
152
|
-
this._value = _toString(value, format.decimals);
|
|
153
|
-
/** @type {bigint} */
|
|
154
|
-
this._tens = _getTens(format.decimals);
|
|
155
|
-
|
|
156
|
-
Object.freeze(this);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
get signed() { return this._format.signed; }
|
|
160
|
-
get width() { return this._format.width; }
|
|
161
|
-
get decimals() { return this._format.decimals; }
|
|
162
|
-
get value() { return this._val; }
|
|
163
|
-
|
|
164
|
-
_checkFormat(other) {
|
|
165
|
-
assertArgument(this.format === other.format,
|
|
166
|
-
"incompatible format; use fixedNumber.toFormat", "other", other);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
_checkValue(val, safeOp) {
|
|
170
|
-
val = _checkValue(val, this._format, safeOp);
|
|
171
|
-
return new FixedNumber(_guard, val, this._format);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// --- Arithmetic -----------------------------------------------------------
|
|
175
|
-
|
|
176
|
-
_add(o, safeOp) {
|
|
177
|
-
this._checkFormat(o);
|
|
178
|
-
return this._checkValue(this._val + o._val, safeOp);
|
|
179
|
-
}
|
|
180
|
-
addUnsafe(other) { return this._add(other); }
|
|
181
|
-
add(other) { return this._add(other, "add"); }
|
|
182
|
-
|
|
183
|
-
_sub(o, safeOp) {
|
|
184
|
-
this._checkFormat(o);
|
|
185
|
-
return this._checkValue(this._val - o._val, safeOp);
|
|
186
|
-
}
|
|
187
|
-
subUnsafe(other) { return this._sub(other); }
|
|
188
|
-
sub(other) { return this._sub(other, "sub"); }
|
|
189
|
-
|
|
190
|
-
_mul(o, safeOp) {
|
|
191
|
-
this._checkFormat(o);
|
|
192
|
-
return this._checkValue((this._val * o._val) / this._tens, safeOp);
|
|
193
|
-
}
|
|
194
|
-
mulUnsafe(other) { return this._mul(other); }
|
|
195
|
-
mul(other) { return this._mul(other, "mul"); }
|
|
196
|
-
|
|
197
|
-
mulSignal(other) {
|
|
198
|
-
this._checkFormat(other);
|
|
199
|
-
const value = this._val * other._val;
|
|
200
|
-
assert(value % this._tens === BN_0, "precision lost during signalling mul", "NUMERIC_FAULT", {
|
|
201
|
-
operation: "mulSignal", fault: "underflow", value: this,
|
|
202
|
-
});
|
|
203
|
-
return this._checkValue(value / this._tens, "mulSignal");
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
_div(o, safeOp) {
|
|
207
|
-
assert(o._val !== BN_0, "division by zero", "NUMERIC_FAULT", {
|
|
208
|
-
operation: "div", fault: "divide-by-zero", value: this,
|
|
209
|
-
});
|
|
210
|
-
this._checkFormat(o);
|
|
211
|
-
return this._checkValue((this._val * this._tens) / o._val, safeOp);
|
|
212
|
-
}
|
|
213
|
-
divUnsafe(other) { return this._div(other); }
|
|
214
|
-
div(other) { return this._div(other, "div"); }
|
|
215
|
-
|
|
216
|
-
divSignal(other) {
|
|
217
|
-
assert(other._val !== BN_0, "division by zero", "NUMERIC_FAULT", {
|
|
218
|
-
operation: "div", fault: "divide-by-zero", value: this,
|
|
219
|
-
});
|
|
220
|
-
this._checkFormat(other);
|
|
221
|
-
const value = this._val * this._tens;
|
|
222
|
-
assert(value % other._val === BN_0, "precision lost during signalling div", "NUMERIC_FAULT", {
|
|
223
|
-
operation: "divSignal", fault: "underflow", value: this,
|
|
224
|
-
});
|
|
225
|
-
return this._checkValue(value / other._val, "divSignal");
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// --- Comparison -----------------------------------------------------------
|
|
229
|
-
|
|
230
|
-
cmp(other) {
|
|
231
|
-
let a = this._val, b = other._val;
|
|
232
|
-
const delta = this.decimals - other.decimals;
|
|
233
|
-
if (delta > 0) { b *= _getTens(delta); }
|
|
234
|
-
else if (delta < 0) { a *= _getTens(-delta); }
|
|
235
|
-
if (a < b) return -1;
|
|
236
|
-
if (a > b) return 1;
|
|
237
|
-
return 0;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
eq(other) { return this.cmp(other) === 0; }
|
|
241
|
-
lt(other) { return this.cmp(other) < 0; }
|
|
242
|
-
lte(other) { return this.cmp(other) <= 0; }
|
|
243
|
-
gt(other) { return this.cmp(other) > 0; }
|
|
244
|
-
gte(other) { return this.cmp(other) >= 0; }
|
|
245
|
-
|
|
246
|
-
// --- Rounding -------------------------------------------------------------
|
|
247
|
-
|
|
248
|
-
floor() {
|
|
249
|
-
let val = this._val;
|
|
250
|
-
if (val < BN_0) { val -= this._tens - BN_1; }
|
|
251
|
-
val = (val / this._tens) * this._tens;
|
|
252
|
-
return this._checkValue(val, "floor");
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
ceiling() {
|
|
256
|
-
let val = this._val;
|
|
257
|
-
if (val > BN_0) { val += this._tens - BN_1; }
|
|
258
|
-
val = (val / this._tens) * this._tens;
|
|
259
|
-
return this._checkValue(val, "ceiling");
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
round(decimals) {
|
|
263
|
-
if (decimals == null) { decimals = 0; }
|
|
264
|
-
if (decimals >= this.decimals) return this;
|
|
265
|
-
const delta = this.decimals - decimals;
|
|
266
|
-
const bump = BN_5 * _getTens(delta - 1);
|
|
267
|
-
let value = this._val;
|
|
268
|
-
if (value < BN_0) { value -= bump; } else { value += bump; }
|
|
269
|
-
const tens = _getTens(delta);
|
|
270
|
-
value = (value / tens) * tens;
|
|
271
|
-
_checkValue(value, this._format, "round");
|
|
272
|
-
return new FixedNumber(_guard, value, this._format);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// --- Inspection -----------------------------------------------------------
|
|
276
|
-
|
|
277
|
-
isZero() { return this._val === BN_0; }
|
|
278
|
-
isNegative() { return this._val < BN_0; }
|
|
279
|
-
|
|
280
|
-
// --- Conversion -----------------------------------------------------------
|
|
281
|
-
|
|
282
|
-
toString() { return this._value; }
|
|
283
|
-
toUnsafeFloat() { return parseFloat(this.toString()); }
|
|
284
|
-
|
|
285
|
-
toFormat(format) {
|
|
286
|
-
return FixedNumber.fromString(this._value, format);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
toHexString(width) {
|
|
290
|
-
let val = this._val;
|
|
291
|
-
const w = width || this.width;
|
|
292
|
-
if (this._format.signed && val < BN_0) {
|
|
293
|
-
val = (BN_1 << BigInt(w)) + val;
|
|
294
|
-
}
|
|
295
|
-
let hex = val.toString(16);
|
|
296
|
-
const byteLen = w / 8;
|
|
297
|
-
while (hex.length < byteLen * 2) { hex = "0" + hex; }
|
|
298
|
-
return "0x" + hex;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// --- Static factories -----------------------------------------------------
|
|
302
|
-
|
|
303
|
-
static fromValue(_value, decimals, _format) {
|
|
304
|
-
if (decimals == null) { decimals = 0; }
|
|
305
|
-
const format = _getFormat(_format);
|
|
306
|
-
let value = _getBigInt(_value);
|
|
307
|
-
const delta = decimals - format.decimals;
|
|
308
|
-
if (delta > 0) {
|
|
309
|
-
const tens = _getTens(delta);
|
|
310
|
-
assert(value % tens === BN_0, "value loses precision for format", "NUMERIC_FAULT", {
|
|
311
|
-
operation: "fromValue", fault: "underflow", value: _value,
|
|
312
|
-
});
|
|
313
|
-
value /= tens;
|
|
314
|
-
} else if (delta < 0) {
|
|
315
|
-
value *= _getTens(-delta);
|
|
316
|
-
}
|
|
317
|
-
_checkValue(value, format, "fromValue");
|
|
318
|
-
return new FixedNumber(_guard, value, format);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
static fromString(_value, _format) {
|
|
322
|
-
const match = _value.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
|
|
323
|
-
assertArgument(match && (match[2].length + match[3].length) > 0,
|
|
324
|
-
"invalid FixedNumber string value", "value", _value);
|
|
325
|
-
const format = _getFormat(_format);
|
|
326
|
-
let whole = match[2] || "0";
|
|
327
|
-
let decimal = match[3] || "";
|
|
328
|
-
|
|
329
|
-
while (decimal.length < format.decimals) { decimal += _zeros; }
|
|
330
|
-
|
|
331
|
-
assert(decimal.substring(format.decimals).match(/^0*$/),
|
|
332
|
-
"too many decimals for format", "NUMERIC_FAULT", {
|
|
333
|
-
operation: "fromString", fault: "underflow", value: _value,
|
|
334
|
-
});
|
|
335
|
-
decimal = decimal.substring(0, format.decimals);
|
|
336
|
-
|
|
337
|
-
const value = BigInt(match[1] + whole + decimal);
|
|
338
|
-
_checkValue(value, format, "fromString");
|
|
339
|
-
return new FixedNumber(_guard, value, format);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
static fromBytes(_value, _format) {
|
|
343
|
-
let value = BigInt("0x" + hexlify(arrayify(_value)).replace(/^0x/, ""));
|
|
344
|
-
const format = _getFormat(_format);
|
|
345
|
-
if (format.signed) { value = _fromTwos(value, format.width); }
|
|
346
|
-
_checkValue(value, format, "fromBytes");
|
|
347
|
-
return new FixedNumber(_guard, value, format);
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
static from(value, format) {
|
|
351
|
-
if (typeof value === "string") return FixedNumber.fromString(value, format);
|
|
352
|
-
if (isBytesLike(value)) return FixedNumber.fromBytes(value, format);
|
|
353
|
-
try {
|
|
354
|
-
return FixedNumber.fromValue(value, 0, format);
|
|
355
|
-
} catch (error) {
|
|
356
|
-
if (error.code !== "INVALID_ARGUMENT") throw error;
|
|
357
|
-
}
|
|
358
|
-
return assertArgument(false, "invalid FixedNumber value", "value", value);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
static isFixedNumber(value) {
|
|
362
|
-
return !!(value && value instanceof FixedNumber);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
module.exports = { FixedNumber };
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* keccak256 hash of BytesLike.
|
|
3
|
-
* @param {string|Uint8Array} data
|
|
4
|
-
* @returns {string}
|
|
5
|
-
*/
|
|
6
|
-
export function keccak256(data: string | Uint8Array): string;
|
|
7
|
-
/**
|
|
8
|
-
* sha256 hash of BytesLike.
|
|
9
|
-
* @param {string|Uint8Array} data
|
|
10
|
-
* @returns {string}
|
|
11
|
-
*/
|
|
12
|
-
export function sha256(data: string | Uint8Array): string;
|
|
13
|
-
/**
|
|
14
|
-
* sha512 hash of BytesLike.
|
|
15
|
-
* @param {string|Uint8Array} data
|
|
16
|
-
* @returns {string}
|
|
17
|
-
*/
|
|
18
|
-
export function sha512(data: string | Uint8Array): string;
|
|
19
|
-
/**
|
|
20
|
-
* ripemd160 hash of BytesLike.
|
|
21
|
-
* @param {string|Uint8Array} data
|
|
22
|
-
* @returns {string}
|
|
23
|
-
*/
|
|
24
|
-
export function ripemd160(data: string | Uint8Array): string;
|
|
25
|
-
/**
|
|
26
|
-
* ethers-style id(text) => keccak256(utf8Bytes(text))
|
|
27
|
-
* @param {string} text
|
|
28
|
-
* @returns {string}
|
|
29
|
-
*/
|
|
30
|
-
export function id(text: string): string;
|
|
31
|
-
/**
|
|
32
|
-
* Generate cryptographically strong random bytes.
|
|
33
|
-
* @param {number} length
|
|
34
|
-
* @returns {Uint8Array}
|
|
35
|
-
*/
|
|
36
|
-
export function randomBytes(length: number): Uint8Array;
|
|
37
|
-
/**
|
|
38
|
-
* Compute HMAC over data.
|
|
39
|
-
* @param {string} algorithm
|
|
40
|
-
* @param {string|Uint8Array} key
|
|
41
|
-
* @param {string|Uint8Array} data
|
|
42
|
-
* @returns {string}
|
|
43
|
-
*/
|
|
44
|
-
export function computeHmac(algorithm: string, key: string | Uint8Array, data: string | Uint8Array): string;
|
|
45
|
-
/**
|
|
46
|
-
* PBKDF2 (sync) helper returning hex string.
|
|
47
|
-
* @param {string|Uint8Array} password
|
|
48
|
-
* @param {string|Uint8Array} salt
|
|
49
|
-
* @param {number} iterations
|
|
50
|
-
* @param {number} keylen
|
|
51
|
-
* @param {string=} algorithm
|
|
52
|
-
* @returns {string}
|
|
53
|
-
*/
|
|
54
|
-
export function pbkdf2(password: string | Uint8Array, salt: string | Uint8Array, iterations: number, keylen: number, algorithm?: string | undefined): string;
|
|
55
|
-
/**
|
|
56
|
-
* scrypt (async) helper returning hex string.
|
|
57
|
-
* @param {string|Uint8Array} password
|
|
58
|
-
* @param {string|Uint8Array} salt
|
|
59
|
-
* @param {number} N
|
|
60
|
-
* @param {number} r
|
|
61
|
-
* @param {number} p
|
|
62
|
-
* @param {number} dkLen
|
|
63
|
-
* @returns {Promise<string>}
|
|
64
|
-
*/
|
|
65
|
-
export function scrypt(password: string | Uint8Array, salt: string | Uint8Array, N: number, r: number, p: number, dkLen: number): Promise<string>;
|
|
66
|
-
/**
|
|
67
|
-
* scrypt (sync) helper returning hex string.
|
|
68
|
-
* @param {string|Uint8Array} password
|
|
69
|
-
* @param {string|Uint8Array} salt
|
|
70
|
-
* @param {number} N
|
|
71
|
-
* @param {number} r
|
|
72
|
-
* @param {number} p
|
|
73
|
-
* @param {number} dkLen
|
|
74
|
-
* @returns {string}
|
|
75
|
-
*/
|
|
76
|
-
export function scryptSync(password: string | Uint8Array, salt: string | Uint8Array, N: number, r: number, p: number, dkLen: number): string;
|