How to use the @zilliqa-js/util.validation.isAddress function in @zilliqa-js/util

To help you get started, we’ve selected a few @zilliqa-js/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zilpay / zil-pay / extension / inpage / browser / zilPay.js View on Github external
setDefaultAccount(account) {
    if (!this.isEnable || !account || !this.isConnect) {
      return null;
    }

    const isAddress = validation.isAddress(account.address);
    
    if (!isAddress) {
      throw new Error('input param is not address type');
    }
    this.defaultAccount = {
      base16: toChecksumAddress(account.address),
      base58: encodeBase58(account.address),
      bech32: toBech32Address(account.address)
    };
    dispatchEvent(onAddressListing);
  }
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / bech32.ts View on Github external
export const toBech32Address = (address: string): string => {
  if (!validation.isAddress(address)) {
    throw new Error('Invalid address format.');
  }

  const addrBz = convertBits(
    Buffer.from(address.replace('0x', ''), 'hex'),
    8,
    5,
  );

  if (addrBz === null) {
    throw new Error('Could not convert byte Buffer to 5-bit Buffer');
  }

  return encode(HRP, addrBz);
};
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / util.ts View on Github external
export const toChecksumAddress = (address: string): string => {
  if (!validation.isAddress(address)) {
    throw new Error(`${address} is not a valid base 16 address`);
  }

  address = address.toLowerCase().replace('0x', '');
  const hash = hashjs
    .sha256()
    .update(address, 'hex')
    .digest('hex');
  const v = new BN(hash, 'hex', 'be');
  let ret = '0x';

  for (let i = 0; i < address.length; i++) {
    if ('0123456789'.indexOf(address[i]) !== -1) {
      ret += address[i];
    } else {
      ret += v.and(new BN(2).pow(new BN(255 - 6 * i))).gte(new BN(1))
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / zilAddress.ts View on Github external
const decoded = fromBech32Address(this.raw, 'tzil').toLowerCase();
      this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
      return;
    }

    if (
      bech32Bool === true &&
      validation.isAddress(fromBech32Address(this.raw))
    ) {
      this.addressType = AddressType.bech32;
      const decoded = fromBech32Address(this.raw).toLowerCase();
      this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
      return;
    }

    if (base58Bool === true && validation.isAddress(decodeBase58(this.raw))) {
      this.addressType = AddressType.base58;
      const decoded = decodeBase58(this.raw).toLowerCase();
      this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
      return;
    }
    throw new Error('unknown address');
  }
}
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / zilAddress.ts View on Github external
private getAddressType() {
    const addrBool = validation.isAddress(this.raw);
    const base58Bool = validation.isBase58(this.raw);
    const bech32Bool = validation.isBech32(this.raw);
    const bech32TestNetBool = validation.isBech32TestNet(this.raw);
    const checksumBool = isValidChecksumAddress(this.raw);

    if (addrBool === true && checksumBool === false) {
      this.addressType = AddressType.bytes20;
      this.bytes20 = this.raw.startsWith('0x')
        ? this.raw.substring(2)
        : this.raw;
      return;
    }

    if (addrBool === true && checksumBool === true) {
      this.addressType = AddressType.checkSum;
      this.bytes20 = this.raw.toLowerCase().substring(2);
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / util.ts View on Github external
export const isValidChecksumAddress = (address: string): boolean => {
  return (
    validation.isAddress(address.replace('0x', '')) &&
    toChecksumAddress(address) === address
  );
};
github zilpay / zil-pay / packages / inpage / utils.js View on Github external
export function toAccountFormat(address) {
  const isAddress = validation.isAddress(address)

  if (!isAddress) {
    throw new Error('input param is not address type')
  }

  return {
    base16: toChecksumAddress(address),
    bech32: toBech32Address(address)
  }
}
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / zilAddress.ts View on Github external
return;
    }

    if (
      bech32TestNetBool === true &&
      validation.isAddress(fromBech32Address(this.raw, 'tzil'))
    ) {
      this.addressType = AddressType.bech32;
      const decoded = fromBech32Address(this.raw, 'tzil').toLowerCase();
      this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
      return;
    }

    if (
      bech32Bool === true &&
      validation.isAddress(fromBech32Address(this.raw))
    ) {
      this.addressType = AddressType.bech32;
      const decoded = fromBech32Address(this.raw).toLowerCase();
      this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
      return;
    }

    if (base58Bool === true && validation.isAddress(decodeBase58(this.raw))) {
      this.addressType = AddressType.base58;
      const decoded = decodeBase58(this.raw).toLowerCase();
      this.bytes20 = decoded.startsWith('0x') ? decoded.substring(2) : decoded;
      return;
    }
    throw new Error('unknown address');
  }
}