Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const sign = (msgHash, privKey) => {
if (typeof msgHash === 'string' && msgHash.slice(0, 2) === '0x') {
msgHash = Buffer.alloc(32, msgHash.slice(2), 'hex')
}
const sig = ecsign(msgHash, privKey)
return `0x${sig.r.toString('hex')}${sig.s.toString('hex')}${sig.v.toString(16)}`
}
confirmSig(root, privateKey) {
const vrs = utils.ecsign(
utils.sha3(Buffer.concat([this.hash(false), root])),
privateKey
)
return utils.toBuffer(utils.toRpcSig(vrs.v, vrs.r, vrs.s))
}
let blockNumber = parseInt(i.split('_')[1]);
try {
let txData = {
prev_hash: utxo.getHash().toString('hex'),
prev_block: blockNumber,
token_id: utxo.token_id.toString(),
new_owner: this.nextAddressGen.next(utxo.new_owner).value
};
let txDataForRlp = [ethUtil.addHexPrefix(txData.prev_hash), txData.prev_block, ethUtil.toBuffer(txData.token_id), txData.new_owner];
let txRlpEncoded = ethUtil.hashPersonalMessage(ethUtil.sha3(RLP.encode(txDataForRlp)));
if (utxo.new_owner instanceof Buffer)
utxo.new_owner = ethUtil.addHexPrefix(utxo.new_owner.toString('hex')).toLowerCase();
let signature = ethUtil.ecsign(txRlpEncoded, prkeys[utxo.new_owner]);
txData.signature = ethUtil.toRpcSig(signature.v, signature.r, signature.s).toString("hex");
let createdTx = createSignedTransaction(txData);
this.alltransactions.push(createdTx);
} catch (e) {
console.log(e);
}
}
console.log('TXcount - ', this.alltransactions.length);
}
accountsJson = JSON.parse(fs.readFileSync("./ganache-accounts.json", "utf8"));
}
for (let i = 0; i < signers.length; i += 1) {
let user = signers[i].toString();
user = user.toLowerCase();
let privKey;
if (privKeys[i]) {
privKey = privKeys[i];
} else {
privKey = accountsJson.private_keys[user];
}
const prefixedMessageHash = soliditySha3("\x19Ethereum Signed Message:\n\x20", msgHash);
const sig = ecsign(Buffer.from(prefixedMessageHash.slice(2), "hex"), Buffer.from(privKey, "hex"));
sigV.push(sig.v);
sigR.push(`0x${sig.r.toString("hex")}`);
sigS.push(`0x${sig.s.toString("hex")}`);
}
return { sigV, sigR, sigS };
}
export function signMessageWithPrivKeyV2(privKey: Buffer, msg: string): string {
const hash = hashPersonalMessage(toBuffer(msg));
const signed = ecsign(hash, privKey);
const combined = Buffer.concat([
Buffer.from(signed.r),
Buffer.from(signed.s),
Buffer.from([signed.v])
]);
const combinedHex = combined.toString('hex');
return addHexPrefix(combinedHex);
}
sign (hash)
{
hash = toBuffer(hash);
const signature = ecsign(hash, this.privateKey);
const v = toNumber(signature.v);
const r = toHex(signature.r);
const s = toHex(signature.s);
return {r, s, v};
}
export const ethSign = (msgHex: String, privateKeyHex: string): string => {
const message = ethUtil.toBuffer(msgHex);
const privateKey = ethUtil.toBuffer(privateKeyHex);
const sigParams = ethUtil.ecsign(message, privateKey);
const result = concatSig(sigParams);
return result;
};
personalSign: function (privateKey, msgParams) {
var message = ethUtil.toBuffer(msgParams.data)
var msgHash = ethUtil.hashPersonalMessage(message)
var sig = ethUtil.ecsign(msgHash, privateKey)
var serialized = ethUtil.bufferToHex(this.concatSig(sig.v, sig.r, sig.s))
return serialized
},