@polkadot/util-crypto#base58Decode TypeScript Examples

The following examples show how to use @polkadot/util-crypto#base58Decode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: getValidAddressFormat.ts    From parity-bridges-ui with GNU General Public License v3.0 6 votes vote down vote up
export default function getValidAddressFormat(address: string) {
  try {
    const decodedReceiverAddress = base58Decode(address);
    const [isValid, , , formatFound] = checkAddressChecksum(decodedReceiverAddress);

    const f = formatFound === GENERIC_SUBSTRATE_PREFIX ? GENERIC : formatFound;
    return { isValid, formatFound: isValid ? f : INCORRECT_FORMAT };
  } catch (e) {
    return { isValid: false, formatFound: INCORRECT_FORMAT };
  }
}
Example #2
Source File: getReceiverAddress.ts    From parity-bridges-ui with GNU General Public License v3.0 5 votes vote down vote up
getReceiverAddress = ({ targetChainDetails, sourceChainDetails, receiverAddress }: Props) => {
  const { configs: sourceConfigs, chain: sourceChain } = sourceChainDetails;
  const {
    apiConnection: { api: targetApi },
    chain: targetChain,
    configs: targetConfigs
  } = targetChainDetails;

  const targetSS58Format = targetConfigs.ss58Format;
  const bridgeId = getBridgeId(targetApi, sourceChain);

  const getChainBySS58Prefix = (prefix: number) => {
    switch (prefix) {
      case GENERIC_SUBSTRATE_PREFIX:
        return GENERIC;
      case targetConfigs.ss58Format:
        return targetChain;
      case sourceConfigs.ss58Format:
        return sourceChain;
      default:
        return '';
    }
  };

  try {
    const decodedReceiverAddress = base58Decode(receiverAddress);
    const [isValidDerivedAcccount, , , ss58Decoded] = checkAddressChecksum(decodedReceiverAddress);

    const formatFound = getChainBySS58Prefix(ss58Decoded) || ss58Decoded.toString();
    const doesTargetFormatMatch = ss58Decoded === targetSS58Format;

    if (isValidDerivedAcccount && doesTargetFormatMatch) {
      return { address: receiverAddress, formatFound };
    }

    const address = getDeriveAccount({
      ss58Format: targetSS58Format,
      address: receiverAddress,
      bridgeId
    });

    return { address, formatFound };
  } catch (e) {
    if (receiverAddress) {
      throw new Error(INCORRECT_FORMAT);
    }
    return { address: '', formatFound: null };
  }
}