Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
};
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': {
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 = {
export const isValidHexString = (hex: string): boolean => (typeof hex === 'string' || hex instanceof String) && isHex(hex)
const validateHexString = str => {
if (str === '') return true;
str =
str.substring(0, 2) === '0x'
? str.substring(2).toUpperCase()
: str.toUpperCase();
return utils.isHex(str);
};
export const isValidHexString = (hex: string): boolean => (typeof hex === 'string' || hex instanceof String) && web3Utils.isHex(hex)
export const isValidHexString = (hex: string): boolean => (typeof hex === 'string' || hex instanceof String) && web3Utils.isHex(hex)
async signMessage(message: any): Promise {
const payload =
typeof message === 'string' && !isHex(message)
? { message }
: { messageData: message };
return this.wallet.signMessage(payload);
}
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();