UNPKG

@ledgerhq/coin-filecoin

Version:
64 lines 2.11 kB
import { PROTOCOL_INDICATOR, fromEthAddress, isEthAddress, fromString, toEthAddress, } from "iso-filecoin/address"; import { log } from "@ledgerhq/logs"; export const isFilEthAddress = (addr) => addr.protocol === PROTOCOL_INDICATOR.DELEGATED && addr.namespace === 10; export const isIdAddress = (addr) => addr.protocol === PROTOCOL_INDICATOR.ID; export const isEthereumConvertableAddr = (addr) => isIdAddress(addr) || isFilEthAddress(addr); export const validateAddress = (input) => { try { const parsedAddress = fromString(input); return { isValid: true, parsedAddress }; } catch (error) { log("debug", `[validateAddress] fromString invalid address`); } try { const parsedAddress = fromEthAddress(input, "mainnet"); return { isValid: true, parsedAddress }; } catch (error) { log("debug", `[validateAddress] fromEthAddress invalid address`); } return { isValid: false }; }; export const isRecipientValidForTokenTransfer = (addr) => { if (addr.length < 2) { return false; } const valid = validateAddress(addr); if (!valid.isValid) { return false; } if (isEthereumConvertableAddr(valid.parsedAddress)) { return true; } return false; }; export const getEquivalentAddress = (addr) => { if (isEthAddress(addr)) { return fromEthAddress(addr, "mainnet").toString(); } else { const parsed = fromString(addr); if (isEthereumConvertableAddr(parsed)) { return toEthAddress(parsed); } return ""; } }; export const convertAddressFilToEth = (addr) => { if (isEthAddress(addr)) { return addr; } const parsed = fromString(addr); if (isEthereumConvertableAddr(parsed)) { return toEthAddress(parsed); } throw new Error("address is not convertible to ethereum address"); }; export const convertAddressEthToFil = (addr) => { if (!isEthAddress(addr)) { return addr; } return fromEthAddress(addr, "mainnet").toString(); }; //# sourceMappingURL=addresses.js.map