@bitpay-labs/crypto-wallet-core 11.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/package.json +58 -0
- package/src/constants/address.ts +7 -0
- package/src/constants/algorithms.ts +9 -0
- package/src/constants/chains.ts +64 -0
- package/src/constants/feeMinimums.ts +20 -0
- package/src/constants/index.ts +37 -0
- package/src/constants/tokens.ts +224 -0
- package/src/constants/units.ts +289 -0
- package/src/derivation/arb/index.ts +3 -0
- package/src/derivation/base/index.ts +3 -0
- package/src/derivation/bch/index.ts +11 -0
- package/src/derivation/btc/index.ts +39 -0
- package/src/derivation/doge/index.ts +6 -0
- package/src/derivation/eth/index.ts +58 -0
- package/src/derivation/index.ts +124 -0
- package/src/derivation/ltc/index.ts +6 -0
- package/src/derivation/matic/index.ts +3 -0
- package/src/derivation/op/index.ts +3 -0
- package/src/derivation/paths.ts +42 -0
- package/src/derivation/sol/index.ts +57 -0
- package/src/derivation/xrp/index.ts +38 -0
- package/src/index.ts +48 -0
- package/src/message/btc.ts +28 -0
- package/src/message/eth.ts +47 -0
- package/src/message/index.ts +70 -0
- package/src/message/sol.ts +3 -0
- package/src/message/xrp.ts +4 -0
- package/src/transactions/arb/index.ts +14 -0
- package/src/transactions/base/index.ts +13 -0
- package/src/transactions/bch/index.ts +25 -0
- package/src/transactions/btc/index.ts +227 -0
- package/src/transactions/doge/index.ts +31 -0
- package/src/transactions/erc20/abi.ts +418 -0
- package/src/transactions/erc20/index.ts +57 -0
- package/src/transactions/eth/index.ts +183 -0
- package/src/transactions/eth-multisig/abi.ts +724 -0
- package/src/transactions/eth-multisig/index.ts +172 -0
- package/src/transactions/index.ts +83 -0
- package/src/transactions/ltc/index.ts +31 -0
- package/src/transactions/matic/index.ts +14 -0
- package/src/transactions/matic-multisig/index.ts +172 -0
- package/src/transactions/op/index.ts +14 -0
- package/src/transactions/sol/index.ts +292 -0
- package/src/transactions/spl/index.ts +192 -0
- package/src/transactions/xrp/index.ts +129 -0
- package/src/types/derivation.ts +17 -0
- package/src/types/index.ts +3 -0
- package/src/types/message.ts +15 -0
- package/src/types/utils.ts +1 -0
- package/src/types/validation.ts +4 -0
- package/src/utils/bigint.ts +238 -0
- package/src/utils/index.ts +94 -0
- package/src/validation/arb/index.ts +8 -0
- package/src/validation/base/index.ts +8 -0
- package/src/validation/bch/index.ts +17 -0
- package/src/validation/btc/index.ts +17 -0
- package/src/validation/doge/index.ts +16 -0
- package/src/validation/eth/index.ts +29 -0
- package/src/validation/index.ts +43 -0
- package/src/validation/ltc/index.ts +16 -0
- package/src/validation/matic/index.ts +8 -0
- package/src/validation/op/index.ts +8 -0
- package/src/validation/sol/index.ts +34 -0
- package/src/validation/xrp/index.ts +50 -0
- package/test/address.test.ts +178 -0
- package/test/bigint.test.ts +373 -0
- package/test/transactions.test.ts +1777 -0
- package/test/utils.test.ts +121 -0
- package/test/validation.test.ts +261 -0
- package/ts_build/src/constants/address.d.ts +8 -0
- package/ts_build/src/constants/address.d.ts.map +1 -0
- package/ts_build/src/constants/address.js +11 -0
- package/ts_build/src/constants/address.js.map +1 -0
- package/ts_build/src/constants/algorithms.d.ts +9 -0
- package/ts_build/src/constants/algorithms.d.ts.map +1 -0
- package/ts_build/src/constants/algorithms.js +12 -0
- package/ts_build/src/constants/algorithms.js.map +1 -0
- package/ts_build/src/constants/chains.d.ts +42 -0
- package/ts_build/src/constants/chains.d.ts.map +1 -0
- package/ts_build/src/constants/chains.js +54 -0
- package/ts_build/src/constants/chains.js.map +1 -0
- package/ts_build/src/constants/feeMinimums.d.ts +21 -0
- package/ts_build/src/constants/feeMinimums.d.ts.map +1 -0
- package/ts_build/src/constants/feeMinimums.js +24 -0
- package/ts_build/src/constants/feeMinimums.js.map +1 -0
- package/ts_build/src/constants/index.d.ts +569 -0
- package/ts_build/src/constants/index.d.ts.map +1 -0
- package/ts_build/src/constants/index.js +31 -0
- package/ts_build/src/constants/index.js.map +1 -0
- package/ts_build/src/constants/tokens.d.ts +423 -0
- package/ts_build/src/constants/tokens.d.ts.map +1 -0
- package/ts_build/src/constants/tokens.js +222 -0
- package/ts_build/src/constants/tokens.js.map +1 -0
- package/ts_build/src/constants/units.d.ts +289 -0
- package/ts_build/src/constants/units.d.ts.map +1 -0
- package/ts_build/src/constants/units.js +292 -0
- package/ts_build/src/constants/units.js.map +1 -0
- package/ts_build/src/derivation/arb/index.d.ts +4 -0
- package/ts_build/src/derivation/arb/index.d.ts.map +1 -0
- package/ts_build/src/derivation/arb/index.js +8 -0
- package/ts_build/src/derivation/arb/index.js.map +1 -0
- package/ts_build/src/derivation/base/index.d.ts +4 -0
- package/ts_build/src/derivation/base/index.d.ts.map +1 -0
- package/ts_build/src/derivation/base/index.js +8 -0
- package/ts_build/src/derivation/base/index.js.map +1 -0
- package/ts_build/src/derivation/bch/index.d.ts +6 -0
- package/ts_build/src/derivation/bch/index.d.ts.map +1 -0
- package/ts_build/src/derivation/bch/index.js +17 -0
- package/ts_build/src/derivation/bch/index.js.map +1 -0
- package/ts_build/src/derivation/btc/index.d.ts +23 -0
- package/ts_build/src/derivation/btc/index.d.ts.map +1 -0
- package/ts_build/src/derivation/btc/index.js +40 -0
- package/ts_build/src/derivation/btc/index.js.map +1 -0
- package/ts_build/src/derivation/doge/index.d.ts +5 -0
- package/ts_build/src/derivation/doge/index.d.ts.map +1 -0
- package/ts_build/src/derivation/doge/index.js +13 -0
- package/ts_build/src/derivation/doge/index.js.map +1 -0
- package/ts_build/src/derivation/eth/index.d.ts +21 -0
- package/ts_build/src/derivation/eth/index.d.ts.map +1 -0
- package/ts_build/src/derivation/eth/index.js +58 -0
- package/ts_build/src/derivation/eth/index.js.map +1 -0
- package/ts_build/src/derivation/index.d.ts +12 -0
- package/ts_build/src/derivation/index.d.ts.map +1 -0
- package/ts_build/src/derivation/index.js +68 -0
- package/ts_build/src/derivation/index.js.map +1 -0
- package/ts_build/src/derivation/ltc/index.d.ts +5 -0
- package/ts_build/src/derivation/ltc/index.d.ts.map +1 -0
- package/ts_build/src/derivation/ltc/index.js +13 -0
- package/ts_build/src/derivation/ltc/index.js.map +1 -0
- package/ts_build/src/derivation/matic/index.d.ts +4 -0
- package/ts_build/src/derivation/matic/index.d.ts.map +1 -0
- package/ts_build/src/derivation/matic/index.js +8 -0
- package/ts_build/src/derivation/matic/index.js.map +1 -0
- package/ts_build/src/derivation/op/index.d.ts +4 -0
- package/ts_build/src/derivation/op/index.d.ts.map +1 -0
- package/ts_build/src/derivation/op/index.js +8 -0
- package/ts_build/src/derivation/op/index.js.map +1 -0
- package/ts_build/src/derivation/paths.d.ts +43 -0
- package/ts_build/src/derivation/paths.d.ts.map +1 -0
- package/ts_build/src/derivation/paths.js +46 -0
- package/ts_build/src/derivation/paths.js.map +1 -0
- package/ts_build/src/derivation/sol/index.d.ts +17 -0
- package/ts_build/src/derivation/sol/index.d.ts.map +1 -0
- package/ts_build/src/derivation/sol/index.js +85 -0
- package/ts_build/src/derivation/sol/index.js.map +1 -0
- package/ts_build/src/derivation/xrp/index.d.ts +19 -0
- package/ts_build/src/derivation/xrp/index.d.ts.map +1 -0
- package/ts_build/src/derivation/xrp/index.js +40 -0
- package/ts_build/src/derivation/xrp/index.js.map +1 -0
- package/ts_build/src/index.d.ts +30 -0
- package/ts_build/src/index.d.ts.map +1 -0
- package/ts_build/src/index.js +81 -0
- package/ts_build/src/index.js.map +1 -0
- package/ts_build/src/message/btc.d.ts +19 -0
- package/ts_build/src/message/btc.d.ts.map +1 -0
- package/ts_build/src/message/btc.js +32 -0
- package/ts_build/src/message/btc.js.map +1 -0
- package/ts_build/src/message/eth.d.ts +19 -0
- package/ts_build/src/message/eth.d.ts.map +1 -0
- package/ts_build/src/message/eth.js +39 -0
- package/ts_build/src/message/eth.js.map +1 -0
- package/ts_build/src/message/index.d.ts +25 -0
- package/ts_build/src/message/index.d.ts.map +1 -0
- package/ts_build/src/message/index.js +50 -0
- package/ts_build/src/message/index.js.map +1 -0
- package/ts_build/src/message/sol.d.ts +4 -0
- package/ts_build/src/message/sol.d.ts.map +1 -0
- package/ts_build/src/message/sol.js +9 -0
- package/ts_build/src/message/sol.js.map +1 -0
- package/ts_build/src/message/xrp.d.ts +4 -0
- package/ts_build/src/message/xrp.d.ts.map +1 -0
- package/ts_build/src/message/xrp.js +9 -0
- package/ts_build/src/message/xrp.js.map +1 -0
- package/ts_build/src/transactions/arb/index.d.ts +9 -0
- package/ts_build/src/transactions/arb/index.d.ts.map +1 -0
- package/ts_build/src/transactions/arb/index.js +18 -0
- package/ts_build/src/transactions/arb/index.js.map +1 -0
- package/ts_build/src/transactions/base/index.d.ts +8 -0
- package/ts_build/src/transactions/base/index.d.ts.map +1 -0
- package/ts_build/src/transactions/base/index.js +17 -0
- package/ts_build/src/transactions/base/index.js.map +1 -0
- package/ts_build/src/transactions/bch/index.d.ts +12 -0
- package/ts_build/src/transactions/bch/index.d.ts.map +1 -0
- package/ts_build/src/transactions/bch/index.js +32 -0
- package/ts_build/src/transactions/bch/index.js.map +1 -0
- package/ts_build/src/transactions/btc/index.d.ts +84 -0
- package/ts_build/src/transactions/btc/index.d.ts.map +1 -0
- package/ts_build/src/transactions/btc/index.js +184 -0
- package/ts_build/src/transactions/btc/index.js.map +1 -0
- package/ts_build/src/transactions/doge/index.d.ts +12 -0
- package/ts_build/src/transactions/doge/index.d.ts.map +1 -0
- package/ts_build/src/transactions/doge/index.js +38 -0
- package/ts_build/src/transactions/doge/index.js.map +1 -0
- package/ts_build/src/transactions/erc20/abi.d.ts +78 -0
- package/ts_build/src/transactions/erc20/abi.d.ts.map +1 -0
- package/ts_build/src/transactions/erc20/abi.js +421 -0
- package/ts_build/src/transactions/erc20/abi.js.map +1 -0
- package/ts_build/src/transactions/erc20/index.d.ts +29 -0
- package/ts_build/src/transactions/erc20/index.d.ts.map +1 -0
- package/ts_build/src/transactions/erc20/index.js +46 -0
- package/ts_build/src/transactions/erc20/index.js.map +1 -0
- package/ts_build/src/transactions/eth/index.d.ts +53 -0
- package/ts_build/src/transactions/eth/index.d.ts.map +1 -0
- package/ts_build/src/transactions/eth/index.js +156 -0
- package/ts_build/src/transactions/eth/index.js.map +1 -0
- package/ts_build/src/transactions/eth-multisig/abi.d.ts +51 -0
- package/ts_build/src/transactions/eth-multisig/abi.d.ts.map +1 -0
- package/ts_build/src/transactions/eth-multisig/abi.js +728 -0
- package/ts_build/src/transactions/eth-multisig/abi.js.map +1 -0
- package/ts_build/src/transactions/eth-multisig/index.d.ts +65 -0
- package/ts_build/src/transactions/eth-multisig/index.d.ts.map +1 -0
- package/ts_build/src/transactions/eth-multisig/index.js +143 -0
- package/ts_build/src/transactions/eth-multisig/index.js.map +1 -0
- package/ts_build/src/transactions/index.d.ts +15 -0
- package/ts_build/src/transactions/index.d.ts.map +1 -0
- package/ts_build/src/transactions/index.js +69 -0
- package/ts_build/src/transactions/index.js.map +1 -0
- package/ts_build/src/transactions/ltc/index.d.ts +12 -0
- package/ts_build/src/transactions/ltc/index.d.ts.map +1 -0
- package/ts_build/src/transactions/ltc/index.js +38 -0
- package/ts_build/src/transactions/ltc/index.js.map +1 -0
- package/ts_build/src/transactions/matic/index.d.ts +9 -0
- package/ts_build/src/transactions/matic/index.d.ts.map +1 -0
- package/ts_build/src/transactions/matic/index.js +18 -0
- package/ts_build/src/transactions/matic/index.js.map +1 -0
- package/ts_build/src/transactions/matic-multisig/index.d.ts +65 -0
- package/ts_build/src/transactions/matic-multisig/index.d.ts.map +1 -0
- package/ts_build/src/transactions/matic-multisig/index.js +143 -0
- package/ts_build/src/transactions/matic-multisig/index.js.map +1 -0
- package/ts_build/src/transactions/op/index.d.ts +9 -0
- package/ts_build/src/transactions/op/index.d.ts.map +1 -0
- package/ts_build/src/transactions/op/index.js +18 -0
- package/ts_build/src/transactions/op/index.js.map +1 -0
- package/ts_build/src/transactions/sol/index.d.ts +56 -0
- package/ts_build/src/transactions/sol/index.d.ts.map +1 -0
- package/ts_build/src/transactions/sol/index.js +261 -0
- package/ts_build/src/transactions/sol/index.js.map +1 -0
- package/ts_build/src/transactions/spl/index.d.ts +57 -0
- package/ts_build/src/transactions/spl/index.d.ts.map +1 -0
- package/ts_build/src/transactions/spl/index.js +129 -0
- package/ts_build/src/transactions/spl/index.js.map +1 -0
- package/ts_build/src/transactions/xrp/index.d.ts +50 -0
- package/ts_build/src/transactions/xrp/index.d.ts.map +1 -0
- package/ts_build/src/transactions/xrp/index.js +146 -0
- package/ts_build/src/transactions/xrp/index.js.map +1 -0
- package/ts_build/src/types/derivation.d.ts +13 -0
- package/ts_build/src/types/derivation.d.ts.map +1 -0
- package/ts_build/src/types/derivation.js +3 -0
- package/ts_build/src/types/derivation.js.map +1 -0
- package/ts_build/src/types/index.d.ts +4 -0
- package/ts_build/src/types/index.d.ts.map +1 -0
- package/ts_build/src/types/index.js +40 -0
- package/ts_build/src/types/index.js.map +1 -0
- package/ts_build/src/types/message.d.ts +26 -0
- package/ts_build/src/types/message.d.ts.map +1 -0
- package/ts_build/src/types/message.js +3 -0
- package/ts_build/src/types/message.js.map +1 -0
- package/ts_build/src/types/utils.d.ts +2 -0
- package/ts_build/src/types/utils.d.ts.map +1 -0
- package/ts_build/src/types/utils.js +3 -0
- package/ts_build/src/types/utils.js.map +1 -0
- package/ts_build/src/types/validation.d.ts +5 -0
- package/ts_build/src/types/validation.d.ts.map +1 -0
- package/ts_build/src/types/validation.js +3 -0
- package/ts_build/src/types/validation.js.map +1 -0
- package/ts_build/src/utils/bigint.d.ts +14 -0
- package/ts_build/src/utils/bigint.d.ts.map +1 -0
- package/ts_build/src/utils/bigint.js +165 -0
- package/ts_build/src/utils/bigint.js.map +1 -0
- package/ts_build/src/utils/index.d.ts +12 -0
- package/ts_build/src/utils/index.d.ts.map +1 -0
- package/ts_build/src/utils/index.js +118 -0
- package/ts_build/src/utils/index.js.map +1 -0
- package/ts_build/src/validation/arb/index.d.ts +5 -0
- package/ts_build/src/validation/arb/index.d.ts.map +1 -0
- package/ts_build/src/validation/arb/index.js +12 -0
- package/ts_build/src/validation/arb/index.js.map +1 -0
- package/ts_build/src/validation/base/index.d.ts +5 -0
- package/ts_build/src/validation/base/index.d.ts.map +1 -0
- package/ts_build/src/validation/base/index.js +12 -0
- package/ts_build/src/validation/base/index.js.map +1 -0
- package/ts_build/src/validation/bch/index.d.ts +6 -0
- package/ts_build/src/validation/bch/index.d.ts.map +1 -0
- package/ts_build/src/validation/bch/index.js +19 -0
- package/ts_build/src/validation/bch/index.js.map +1 -0
- package/ts_build/src/validation/btc/index.d.ts +6 -0
- package/ts_build/src/validation/btc/index.d.ts.map +1 -0
- package/ts_build/src/validation/btc/index.js +19 -0
- package/ts_build/src/validation/btc/index.js.map +1 -0
- package/ts_build/src/validation/doge/index.d.ts +6 -0
- package/ts_build/src/validation/doge/index.d.ts.map +1 -0
- package/ts_build/src/validation/doge/index.js +19 -0
- package/ts_build/src/validation/doge/index.js.map +1 -0
- package/ts_build/src/validation/eth/index.d.ts +9 -0
- package/ts_build/src/validation/eth/index.d.ts.map +1 -0
- package/ts_build/src/validation/eth/index.js +28 -0
- package/ts_build/src/validation/eth/index.js.map +1 -0
- package/ts_build/src/validation/index.d.ts +9 -0
- package/ts_build/src/validation/index.d.ts.map +1 -0
- package/ts_build/src/validation/index.js +42 -0
- package/ts_build/src/validation/index.js.map +1 -0
- package/ts_build/src/validation/ltc/index.d.ts +6 -0
- package/ts_build/src/validation/ltc/index.d.ts.map +1 -0
- package/ts_build/src/validation/ltc/index.js +19 -0
- package/ts_build/src/validation/ltc/index.js.map +1 -0
- package/ts_build/src/validation/matic/index.d.ts +5 -0
- package/ts_build/src/validation/matic/index.d.ts.map +1 -0
- package/ts_build/src/validation/matic/index.js +12 -0
- package/ts_build/src/validation/matic/index.js.map +1 -0
- package/ts_build/src/validation/op/index.d.ts +5 -0
- package/ts_build/src/validation/op/index.d.ts.map +1 -0
- package/ts_build/src/validation/op/index.js +12 -0
- package/ts_build/src/validation/op/index.js.map +1 -0
- package/ts_build/src/validation/sol/index.d.ts +9 -0
- package/ts_build/src/validation/sol/index.d.ts.map +1 -0
- package/ts_build/src/validation/sol/index.js +34 -0
- package/ts_build/src/validation/sol/index.js.map +1 -0
- package/ts_build/src/validation/xrp/index.d.ts +7 -0
- package/ts_build/src/validation/xrp/index.d.ts.map +1 -0
- package/ts_build/src/validation/xrp/index.js +50 -0
- package/ts_build/src/validation/xrp/index.js.map +1 -0
- package/ts_build/test/address.test.d.ts +2 -0
- package/ts_build/test/address.test.d.ts.map +1 -0
- package/ts_build/test/address.test.js +139 -0
- package/ts_build/test/address.test.js.map +1 -0
- package/ts_build/test/bigint.test.d.ts +2 -0
- package/ts_build/test/bigint.test.d.ts.map +1 -0
- package/ts_build/test/bigint.test.js +298 -0
- package/ts_build/test/bigint.test.js.map +1 -0
- package/ts_build/test/transactions.test.d.ts +2 -0
- package/ts_build/test/transactions.test.d.ts.map +1 -0
- package/ts_build/test/transactions.test.js +1549 -0
- package/ts_build/test/transactions.test.js.map +1 -0
- package/ts_build/test/utils.test.d.ts +2 -0
- package/ts_build/test/utils.test.d.ts.map +1 -0
- package/ts_build/test/utils.test.js +137 -0
- package/ts_build/test/utils.test.js.map +1 -0
- package/ts_build/test/validation.test.d.ts +2 -0
- package/ts_build/test/validation.test.d.ts.map +1 -0
- package/ts_build/test/validation.test.js +210 -0
- package/ts_build/test/validation.test.js.map +1 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { Deriver } from '../src';
|
|
3
|
+
import { encoding } from '@bitpay-labs/bitcore-lib';
|
|
4
|
+
import { createKeyPairFromPrivateKeyBytes, getAddressDecoder } from '@solana/kit';
|
|
5
|
+
|
|
6
|
+
describe('Address Derivation', () => {
|
|
7
|
+
it('should be able to generate a valid BTC address', () => {
|
|
8
|
+
const xPub = 'xpub6A4DRTebM6TS5f6cUt5YAxKXikhDZd6ANwbyomHhFFkjJApdAwnADvMRfimdaKXf1xk1LTrPuRo7eGCGYARApdytZ18SqoUv6MTYtbCx4Xv';
|
|
9
|
+
// 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
|
|
10
|
+
|
|
11
|
+
const path = Deriver.pathFor('BTC', 'mainnet');
|
|
12
|
+
expect(path).to.equal("m/44'/0'/0'");
|
|
13
|
+
|
|
14
|
+
const address = Deriver.deriveAddress('BTC', 'mainnet', xPub, 0, false);
|
|
15
|
+
const expectedAddress = '1EDh8HXc2jwbyDPLEyDDnYea48f2D8z3ER';
|
|
16
|
+
expect(address).to.equal(expectedAddress);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should be able to generate a valid BCH address', () => {
|
|
20
|
+
const xPub = 'xpub6BM8E3eh3homN5uvHh12P4HQEP1QrXCuCA32JRgTTeR7Y7ChML4iV12TfSVV3KHnCGPB1kkDU26EnDHYqw3Koab2a2GjxHQuUVXE3fvcpek';
|
|
21
|
+
// 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
|
|
22
|
+
|
|
23
|
+
const path = Deriver.pathFor('BCH', 'mainnet');
|
|
24
|
+
expect(path).to.equal("m/44'/145'/0'");
|
|
25
|
+
|
|
26
|
+
const address = Deriver.deriveAddress('BCH', 'mainnet', xPub, 0, false);
|
|
27
|
+
const expectedAddress = 'qzltyqgzd3hvj7fvfdymzk6e47zrrczvzc9y37nnrr';
|
|
28
|
+
expect(address).to.equal(expectedAddress);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should be able to generate a valid DOGE address', () => {
|
|
32
|
+
const xPub = 'tpubDCs1d9wgHkn2LYY8KrWozaUwG7a3XSkiaGtn8isiNWmLhepenSdqgH1JCMwCLW9eeRZW37D14tpBjxvb3rjkMzak4ub1qHyF4tLbQDNXZ8K';
|
|
33
|
+
// 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
|
|
34
|
+
|
|
35
|
+
const path = Deriver.pathFor('DOGE', 'mainnet');
|
|
36
|
+
expect(path).to.equal("m/44'/3'/0'");
|
|
37
|
+
|
|
38
|
+
const address = Deriver.deriveAddress('DOGE', 'mainnet', xPub, 0, false);
|
|
39
|
+
const expectedAddress = 'DQnSpKaUdXYZz8Q4WUBCdaGBSthiAJbWBr';
|
|
40
|
+
expect(address).to.equal(expectedAddress);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should be able to generate a valid LTC address', () => {
|
|
44
|
+
const xPub = 'tpubDCetVaW8PG7S6SuLjRGmmyVA4QtXyXTUsqXySaC4WV44igJ3aJNQgYedjL1FX12a1tDctnh3kRPybKTMUjdi9mzUm4wJF3GNSjf8GwDUHpp';
|
|
45
|
+
// 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
|
|
46
|
+
|
|
47
|
+
const path = Deriver.pathFor('LTC', 'mainnet');
|
|
48
|
+
expect(path).to.equal("m/44'/2'/0'");
|
|
49
|
+
|
|
50
|
+
const address = Deriver.deriveAddress('LTC', 'mainnet', xPub, 0, false);
|
|
51
|
+
const expectedAddress = 'LYgDcZ3oW3aZBhZUyiC84fb99hyUPVxLwB';
|
|
52
|
+
expect(address).to.equal(expectedAddress);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should be able to generate a valid ETH address', () => {
|
|
56
|
+
const xPub = 'xpub6D8rChqkgFuaZULuq2n6VrS4zB5Cmv24gcRc889dFRRgYAH1CGQmQZ9kcPfMAfWGPnyMd1X5foBYFmJ5ZPfvwhm6tXjaY13ao1rQHRtkKDv';
|
|
57
|
+
// 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
|
|
58
|
+
|
|
59
|
+
const path = Deriver.pathFor('ETH', 'mainnet');
|
|
60
|
+
expect(path).to.equal("m/44'/60'/0'");
|
|
61
|
+
|
|
62
|
+
const address = Deriver.deriveAddress('ETH', 'mainnet', xPub, 0, false);
|
|
63
|
+
const expectedAddress = '0x9dbfE221A6EEa27a0e2f52961B339e95426931F9';
|
|
64
|
+
expect(address).to.equal(expectedAddress);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should be able to generate a valid XRP address', () => {
|
|
68
|
+
const xPub = 'xpub6J8BBe8QHMMiVQK1F8hLpRKnmkwNTa7tkg753KWjkafzcxfWVFBMkpbPjfY9Fz4bgSvn6jiUYg1ivpeF5HjE6jvrdHm6Se7HKgEAjPFGFfr';
|
|
69
|
+
// 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
|
|
70
|
+
|
|
71
|
+
const path = Deriver.pathFor('XRP', 'mainnet');
|
|
72
|
+
expect(path).to.equal("m/44'/144'/0'");
|
|
73
|
+
|
|
74
|
+
const address = Deriver.deriveAddress('XRP', 'mainnet', xPub, 0, false);
|
|
75
|
+
const expectedAddress = 'r9dmAJBfBe7JL2RRLiFWGJ8kM4CHEeTpgN';
|
|
76
|
+
expect(address).to.equal(expectedAddress);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should be able to generate a valid MATIC address', () => {
|
|
80
|
+
const xPub = 'xpub6D8rChqkgFuaZULuq2n6VrS4zB5Cmv24gcRc889dFRRgYAH1CGQmQZ9kcPfMAfWGPnyMd1X5foBYFmJ5ZPfvwhm6tXjaY13ao1rQHRtkKDv';
|
|
81
|
+
// 'select scout crash enforce riot rival spring whale hollow radar rule sentence';
|
|
82
|
+
|
|
83
|
+
const path = Deriver.pathFor('MATIC', 'mainnet');
|
|
84
|
+
expect(path).to.equal("m/44'/60'/0'");
|
|
85
|
+
|
|
86
|
+
const address = Deriver.deriveAddress('MATIC', 'mainnet', xPub, 0, false);
|
|
87
|
+
const expectedAddress = '0x9dbfE221A6EEa27a0e2f52961B339e95426931F9';
|
|
88
|
+
expect(address).to.equal(expectedAddress);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should be able to generate a valid ETH address, privKey, pubKey', () => {
|
|
92
|
+
const privKey = 'xprv9ypBjKErGMqCdzd44hfSdy1Vk6PGtU3si8ogZcow7rA23HTxMi9XfT99EKmiNdLMr9BAZ9S8ZKCYfN1eCmzYSmXYHje1jnYQseV1VJDDfdS';
|
|
93
|
+
|
|
94
|
+
const path = Deriver.pathFor('ETH', 'mainnet');
|
|
95
|
+
expect(path).to.equal("m/44'/60'/0'");
|
|
96
|
+
|
|
97
|
+
const result = Deriver.derivePrivateKey('ETH', 'mainnet', privKey, 0, false);
|
|
98
|
+
const expectedResult = {
|
|
99
|
+
address: '0xb497281830dE4F19a3482AbF3D5C35c514e6fB36',
|
|
100
|
+
privKey: '62b8311c71f355c5c07f6bffe9b1ae60aa20d90e2e2ec93ec11b6014b2ae6340',
|
|
101
|
+
pubKey: '0386d153aad9395924631dbc78fa560107123a759eaa3e105958248c60cd4472ad'
|
|
102
|
+
};
|
|
103
|
+
expect(result.address).to.equal(expectedResult.address);
|
|
104
|
+
expect(result.privKey).to.equal(expectedResult.privKey);
|
|
105
|
+
expect(result.pubKey).to.equal(expectedResult.pubKey);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should be able to generate a valid XRP address, privKey, pubKey', () => {
|
|
109
|
+
const privKey = 'xprvA58pn8bWSyoRGvEY97ALTHP4Dj6t47Q3PTBUEw78CF91kALMwhs7D2GutQSvpRN6ACR4RX4HbF3KmF7zDf48gR8nwG7DqLp6ezUcMiPHDtV';
|
|
110
|
+
|
|
111
|
+
const path = Deriver.pathFor('XRP', 'mainnet');
|
|
112
|
+
expect(path).to.equal("m/44'/144'/0'");
|
|
113
|
+
|
|
114
|
+
const result = Deriver.derivePrivateKey('XRP', 'mainnet', privKey, 0, false);
|
|
115
|
+
const expectedResult = {
|
|
116
|
+
address: 'r9dmAJBfBe7JL2RRLiFWGJ8kM4CHEeTpgN',
|
|
117
|
+
privKey: 'd02c6801d8f328ff2ead51d01f9580af36c8d74e2bd463963ac4adbe51ae5f2c',
|
|
118
|
+
pubKey: '03dbeec5e9e76da09c5b502a67136bc2d73423e8902a7c35a8cbc0c5a6ac0469e8'
|
|
119
|
+
};
|
|
120
|
+
expect(result.address).to.equal(expectedResult.address);
|
|
121
|
+
expect(result.privKey).to.equal(expectedResult.privKey.toUpperCase());
|
|
122
|
+
expect(result.pubKey).to.equal(expectedResult.pubKey.toUpperCase());
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should be able to get a valid XRP address from pubKey', () => {
|
|
126
|
+
const pubKey = '03dbeec5e9e76da09c5b502a67136bc2d73423e8902a7c35a8cbc0c5a6ac0469e8';
|
|
127
|
+
const result = Deriver.getAddress('XRP', 'mainnet', pubKey, 'asd');
|
|
128
|
+
const expectedResult = 'r9dmAJBfBe7JL2RRLiFWGJ8kM4CHEeTpgN';
|
|
129
|
+
expect(result).to.equal(expectedResult);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should be able to generate a valid MATIC address, privKey, pubKey', () => {
|
|
133
|
+
const privKey = 'xprv9ypBjKErGMqCdzd44hfSdy1Vk6PGtU3si8ogZcow7rA23HTxMi9XfT99EKmiNdLMr9BAZ9S8ZKCYfN1eCmzYSmXYHje1jnYQseV1VJDDfdS';
|
|
134
|
+
|
|
135
|
+
const path = Deriver.pathFor('MATIC', 'mainnet');
|
|
136
|
+
expect(path).to.equal("m/44'/60'/0'");
|
|
137
|
+
|
|
138
|
+
const result = Deriver.derivePrivateKey('MATIC', 'mainnet', privKey, 0, false);
|
|
139
|
+
const expectedResult = {
|
|
140
|
+
address: '0xb497281830dE4F19a3482AbF3D5C35c514e6fB36',
|
|
141
|
+
privKey: '62b8311c71f355c5c07f6bffe9b1ae60aa20d90e2e2ec93ec11b6014b2ae6340',
|
|
142
|
+
pubKey: '0386d153aad9395924631dbc78fa560107123a759eaa3e105958248c60cd4472ad'
|
|
143
|
+
};
|
|
144
|
+
expect(result.address).to.equal(expectedResult.address);
|
|
145
|
+
expect(result.privKey).to.equal(expectedResult.privKey);
|
|
146
|
+
expect(result.pubKey).to.equal(expectedResult.pubKey);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should be able to generate a valid SOL address, privKey', async () => {
|
|
150
|
+
// crush desk brain index action subject tackle idea trim unveil lawn live
|
|
151
|
+
const privKey = 'xprv9s21ZrQH143K3aKdQ6kXF1vj7R6LtkoLCiUXfM5bdbGXmhQkC1iXdnFfrxAAtaTunPUCCLwUQ3cpNixGLMbLAH1gzeCr8VZDe4gPgmKLb2X';
|
|
152
|
+
const result = Deriver.derivePrivateKey('SOL', 'mainnet', privKey, 0, false);
|
|
153
|
+
const expectedResult = {
|
|
154
|
+
address: '7EWwMxKQa5Gru7oTcS1Wi3AaEgTfA6MU3z7MaLUT6hnD',
|
|
155
|
+
privKey: 'E4Tp4nTgMCa5dtGwqvkWoMGrJC7FKRNjcpeFFXi4nNb9',
|
|
156
|
+
pubKey: '5c9c85b20525ee81d3cc56da1f8307ec169086ae41458c5458519aced7683b66'
|
|
157
|
+
};
|
|
158
|
+
expect(result.address).to.equal(expectedResult.address);
|
|
159
|
+
expect(result.privKey).to.equal(expectedResult.privKey);
|
|
160
|
+
expect(result.pubKey).to.equal(expectedResult.pubKey);
|
|
161
|
+
const a = Deriver.getAddress('SOL', 'mainnet', result.pubKey);
|
|
162
|
+
expect(a).to.equal(expectedResult.address);
|
|
163
|
+
const keypair = await createKeyPairFromPrivateKeyBytes(encoding.Base58.decode(result.privKey), true);
|
|
164
|
+
const publicKeyBytes = await crypto.subtle.exportKey('raw', keypair.publicKey);
|
|
165
|
+
const publicKey = Buffer.from(publicKeyBytes).toString('hex');
|
|
166
|
+
expect(result.pubKey).to.equal(publicKey);
|
|
167
|
+
expect(result.address).to.equal(getAddressDecoder().decode(new Uint8Array(publicKeyBytes)));
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should get SOL address from public key string', () => {
|
|
171
|
+
const expectedResult = '7EWwMxKQa5Gru7oTcS1Wi3AaEgTfA6MU3z7MaLUT6hnD';
|
|
172
|
+
// public key with zero byte padding
|
|
173
|
+
const a1 = Deriver.getAddress('SOL', 'mainnet', '005c9c85b20525ee81d3cc56da1f8307ec169086ae41458c5458519aced7683b66');
|
|
174
|
+
expect(a1).to.equal(expectedResult);
|
|
175
|
+
const a2 = Deriver.getAddress('SOL', 'mainnet', '5c9c85b20525ee81d3cc56da1f8307ec169086ae41458c5458519aced7683b66');
|
|
176
|
+
expect(a2).to.equal(expectedResult);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import { BI } from '../src/utils';
|
|
3
|
+
|
|
4
|
+
describe('BigInt', function() {
|
|
5
|
+
describe('isBigIntLike', function() {
|
|
6
|
+
it('should return true for a number', function() {
|
|
7
|
+
expect(BI.isBigIntLike(123)).to.be.true;
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should return true for a bigint', function() {
|
|
11
|
+
expect(BI.isBigIntLike(123n)).to.be.true;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should return true for a numeric string', function() {
|
|
15
|
+
expect(BI.isBigIntLike('123')).to.be.true;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should return true for a numeric hex string', function() {
|
|
19
|
+
expect(BI.isBigIntLike('0x123')).to.be.true;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should return false for a non-numeric string', function() {
|
|
23
|
+
// Notice that `abc` are valid hex characters and sToBigInt('abc') => 2748n.
|
|
24
|
+
// However, `abc` is not a valid number if it's desired to be treated as hex,
|
|
25
|
+
// you should call sToBigInt('abc') first OR append a '0x' hex prefix.
|
|
26
|
+
expect(BI.isBigIntLike('abc')).to.be.false;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return false for an empty string', function() {
|
|
30
|
+
expect(BI.isBigIntLike('')).to.be.false;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should return false for a null value', function() {
|
|
34
|
+
expect(BI.isBigIntLike(null)).to.be.false;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return false for a NaN value', function() {
|
|
38
|
+
expect(BI.isBigIntLike(NaN)).to.be.false;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should return false for undefined value', function() {
|
|
42
|
+
expect(BI.isBigIntLike(undefined)).to.be.false;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should return false for a totally off the wall value', function() {
|
|
46
|
+
expect(BI.isBigIntLike({})).to.be.false;
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('sToBigInt', function() {
|
|
51
|
+
it('should convert a number to bigint', function() {
|
|
52
|
+
expect(BI.sToBigInt(123)).to.equal(123n);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should convert a 0x-prefix hex string to bigint', function() {
|
|
56
|
+
expect(BI.sToBigInt('0x12')).to.equal(18n);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should convert a 0o-prefix octal string to bigint', function() {
|
|
60
|
+
expect(BI.sToBigInt('0o32')).to.equal(26n);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should treat a non-prefix string as hex', function() {
|
|
64
|
+
expect(BI.sToBigInt('12')).to.equal(18n);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should throw on an invalid hex string', function() {
|
|
68
|
+
expect(() => BI.sToBigInt('invalid')).to.throw('Cannot convert 0xinvalid to a BigInt');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should throw on an empty string', function() {
|
|
72
|
+
expect(() => BI.sToBigInt('')).to.throw('Cannot convert 0x to a BigInt');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('max', function() {
|
|
77
|
+
it('should return the maximum bigint from an array of bigints', function() {
|
|
78
|
+
expect(BI.max([1n, 2n, 3n, 2n, 1n])).to.equal(3n);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should return the maximum bigint from an array of mixed types', function() {
|
|
82
|
+
expect(BI.max([1n, 2, '3', 2n, '1'])).to.equal('3');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should return undefined for an empty array', function() {
|
|
86
|
+
expect(BI.max([])).to.be.undefined;
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should throw on a null input', function() {
|
|
90
|
+
expect(() => BI.max(null)).to.throw('Input must be an array');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should throw on an array of invalid types', function() {
|
|
94
|
+
expect(() => BI.max(['x', 'y', 'z'])).to.throw('Array must contain only BigInt-like values');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should throw on an array with NaN, null, undefined, empty string', function() {
|
|
98
|
+
expect(() => BI.max([1n, 2, '3', 2n, '1', NaN])).to.throw('Array must contain only BigInt-like values');
|
|
99
|
+
expect(() => BI.max([1n, 2, '3', 2n, '1', null])).to.throw('Array must contain only BigInt-like values');
|
|
100
|
+
expect(() => BI.max([1n, 2, '3', 2n, '1', undefined])).to.throw('Array must contain only BigInt-like values');
|
|
101
|
+
expect(() => BI.max([1n, 2, '3', 2n, '1', ''])).to.throw('Array must contain only BigInt-like values');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('min', function() {
|
|
106
|
+
it('should return the minimum bigint from an array of bigints', function() {
|
|
107
|
+
expect(BI.min([1n, 2n, 3n, 2n, 1n])).to.equal(1n);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should return the minimum bigint from an array of mixed types', function() {
|
|
111
|
+
expect(BI.min([1n, 2, '3', 2n, '1'])).to.equal(1n);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should return undefined for an empty array', function() {
|
|
115
|
+
expect(BI.min([])).to.be.undefined;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should throw on a null input', function() {
|
|
119
|
+
expect(() => BI.min(null)).to.throw('Input must be an array');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should throw on an array of invalid types', function() {
|
|
123
|
+
expect(() => BI.min(['x', 'y', 'z'])).to.throw('Array must contain only BigInt-like values');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('should throw on an array with NaN, null, undefined, empty string', function() {
|
|
127
|
+
expect(() => BI.min([1n, 2, '3', 2n, '1', NaN])).to.throw('Array must contain only BigInt-like values');
|
|
128
|
+
expect(() => BI.min([1n, 2, '3', 2n, '1', null])).to.throw('Array must contain only BigInt-like values');
|
|
129
|
+
expect(() => BI.min([1n, 2, '3', 2n, '1', undefined])).to.throw('Array must contain only BigInt-like values');
|
|
130
|
+
expect(() => BI.min([1n, 2, '3', 2n, '1', ''])).to.throw('Array must contain only BigInt-like values');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('divToFloat', function() {
|
|
135
|
+
it('should return a number', function() {
|
|
136
|
+
expect(BI.divToFloat(1n, 3n)).to.be.a('number');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should convert a bigint to a float', function() {
|
|
140
|
+
expect(BI.divToFloat(1n, 3n).toString()).to.equal('0.3333333333333333');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should throw on division by zero', function() {
|
|
144
|
+
expect(() => BI.divToFloat(1n, 0n)).to.throw('Division by zero');
|
|
145
|
+
expect(() => BI.divToFloat(1n, 0)).to.throw('Division by zero');
|
|
146
|
+
expect(() => BI.divToFloat(1n, '0')).to.throw('Division by zero');
|
|
147
|
+
expect(() => BI.divToFloat(1n, '0x0')).to.throw('Division by zero');
|
|
148
|
+
expect(() => BI.divToFloat(1n, '0o0')).to.throw('Division by zero');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should throw on invalid numerator', function() {
|
|
152
|
+
expect(() => BI.divToFloat('', 3n)).to.throw('Invalid numerator');
|
|
153
|
+
expect(() => BI.divToFloat(null, 3n)).to.throw('Invalid numerator');
|
|
154
|
+
expect(() => BI.divToFloat(undefined, 3n)).to.throw('Invalid numerator');
|
|
155
|
+
expect(() => BI.divToFloat('invalid', 3n)).to.throw('Invalid numerator');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should throw on invalid denominator', function() {
|
|
159
|
+
expect(() => BI.divToFloat(1n, '')).to.throw('Invalid denominator');
|
|
160
|
+
expect(() => BI.divToFloat(1n, null)).to.throw('Invalid denominator');
|
|
161
|
+
expect(() => BI.divToFloat(1n, undefined)).to.throw('Invalid denominator');
|
|
162
|
+
expect(() => BI.divToFloat(1n, 'invalid')).to.throw('Invalid denominator');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('should convert a bigint to a float with specified precision', function() {
|
|
166
|
+
expect(BI.divToFloat(1n, 3n, 5).toString()).to.equal('0.33333');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('should maintain large float precision', function() {
|
|
170
|
+
// Note that numerator / denominator with Numbers & strings != result
|
|
171
|
+
// e.g. 12.4444444444444444444444444445 / 2 => 6.222222222222222
|
|
172
|
+
// Thus, this test case tests that divToFloat() maintains precision
|
|
173
|
+
const numerator = '12.4444444444444444444444444445'; // 28 decimals
|
|
174
|
+
const denominator = 2;
|
|
175
|
+
const result = 6.222222222222223; // the 3 at the end is important
|
|
176
|
+
expect(BI.divToFloat(numerator, denominator)).to.equal(result);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('div', function() {
|
|
181
|
+
it('should return a bigint', function() {
|
|
182
|
+
expect(BI.div(10n, 5n)).to.be.a('bigint');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should return a divided bigint', function() {
|
|
186
|
+
expect(BI.div(10n, 5n)).to.equal(2n);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('should return a rounded bigint', function() {
|
|
190
|
+
expect(BI.div(1n, 2n)).to.equal(1n); // 1 / 2 = 0.5 => 1
|
|
191
|
+
expect(BI.div(1n, 3n)).to.equal(0n); // 1 / 3 = 0.333... => 0
|
|
192
|
+
expect(BI.div(10n, 3n)).to.equal(3n); // 10 / 3 = 3.333... => 3
|
|
193
|
+
expect(BI.div(20n, 3n)).to.equal(7n); // 20 / 3 = 6.666... => 7
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should throw on division by zero', function() {
|
|
197
|
+
expect(() => BI.div(1n, 0n)).to.throw('Division by zero');
|
|
198
|
+
expect(() => BI.divToFloat(1n, 0)).to.throw('Division by zero');
|
|
199
|
+
expect(() => BI.divToFloat(1n, '0')).to.throw('Division by zero');
|
|
200
|
+
expect(() => BI.divToFloat(1n, '0x0')).to.throw('Division by zero');
|
|
201
|
+
expect(() => BI.divToFloat(1n, '0o0')).to.throw('Division by zero');
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('should throw on invalid numerator', function() {
|
|
205
|
+
expect(() => BI.divToFloat('', 3n)).to.throw('Invalid numerator');
|
|
206
|
+
expect(() => BI.divToFloat(null, 3n)).to.throw('Invalid numerator');
|
|
207
|
+
expect(() => BI.divToFloat(undefined, 3n)).to.throw('Invalid numerator');
|
|
208
|
+
expect(() => BI.divToFloat('invalid', 3n)).to.throw('Invalid numerator');
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('should throw on invalid denominator', function() {
|
|
212
|
+
expect(() => BI.divToFloat(1n, '')).to.throw('Invalid denominator');
|
|
213
|
+
expect(() => BI.divToFloat(1n, null)).to.throw('Invalid denominator');
|
|
214
|
+
expect(() => BI.divToFloat(1n, undefined)).to.throw('Invalid denominator');
|
|
215
|
+
expect(() => BI.divToFloat(1n, 'invalid')).to.throw('Invalid denominator');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should maintain large number precision', function() {
|
|
219
|
+
// Note that numerator / denominator with Numbers != result
|
|
220
|
+
// e.g. BigInt(123456789012345678901234567890 / 2) => 61728394506172838938859798528n
|
|
221
|
+
// Thus, this test case tests that div() maintains precision
|
|
222
|
+
const numerator = 123456789012345678901234567890n; // 30 digits
|
|
223
|
+
const denominator = 2n;
|
|
224
|
+
const result = 61728394506172839450617283945n; // 29 digits
|
|
225
|
+
expect(BI.div(numerator, denominator)).to.equal(result);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should maintain large number precision with string inputs', function() {
|
|
229
|
+
// Note that numerator / denominator with Numbers != result
|
|
230
|
+
// e.g. BigInt(123456789012345678901234567890 / 2) => 61728394506172838938859798528n
|
|
231
|
+
// Thus, this test case tests that div() maintains precision
|
|
232
|
+
const numerator = '123456789012345678901234567890'; //
|
|
233
|
+
const denominator = '2';
|
|
234
|
+
const result = 61728394506172839450617283945n; // 29 digits
|
|
235
|
+
expect(BI.div(numerator, denominator)).to.equal(result);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe('mul', function() {
|
|
240
|
+
it('should return a bigint', function() {
|
|
241
|
+
expect(BI.mul(2n, 3n)).to.be.a('bigint');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should return the product of two bigints', function() {
|
|
245
|
+
expect(BI.mul(2n, 3n)).to.equal(6n);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should return the product of a bigint and a number', function() {
|
|
249
|
+
expect(BI.mul(2n, 3)).to.equal(6n);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('should return the product of a bigint and a numeric string', function() {
|
|
253
|
+
expect(BI.mul(2n, '3')).to.equal(6n);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should throw on invalid inputs', function() {
|
|
257
|
+
expect(() => BI.mul()).to.throw('Input must contain only BigInt-like values');
|
|
258
|
+
expect(() => BI.mul(2n, null)).to.throw('Input must contain only BigInt-like values');
|
|
259
|
+
expect(() => BI.mul(2n, undefined)).to.throw('Input must contain only BigInt-like values');
|
|
260
|
+
expect(() => BI.mul(2n, 'x')).to.throw('Input must contain only BigInt-like values');
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('should return the rounded product of a bigint and a float', function() {
|
|
264
|
+
expect(BI.mul(2n, 1.4)).to.equal(3n); // 2 * 1.4 = 2.8 => 3
|
|
265
|
+
expect(BI.mul(2n, 1.1)).to.equal(2n); // 2 * 1.1 = 2.2 => 2
|
|
266
|
+
expect(BI.mul(2n, 1.4, 3n)).to.equal(8n); // 2 * 1.4 * 3 = 8.4 => 8
|
|
267
|
+
expect(BI.mul(2n, 1.1, 3n)).to.equal(7n); // 2 * 1.1 * 3 = 6.6 => 7
|
|
268
|
+
expect(BI.mul(2n, 1.223)).to.equal(2n); // 2 * 1.223 = 2.446 => 2 (rounds to first decimal)
|
|
269
|
+
expect(BI.mul(2n, 1.25)).to.equal(3n); // 2 * 1.25 = 2.5 => 3
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('mulFloor', function() {
|
|
274
|
+
it('should return a bigint', function() {
|
|
275
|
+
expect(BI.mulFloor(2n, 3n)).to.be.a('bigint');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('should return the product of two bigints', function() {
|
|
279
|
+
expect(BI.mulFloor(2n, 3n)).to.equal(6n);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should return the product of a bigint and a number', function() {
|
|
283
|
+
expect(BI.mulFloor(2n, 3)).to.equal(6n);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should return the product of a bigint and a numeric string', function() {
|
|
287
|
+
expect(BI.mulFloor(2n, '3')).to.equal(6n);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('should throw on invalid inputs', function() {
|
|
291
|
+
expect(() => BI.mulFloor()).to.throw('Input must contain only BigInt-like values');
|
|
292
|
+
expect(() => BI.mulFloor(2n, null)).to.throw('Input must contain only BigInt-like values');
|
|
293
|
+
expect(() => BI.mulFloor(2n, undefined)).to.throw('Input must contain only BigInt-like values');
|
|
294
|
+
expect(() => BI.mulFloor(2n, 'x')).to.throw('Input must contain only BigInt-like values');
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should return the floored product of a bigint and a float', function() {
|
|
298
|
+
expect(BI.mulFloor(2n, 1.4)).to.equal(2n); // 2 * 1.4 = 2.8 => 2
|
|
299
|
+
expect(BI.mulFloor(2n, 1.1)).to.equal(2n); // 2 * 1.1 = 2.2 => 2
|
|
300
|
+
expect(BI.mulFloor(2n, 1.4, 3n)).to.equal(8n); // 2 * 1.4 * 3 = 8.4 => 8
|
|
301
|
+
expect(BI.mulFloor(2n, 1.1, 3n)).to.equal(6n); // 2 * 1.1 * 3 = 6.6 => 6
|
|
302
|
+
expect(BI.mulFloor(2n, 1.223)).to.equal(2n); // 2 * 1.223 = 2.446 => 2
|
|
303
|
+
expect(BI.mulFloor(2n, 1.25)).to.equal(2n); // 2 * 1.25 = 2.5 => 2
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
describe('mulCeil', function() {
|
|
308
|
+
it('should return a bigint', function() {
|
|
309
|
+
expect(BI.mulCeil(2n, 3n)).to.be.a('bigint');
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('should return the product of two bigints', function() {
|
|
313
|
+
expect(BI.mulCeil(2n, 3n)).to.equal(6n);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('should return the product of a bigint and a number', function() {
|
|
317
|
+
expect(BI.mulCeil(2n, 3)).to.equal(6n);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it('should return the product of a bigint and a numeric string', function() {
|
|
321
|
+
expect(BI.mulCeil(2n, '3')).to.equal(6n);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('should throw on invalid inputs', function() {
|
|
325
|
+
expect(() => BI.mulCeil()).to.throw('Input must contain only BigInt-like values');
|
|
326
|
+
expect(() => BI.mulCeil(2n, null)).to.throw('Input must contain only BigInt-like values');
|
|
327
|
+
expect(() => BI.mulCeil(2n, undefined)).to.throw('Input must contain only BigInt-like values');
|
|
328
|
+
expect(() => BI.mulCeil(2n, 'x')).to.throw('Input must contain only BigInt-like values');
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('should return the ceiling of the product of a bigint and a float', function() {
|
|
332
|
+
expect(BI.mulCeil(2n, 1.4)).to.equal(3n); // 2 * 1.4 = 2.8 => 3
|
|
333
|
+
expect(BI.mulCeil(2n, 1.1)).to.equal(3n); // 2 * 1.1 = 2.2 => 3
|
|
334
|
+
expect(BI.mulCeil(2n, 1.4, 3n)).to.equal(9n); // 2 * 1.4 * 3 = 8.4 => 9
|
|
335
|
+
expect(BI.mulCeil(2n, 1.1, 3n)).to.equal(7n); // 2 * 1.1 * 3 = 6.6 => 7
|
|
336
|
+
expect(BI.mulCeil(2n, 1.223)).to.equal(3n); // 2 * 1.223 = 2.446 => 3
|
|
337
|
+
expect(BI.mulCeil(2n, 1.25)).to.equal(3n); // 2 * 1.25 = 2.5 => 3
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
describe('JSONStringifyBigIntReplacer', function() {
|
|
342
|
+
it('should convert bigint values to serializable objects during JSON.stringify', function() {
|
|
343
|
+
const obj = { a: 123n, b: 'test' };
|
|
344
|
+
const jsonString = JSON.stringify(obj, BI.JSONStringifyBigIntReplacer);
|
|
345
|
+
expect(jsonString).to.equal('{"a":{"data":"123","type":"BigInt"},"b":"test"}');
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe('JSONParseBigIntReviver', function() {
|
|
350
|
+
it('should convert bigint values from serializable objects during JSON.parse', function() {
|
|
351
|
+
const str = '{"a":{"data":"123","type":"BigInt"},"b":"test"}';
|
|
352
|
+
const jsonObject = JSON.parse(str, BI.JSONParseBigIntReviver);
|
|
353
|
+
expect(jsonObject).to.deep.equal({ a: 123n, b: 'test' });
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
describe('scrubBigIntsInObject', function() {
|
|
358
|
+
it('should convert all bigint values in an object to number by default', function() {
|
|
359
|
+
const result = BI.scrubBigIntsInObject({ a: 123n, b: { c: 456n, d: 'test' }, e: [789n, 'value'] });
|
|
360
|
+
expect(result).to.deep.equal({ a: 123, b: { c: 456, d: 'test' }, e: [789, 'value'] });
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it('should convert all bigint values in an object to string when destType is "string"', function() {
|
|
364
|
+
const result = BI.scrubBigIntsInObject({ a: 123n, b: { c: 456n, d: 'test' }, e: [789n, 'value'] }, 'string');
|
|
365
|
+
expect(result).to.deep.equal({ a: '123', b: { c: '456', d: 'test' }, e: ['789', 'value'] });
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it('should convert all bigint values in an object to hex string when destType is "hex"', function() {
|
|
369
|
+
const result = BI.scrubBigIntsInObject({ a: 123n, b: { c: 456n, d: 'test' }, e: [789n, 'value'] }, 'hex');
|
|
370
|
+
expect(result).to.deep.equal({ a: '0x7b', b: { c: '0x1c8', d: 'test' }, e: ['0x315', 'value'] });
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
});
|