UNPKG

dotbit

Version:

A complete .bit SDK and utilities in TypeScript

71 lines 2.32 kB
import { CoinType } from '../const'; import { isAddress } from 'ethers'; import GraphemeSplitter from 'grapheme-splitter'; import { validate } from 'multicoin-address-validator'; import { SignTypedDataVersion, TypedDataUtils } from '@metamask/eth-sig-util'; export function mmJsonHashAndChainIdHex(typedData, chainId) { const mmHash = TypedDataUtils.eip712Hash(typedData, SignTypedDataVersion.V4).toString('hex'); const chainIdHex = chainId.toString(16).padStart(16, '0'); return mmHash + chainIdHex; } 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 && validate(address, 'Tron'); } 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) && isAddress(address); } catch (err) { console.warn(`invalid ETH address: ${address}`); return false; } } export function checkKeyInfo(keyInfo) { let ret; if ([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 matchDWebProtocol(str) { return str.match(/^(ipns|ipfs|sia|arweave|ar|resilio):\/\/(.*)/); } //# sourceMappingURL=common.js.map