How to use the web3-utils.isHex function in web3-utils

To help you get started, we’ve selected a few web3-utils 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 ethereum / web3.js / packages / web3-core-helpers / src / formatters.js View on Github external
var _txInputFormatter = function (options) {

    if (options.to) { // it might be contract creation
        options.to = inputAddressFormatter(options.to);
    }

    if (options.data && options.input) {
        throw new Error('You can\'t have "data" and "input" as properties of transactions at the same time, please use either "data" or "input" instead.');
    }

    if (!options.data && options.input) {
        options.data = options.input;
        delete options.input;
    }

    if (options.data && !utils.isHex(options.data)) {
        throw new Error('The data field must be HEX encoded data.');
    }

    // allow both
    if (options.gas || options.gasLimit) {
        options.gas = options.gas || options.gasLimit;
    }

    ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) {
        return options[key] !== undefined;
    }).forEach(function (key) {
        options[key] = utils.numberToHex(options[key]);
    });

    return options;
};
github MyEtherWallet / MyEtherWallet / src / wallets / software / basicWallet.js View on Github external
options.manualPrivateKey,
            options.privPassword
          );
          break;
        }
        case 'manualPrivateKey': {
          if (typeof options.manualPrivateKey === 'object')
            throw new Error('Supplied Private Key Must Be A String');

          // eslint-disable-next-line
          const privKey =
            options.manualPrivateKey.indexOf('0x') === 0
              ? options.manualPrivateKey
              : '0x' + options.manualPrivateKey;

          if (!utils.isHex(options.manualPrivateKey)) {
            throw Error(
              'BasicWallet decryptWallet manualPrivateKey: Invalid Hex'
            );
          } else if (!ethUtil.isValidPrivate(ethUtil.toBuffer(privKey))) {
            this.wallet = null;
            throw Error(
              'BasicWallet decryptWallet manualPrivateKey: Invalid Private Key'
            );
          } else {
            this.wallet = BasicWallet.createWallet(
              this.fixPkey(options.manualPrivateKey)
            );
          }
          break;
        }
        case 'fromPrivateKeyFile': {
github citahub / cita-sdk-js / packages / cita-sdk / lib / utils / validators.js View on Github external
exports.isPrivateKey = (privateKey) => {
    if (!privateKey) {
        console.warn('No Private Key');
        return false;
    }
    if (typeof privateKey !== 'string') {
        console.warn('Invalid Type of Private Key');
        return false;
    }
    const pk = privateKey.replace(/^0x/, '');
    if (pk.length !== 64) {
        console.warn('Invalid Length of Private Key');
        return false;
    }
    if (!utils.isHex(pk)) {
        console.warn('Invalid Hex of Private Key');
        return false;
    }
    return true;
};
exports.default = {
github streamr-dev / streamr-platform / app / src / marketplace / utils / smartContract.js View on Github external
export const isValidHexString = (hex: string): boolean => (typeof hex === 'string' || hex instanceof String) && isHex(hex)
github MyEtherWallet / MyEtherWallet / src / helpers / misc.js View on Github external
const validateHexString = str => {
  if (str === '') return true;
  str =
    str.substring(0, 2) === '0x'
      ? str.substring(2).toUpperCase()
      : str.toUpperCase();
  return utils.isHex(str);
};
github streamr-dev / streamr-platform / marketplace / src / utils / smartContract.js View on Github external
export const isValidHexString = (hex: string): boolean => (typeof hex === 'string' || hex instanceof String) && web3Utils.isHex(hex)
github streamr-dev / streamr-platform / src / utils / smartContract.js View on Github external
export const isValidHexString = (hex: string): boolean => (typeof hex === 'string' || hex instanceof String) && web3Utils.isHex(hex)
github JoinColony / colonyJS / packages / colony-js-client / src / lib / EthersWrappedWallet / index.js View on Github external
async signMessage(message: any): Promise {
    const payload =
      typeof message === 'string' && !isHex(message)
        ? { message }
        : { messageData: message };
    return this.wallet.signMessage(payload);
  }
github citahub / cita-sdk-js / packages / cita-signer / lib / index.js View on Github external
throw new Error(`Chain Id should be set`);
    }
    else {
        tx[`setChainId${_version}`](_chainId);
    }
    try {
        const _data = exports.hex2bytes(data);
        tx.setData(new Uint8Array(_data));
    }
    catch (err) {
        throw err;
    }
    tx.setVersion(+version);
    const txMsg = tx.serializeBinary();
    const hashedMsg = exports.sha3(txMsg).slice(2);
    if (_privateKey.replace(/^0x/, '').length !== 64 || !utils.isHex(_privateKey)) {
        throw new Error('Invalid Private Key');
    }
    const key = exports.ec.keyFromPrivate(_privateKey.replace(/^0x/, ''), 'hex');
    const sign = key.sign(new Buffer(hashedMsg.toString(), 'hex'), { canonical: true });
    let sign_r = sign.r.toString(16).padStart(64, 0);
    let sign_s = sign.s.toString(16).padStart(64, 0);
    const signature = (sign_r + sign_s).padStart(128, 0);
    const sign_buffer = new Buffer(signature, 'hex');
    const sigBytes = new Uint8Array(65);
    sigBytes.set(sign_buffer);
    sigBytes[64] = sign.recoveryParam;
    const unverifiedTx = new blockchainPb.UnverifiedTransaction();
    unverifiedTx.setTransaction(tx);
    unverifiedTx.setCrypto(blockchainPb.Crypto.DEFAULT);
    unverifiedTx.setSignature(sigBytes);
    const serializedUnverifiedTx = unverifiedTx.serializeBinary();