UNPKG

dotbit-sdk-allin

Version:

A complete .bit SDK and utilities in TypeScript

94 lines 3.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.matchDWebProtocol = exports.computeChainTypeByCoinType = exports.stringVisualLength = exports.checkKeyInfo = exports.isEthAddress = exports.isTronAddress = exports.sleep = exports.isEmptyAddress = exports.remove0x = exports.pad0x = void 0; const const_1 = require("../const"); const ethers_1 = require("ethers"); const grapheme_splitter_1 = __importDefault(require("grapheme-splitter")); const tronweb_1 = __importDefault(require("tronweb")); function pad0x(str) { return str.startsWith('0x') ? str : `0x${str}`; } exports.pad0x = pad0x; function remove0x(str) { return str.startsWith('0x') ? str.substring(2) : str; } exports.remove0x = remove0x; function isEmptyAddress(address) { return !address || address === '0x' || address === '0x0' || address === '0x0000000000000000000000000000000000000000'; } exports.isEmptyAddress = isEmptyAddress; function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } exports.sleep = sleep; function isTronAddress(address) { try { return address.length !== 42 && tronweb_1.default.isAddress(address); } catch (err) { console.warn(`invalid Tron address: ${address}`); return false; } } exports.isTronAddress = isTronAddress; function isEthAddress(address) { try { return /^(0x|0X)[0-9a-f]{40}$/i.test(address) && ethers_1.utils.isAddress(address); } catch (err) { console.warn(`invalid ETH address: ${address}`); return false; } } exports.isEthAddress = isEthAddress; function checkKeyInfo(keyInfo) { let ret; if (typeof keyInfo.chain_id !== 'undefined') { console.warn('chain_id is deprecated, please use coin_type.'); } if ([const_1.EvmChainId.ETH, const_1.EvmChainId.BSC, const_1.EvmChainId.MATIC].includes(Number(keyInfo.chain_id)) || [const_1.CoinType.ETH, const_1.CoinType.MATIC, const_1.CoinType.BSC].includes(keyInfo.coin_type)) { if (isEthAddress(keyInfo.key)) { ret = true; } else { console.warn(`invalid ETH address: ${keyInfo.key}`); ret = false; } } else if (keyInfo.coin_type === const_1.CoinType.TRX) { if (isTronAddress(keyInfo.key)) { ret = true; } else { console.warn(`invalid Tron address: ${keyInfo.key}`); ret = false; } } return ret; } exports.checkKeyInfo = checkKeyInfo; function stringVisualLength(str) { const splitter = new grapheme_splitter_1.default(); const split = splitter.splitGraphemes(str); return split.length; } exports.stringVisualLength = stringVisualLength; function computeChainTypeByCoinType(coinType) { let _chainType; if (const_1.EvmCoinTypes.includes(coinType)) { _chainType = const_1.ChainType.eth; } else if (const_1.CoinType.TRX === coinType) { _chainType = const_1.ChainType.tron; } return _chainType; } exports.computeChainTypeByCoinType = computeChainTypeByCoinType; function matchDWebProtocol(str) { return str.match(/^(ipns|ipfs|sia|arweave|ar|resilio):\/\/(.*)/); } exports.matchDWebProtocol = matchDWebProtocol; //# sourceMappingURL=common.js.map