Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * ECDSA public key recovery from signature
- * @param {Buffer} msgHash
- * @param {Number} v
- * @param {Buffer} r
- * @param {Buffer} s
- * @return {Buffer} publicKey
- */
- exports.ecrecover = function (msgHash, v, r, s) {
- var signature = Buffer.concat([exports.setLength(r, 32), exports.setLength(s, 32)], 64)
- var recovery = v - 27
- if (recovery !== 0 && recovery !== 1) {
- throw new Error('Invalid signature v value')
- }
- var senderPubKey = secp256k1.recover(msgHash, signature, recovery)
- return secp256k1.publicKeyConvert(senderPubKey, false).slice(1)
- }
- /**
- * Determines if the signature is valid
- * @return {Boolean}
- */
- 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
- }
- var sig = secp256k1.sign(msgHash, privateKey)
- var ret = {}
- ret.r = sig.signature.slice(0, 32)
- ret.s = sig.signature.slice(32, 64)
- ret.v = sig.recovery + 27
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement