UNPKG

dotbit-sdk-allin

Version:

A complete .bit SDK and utilities in TypeScript

78 lines 2.57 kB
import { ChainType, CoinType, EvmChainId, EvmCoinTypes } from '../const'; import { utils } from 'ethers'; import GraphemeSplitter from 'grapheme-splitter'; import TronWeb from 'tronweb'; export function pad0x(str) { return str.startsWith('0x') ? str : `0x${str}`; } export function remove0x(str) { return str.startsWith('0x') ? str.substring(2) : str; } export function isEmptyAddress(address) { return !address || address === '0x' || address === '0x0' || address === '0x0000000000000000000000000000000000000000'; } export function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } export function isTronAddress(address) { try { return address.length !== 42 && TronWeb.isAddress(address); } catch (err) { console.warn(`invalid Tron address: ${address}`); return false; } } export function isEthAddress(address) { try { return /^(0x|0X)[0-9a-f]{40}$/i.test(address) && utils.isAddress(address); } catch (err) { console.warn(`invalid ETH address: ${address}`); return false; } } export function checkKeyInfo(keyInfo) { let ret; if (typeof keyInfo.chain_id !== 'undefined') { console.warn('chain_id is deprecated, please use coin_type.'); } if ([EvmChainId.ETH, EvmChainId.BSC, EvmChainId.MATIC].includes(Number(keyInfo.chain_id)) || [CoinType.ETH, CoinType.MATIC, 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 === CoinType.TRX) { if (isTronAddress(keyInfo.key)) { ret = true; } else { console.warn(`invalid Tron address: ${keyInfo.key}`); ret = false; } } return ret; } export function stringVisualLength(str) { const splitter = new GraphemeSplitter(); const split = splitter.splitGraphemes(str); return split.length; } export function computeChainTypeByCoinType(coinType) { let _chainType; if (EvmCoinTypes.includes(coinType)) { _chainType = ChainType.eth; } else if (CoinType.TRX === coinType) { _chainType = ChainType.tron; } return _chainType; } export function matchDWebProtocol(str) { return str.match(/^(ipns|ipfs|sia|arweave|ar|resilio):\/\/(.*)/); } //# sourceMappingURL=common.js.map