Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
verifySignature () {
const msgHash = this.hash(false, false);
// All transaction signatures whose s-value is greater than secp256k1n/2 are considered invalid.
if (new BN(this.s).cmp(N_DIV_2) === 1) {
return false
}
try {
let v = ethUtil.bufferToInt(this.v)
// if (this._chainId > 0) {
// v -= this._chainId * 2 + 8
// }
this._senderPubKey = ethUtil.ecrecover(msgHash, v, this.r, this.s)
} catch (e) {
return false
}
return !!this._senderPubKey
}
var claim = this.props.claim,
hasKey
var hashedSignature = web3.utils.soliditySha3(
this.props.subject,
claim.claimType,
claim.data
)
const prefixedMsg = web3.eth.accounts.hashMessage(hashedSignature)
if (claim.scheme === '4') {
hasKey = true
} else {
var dataBuf = toBuffer(prefixedMsg)
var sig = fromRpcSig(claim.signature)
var recovered = ecrecover(dataBuf, sig.v, sig.r, sig.s)
var recoveredKeyBuf = pubToAddress(recovered)
var recoveredKey = bufferToHex(recoveredKeyBuf)
var hashedRecovered = web3.utils.soliditySha3(recoveredKey)
var issuer = new web3.eth.Contract(ClaimHolder.abi, claim.issuer)
try {
hasKey = await issuer.methods.keyHasPurpose(hashedRecovered, 3).call()
} catch (e) {
/* Ignore */
}
}
this.setState({
icon: hasKey ? 'fa-check' : 'fa-times',
className: hasKey ? 'text-success' : 'text-danger',
text: hasKey ? 'Valid' : 'Invalid'
verifySignature () {
const msgHash = this.hash(false)
// All transaction signatures whose s-value is greater than secp256k1n/2 are considered invalid.
if (this._homestead && new BN(this.s).cmp(N_DIV_2) === 1) {
return false
}
try {
let v = ethUtil.bufferToInt(this.v)
if (this._chainId > 0) {
v -= this._chainId * 2 + 8
}
this._senderPubKey = ethUtil.ecrecover(msgHash, v, this.r, this.s)
} catch (e) {
return false
}
return !!this._senderPubKey
}
signHeader = (headerHash, privateKey, extraData) => {
const sig = eth_util.ecsign(headerHash, privateKey)
if (this._chainId > 0) {
sig.v += this._chainId * 2 + 8
}
const pubKey = eth_util.ecrecover(headerHash, sig.v, sig.r, sig.s);
const addrBuf = eth_util.pubToAddress(pubKey);
const newSigBytes = Buffer.concat([sig.r, sig.s]);
let newSig;
const bytes = utils.hexToBytes(extraData)
const finalByte = bytes.splice(bytes.length-1)
if (finalByte.toString('hex')=="0") {
newSig = newSigBytes.toString('hex') + '00';
}
if (finalByte.toString('hex')=="1") {
newSig = newSigBytes.toString('hex') + '01';
}
return newSig;
}
const ecrecover = (msg, sig) => {
const r = ethUtils.toBuffer(sig.slice(0, 66))
const s = ethUtils.toBuffer('0x' + sig.slice(66, 130))
const v = 27 + parseInt(sig.slice(130, 132))
const m = ethUtils.toBuffer(msg)
const pub = ethUtils.ecrecover(m, v, r, s)
return '0x' + ethUtils.pubToAddress(pub).toString('hex')
}
function recoverPublicKey(hash, sig) {
const signature = ethUtil.toBuffer(sig)
const sigParams = ethUtil.fromRpcSig(signature)
return ethUtil.ecrecover(hash, sigParams.v, sigParams.r, sigParams.s)
}
const isValidSignature = (account, signature, message) => {
let pubkey;
const v = parseInt(signature.config.slice(2, 4), 16);
const method = parseInt(signature.config.slice(4, 6), 16);
if (method === 0) {
pubkey = ecrecover(
hashPersonalMessage(toBuffer(message)),
v,
toBuffer(signature.r),
toBuffer(signature.s)
);
} else if (method === 1) {
pubkey = ecrecover(toBuffer(message), v, toBuffer(signature.r), toBuffer(signature.s));
} else {
throw new Error('wrong method');
}
const address = '0x' + pubToAddress(pubkey).toString('hex');
return address.toLowerCase() == account.toLowerCase();
};
export function recoverPublicKey(sig: string, hash: string): string {
const sigParams = ethUtil.fromRpcSig(sig);
const hashBuffer = ethUtil.toBuffer(hash);
const result = ethUtil.ecrecover(
hashBuffer,
sigParams.v,
sigParams.r,
sigParams.s
);
const signer = ethUtil.bufferToHex(ethUtil.publicToAddress(result));
return signer;
}
async function recoverSigner(message, signature) {
let split = util.fromRpcSig(signature);
let publicKey = util.ecrecover(message, split.v, split.r, split.s);
let signer = util.pubToAddress(publicKey).toString("hex");
return signer;
}