How to use the @polkadot/util.isHex function in @polkadot/util

To help you get started, we’ve selected a few @polkadot/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 polkadot-js / ui / packages / ui-keyring / src / Keyring.ts View on Github external
private loadAddress (json: KeyringJson, key: string): void {
    const { isRecent, whenCreated = 0 } = json.meta;

    if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
      this._store.remove(key);
      return;
    }

    const address = this.encodeAddress(
      isHex(json.address)
        ? hexToU8a(json.address)
        // FIXME Just for the transition period (ignoreChecksum)
        : this.decodeAddress(json.address, true)
    );
    const [, hexAddr] = key.split(':');

    this.addresses.add(this._store, address, json);
    this.rewriteKey(json, key, hexAddr, addressKey);
  }
github vue-polkadot / vue-ui / packages / vue-keyring / src / Keyring.ts View on Github external
private loadAddress(json: KeyringJson, key: string): void {
    const { isRecent, whenCreated = 0 } = json.meta;

    if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
      this._store.remove(key);
      return;
    }

    const address = this.encodeAddress(
      isHex(json.address)
        ? hexToU8a(json.address)
        // FIXME Just for the transition period (ignoreChecksum)
        : this.decodeAddress(json.address, true)
    );
    const [, hexAddr] = key.split(':');

    this.addresses.add(this._store, address, json);
    this.rewriteKey(json, key, hexAddr, addressKey);
  }
github polkadot-js / api / packages / types / src / codec / EnumType.ts View on Github external
private static decodeViaValue (def: TypesDef, value?: any): Decoded {
    if (value instanceof Enum) {
      return Enum.createValue(def, value._index, value.raw);
    } else if (isU8a(value)) {
      return Enum.createValue(def, value[0], value.subarray(1));
    } else if (isNumber(value)) {
      return Enum.createValue(def, value);
    } else if (isString(value)) {
      const _str = value.toString();

      return isHex(_str)
        ? Enum.decodeViaValue(def, hexToU8a(_str))
        : Enum.createViaJSON(def, _str);
    } else if (isObject(value)) {
      const key = Object.keys(value)[0];

      return Enum.createViaJSON(def, key, value[key]);
    }

    // Worst-case scenario, return the first with default
    return Enum.createValue(def, 0);
  }
github polkadot-js / apps / packages / react-components / src / InputFile.tsx View on Github external
function convertResult (result: ArrayBuffer, convertHex?: boolean): Uint8Array {
  const data = new Uint8Array(result);

  // this converts the input (if detected as hex), vai the hex conversion route
  if (convertHex && data[0] === BYTE_STR_0 && data[1] === BYTE_STR_X) {
    const hex = u8aToString(data);

    if (isHex(hex)) {
      return hexToU8a(hex);
    }
  }

  return data;
}
github polkadot-js / apps / packages / app-explorer / src / Query.tsx View on Github external
function stateFromValue (value: string): State {
  const isValidHex = isHex(value, 256);
  const isNumber = !isValidHex && /^\d+$/.test(value);

  return {
    value,
    isValid: isValidHex || isNumber
  };
}
github polkadot-js / apps / packages / app-accounts / src / modals / Create.tsx View on Github external
function isHexSeed (seed: string): boolean {
  return isHex(seed) && seed.length === 66;
}
github polkadot-js / apps / packages / app-accounts / src / modals / Create.tsx View on Github external
function isHexSeed (seed: string): boolean {
  return isHex(seed) && seed.length === 66;
}
github polkadot-js / common / docs / examples / keyring / 03_validate_address / index.js View on Github external
const isValidAddressPolkadotAddress = () => {
  try {
    encodeAddress(isHex(address) ? hexToU8a(address) : decodeAddress(address));
    return true;
  } catch (error) {
    return false;
  }
};
github polkadot-js / apps / packages / app-toolbox / src / Hash.tsx View on Github external
const _onChangeData = (data: string): void => {
    const isHexData = isHex(data);

    setState({
      data,
      hash: blake2AsHex(
        isHexData
          ? hexToU8a(data)
          : stringToU8a(data),
        256
      ),
      isHexData
    });
  };
github polkadot-js / apps / packages / app-explorer / src / BlockInfo / index.tsx View on Github external
const { value } = useParams();
  const [stateValue, setStateValue] = useState(value);

  useEffect((): void => {
    if (value && value !== stateValue) {
      setStateValue(value);
    } else if (!stateValue && bestNumber) {
      setStateValue(bestNumber.toString());
    }
  }, [bestNumber, value]);

  if (!stateValue) {
    return null;
  }

  const Component = isHex(stateValue)
    ? BlockByHash
    : BlockByNumber;

  return (
    <>
      
      
    
  );
}