quantumcoin 6.14.2 → 6.14.5
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/CHANGELOG.md +442 -442
- package/FUNDING.json +10 -10
- package/LICENSE.md +21 -21
- package/README.md +132 -142
- package/SECURITY.md +34 -34
- package/dist/README.md +22 -22
- package/dist/quantumcoin.js +1127 -1324
- package/dist/quantumcoin.js.map +1 -1
- package/dist/quantumcoin.min.js +1 -1
- package/dist/quantumcoin.umd.js +1128 -1327
- package/dist/quantumcoin.umd.js.map +1 -1
- package/dist/quantumcoin.umd.min.js +1 -1
- package/dist/wordlists-extra.js +1 -1
- package/dist/wordlists-extra.js.map +1 -1
- package/dist/wordlists-extra.min.js +1 -1
- package/lib.commonjs/README.md +16 -16
- package/lib.commonjs/_version.js +1 -1
- package/lib.commonjs/crypto/signature.d.ts +3 -76
- package/lib.commonjs/crypto/signature.d.ts.map +1 -1
- package/lib.commonjs/crypto/signature.js +15 -199
- package/lib.commonjs/crypto/signature.js.map +1 -1
- package/lib.commonjs/crypto/signing-key.d.ts +1 -1
- package/lib.commonjs/crypto/signing-key.d.ts.map +1 -1
- package/lib.commonjs/crypto/signing-key.js +19 -10
- package/lib.commonjs/crypto/signing-key.js.map +1 -1
- package/lib.commonjs/package.json +12 -12
- package/lib.commonjs/providers/provider-jsonrpc.d.ts +0 -1
- package/lib.commonjs/providers/provider-jsonrpc.d.ts.map +1 -1
- package/lib.commonjs/providers/provider-jsonrpc.js +0 -1
- package/lib.commonjs/providers/provider-jsonrpc.js.map +1 -1
- package/lib.commonjs/quantumcoin.d.ts +2 -0
- package/lib.commonjs/quantumcoin.d.ts.map +1 -1
- package/lib.commonjs/quantumcoin.js +11 -5
- package/lib.commonjs/quantumcoin.js.map +1 -1
- package/lib.commonjs/transaction/address.d.ts.map +1 -1
- package/lib.commonjs/transaction/address.js +8 -3
- package/lib.commonjs/transaction/address.js.map +1 -1
- package/lib.commonjs/transaction/transaction.d.ts.map +1 -1
- package/lib.commonjs/transaction/transaction.js +7 -40
- package/lib.commonjs/transaction/transaction.js.map +1 -1
- package/lib.commonjs/wallet/json-keystore.d.ts.map +1 -1
- package/lib.commonjs/wallet/json-keystore.js +7 -7
- package/lib.commonjs/wallet/json-keystore.js.map +1 -1
- package/lib.commonjs/wallet/wallet.d.ts.map +1 -1
- package/lib.commonjs/wallet/wallet.js +2 -2
- package/lib.commonjs/wallet/wallet.js.map +1 -1
- package/lib.esm/README.md +16 -16
- package/lib.esm/_version.js +1 -1
- package/lib.esm/crypto/signature.d.ts +3 -76
- package/lib.esm/crypto/signature.d.ts.map +1 -1
- package/lib.esm/crypto/signature.js +16 -202
- package/lib.esm/crypto/signature.js.map +1 -1
- package/lib.esm/crypto/signing-key.d.ts +1 -1
- package/lib.esm/crypto/signing-key.d.ts.map +1 -1
- package/lib.esm/crypto/signing-key.js +20 -9
- package/lib.esm/crypto/signing-key.js.map +1 -1
- package/lib.esm/package.json +12 -12
- package/lib.esm/providers/provider-jsonrpc.d.ts +0 -1
- package/lib.esm/providers/provider-jsonrpc.d.ts.map +1 -1
- package/lib.esm/providers/provider-jsonrpc.js +0 -1
- package/lib.esm/providers/provider-jsonrpc.js.map +1 -1
- package/lib.esm/quantumcoin.d.ts +2 -0
- package/lib.esm/quantumcoin.d.ts.map +1 -1
- package/lib.esm/quantumcoin.js +6 -0
- package/lib.esm/quantumcoin.js.map +1 -1
- package/lib.esm/transaction/address.d.ts.map +1 -1
- package/lib.esm/transaction/address.js +8 -2
- package/lib.esm/transaction/address.js.map +1 -1
- package/lib.esm/transaction/transaction.d.ts.map +1 -1
- package/lib.esm/transaction/transaction.js +7 -40
- package/lib.esm/transaction/transaction.js.map +1 -1
- package/lib.esm/wallet/json-keystore.d.ts.map +1 -1
- package/lib.esm/wallet/json-keystore.js +11 -5
- package/lib.esm/wallet/json-keystore.js.map +1 -1
- package/lib.esm/wallet/wallet.d.ts.map +1 -1
- package/lib.esm/wallet/wallet.js +3 -1
- package/lib.esm/wallet/wallet.js.map +1 -1
- package/package.json +6 -5
- package/rollup.config.mjs +50 -50
- package/src.ts/_version.ts +1 -1
- package/src.ts/abi/abi-coder.ts +237 -237
- package/src.ts/abi/bytes32.ts +45 -45
- package/src.ts/abi/coders/abstract-coder.ts +541 -541
- package/src.ts/abi/coders/address.ts +36 -36
- package/src.ts/abi/coders/anonymous.ts +29 -29
- package/src.ts/abi/coders/array.ts +199 -199
- package/src.ts/abi/coders/boolean.ts +27 -27
- package/src.ts/abi/coders/bytes.ts +43 -43
- package/src.ts/abi/coders/fixed-bytes.ts +37 -37
- package/src.ts/abi/coders/null.ts +28 -28
- package/src.ts/abi/coders/number.ts +63 -63
- package/src.ts/abi/coders/string.ts +29 -29
- package/src.ts/abi/coders/tuple.ts +69 -69
- package/src.ts/abi/fragments.ts +1617 -1617
- package/src.ts/abi/index.ts +41 -41
- package/src.ts/abi/interface.ts +1271 -1271
- package/src.ts/abi/typed.ts +796 -796
- package/src.ts/address/address.ts +148 -148
- package/src.ts/address/checks.ts +123 -123
- package/src.ts/address/contract-address.ts +80 -80
- package/src.ts/address/index.ts +57 -57
- package/src.ts/constants/addresses.ts +8 -8
- package/src.ts/constants/hashes.ts +7 -7
- package/src.ts/constants/index.ts +16 -16
- package/src.ts/constants/numbers.ts +35 -35
- package/src.ts/constants/strings.ts +16 -16
- package/src.ts/contract/contract.ts +1120 -1120
- package/src.ts/contract/factory.ts +143 -143
- package/src.ts/contract/index.ts +31 -31
- package/src.ts/contract/types.ts +236 -236
- package/src.ts/contract/wrappers.ts +225 -225
- package/src.ts/crypto/crypto-browser.ts +64 -64
- package/src.ts/crypto/crypto.ts +4 -4
- package/src.ts/crypto/hmac.ts +51 -51
- package/src.ts/crypto/index.ts +59 -59
- package/src.ts/crypto/keccak.ts +54 -54
- package/src.ts/crypto/pbkdf2.ts +55 -55
- package/src.ts/crypto/random.ts +36 -36
- package/src.ts/crypto/ripemd160.ts +43 -43
- package/src.ts/crypto/scrypt.ts +114 -114
- package/src.ts/crypto/sha2.ts +78 -78
- package/src.ts/crypto/signature.ts +145 -349
- package/src.ts/crypto/signing-key.ts +126 -118
- package/src.ts/hash/authorization.ts +38 -38
- package/src.ts/hash/id.ts +17 -17
- package/src.ts/hash/index.ts +18 -18
- package/src.ts/hash/message.ts +51 -51
- package/src.ts/hash/namehash.ts +101 -101
- package/src.ts/hash/solidity.ts +117 -117
- package/src.ts/hash/typed-data.ts +658 -658
- package/src.ts/index.ts +12 -12
- package/src.ts/providers/abstract-provider.ts +1761 -1761
- package/src.ts/providers/abstract-signer.ts +314 -314
- package/src.ts/providers/community.ts +49 -49
- package/src.ts/providers/contracts.ts +42 -42
- package/src.ts/providers/default-provider.ts +96 -96
- package/src.ts/providers/ens-resolver.ts +606 -606
- package/src.ts/providers/format.ts +320 -320
- package/src.ts/providers/formatting.ts +418 -418
- package/src.ts/providers/index.ts +125 -125
- package/src.ts/providers/network.ts +327 -327
- package/src.ts/providers/pagination.ts +8 -8
- package/src.ts/providers/plugin-fallback.ts +35 -35
- package/src.ts/providers/plugins-network.ts +281 -281
- package/src.ts/providers/provider-browser.ts +334 -334
- package/src.ts/providers/provider-fallback.ts +801 -801
- package/src.ts/providers/provider-ipcsocket-browser.ts +3 -3
- package/src.ts/providers/provider-ipcsocket.ts +81 -81
- package/src.ts/providers/provider-jsonrpc.ts +1334 -1335
- package/src.ts/providers/provider-socket.ts +352 -352
- package/src.ts/providers/provider-websocket.ts +103 -103
- package/src.ts/providers/provider.ts +2136 -2136
- package/src.ts/providers/signer-noncemanager.ts +98 -98
- package/src.ts/providers/signer.ts +166 -166
- package/src.ts/providers/subscriber-connection.ts +74 -74
- package/src.ts/providers/subscriber-filterid.ts +199 -199
- package/src.ts/providers/subscriber-polling.ts +321 -321
- package/src.ts/providers/ws-browser.ts +11 -11
- package/src.ts/providers/ws.ts +3 -3
- package/src.ts/quantumcoin.ts +219 -211
- package/src.ts/thirdparty.d.ts +16 -16
- package/src.ts/transaction/accesslist.ts +43 -43
- package/src.ts/transaction/address.ts +35 -31
- package/src.ts/transaction/authorization.ts +14 -14
- package/src.ts/transaction/index.ts +51 -51
- package/src.ts/transaction/transaction.ts +1349 -1379
- package/src.ts/utils/base58.ts +73 -73
- package/src.ts/utils/base64-browser.ts +25 -25
- package/src.ts/utils/base64.ts +56 -56
- package/src.ts/utils/data.ts +199 -199
- package/src.ts/utils/errors.ts +793 -793
- package/src.ts/utils/events.ts +105 -105
- package/src.ts/utils/fetch.ts +970 -970
- package/src.ts/utils/fixednumber.ts +643 -643
- package/src.ts/utils/geturl-browser.ts +81 -81
- package/src.ts/utils/geturl.ts +134 -134
- package/src.ts/utils/index.ts +95 -95
- package/src.ts/utils/maths.ts +240 -240
- package/src.ts/utils/properties.ts +60 -60
- package/src.ts/utils/rlp-decode.ts +104 -104
- package/src.ts/utils/rlp-encode.ts +64 -64
- package/src.ts/utils/rlp.ts +20 -20
- package/src.ts/utils/units.ts +91 -91
- package/src.ts/utils/utf8.ts +325 -325
- package/src.ts/utils/uuid.ts +36 -36
- package/src.ts/wallet/base-wallet.ts +160 -160
- package/src.ts/wallet/index.ts +32 -32
- package/src.ts/wallet/json-keystore.ts +108 -106
- package/src.ts/wallet/utils.ts +147 -147
- package/src.ts/wallet/wallet.ts +138 -139
- package/src.ts/wordlists/bit-reader.ts +35 -35
- package/src.ts/wordlists/decode-owl.ts +58 -58
- package/src.ts/wordlists/decode-owla.ts +33 -33
- package/src.ts/wordlists/generation/encode-latin.ts +370 -370
- package/src.ts/wordlists/index.ts +26 -26
- package/src.ts/wordlists/lang-cz.ts +33 -33
- package/src.ts/wordlists/lang-en.ts +33 -33
- package/src.ts/wordlists/lang-es.ts +35 -35
- package/src.ts/wordlists/lang-fr.ts +34 -34
- package/src.ts/wordlists/lang-it.ts +33 -33
- package/src.ts/wordlists/lang-ja.ts +181 -181
- package/src.ts/wordlists/lang-ko.ts +104 -104
- package/src.ts/wordlists/lang-pt.ts +34 -34
- package/src.ts/wordlists/lang-zh.ts +112 -112
- package/src.ts/wordlists/wordlist-owl.ts +77 -77
- package/src.ts/wordlists/wordlist-owla.ts +41 -41
- package/src.ts/wordlists/wordlist.ts +59 -59
- package/src.ts/wordlists/wordlists-browser.ts +8 -8
- package/src.ts/wordlists/wordlists-extra.ts +9 -9
- package/src.ts/wordlists/wordlists.ts +38 -38
- package/dist/quantumcoin.min.js'.gz' +0 -0
- package/dist/quantumcoin.umd.min.js'.gz' +0 -0
- package/dist/wordlists-extra.min.js'.gz' +0 -0
- package/lib.commonjs/providers/provider-alchemy.d.ts +0 -50
- package/lib.commonjs/providers/provider-alchemy.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-alchemy.js +0 -151
- package/lib.commonjs/providers/provider-alchemy.js.map +0 -1
- package/lib.commonjs/providers/provider-ankr.d.ts +0 -61
- package/lib.commonjs/providers/provider-ankr.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-ankr.js +0 -137
- package/lib.commonjs/providers/provider-ankr.js.map +0 -1
- package/lib.commonjs/providers/provider-blockscout.d.ts +0 -59
- package/lib.commonjs/providers/provider-blockscout.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-blockscout.js +0 -145
- package/lib.commonjs/providers/provider-blockscout.js.map +0 -1
- package/lib.commonjs/providers/provider-chainstack.d.ts +0 -46
- package/lib.commonjs/providers/provider-chainstack.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-chainstack.js +0 -102
- package/lib.commonjs/providers/provider-chainstack.js.map +0 -1
- package/lib.commonjs/providers/provider-cloudflare.d.ts +0 -14
- package/lib.commonjs/providers/provider-cloudflare.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-cloudflare.js +0 -26
- package/lib.commonjs/providers/provider-cloudflare.js.map +0 -1
- package/lib.commonjs/providers/provider-etherscan.d.ts +0 -147
- package/lib.commonjs/providers/provider-etherscan.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-etherscan.js +0 -587
- package/lib.commonjs/providers/provider-etherscan.js.map +0 -1
- package/lib.commonjs/providers/provider-infura.d.ts +0 -101
- package/lib.commonjs/providers/provider-infura.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-infura.js +0 -206
- package/lib.commonjs/providers/provider-infura.js.map +0 -1
- package/lib.commonjs/providers/provider-pocket.d.ts +0 -54
- package/lib.commonjs/providers/provider-pocket.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-pocket.js +0 -109
- package/lib.commonjs/providers/provider-pocket.js.map +0 -1
- package/lib.commonjs/providers/provider-quicknode.d.ts +0 -59
- package/lib.commonjs/providers/provider-quicknode.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-quicknode.js +0 -163
- package/lib.commonjs/providers/provider-quicknode.js.map +0 -1
- package/lib.commonjs/wallet/hdwallet.d.ts +0 -248
- package/lib.commonjs/wallet/hdwallet.d.ts.map +0 -1
- package/lib.commonjs/wallet/hdwallet.js +0 -505
- package/lib.commonjs/wallet/hdwallet.js.map +0 -1
- package/lib.commonjs/wallet/json-crowdsale.d.ts +0 -27
- package/lib.commonjs/wallet/json-crowdsale.d.ts.map +0 -1
- package/lib.commonjs/wallet/json-crowdsale.js +0 -60
- package/lib.commonjs/wallet/json-crowdsale.js.map +0 -1
- package/lib.commonjs/wallet/mnemonic.d.ts +0 -65
- package/lib.commonjs/wallet/mnemonic.d.ts.map +0 -1
- package/lib.commonjs/wallet/mnemonic.js +0 -169
- package/lib.commonjs/wallet/mnemonic.js.map +0 -1
- package/lib.commonjs/wallet/seedwallet.d.ts +0 -4
- package/lib.commonjs/wallet/seedwallet.d.ts.map +0 -1
- package/lib.commonjs/wallet/seedwallet.js +0 -8
- package/lib.commonjs/wallet/seedwallet.js.map +0 -1
- package/lib.esm/providers/provider-alchemy.d.ts +0 -50
- package/lib.esm/providers/provider-alchemy.d.ts.map +0 -1
- package/lib.esm/providers/provider-alchemy.js +0 -147
- package/lib.esm/providers/provider-alchemy.js.map +0 -1
- package/lib.esm/providers/provider-ankr.d.ts +0 -61
- package/lib.esm/providers/provider-ankr.d.ts.map +0 -1
- package/lib.esm/providers/provider-ankr.js +0 -133
- package/lib.esm/providers/provider-ankr.js.map +0 -1
- package/lib.esm/providers/provider-blockscout.d.ts +0 -59
- package/lib.esm/providers/provider-blockscout.d.ts.map +0 -1
- package/lib.esm/providers/provider-blockscout.js +0 -141
- package/lib.esm/providers/provider-blockscout.js.map +0 -1
- package/lib.esm/providers/provider-chainstack.d.ts +0 -46
- package/lib.esm/providers/provider-chainstack.d.ts.map +0 -1
- package/lib.esm/providers/provider-chainstack.js +0 -98
- package/lib.esm/providers/provider-chainstack.js.map +0 -1
- package/lib.esm/providers/provider-cloudflare.d.ts +0 -14
- package/lib.esm/providers/provider-cloudflare.d.ts.map +0 -1
- package/lib.esm/providers/provider-cloudflare.js +0 -22
- package/lib.esm/providers/provider-cloudflare.js.map +0 -1
- package/lib.esm/providers/provider-etherscan.d.ts +0 -147
- package/lib.esm/providers/provider-etherscan.d.ts.map +0 -1
- package/lib.esm/providers/provider-etherscan.js +0 -584
- package/lib.esm/providers/provider-etherscan.js.map +0 -1
- package/lib.esm/providers/provider-infura.d.ts +0 -101
- package/lib.esm/providers/provider-infura.d.ts.map +0 -1
- package/lib.esm/providers/provider-infura.js +0 -201
- package/lib.esm/providers/provider-infura.js.map +0 -1
- package/lib.esm/providers/provider-pocket.d.ts +0 -54
- package/lib.esm/providers/provider-pocket.d.ts.map +0 -1
- package/lib.esm/providers/provider-pocket.js +0 -105
- package/lib.esm/providers/provider-pocket.js.map +0 -1
- package/lib.esm/providers/provider-quicknode.d.ts +0 -59
- package/lib.esm/providers/provider-quicknode.d.ts.map +0 -1
- package/lib.esm/providers/provider-quicknode.js +0 -159
- package/lib.esm/providers/provider-quicknode.js.map +0 -1
- package/lib.esm/wallet/hdwallet.d.ts +0 -248
- package/lib.esm/wallet/hdwallet.d.ts.map +0 -1
- package/lib.esm/wallet/hdwallet.js +0 -498
- package/lib.esm/wallet/hdwallet.js.map +0 -1
- package/lib.esm/wallet/json-crowdsale.d.ts +0 -27
- package/lib.esm/wallet/json-crowdsale.d.ts.map +0 -1
- package/lib.esm/wallet/json-crowdsale.js +0 -55
- package/lib.esm/wallet/json-crowdsale.js.map +0 -1
- package/lib.esm/wallet/mnemonic.d.ts +0 -65
- package/lib.esm/wallet/mnemonic.d.ts.map +0 -1
- package/lib.esm/wallet/mnemonic.js +0 -165
- package/lib.esm/wallet/mnemonic.js.map +0 -1
- package/lib.esm/wallet/seedwallet.d.ts +0 -4
- package/lib.esm/wallet/seedwallet.d.ts.map +0 -1
- package/lib.esm/wallet/seedwallet.js +0 -4
- package/lib.esm/wallet/seedwallet.js.map +0 -1
package/src.ts/utils/maths.ts
CHANGED
|
@@ -1,240 +1,240 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Some mathematic operations.
|
|
3
|
-
*
|
|
4
|
-
* @_subsection: api/utils:Math Helpers [about-maths]
|
|
5
|
-
*/
|
|
6
|
-
import { hexlify, isBytesLike } from "./data.js";
|
|
7
|
-
import { assert, assertArgument } from "./errors.js";
|
|
8
|
-
|
|
9
|
-
import type { BytesLike } from "./data.js";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Any type that can be used where a numeric value is needed.
|
|
13
|
-
*/
|
|
14
|
-
export type Numeric = number | bigint;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Any type that can be used where a big number is needed.
|
|
18
|
-
*/
|
|
19
|
-
export type BigNumberish = string | Numeric;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const BN_0 = BigInt(0);
|
|
23
|
-
const BN_1 = BigInt(1);
|
|
24
|
-
|
|
25
|
-
//const BN_Max256 = (BN_1 << BigInt(256)) - BN_1;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// IEEE 754 support 53-bits of mantissa
|
|
29
|
-
const maxValue = 0x1fffffffffffff;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Convert %%value%% from a twos-compliment representation of %%width%%
|
|
33
|
-
* bits to its value.
|
|
34
|
-
*
|
|
35
|
-
* If the highest bit is ``1``, the result will be negative.
|
|
36
|
-
*/
|
|
37
|
-
export function fromTwos(_value: BigNumberish, _width: Numeric): bigint {
|
|
38
|
-
const value = getUint(_value, "value");
|
|
39
|
-
const width = BigInt(getNumber(_width, "width"));
|
|
40
|
-
|
|
41
|
-
assert((value >> width) === BN_0, "overflow", "NUMERIC_FAULT", {
|
|
42
|
-
operation: "fromTwos", fault: "overflow", value: _value
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Top bit set; treat as a negative value
|
|
46
|
-
if (value >> (width - BN_1)) {
|
|
47
|
-
const mask = (BN_1 << width) - BN_1;
|
|
48
|
-
return -(((~value) & mask) + BN_1);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Convert %%value%% to a twos-compliment representation of
|
|
56
|
-
* %%width%% bits.
|
|
57
|
-
*
|
|
58
|
-
* The result will always be positive.
|
|
59
|
-
*/
|
|
60
|
-
export function toTwos(_value: BigNumberish, _width: Numeric): bigint {
|
|
61
|
-
let value = getBigInt(_value, "value");
|
|
62
|
-
const width = BigInt(getNumber(_width, "width"));
|
|
63
|
-
|
|
64
|
-
const limit = (BN_1 << (width - BN_1));
|
|
65
|
-
|
|
66
|
-
if (value < BN_0) {
|
|
67
|
-
value = -value;
|
|
68
|
-
assert(value <= limit, "too low", "NUMERIC_FAULT", {
|
|
69
|
-
operation: "toTwos", fault: "overflow", value: _value
|
|
70
|
-
});
|
|
71
|
-
const mask = (BN_1 << width) - BN_1;
|
|
72
|
-
return ((~value) & mask) + BN_1;
|
|
73
|
-
} else {
|
|
74
|
-
assert(value < limit, "too high", "NUMERIC_FAULT", {
|
|
75
|
-
operation: "toTwos", fault: "overflow", value: _value
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return value;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Mask %%value%% with a bitmask of %%bits%% ones.
|
|
84
|
-
*/
|
|
85
|
-
export function mask(_value: BigNumberish, _bits: Numeric): bigint {
|
|
86
|
-
const value = getUint(_value, "value");
|
|
87
|
-
const bits = BigInt(getNumber(_bits, "bits"));
|
|
88
|
-
return value & ((BN_1 << bits) - BN_1);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Gets a BigInt from %%value%%. If it is an invalid value for
|
|
93
|
-
* a BigInt, then an ArgumentError will be thrown for %%name%%.
|
|
94
|
-
*/
|
|
95
|
-
export function getBigInt(value: BigNumberish, name?: string): bigint {
|
|
96
|
-
switch (typeof(value)) {
|
|
97
|
-
case "bigint": return value;
|
|
98
|
-
case "number":
|
|
99
|
-
assertArgument(Number.isInteger(value), "underflow", name || "value", value);
|
|
100
|
-
assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value);
|
|
101
|
-
return BigInt(value);
|
|
102
|
-
case "string":
|
|
103
|
-
try {
|
|
104
|
-
if (value === "") { throw new Error("empty string"); }
|
|
105
|
-
if (value[0] === "-" && value[1] !== "-") {
|
|
106
|
-
return -BigInt(value.substring(1));
|
|
107
|
-
}
|
|
108
|
-
return BigInt(value);
|
|
109
|
-
} catch(e: any) {
|
|
110
|
-
assertArgument(false, `invalid BigNumberish string: ${ e.message }`, name || "value", value);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
assertArgument(false, "invalid BigNumberish value", name || "value", value);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Returns %%value%% as a bigint, validating it is valid as a bigint
|
|
118
|
-
* value and that it is positive.
|
|
119
|
-
*/
|
|
120
|
-
export function getUint(value: BigNumberish, name?: string): bigint {
|
|
121
|
-
const result = getBigInt(value, name);
|
|
122
|
-
assert(result >= BN_0, "unsigned value cannot be negative", "NUMERIC_FAULT", {
|
|
123
|
-
fault: "overflow", operation: "getUint", value
|
|
124
|
-
});
|
|
125
|
-
return result;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const Nibbles = "0123456789abcdef";
|
|
129
|
-
|
|
130
|
-
/*
|
|
131
|
-
* Converts %%value%% to a BigInt. If %%value%% is a Uint8Array, it
|
|
132
|
-
* is treated as Big Endian data.
|
|
133
|
-
*/
|
|
134
|
-
export function toBigInt(value: BigNumberish | Uint8Array): bigint {
|
|
135
|
-
if (value instanceof Uint8Array) {
|
|
136
|
-
let result = "0x0";
|
|
137
|
-
for (const v of value) {
|
|
138
|
-
result += Nibbles[v >> 4];
|
|
139
|
-
result += Nibbles[v & 0x0f];
|
|
140
|
-
}
|
|
141
|
-
return BigInt(result);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return getBigInt(value);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Gets a //number// from %%value%%. If it is an invalid value for
|
|
149
|
-
* a //number//, then an ArgumentError will be thrown for %%name%%.
|
|
150
|
-
*/
|
|
151
|
-
export function getNumber(value: BigNumberish, name?: string): number {
|
|
152
|
-
switch (typeof(value)) {
|
|
153
|
-
case "bigint":
|
|
154
|
-
assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value);
|
|
155
|
-
return Number(value);
|
|
156
|
-
case "number":
|
|
157
|
-
assertArgument(Number.isInteger(value), "underflow", name || "value", value);
|
|
158
|
-
assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value);
|
|
159
|
-
return value;
|
|
160
|
-
case "string":
|
|
161
|
-
try {
|
|
162
|
-
if (value === "") { throw new Error("empty string"); }
|
|
163
|
-
return getNumber(BigInt(value), name);
|
|
164
|
-
} catch(e: any) {
|
|
165
|
-
assertArgument(false, `invalid numeric string: ${ e.message }`, name || "value", value);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
assertArgument(false, "invalid numeric value", name || "value", value);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Converts %%value%% to a number. If %%value%% is a Uint8Array, it
|
|
174
|
-
* is treated as Big Endian data. Throws if the value is not safe.
|
|
175
|
-
*/
|
|
176
|
-
export function toNumber(value: BigNumberish | Uint8Array): number {
|
|
177
|
-
return getNumber(toBigInt(value));
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Converts %%value%% to a Big Endian hexstring, optionally padded to
|
|
182
|
-
* %%width%% bytes.
|
|
183
|
-
*/
|
|
184
|
-
export function toBeHex(_value: BigNumberish, _width?: Numeric): string {
|
|
185
|
-
const value = getUint(_value, "value");
|
|
186
|
-
|
|
187
|
-
let result = value.toString(16);
|
|
188
|
-
|
|
189
|
-
if (_width == null) {
|
|
190
|
-
// Ensure the value is of even length
|
|
191
|
-
if (result.length % 2) { result = "0" + result; }
|
|
192
|
-
} else {
|
|
193
|
-
const width = getNumber(_width, "width");
|
|
194
|
-
assert(width * 2 >= result.length, `value exceeds width (${ width } bytes)`, "NUMERIC_FAULT", {
|
|
195
|
-
operation: "toBeHex",
|
|
196
|
-
fault: "overflow",
|
|
197
|
-
value: _value
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
// Pad the value to the required width
|
|
201
|
-
while (result.length < (width * 2)) { result = "0" + result; }
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return "0x" + result;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Converts %%value%% to a Big Endian Uint8Array.
|
|
210
|
-
*/
|
|
211
|
-
export function toBeArray(_value: BigNumberish): Uint8Array {
|
|
212
|
-
const value = getUint(_value, "value");
|
|
213
|
-
|
|
214
|
-
if (value === BN_0) { return new Uint8Array([ ]); }
|
|
215
|
-
|
|
216
|
-
let hex = value.toString(16);
|
|
217
|
-
if (hex.length % 2) { hex = "0" + hex; }
|
|
218
|
-
|
|
219
|
-
const result = new Uint8Array(hex.length / 2);
|
|
220
|
-
for (let i = 0; i < result.length; i++) {
|
|
221
|
-
const offset = i * 2;
|
|
222
|
-
result[i] = parseInt(hex.substring(offset, offset + 2), 16);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return result;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Returns a [[HexString]] for %%value%% safe to use as a //Quantity//.
|
|
230
|
-
*
|
|
231
|
-
* A //Quantity// does not have and leading 0 values unless the value is
|
|
232
|
-
* the literal value `0x0`. This is most commonly used for JSSON-RPC
|
|
233
|
-
* numeric values.
|
|
234
|
-
*/
|
|
235
|
-
export function toQuantity(value: BytesLike | BigNumberish): string {
|
|
236
|
-
let result = hexlify(isBytesLike(value) ? value: toBeArray(value)).substring(2);
|
|
237
|
-
while (result.startsWith("0")) { result = result.substring(1); }
|
|
238
|
-
if (result === "") { result = "0"; }
|
|
239
|
-
return "0x" + result;
|
|
240
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Some mathematic operations.
|
|
3
|
+
*
|
|
4
|
+
* @_subsection: api/utils:Math Helpers [about-maths]
|
|
5
|
+
*/
|
|
6
|
+
import { hexlify, isBytesLike } from "./data.js";
|
|
7
|
+
import { assert, assertArgument } from "./errors.js";
|
|
8
|
+
|
|
9
|
+
import type { BytesLike } from "./data.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Any type that can be used where a numeric value is needed.
|
|
13
|
+
*/
|
|
14
|
+
export type Numeric = number | bigint;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Any type that can be used where a big number is needed.
|
|
18
|
+
*/
|
|
19
|
+
export type BigNumberish = string | Numeric;
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const BN_0 = BigInt(0);
|
|
23
|
+
const BN_1 = BigInt(1);
|
|
24
|
+
|
|
25
|
+
//const BN_Max256 = (BN_1 << BigInt(256)) - BN_1;
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// IEEE 754 support 53-bits of mantissa
|
|
29
|
+
const maxValue = 0x1fffffffffffff;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Convert %%value%% from a twos-compliment representation of %%width%%
|
|
33
|
+
* bits to its value.
|
|
34
|
+
*
|
|
35
|
+
* If the highest bit is ``1``, the result will be negative.
|
|
36
|
+
*/
|
|
37
|
+
export function fromTwos(_value: BigNumberish, _width: Numeric): bigint {
|
|
38
|
+
const value = getUint(_value, "value");
|
|
39
|
+
const width = BigInt(getNumber(_width, "width"));
|
|
40
|
+
|
|
41
|
+
assert((value >> width) === BN_0, "overflow", "NUMERIC_FAULT", {
|
|
42
|
+
operation: "fromTwos", fault: "overflow", value: _value
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Top bit set; treat as a negative value
|
|
46
|
+
if (value >> (width - BN_1)) {
|
|
47
|
+
const mask = (BN_1 << width) - BN_1;
|
|
48
|
+
return -(((~value) & mask) + BN_1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Convert %%value%% to a twos-compliment representation of
|
|
56
|
+
* %%width%% bits.
|
|
57
|
+
*
|
|
58
|
+
* The result will always be positive.
|
|
59
|
+
*/
|
|
60
|
+
export function toTwos(_value: BigNumberish, _width: Numeric): bigint {
|
|
61
|
+
let value = getBigInt(_value, "value");
|
|
62
|
+
const width = BigInt(getNumber(_width, "width"));
|
|
63
|
+
|
|
64
|
+
const limit = (BN_1 << (width - BN_1));
|
|
65
|
+
|
|
66
|
+
if (value < BN_0) {
|
|
67
|
+
value = -value;
|
|
68
|
+
assert(value <= limit, "too low", "NUMERIC_FAULT", {
|
|
69
|
+
operation: "toTwos", fault: "overflow", value: _value
|
|
70
|
+
});
|
|
71
|
+
const mask = (BN_1 << width) - BN_1;
|
|
72
|
+
return ((~value) & mask) + BN_1;
|
|
73
|
+
} else {
|
|
74
|
+
assert(value < limit, "too high", "NUMERIC_FAULT", {
|
|
75
|
+
operation: "toTwos", fault: "overflow", value: _value
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Mask %%value%% with a bitmask of %%bits%% ones.
|
|
84
|
+
*/
|
|
85
|
+
export function mask(_value: BigNumberish, _bits: Numeric): bigint {
|
|
86
|
+
const value = getUint(_value, "value");
|
|
87
|
+
const bits = BigInt(getNumber(_bits, "bits"));
|
|
88
|
+
return value & ((BN_1 << bits) - BN_1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Gets a BigInt from %%value%%. If it is an invalid value for
|
|
93
|
+
* a BigInt, then an ArgumentError will be thrown for %%name%%.
|
|
94
|
+
*/
|
|
95
|
+
export function getBigInt(value: BigNumberish, name?: string): bigint {
|
|
96
|
+
switch (typeof(value)) {
|
|
97
|
+
case "bigint": return value;
|
|
98
|
+
case "number":
|
|
99
|
+
assertArgument(Number.isInteger(value), "underflow", name || "value", value);
|
|
100
|
+
assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value);
|
|
101
|
+
return BigInt(value);
|
|
102
|
+
case "string":
|
|
103
|
+
try {
|
|
104
|
+
if (value === "") { throw new Error("empty string"); }
|
|
105
|
+
if (value[0] === "-" && value[1] !== "-") {
|
|
106
|
+
return -BigInt(value.substring(1));
|
|
107
|
+
}
|
|
108
|
+
return BigInt(value);
|
|
109
|
+
} catch(e: any) {
|
|
110
|
+
assertArgument(false, `invalid BigNumberish string: ${ e.message }`, name || "value", value);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
assertArgument(false, "invalid BigNumberish value", name || "value", value);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Returns %%value%% as a bigint, validating it is valid as a bigint
|
|
118
|
+
* value and that it is positive.
|
|
119
|
+
*/
|
|
120
|
+
export function getUint(value: BigNumberish, name?: string): bigint {
|
|
121
|
+
const result = getBigInt(value, name);
|
|
122
|
+
assert(result >= BN_0, "unsigned value cannot be negative", "NUMERIC_FAULT", {
|
|
123
|
+
fault: "overflow", operation: "getUint", value
|
|
124
|
+
});
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const Nibbles = "0123456789abcdef";
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* Converts %%value%% to a BigInt. If %%value%% is a Uint8Array, it
|
|
132
|
+
* is treated as Big Endian data.
|
|
133
|
+
*/
|
|
134
|
+
export function toBigInt(value: BigNumberish | Uint8Array): bigint {
|
|
135
|
+
if (value instanceof Uint8Array) {
|
|
136
|
+
let result = "0x0";
|
|
137
|
+
for (const v of value) {
|
|
138
|
+
result += Nibbles[v >> 4];
|
|
139
|
+
result += Nibbles[v & 0x0f];
|
|
140
|
+
}
|
|
141
|
+
return BigInt(result);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return getBigInt(value);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Gets a //number// from %%value%%. If it is an invalid value for
|
|
149
|
+
* a //number//, then an ArgumentError will be thrown for %%name%%.
|
|
150
|
+
*/
|
|
151
|
+
export function getNumber(value: BigNumberish, name?: string): number {
|
|
152
|
+
switch (typeof(value)) {
|
|
153
|
+
case "bigint":
|
|
154
|
+
assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value);
|
|
155
|
+
return Number(value);
|
|
156
|
+
case "number":
|
|
157
|
+
assertArgument(Number.isInteger(value), "underflow", name || "value", value);
|
|
158
|
+
assertArgument(value >= -maxValue && value <= maxValue, "overflow", name || "value", value);
|
|
159
|
+
return value;
|
|
160
|
+
case "string":
|
|
161
|
+
try {
|
|
162
|
+
if (value === "") { throw new Error("empty string"); }
|
|
163
|
+
return getNumber(BigInt(value), name);
|
|
164
|
+
} catch(e: any) {
|
|
165
|
+
assertArgument(false, `invalid numeric string: ${ e.message }`, name || "value", value);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
assertArgument(false, "invalid numeric value", name || "value", value);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Converts %%value%% to a number. If %%value%% is a Uint8Array, it
|
|
174
|
+
* is treated as Big Endian data. Throws if the value is not safe.
|
|
175
|
+
*/
|
|
176
|
+
export function toNumber(value: BigNumberish | Uint8Array): number {
|
|
177
|
+
return getNumber(toBigInt(value));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Converts %%value%% to a Big Endian hexstring, optionally padded to
|
|
182
|
+
* %%width%% bytes.
|
|
183
|
+
*/
|
|
184
|
+
export function toBeHex(_value: BigNumberish, _width?: Numeric): string {
|
|
185
|
+
const value = getUint(_value, "value");
|
|
186
|
+
|
|
187
|
+
let result = value.toString(16);
|
|
188
|
+
|
|
189
|
+
if (_width == null) {
|
|
190
|
+
// Ensure the value is of even length
|
|
191
|
+
if (result.length % 2) { result = "0" + result; }
|
|
192
|
+
} else {
|
|
193
|
+
const width = getNumber(_width, "width");
|
|
194
|
+
assert(width * 2 >= result.length, `value exceeds width (${ width } bytes)`, "NUMERIC_FAULT", {
|
|
195
|
+
operation: "toBeHex",
|
|
196
|
+
fault: "overflow",
|
|
197
|
+
value: _value
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Pad the value to the required width
|
|
201
|
+
while (result.length < (width * 2)) { result = "0" + result; }
|
|
202
|
+
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return "0x" + result;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Converts %%value%% to a Big Endian Uint8Array.
|
|
210
|
+
*/
|
|
211
|
+
export function toBeArray(_value: BigNumberish): Uint8Array {
|
|
212
|
+
const value = getUint(_value, "value");
|
|
213
|
+
|
|
214
|
+
if (value === BN_0) { return new Uint8Array([ ]); }
|
|
215
|
+
|
|
216
|
+
let hex = value.toString(16);
|
|
217
|
+
if (hex.length % 2) { hex = "0" + hex; }
|
|
218
|
+
|
|
219
|
+
const result = new Uint8Array(hex.length / 2);
|
|
220
|
+
for (let i = 0; i < result.length; i++) {
|
|
221
|
+
const offset = i * 2;
|
|
222
|
+
result[i] = parseInt(hex.substring(offset, offset + 2), 16);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Returns a [[HexString]] for %%value%% safe to use as a //Quantity//.
|
|
230
|
+
*
|
|
231
|
+
* A //Quantity// does not have and leading 0 values unless the value is
|
|
232
|
+
* the literal value `0x0`. This is most commonly used for JSSON-RPC
|
|
233
|
+
* numeric values.
|
|
234
|
+
*/
|
|
235
|
+
export function toQuantity(value: BytesLike | BigNumberish): string {
|
|
236
|
+
let result = hexlify(isBytesLike(value) ? value: toBeArray(value)).substring(2);
|
|
237
|
+
while (result.startsWith("0")) { result = result.substring(1); }
|
|
238
|
+
if (result === "") { result = "0"; }
|
|
239
|
+
return "0x" + result;
|
|
240
|
+
}
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Property helper functions.
|
|
3
|
-
*
|
|
4
|
-
* @_subsection api/utils:Properties [about-properties]
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
function checkType(value: any, type: string, name: string): void {
|
|
8
|
-
const types = type.split("|").map(t => t.trim());
|
|
9
|
-
for (let i = 0; i < types.length; i++) {
|
|
10
|
-
switch (type) {
|
|
11
|
-
case "any":
|
|
12
|
-
return;
|
|
13
|
-
case "bigint":
|
|
14
|
-
case "boolean":
|
|
15
|
-
case "number":
|
|
16
|
-
case "string":
|
|
17
|
-
if (typeof(value) === type) { return; }
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const error: any = new Error(`invalid value for type ${ type }`);
|
|
22
|
-
error.code = "INVALID_ARGUMENT";
|
|
23
|
-
error.argument = `value.${ name }`;
|
|
24
|
-
error.value = value;
|
|
25
|
-
|
|
26
|
-
throw error;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Resolves to a new object that is a copy of %%value%%, but with all
|
|
31
|
-
* values resolved.
|
|
32
|
-
*/
|
|
33
|
-
export async function resolveProperties<T>(value: { [ P in keyof T ]: T[P] | Promise<T[P]>}): Promise<T> {
|
|
34
|
-
const keys = Object.keys(value);
|
|
35
|
-
const results = await Promise.all(keys.map((k) => Promise.resolve(value[<keyof T>k])));
|
|
36
|
-
return results.reduce((accum: any, v, index) => {
|
|
37
|
-
accum[keys[index]] = v;
|
|
38
|
-
return accum;
|
|
39
|
-
}, <{ [ P in keyof T]: T[P] }>{ });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Assigns the %%values%% to %%target%% as read-only values.
|
|
44
|
-
*
|
|
45
|
-
* It %%types%% is specified, the values are checked.
|
|
46
|
-
*/
|
|
47
|
-
export function defineProperties<T>(
|
|
48
|
-
target: T,
|
|
49
|
-
values: { [ K in keyof T ]?: T[K] },
|
|
50
|
-
types?: { [ K in keyof T ]?: string }): void {
|
|
51
|
-
|
|
52
|
-
for (let key in values) {
|
|
53
|
-
let value = values[key];
|
|
54
|
-
|
|
55
|
-
const type = (types ? types[key]: null);
|
|
56
|
-
if (type) { checkType(value, type, key); }
|
|
57
|
-
|
|
58
|
-
Object.defineProperty(target, key, { enumerable: true, value, writable: false });
|
|
59
|
-
}
|
|
60
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Property helper functions.
|
|
3
|
+
*
|
|
4
|
+
* @_subsection api/utils:Properties [about-properties]
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
function checkType(value: any, type: string, name: string): void {
|
|
8
|
+
const types = type.split("|").map(t => t.trim());
|
|
9
|
+
for (let i = 0; i < types.length; i++) {
|
|
10
|
+
switch (type) {
|
|
11
|
+
case "any":
|
|
12
|
+
return;
|
|
13
|
+
case "bigint":
|
|
14
|
+
case "boolean":
|
|
15
|
+
case "number":
|
|
16
|
+
case "string":
|
|
17
|
+
if (typeof(value) === type) { return; }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const error: any = new Error(`invalid value for type ${ type }`);
|
|
22
|
+
error.code = "INVALID_ARGUMENT";
|
|
23
|
+
error.argument = `value.${ name }`;
|
|
24
|
+
error.value = value;
|
|
25
|
+
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Resolves to a new object that is a copy of %%value%%, but with all
|
|
31
|
+
* values resolved.
|
|
32
|
+
*/
|
|
33
|
+
export async function resolveProperties<T>(value: { [ P in keyof T ]: T[P] | Promise<T[P]>}): Promise<T> {
|
|
34
|
+
const keys = Object.keys(value);
|
|
35
|
+
const results = await Promise.all(keys.map((k) => Promise.resolve(value[<keyof T>k])));
|
|
36
|
+
return results.reduce((accum: any, v, index) => {
|
|
37
|
+
accum[keys[index]] = v;
|
|
38
|
+
return accum;
|
|
39
|
+
}, <{ [ P in keyof T]: T[P] }>{ });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Assigns the %%values%% to %%target%% as read-only values.
|
|
44
|
+
*
|
|
45
|
+
* It %%types%% is specified, the values are checked.
|
|
46
|
+
*/
|
|
47
|
+
export function defineProperties<T>(
|
|
48
|
+
target: T,
|
|
49
|
+
values: { [ K in keyof T ]?: T[K] },
|
|
50
|
+
types?: { [ K in keyof T ]?: string }): void {
|
|
51
|
+
|
|
52
|
+
for (let key in values) {
|
|
53
|
+
let value = values[key];
|
|
54
|
+
|
|
55
|
+
const type = (types ? types[key]: null);
|
|
56
|
+
if (type) { checkType(value, type, key); }
|
|
57
|
+
|
|
58
|
+
Object.defineProperty(target, key, { enumerable: true, value, writable: false });
|
|
59
|
+
}
|
|
60
|
+
}
|