tronwb 6.0.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/LICENSE +21 -0
- package/README.md +201 -0
- package/dist/TronWeb.js +3 -0
- package/dist/TronWeb.js.LICENSE.txt +12 -0
- package/dist/TronWeb.js.map +1 -0
- package/dist/TronWeb.node.js +2 -0
- package/dist/TronWeb.node.js.map +1 -0
- package/kj9btptx.cjs +1 -0
- package/lib/commonjs/index.d.ts +64 -0
- package/lib/commonjs/index.js +37 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
- package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js +1771 -0
- package/lib/commonjs/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
- package/lib/commonjs/lib/TransactionBuilder/helper.d.ts +28 -0
- package/lib/commonjs/lib/TransactionBuilder/helper.js +122 -0
- package/lib/commonjs/lib/TransactionBuilder/helper.js.map +1 -0
- package/lib/commonjs/lib/contract/index.d.ts +29 -0
- package/lib/commonjs/lib/contract/index.js +124 -0
- package/lib/commonjs/lib/contract/index.js.map +1 -0
- package/lib/commonjs/lib/contract/method.d.ts +50 -0
- package/lib/commonjs/lib/contract/method.js +238 -0
- package/lib/commonjs/lib/contract/method.js.map +1 -0
- package/lib/commonjs/lib/event.d.ts +85 -0
- package/lib/commonjs/lib/event.js +137 -0
- package/lib/commonjs/lib/event.js.map +1 -0
- package/lib/commonjs/lib/plugin.d.ts +28 -0
- package/lib/commonjs/lib/plugin.js +84 -0
- package/lib/commonjs/lib/plugin.js.map +1 -0
- package/lib/commonjs/lib/providers/HttpProvider.d.ts +15 -0
- package/lib/commonjs/lib/providers/HttpProvider.js +63 -0
- package/lib/commonjs/lib/providers/HttpProvider.js.map +1 -0
- package/lib/commonjs/lib/providers/index.d.ts +6 -0
- package/lib/commonjs/lib/providers/index.js +8 -0
- package/lib/commonjs/lib/providers/index.js.map +1 -0
- package/lib/commonjs/lib/trx.d.ts +315 -0
- package/lib/commonjs/lib/trx.js +965 -0
- package/lib/commonjs/lib/trx.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/paramValidator/index.d.ts +19 -0
- package/lib/commonjs/paramValidator/index.js +110 -0
- package/lib/commonjs/paramValidator/index.js.map +1 -0
- package/lib/commonjs/protocol/core/Tron_pb.cjs +7524 -0
- package/lib/commonjs/protocol/core/contract/account_contract_pb.cjs +961 -0
- package/lib/commonjs/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
- package/lib/commonjs/protocol/core/contract/balance_contract_pb.cjs +3826 -0
- package/lib/commonjs/protocol/core/contract/common_pb.cjs +34 -0
- package/lib/commonjs/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
- package/lib/commonjs/protocol/core/contract/proposal_contract_pb.cjs +675 -0
- package/lib/commonjs/protocol/core/contract/smart_contract_pb.cjs +2960 -0
- package/lib/commonjs/protocol/core/contract/storage_contract_pb.cjs +848 -0
- package/lib/commonjs/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
- package/lib/commonjs/protocol/core/contract/witness_contract_pb.cjs +956 -0
- package/lib/commonjs/tronweb.d.ts +111 -0
- package/lib/commonjs/tronweb.js +365 -0
- package/lib/commonjs/tronweb.js.map +1 -0
- package/lib/commonjs/types/ABI.d.ts +59 -0
- package/lib/commonjs/types/ABI.js +3 -0
- package/lib/commonjs/types/ABI.js.map +1 -0
- package/lib/commonjs/types/APIResponse.d.ts +31 -0
- package/lib/commonjs/types/APIResponse.js +3 -0
- package/lib/commonjs/types/APIResponse.js.map +1 -0
- package/lib/commonjs/types/Contract.d.ts +287 -0
- package/lib/commonjs/types/Contract.js +63 -0
- package/lib/commonjs/types/Contract.js.map +1 -0
- package/lib/commonjs/types/Providers.d.ts +8 -0
- package/lib/commonjs/types/Providers.js +3 -0
- package/lib/commonjs/types/Providers.js.map +1 -0
- package/lib/commonjs/types/Transaction.d.ts +65 -0
- package/lib/commonjs/types/Transaction.js +3 -0
- package/lib/commonjs/types/Transaction.js.map +1 -0
- package/lib/commonjs/types/TransactionBuilder.d.ts +270 -0
- package/lib/commonjs/types/TransactionBuilder.js +3 -0
- package/lib/commonjs/types/TransactionBuilder.js.map +1 -0
- package/lib/commonjs/types/TronWeb.d.ts +20 -0
- package/lib/commonjs/types/TronWeb.js +3 -0
- package/lib/commonjs/types/TronWeb.js.map +1 -0
- package/lib/commonjs/types/Trx.d.ts +263 -0
- package/lib/commonjs/types/Trx.js +42 -0
- package/lib/commonjs/types/Trx.js.map +1 -0
- package/lib/commonjs/types/UtilsTypes.d.ts +23 -0
- package/lib/commonjs/types/UtilsTypes.js +3 -0
- package/lib/commonjs/types/UtilsTypes.js.map +1 -0
- package/lib/commonjs/types/index.d.ts +9 -0
- package/lib/commonjs/types/index.js +13 -0
- package/lib/commonjs/types/index.js.map +1 -0
- package/lib/commonjs/utils/abi.d.ts +5 -0
- package/lib/commonjs/utils/abi.js +251 -0
- package/lib/commonjs/utils/abi.js.map +1 -0
- package/lib/commonjs/utils/accounts.d.ts +22 -0
- package/lib/commonjs/utils/accounts.js +51 -0
- package/lib/commonjs/utils/accounts.js.map +1 -0
- package/lib/commonjs/utils/address.d.ts +12 -0
- package/lib/commonjs/utils/address.js +91 -0
- package/lib/commonjs/utils/address.js.map +1 -0
- package/lib/commonjs/utils/base58.d.ts +3 -0
- package/lib/commonjs/utils/base58.js +68 -0
- package/lib/commonjs/utils/base58.js.map +1 -0
- package/lib/commonjs/utils/base64.d.ts +10 -0
- package/lib/commonjs/utils/base64.js +173 -0
- package/lib/commonjs/utils/base64.js.map +1 -0
- package/lib/commonjs/utils/bytes.d.ts +7 -0
- package/lib/commonjs/utils/bytes.js +61 -0
- package/lib/commonjs/utils/bytes.js.map +1 -0
- package/lib/commonjs/utils/code.d.ts +12 -0
- package/lib/commonjs/utils/code.js +166 -0
- package/lib/commonjs/utils/code.js.map +1 -0
- package/lib/commonjs/utils/crypto.d.ts +25 -0
- package/lib/commonjs/utils/crypto.js +222 -0
- package/lib/commonjs/utils/crypto.js.map +1 -0
- package/lib/commonjs/utils/ethersUtils.d.ts +14 -0
- package/lib/commonjs/utils/ethersUtils.js +39 -0
- package/lib/commonjs/utils/ethersUtils.js.map +1 -0
- package/lib/commonjs/utils/fragments.d.ts +465 -0
- package/lib/commonjs/utils/fragments.js +1342 -0
- package/lib/commonjs/utils/fragments.js.map +1 -0
- package/lib/commonjs/utils/help.d.ts +8 -0
- package/lib/commonjs/utils/help.js +61 -0
- package/lib/commonjs/utils/help.js.map +1 -0
- package/lib/commonjs/utils/index.d.ts +56 -0
- package/lib/commonjs/utils/index.js +43 -0
- package/lib/commonjs/utils/index.js.map +1 -0
- package/lib/commonjs/utils/interface.d.ts +381 -0
- package/lib/commonjs/utils/interface.js +1121 -0
- package/lib/commonjs/utils/interface.js.map +1 -0
- package/lib/commonjs/utils/message.d.ts +4 -0
- package/lib/commonjs/utils/message.js +38 -0
- package/lib/commonjs/utils/message.js.map +1 -0
- package/lib/commonjs/utils/transaction.d.ts +17 -0
- package/lib/commonjs/utils/transaction.js +678 -0
- package/lib/commonjs/utils/transaction.js.map +1 -0
- package/lib/commonjs/utils/typedData.d.ts +148 -0
- package/lib/commonjs/utils/typedData.js +498 -0
- package/lib/commonjs/utils/typedData.js.map +1 -0
- package/lib/commonjs/utils/validations.d.ts +22 -0
- package/lib/commonjs/utils/validations.js +128 -0
- package/lib/commonjs/utils/validations.js.map +1 -0
- package/lib/esm/index.d.ts +64 -0
- package/lib/esm/index.js +33 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/lib/TransactionBuilder/TransactionBuilder.d.ts +127 -0
- package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js +1767 -0
- package/lib/esm/lib/TransactionBuilder/TransactionBuilder.js.map +1 -0
- package/lib/esm/lib/TransactionBuilder/helper.d.ts +28 -0
- package/lib/esm/lib/TransactionBuilder/helper.js +112 -0
- package/lib/esm/lib/TransactionBuilder/helper.js.map +1 -0
- package/lib/esm/lib/contract/index.d.ts +29 -0
- package/lib/esm/lib/contract/index.js +118 -0
- package/lib/esm/lib/contract/index.js.map +1 -0
- package/lib/esm/lib/contract/method.d.ts +50 -0
- package/lib/esm/lib/contract/method.js +233 -0
- package/lib/esm/lib/contract/method.js.map +1 -0
- package/lib/esm/lib/event.d.ts +85 -0
- package/lib/esm/lib/event.js +132 -0
- package/lib/esm/lib/event.js.map +1 -0
- package/lib/esm/lib/plugin.d.ts +28 -0
- package/lib/esm/lib/plugin.js +79 -0
- package/lib/esm/lib/plugin.js.map +1 -0
- package/lib/esm/lib/providers/HttpProvider.d.ts +15 -0
- package/lib/esm/lib/providers/HttpProvider.js +59 -0
- package/lib/esm/lib/providers/HttpProvider.js.map +1 -0
- package/lib/esm/lib/providers/index.d.ts +6 -0
- package/lib/esm/lib/providers/index.js +4 -0
- package/lib/esm/lib/providers/index.js.map +1 -0
- package/lib/esm/lib/trx.d.ts +315 -0
- package/lib/esm/lib/trx.js +960 -0
- package/lib/esm/lib/trx.js.map +1 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/paramValidator/index.d.ts +19 -0
- package/lib/esm/paramValidator/index.js +106 -0
- package/lib/esm/paramValidator/index.js.map +1 -0
- package/lib/esm/protocol/core/Tron_pb.cjs +7524 -0
- package/lib/esm/protocol/core/contract/account_contract_pb.cjs +961 -0
- package/lib/esm/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
- package/lib/esm/protocol/core/contract/balance_contract_pb.cjs +3826 -0
- package/lib/esm/protocol/core/contract/common_pb.cjs +34 -0
- package/lib/esm/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
- package/lib/esm/protocol/core/contract/proposal_contract_pb.cjs +675 -0
- package/lib/esm/protocol/core/contract/smart_contract_pb.cjs +2960 -0
- package/lib/esm/protocol/core/contract/storage_contract_pb.cjs +848 -0
- package/lib/esm/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
- package/lib/esm/protocol/core/contract/witness_contract_pb.cjs +956 -0
- package/lib/esm/tronweb.d.ts +111 -0
- package/lib/esm/tronweb.js +360 -0
- package/lib/esm/tronweb.js.map +1 -0
- package/lib/esm/types/ABI.d.ts +59 -0
- package/lib/esm/types/ABI.js +2 -0
- package/lib/esm/types/ABI.js.map +1 -0
- package/lib/esm/types/APIResponse.d.ts +31 -0
- package/lib/esm/types/APIResponse.js +2 -0
- package/lib/esm/types/APIResponse.js.map +1 -0
- package/lib/esm/types/Contract.d.ts +287 -0
- package/lib/esm/types/Contract.js +60 -0
- package/lib/esm/types/Contract.js.map +1 -0
- package/lib/esm/types/Providers.d.ts +8 -0
- package/lib/esm/types/Providers.js +2 -0
- package/lib/esm/types/Providers.js.map +1 -0
- package/lib/esm/types/Transaction.d.ts +65 -0
- package/lib/esm/types/Transaction.js +2 -0
- package/lib/esm/types/Transaction.js.map +1 -0
- package/lib/esm/types/TransactionBuilder.d.ts +270 -0
- package/lib/esm/types/TransactionBuilder.js +2 -0
- package/lib/esm/types/TransactionBuilder.js.map +1 -0
- package/lib/esm/types/TronWeb.d.ts +20 -0
- package/lib/esm/types/TronWeb.js +2 -0
- package/lib/esm/types/TronWeb.js.map +1 -0
- package/lib/esm/types/Trx.d.ts +263 -0
- package/lib/esm/types/Trx.js +39 -0
- package/lib/esm/types/Trx.js.map +1 -0
- package/lib/esm/types/UtilsTypes.d.ts +23 -0
- package/lib/esm/types/UtilsTypes.js +2 -0
- package/lib/esm/types/UtilsTypes.js.map +1 -0
- package/lib/esm/types/index.d.ts +9 -0
- package/lib/esm/types/index.js +10 -0
- package/lib/esm/types/index.js.map +1 -0
- package/lib/esm/utils/abi.d.ts +5 -0
- package/lib/esm/utils/abi.js +245 -0
- package/lib/esm/utils/abi.js.map +1 -0
- package/lib/esm/utils/accounts.d.ts +22 -0
- package/lib/esm/utils/accounts.js +46 -0
- package/lib/esm/utils/accounts.js.map +1 -0
- package/lib/esm/utils/address.d.ts +12 -0
- package/lib/esm/utils/address.js +82 -0
- package/lib/esm/utils/address.js.map +1 -0
- package/lib/esm/utils/base58.d.ts +3 -0
- package/lib/esm/utils/base58.js +64 -0
- package/lib/esm/utils/base58.js.map +1 -0
- package/lib/esm/utils/base64.d.ts +10 -0
- package/lib/esm/utils/base64.js +169 -0
- package/lib/esm/utils/base64.js.map +1 -0
- package/lib/esm/utils/bytes.d.ts +7 -0
- package/lib/esm/utils/bytes.js +53 -0
- package/lib/esm/utils/bytes.js.map +1 -0
- package/lib/esm/utils/code.d.ts +12 -0
- package/lib/esm/utils/code.js +149 -0
- package/lib/esm/utils/code.js.map +1 -0
- package/lib/esm/utils/crypto.d.ts +25 -0
- package/lib/esm/utils/crypto.js +198 -0
- package/lib/esm/utils/crypto.js.map +1 -0
- package/lib/esm/utils/ethersUtils.d.ts +14 -0
- package/lib/esm/utils/ethersUtils.js +17 -0
- package/lib/esm/utils/ethersUtils.js.map +1 -0
- package/lib/esm/utils/fragments.d.ts +465 -0
- package/lib/esm/utils/fragments.js +1330 -0
- package/lib/esm/utils/fragments.js.map +1 -0
- package/lib/esm/utils/help.d.ts +8 -0
- package/lib/esm/utils/help.js +51 -0
- package/lib/esm/utils/help.js.map +1 -0
- package/lib/esm/utils/index.d.ts +56 -0
- package/lib/esm/utils/index.js +40 -0
- package/lib/esm/utils/index.js.map +1 -0
- package/lib/esm/utils/interface.d.ts +381 -0
- package/lib/esm/utils/interface.js +1112 -0
- package/lib/esm/utils/interface.js.map +1 -0
- package/lib/esm/utils/message.d.ts +4 -0
- package/lib/esm/utils/message.js +32 -0
- package/lib/esm/utils/message.js.map +1 -0
- package/lib/esm/utils/transaction.d.ts +17 -0
- package/lib/esm/utils/transaction.js +669 -0
- package/lib/esm/utils/transaction.js.map +1 -0
- package/lib/esm/utils/typedData.d.ts +148 -0
- package/lib/esm/utils/typedData.js +493 -0
- package/lib/esm/utils/typedData.js.map +1 -0
- package/lib/esm/utils/validations.d.ts +22 -0
- package/lib/esm/utils/validations.js +108 -0
- package/lib/esm/utils/validations.js.map +1 -0
- package/package.json +110 -0
- package/src/index.ts +42 -0
- package/src/lib/TransactionBuilder/TransactionBuilder.ts +2272 -0
- package/src/lib/TransactionBuilder/helper.ts +139 -0
- package/src/lib/contract/index.ts +151 -0
- package/src/lib/contract/method.ts +336 -0
- package/src/lib/event.ts +248 -0
- package/src/lib/plugin.ts +102 -0
- package/src/lib/providers/HttpProvider.ts +66 -0
- package/src/lib/providers/index.ts +6 -0
- package/src/lib/trx.ts +1463 -0
- package/src/paramValidator/index.ts +136 -0
- package/src/protocol/core/Tron_pb.cjs +7524 -0
- package/src/protocol/core/contract/account_contract_pb.cjs +961 -0
- package/src/protocol/core/contract/asset_issue_contract_pb.cjs +2196 -0
- package/src/protocol/core/contract/balance_contract_pb.cjs +3826 -0
- package/src/protocol/core/contract/common_pb.cjs +34 -0
- package/src/protocol/core/contract/exchange_contract_pb.cjs +1268 -0
- package/src/protocol/core/contract/proposal_contract_pb.cjs +675 -0
- package/src/protocol/core/contract/smart_contract_pb.cjs +2960 -0
- package/src/protocol/core/contract/storage_contract_pb.cjs +848 -0
- package/src/protocol/core/contract/vote_asset_contract_pb.cjs +340 -0
- package/src/protocol/core/contract/witness_contract_pb.cjs +956 -0
- package/src/tronweb.ts +437 -0
- package/src/types/ABI.ts +82 -0
- package/src/types/APIResponse.ts +34 -0
- package/src/types/Contract.ts +352 -0
- package/src/types/Providers.ts +7 -0
- package/src/types/Transaction.ts +70 -0
- package/src/types/TransactionBuilder.ts +281 -0
- package/src/types/TronWeb.ts +23 -0
- package/src/types/Trx.ts +285 -0
- package/src/types/UtilsTypes.ts +25 -0
- package/src/types/index.ts +9 -0
- package/src/types/interface.d.ts +1 -0
- package/src/utils/abi.ts +257 -0
- package/src/utils/accounts.ts +60 -0
- package/src/utils/address.ts +89 -0
- package/src/utils/base58.ts +78 -0
- package/src/utils/base64.ts +200 -0
- package/src/utils/bytes.ts +68 -0
- package/src/utils/code.ts +172 -0
- package/src/utils/crypto.ts +264 -0
- package/src/utils/ethersUtils.ts +60 -0
- package/src/utils/fragments.ts +1738 -0
- package/src/utils/help.ts +59 -0
- package/src/utils/index.ts +42 -0
- package/src/utils/interface.ts +1362 -0
- package/src/utils/message.ts +38 -0
- package/src/utils/transaction.ts +991 -0
- package/src/utils/typedData.ts +688 -0
- package/src/utils/validations.ts +130 -0
@@ -0,0 +1,89 @@
|
|
1
|
+
import { hexStr2byteArray, byteArray2hexStr } from './code.js';
|
2
|
+
import { decodeBase58Address, getBase58CheckAddress, isAddressValid, pkToAddress } from './crypto.js';
|
3
|
+
import { isHex, isString } from './validations.js';
|
4
|
+
import { keccak256 } from './ethersUtils.js';
|
5
|
+
|
6
|
+
export const ADDRESS_SIZE = 34;
|
7
|
+
export const ADDRESS_PREFIX = '41';
|
8
|
+
export const ADDRESS_PREFIX_BYTE = 0x41;
|
9
|
+
export const ADDRESS_PREFIX_REGEX = /^(41)/;
|
10
|
+
|
11
|
+
export const TRON_BIP39_PATH_PREFIX = "m/44'/195'";
|
12
|
+
export const TRON_BIP39_PATH_INDEX_0 = TRON_BIP39_PATH_PREFIX + "/0'/0/0";
|
13
|
+
|
14
|
+
export function fromHex(address: string) {
|
15
|
+
if (!isHex(address)) return address;
|
16
|
+
|
17
|
+
return getBase58CheckAddress(hexStr2byteArray(address.replace(/^0x/, ADDRESS_PREFIX)));
|
18
|
+
}
|
19
|
+
|
20
|
+
export function toHex(address: string) {
|
21
|
+
if (isHex(address)) return address.toLowerCase().replace(/^0x/, ADDRESS_PREFIX);
|
22
|
+
|
23
|
+
return byteArray2hexStr(decodeBase58Address(address) as number[]).toLowerCase();
|
24
|
+
}
|
25
|
+
|
26
|
+
function getChecksumAddress(address: string): string {
|
27
|
+
address = address.toLowerCase();
|
28
|
+
|
29
|
+
const chars = address.substring(2).split('');
|
30
|
+
|
31
|
+
const expanded = new Uint8Array(40);
|
32
|
+
for (let i = 0; i < 40; i++) {
|
33
|
+
expanded[i] = chars[i].charCodeAt(0);
|
34
|
+
}
|
35
|
+
|
36
|
+
const hashed = hexStr2byteArray(keccak256(expanded).slice(2));
|
37
|
+
|
38
|
+
for (let i = 0; i < 40; i += 2) {
|
39
|
+
if ((hashed[i >> 1] >> 4) >= 8) {
|
40
|
+
chars[i] = chars[i].toUpperCase();
|
41
|
+
}
|
42
|
+
if ((hashed[i >> 1] & 0x0f) >= 8) {
|
43
|
+
chars[i + 1] = chars[i + 1].toUpperCase();
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
return ADDRESS_PREFIX + chars.join('');
|
48
|
+
}
|
49
|
+
|
50
|
+
export function toChecksumAddress(address: string) {
|
51
|
+
if (!isAddress(address)) throw new Error(`'${address}' is not a valid address string`);
|
52
|
+
return getChecksumAddress(toHex(address));
|
53
|
+
}
|
54
|
+
|
55
|
+
export function isChecksumAddress(address: string) {
|
56
|
+
if (!isHex(address) || address.length !== 42) return false;
|
57
|
+
try {
|
58
|
+
return toChecksumAddress(address) === address;
|
59
|
+
} catch {
|
60
|
+
return false;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
export function fromPrivateKey(privateKey: string, strict = false) {
|
65
|
+
try {
|
66
|
+
return pkToAddress(privateKey, strict);
|
67
|
+
} catch {
|
68
|
+
return false;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
export function isAddress(address: unknown): boolean {
|
73
|
+
if (!address || !isString(address)) return false;
|
74
|
+
|
75
|
+
// Convert HEX to Base58
|
76
|
+
if (address.length === 42) {
|
77
|
+
try {
|
78
|
+
// it throws an error if the address starts with 0x
|
79
|
+
return isAddress(getBase58CheckAddress(hexStr2byteArray(address)));
|
80
|
+
} catch (err) {
|
81
|
+
return false;
|
82
|
+
}
|
83
|
+
}
|
84
|
+
try {
|
85
|
+
return isAddressValid(address);
|
86
|
+
} catch (err) {
|
87
|
+
return false;
|
88
|
+
}
|
89
|
+
}
|
@@ -0,0 +1,78 @@
|
|
1
|
+
import type { BytesLike } from '../types/UtilsTypes.js';
|
2
|
+
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
3
|
+
const ALPHABET_MAP = {} as Record<string, number>;
|
4
|
+
|
5
|
+
for (let i = 0; i < ALPHABET.length; i++) ALPHABET_MAP[ALPHABET.charAt(i)] = i;
|
6
|
+
|
7
|
+
const BASE = 58;
|
8
|
+
|
9
|
+
export function encode58(buffer: BytesLike | string) {
|
10
|
+
if (buffer.length === 0) return '';
|
11
|
+
|
12
|
+
let i: number;
|
13
|
+
let j: number;
|
14
|
+
|
15
|
+
const digits = [0];
|
16
|
+
|
17
|
+
for (i = 0; i < buffer.length; i++) {
|
18
|
+
for (j = 0; j < digits.length; j++) digits[j] <<= 8;
|
19
|
+
|
20
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
21
|
+
// @ts-ignore
|
22
|
+
digits[0] += buffer[i];
|
23
|
+
let carry = 0;
|
24
|
+
|
25
|
+
for (j = 0; j < digits.length; ++j) {
|
26
|
+
digits[j] += carry;
|
27
|
+
carry = (digits[j] / BASE) | 0;
|
28
|
+
digits[j] %= BASE;
|
29
|
+
}
|
30
|
+
|
31
|
+
while (carry) {
|
32
|
+
digits.push(carry % BASE);
|
33
|
+
carry = (carry / BASE) | 0;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
for (i = 0; buffer[i] === 0 && i < buffer.length - 1; i++) digits.push(0);
|
38
|
+
|
39
|
+
return digits
|
40
|
+
.reverse()
|
41
|
+
.map((digit) => ALPHABET[digit])
|
42
|
+
.join('');
|
43
|
+
}
|
44
|
+
|
45
|
+
export function decode58(string: string) {
|
46
|
+
if (string.length === 0) return [];
|
47
|
+
|
48
|
+
let i;
|
49
|
+
let j;
|
50
|
+
|
51
|
+
const bytes = [0];
|
52
|
+
|
53
|
+
for (i = 0; i < string.length; i++) {
|
54
|
+
const c: string = string[i];
|
55
|
+
|
56
|
+
if (!(c in ALPHABET_MAP)) throw new Error('Non-base58 character');
|
57
|
+
|
58
|
+
for (j = 0; j < bytes.length; j++) bytes[j] *= BASE;
|
59
|
+
|
60
|
+
bytes[0] += ALPHABET_MAP[c];
|
61
|
+
let carry = 0;
|
62
|
+
|
63
|
+
for (j = 0; j < bytes.length; ++j) {
|
64
|
+
bytes[j] += carry;
|
65
|
+
carry = bytes[j] >> 8;
|
66
|
+
bytes[j] &= 0xff;
|
67
|
+
}
|
68
|
+
|
69
|
+
while (carry) {
|
70
|
+
bytes.push(carry & 0xff);
|
71
|
+
carry >>= 8;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
for (i = 0; string[i] === '1' && i < string.length - 1; i++) bytes.push(0);
|
76
|
+
|
77
|
+
return bytes.reverse();
|
78
|
+
}
|
@@ -0,0 +1,200 @@
|
|
1
|
+
import type { BytesLike } from '../types/UtilsTypes.js';
|
2
|
+
|
3
|
+
const _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
4
|
+
|
5
|
+
export class Base64 {
|
6
|
+
encode(input: string) {
|
7
|
+
let output = '';
|
8
|
+
let chr1: number;
|
9
|
+
let chr2: number;
|
10
|
+
let chr3: number;
|
11
|
+
let enc1: number;
|
12
|
+
let enc2: number;
|
13
|
+
let enc3: number;
|
14
|
+
let enc4: number;
|
15
|
+
let i = 0;
|
16
|
+
|
17
|
+
while (i < input.length) {
|
18
|
+
chr1 = input.charCodeAt(i++);
|
19
|
+
chr2 = input.charCodeAt(i++);
|
20
|
+
chr3 = input.charCodeAt(i++);
|
21
|
+
|
22
|
+
enc1 = chr1 >> 2;
|
23
|
+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
24
|
+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
25
|
+
enc4 = chr3 & 63;
|
26
|
+
|
27
|
+
if (isNaN(chr2)) enc3 = enc4 = 64;
|
28
|
+
else if (isNaN(chr3)) enc4 = 64;
|
29
|
+
|
30
|
+
output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
|
31
|
+
}
|
32
|
+
|
33
|
+
return output;
|
34
|
+
}
|
35
|
+
|
36
|
+
encodeIgnoreUtf8(inputBytes: BytesLike) {
|
37
|
+
let output = '';
|
38
|
+
let chr1: number;
|
39
|
+
let chr2: number;
|
40
|
+
let chr3: number;
|
41
|
+
let enc1: number;
|
42
|
+
let enc2: number;
|
43
|
+
let enc3: number;
|
44
|
+
let enc4: number;
|
45
|
+
let i = 0;
|
46
|
+
|
47
|
+
while (i < inputBytes.length) {
|
48
|
+
chr1 = inputBytes[i++];
|
49
|
+
chr2 = inputBytes[i++];
|
50
|
+
chr3 = inputBytes[i++];
|
51
|
+
|
52
|
+
enc1 = chr1 >> 2;
|
53
|
+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
54
|
+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
55
|
+
enc4 = chr3 & 63;
|
56
|
+
|
57
|
+
if (isNaN(chr2)) enc3 = enc4 = 64;
|
58
|
+
else if (isNaN(chr3)) enc4 = 64;
|
59
|
+
|
60
|
+
output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
|
61
|
+
}
|
62
|
+
|
63
|
+
return output;
|
64
|
+
}
|
65
|
+
|
66
|
+
decode(input: string) {
|
67
|
+
let output = '';
|
68
|
+
let chr1: number;
|
69
|
+
let chr2: number;
|
70
|
+
let chr3: number;
|
71
|
+
let enc1: number;
|
72
|
+
let enc2: number;
|
73
|
+
let enc3: number;
|
74
|
+
let enc4: number;
|
75
|
+
let i = 0;
|
76
|
+
|
77
|
+
// eslint-disable-next-line no-useless-escape
|
78
|
+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
|
79
|
+
|
80
|
+
while (i < input.length) {
|
81
|
+
enc1 = _keyStr.indexOf(input.charAt(i++));
|
82
|
+
enc2 = _keyStr.indexOf(input.charAt(i++));
|
83
|
+
enc3 = _keyStr.indexOf(input.charAt(i++));
|
84
|
+
enc4 = _keyStr.indexOf(input.charAt(i++));
|
85
|
+
|
86
|
+
chr1 = (enc1 << 2) | (enc2 >> 4);
|
87
|
+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
88
|
+
chr3 = ((enc3 & 3) << 6) | enc4;
|
89
|
+
|
90
|
+
output = output + String.fromCharCode(chr1);
|
91
|
+
|
92
|
+
if (enc3 != 64) output = output + String.fromCharCode(chr2);
|
93
|
+
|
94
|
+
if (enc4 != 64) output = output + String.fromCharCode(chr3);
|
95
|
+
}
|
96
|
+
|
97
|
+
return this._utf8_decode(output);
|
98
|
+
}
|
99
|
+
|
100
|
+
decodeToByteArray(input: string) {
|
101
|
+
let output = '';
|
102
|
+
let chr1: number;
|
103
|
+
let chr2: number;
|
104
|
+
let chr3: number;
|
105
|
+
let enc1: number;
|
106
|
+
let enc2: number;
|
107
|
+
let enc3: number;
|
108
|
+
let enc4: number;
|
109
|
+
let i = 0;
|
110
|
+
|
111
|
+
// eslint-disable-next-line no-useless-escape
|
112
|
+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
|
113
|
+
|
114
|
+
while (i < input.length) {
|
115
|
+
enc1 = _keyStr.indexOf(input.charAt(i++));
|
116
|
+
enc2 = _keyStr.indexOf(input.charAt(i++));
|
117
|
+
enc3 = _keyStr.indexOf(input.charAt(i++));
|
118
|
+
enc4 = _keyStr.indexOf(input.charAt(i++));
|
119
|
+
|
120
|
+
chr1 = (enc1 << 2) | (enc2 >> 4);
|
121
|
+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
122
|
+
chr3 = ((enc3 & 3) << 6) | enc4;
|
123
|
+
|
124
|
+
output = output + String.fromCharCode(chr1);
|
125
|
+
|
126
|
+
if (enc3 != 64) output = output + String.fromCharCode(chr2);
|
127
|
+
|
128
|
+
if (enc4 != 64) output = output + String.fromCharCode(chr3);
|
129
|
+
}
|
130
|
+
|
131
|
+
return this._out2ByteArray(output);
|
132
|
+
}
|
133
|
+
|
134
|
+
_out2ByteArray(utftext: string) {
|
135
|
+
const byteArray: BytesLike = new Array(utftext.length);
|
136
|
+
|
137
|
+
let i = 0;
|
138
|
+
let c = 0;
|
139
|
+
|
140
|
+
while (i < utftext.length) {
|
141
|
+
c = utftext.charCodeAt(i);
|
142
|
+
byteArray[i] = c;
|
143
|
+
i++;
|
144
|
+
}
|
145
|
+
|
146
|
+
return byteArray;
|
147
|
+
}
|
148
|
+
|
149
|
+
_utf8_encode(string: string) {
|
150
|
+
string = string.replace(/\r\n/g, '\n');
|
151
|
+
let utftext = '';
|
152
|
+
|
153
|
+
for (let n = 0; n < string.length; n++) {
|
154
|
+
const c = string.charCodeAt(n);
|
155
|
+
|
156
|
+
if (c < 128) {
|
157
|
+
utftext += String.fromCharCode(c);
|
158
|
+
} else if (c > 127 && c < 2048) {
|
159
|
+
utftext += String.fromCharCode((c >> 6) | 192);
|
160
|
+
utftext += String.fromCharCode((c & 63) | 128);
|
161
|
+
} else {
|
162
|
+
utftext += String.fromCharCode((c >> 12) | 224);
|
163
|
+
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
164
|
+
utftext += String.fromCharCode((c & 63) | 128);
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
return utftext;
|
169
|
+
}
|
170
|
+
|
171
|
+
_utf8_decode(utftext: string) {
|
172
|
+
let string = '';
|
173
|
+
let i = 0;
|
174
|
+
let c = 0;
|
175
|
+
let c2 = 0;
|
176
|
+
let c3 = 0;
|
177
|
+
|
178
|
+
while (i < utftext.length) {
|
179
|
+
c = utftext.charCodeAt(i);
|
180
|
+
|
181
|
+
if (c < 128) {
|
182
|
+
string += String.fromCharCode(c);
|
183
|
+
i++;
|
184
|
+
} else if (c > 191 && c < 224) {
|
185
|
+
c2 = utftext.charCodeAt(i + 1);
|
186
|
+
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
187
|
+
i += 2;
|
188
|
+
} else {
|
189
|
+
c2 = utftext.charCodeAt(i + 1);
|
190
|
+
c3 = utftext.charCodeAt(i + 2);
|
191
|
+
|
192
|
+
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
193
|
+
|
194
|
+
i += 3;
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
return string;
|
199
|
+
}
|
200
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
import { Base64 } from './base64.js';
|
2
|
+
import { BytesLike } from '../types/UtilsTypes.js';
|
3
|
+
|
4
|
+
export function byte2hexStr(byte: number) {
|
5
|
+
if (byte < 0 || byte > 255) throw new Error('Input must be a byte');
|
6
|
+
|
7
|
+
const hexByteMap = '0123456789ABCDEF';
|
8
|
+
|
9
|
+
let str = '';
|
10
|
+
str += hexByteMap.charAt(byte >> 4);
|
11
|
+
str += hexByteMap.charAt(byte & 0x0f);
|
12
|
+
|
13
|
+
return str;
|
14
|
+
}
|
15
|
+
|
16
|
+
export function bytesToString(arr: BytesLike) {
|
17
|
+
let str = '';
|
18
|
+
|
19
|
+
for (let i = 0; i < arr.length; i++) {
|
20
|
+
const one = arr[i].toString(2);
|
21
|
+
const v = one.match(/^1+?(?=0)/);
|
22
|
+
|
23
|
+
if (v && one.length === 8) {
|
24
|
+
const bytesLength = v[0].length;
|
25
|
+
let store = arr[i].toString(2).slice(7 - bytesLength);
|
26
|
+
|
27
|
+
for (let st = 1; st < bytesLength; st++) store += arr[st + i].toString(2).slice(2);
|
28
|
+
|
29
|
+
str += String.fromCharCode(parseInt(store, 2));
|
30
|
+
i += bytesLength - 1;
|
31
|
+
} else {
|
32
|
+
str += String.fromCharCode(arr[i]);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
return str;
|
37
|
+
}
|
38
|
+
|
39
|
+
export function hextoString(hex: string) {
|
40
|
+
const arr = hex.replace(/^0x/, '').split('');
|
41
|
+
let out = '';
|
42
|
+
|
43
|
+
for (let i = 0; i < arr.length / 2; i++) {
|
44
|
+
const tmp = `0x${arr[i * 2]}${arr[i * 2 + 1]}`;
|
45
|
+
out += String.fromCharCode(parseInt(tmp));
|
46
|
+
}
|
47
|
+
|
48
|
+
return out;
|
49
|
+
}
|
50
|
+
|
51
|
+
export function byteArray2hexStr(byteArray: BytesLike) {
|
52
|
+
let str = '';
|
53
|
+
|
54
|
+
for (let i = 0; i < byteArray.length; i++) str += byte2hexStr(byteArray[i]);
|
55
|
+
|
56
|
+
return str;
|
57
|
+
}
|
58
|
+
|
59
|
+
export function base64DecodeFromString(string64: string) {
|
60
|
+
return new Base64().decodeToByteArray(string64);
|
61
|
+
}
|
62
|
+
|
63
|
+
export function base64EncodeToString(bytes: BytesLike) {
|
64
|
+
const b = new Base64();
|
65
|
+
const string64 = b.encodeIgnoreUtf8(bytes);
|
66
|
+
|
67
|
+
return string64;
|
68
|
+
}
|
@@ -0,0 +1,172 @@
|
|
1
|
+
import {
|
2
|
+
byte2hexStr,
|
3
|
+
bytesToString,
|
4
|
+
hextoString,
|
5
|
+
byteArray2hexStr,
|
6
|
+
base64DecodeFromString,
|
7
|
+
base64EncodeToString,
|
8
|
+
} from './bytes.js';
|
9
|
+
|
10
|
+
import type { BytesLike } from '../types/UtilsTypes.js';
|
11
|
+
|
12
|
+
export function bin2String(array: BytesLike) {
|
13
|
+
return bytesToString(array);
|
14
|
+
}
|
15
|
+
|
16
|
+
export function arrayEquals(array1: any[], array2: any[], strict = false) {
|
17
|
+
if (array1.length != array2.length) return false;
|
18
|
+
|
19
|
+
for (let i = 0; i < array1.length; i++) {
|
20
|
+
if (strict) {
|
21
|
+
if (array1[i] != array2[i]) return false;
|
22
|
+
} else if (JSON.stringify(array1[i]) != JSON.stringify(array2[i])) return false;
|
23
|
+
}
|
24
|
+
|
25
|
+
return true;
|
26
|
+
}
|
27
|
+
|
28
|
+
export function stringToBytes(str: string) {
|
29
|
+
const bytes = [];
|
30
|
+
const len: number = str.length;
|
31
|
+
let c: number;
|
32
|
+
|
33
|
+
for (let i = 0; i < len; i++) {
|
34
|
+
c = str.charCodeAt(i);
|
35
|
+
|
36
|
+
if (c >= 0x010000 && c <= 0x10ffff) {
|
37
|
+
bytes.push(((c >> 18) & 0x07) | 0xf0);
|
38
|
+
bytes.push(((c >> 12) & 0x3f) | 0x80);
|
39
|
+
bytes.push(((c >> 6) & 0x3f) | 0x80);
|
40
|
+
bytes.push((c & 0x3f) | 0x80);
|
41
|
+
} else if (c >= 0x000800 && c <= 0x00ffff) {
|
42
|
+
bytes.push(((c >> 12) & 0x0f) | 0xe0);
|
43
|
+
bytes.push(((c >> 6) & 0x3f) | 0x80);
|
44
|
+
bytes.push((c & 0x3f) | 0x80);
|
45
|
+
} else if (c >= 0x000080 && c <= 0x0007ff) {
|
46
|
+
bytes.push(((c >> 6) & 0x1f) | 0xc0);
|
47
|
+
bytes.push((c & 0x3f) | 0x80);
|
48
|
+
} else bytes.push(c & 0xff);
|
49
|
+
}
|
50
|
+
|
51
|
+
return bytes;
|
52
|
+
}
|
53
|
+
|
54
|
+
export { byte2hexStr, bytesToString, hextoString, byteArray2hexStr, base64DecodeFromString, base64EncodeToString };
|
55
|
+
|
56
|
+
export function hexChar2byte(c: string) {
|
57
|
+
let d: number | undefined;
|
58
|
+
|
59
|
+
if (c >= 'A' && c <= 'F') d = c.charCodeAt(0) - 'A'.charCodeAt(0) + 10;
|
60
|
+
else if (c >= 'a' && c <= 'f') d = c.charCodeAt(0) - 'a'.charCodeAt(0) + 10;
|
61
|
+
else if (c >= '0' && c <= '9') d = c.charCodeAt(0) - '0'.charCodeAt(0);
|
62
|
+
|
63
|
+
if (typeof d === 'number') return d;
|
64
|
+
else throw new Error('The passed hex char is not a valid hex char');
|
65
|
+
}
|
66
|
+
|
67
|
+
export function isHexChar(c: string) {
|
68
|
+
if ((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9')) {
|
69
|
+
return 1;
|
70
|
+
}
|
71
|
+
|
72
|
+
return 0;
|
73
|
+
}
|
74
|
+
|
75
|
+
// set strict as true: if the length of str is odd, add 0 before the str to make its length as even
|
76
|
+
export function hexStr2byteArray(str: string, strict = false) {
|
77
|
+
let len = str.length;
|
78
|
+
|
79
|
+
if (strict) {
|
80
|
+
if (len % 2) {
|
81
|
+
str = `0${str}`;
|
82
|
+
len++;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
const byteArray: number[] = [];
|
86
|
+
let d = 0;
|
87
|
+
let j = 0;
|
88
|
+
let k = 0;
|
89
|
+
|
90
|
+
for (let i = 0; i < len; i++) {
|
91
|
+
const c = str.charAt(i);
|
92
|
+
|
93
|
+
if (isHexChar(c)) {
|
94
|
+
d <<= 4;
|
95
|
+
d += hexChar2byte(c);
|
96
|
+
j++;
|
97
|
+
|
98
|
+
if (0 === j % 2) {
|
99
|
+
byteArray[k++] = d;
|
100
|
+
d = 0;
|
101
|
+
}
|
102
|
+
} else throw new Error('The passed hex char is not a valid hex string');
|
103
|
+
}
|
104
|
+
|
105
|
+
return byteArray;
|
106
|
+
}
|
107
|
+
|
108
|
+
//yyyy-MM-DD HH-mm-ss
|
109
|
+
export function strToDate(str: string) {
|
110
|
+
if (!/^\d{4}-\d{2}-\d{2}( \d{2}-\d{2}-\d{2}|)/.test(str)) throw new Error('The passed date string is not valid');
|
111
|
+
|
112
|
+
const tempStrs = str.split(' ');
|
113
|
+
const dateStrs = tempStrs[0].split('-');
|
114
|
+
const year = parseInt(dateStrs[0], 10);
|
115
|
+
const month = parseInt(dateStrs[1], 10) - 1;
|
116
|
+
const day = parseInt(dateStrs[2], 10);
|
117
|
+
|
118
|
+
if (tempStrs.length > 1) {
|
119
|
+
const timeStrs = tempStrs[1].split('-');
|
120
|
+
const hour = parseInt(timeStrs[0], 10);
|
121
|
+
const minute = parseInt(timeStrs[1], 10);
|
122
|
+
const second = parseInt(timeStrs[2], 10);
|
123
|
+
|
124
|
+
return new Date(year, month, day, hour, minute, second);
|
125
|
+
}
|
126
|
+
|
127
|
+
return new Date(year, month, day);
|
128
|
+
}
|
129
|
+
|
130
|
+
export function isNumber(c: string) {
|
131
|
+
if (c >= '0' && c <= '9') return 1;
|
132
|
+
|
133
|
+
return 0;
|
134
|
+
}
|
135
|
+
|
136
|
+
//return 1: address --- 20Bytes HexString
|
137
|
+
//return 2: blockNumber ------ Decimal number
|
138
|
+
//return 3: assetName ------ String
|
139
|
+
//return other: error
|
140
|
+
export function getStringType(str: string) {
|
141
|
+
if (null == str) return -1;
|
142
|
+
|
143
|
+
if (str.length == 0 || str == '') return -1;
|
144
|
+
|
145
|
+
let i = 0;
|
146
|
+
|
147
|
+
if (str.length == 40) {
|
148
|
+
for (; i < 40; i++) {
|
149
|
+
const c = str.charAt(i);
|
150
|
+
|
151
|
+
if (!isHexChar(c)) break;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
if (i == 40) return 1; //40 Hex, Address
|
156
|
+
|
157
|
+
for (i = 0; i < str.length; i++) {
|
158
|
+
const c = str.charAt(i);
|
159
|
+
|
160
|
+
if (!isNumber(c)) break;
|
161
|
+
}
|
162
|
+
|
163
|
+
if (i == str.length) return 2; // All Decimal number, BlockNumber
|
164
|
+
|
165
|
+
for (i = 0; i < str.length; i++) {
|
166
|
+
const c = str.charAt(i);
|
167
|
+
|
168
|
+
if (c > ' ') return 3; // At least one visible character
|
169
|
+
}
|
170
|
+
|
171
|
+
return -1;
|
172
|
+
}
|