Skip to main content
formatting
Source Link
Aniket
  • 3.5k
  • 2
  • 21
  • 43

I've been using ecrecoverecrecover and web3.eth.signweb3.eth.sign to authorize transactions, but when I run web3.eth.signweb3.eth.sign a second time on my server, the VV part of the signature is wrong(or, at least, ecrecoverecrecover thinks it's wrong). I read that it should be 27 or 28, but sign()sign() is giving me 54 and 55. However, if I changes these to 27 and 28, respectively, ecrecoverecrecover works again. What's going on????

exports.sign = async(signer, nonce, amount, forUser) => {
  // pack data into the message
  let ret = [];
  let data = (nonce * 2 ** 32) + amount;
  let hexData = web3.utils.toHex(data).slice(2);
  for(let i = hexData.length; i < 16; i++) {
    hexData = "0" + hexData
  }
  let message = "0x1337beef" + forUser.slice(2) + hexData;
  // sign the message
  let sig = (await web3.eth.sign(message, signer)).slice(2);
  let r = "0x" + sig.slice(0, 64);
  let s = "0x" + sig.slice(64, 128);
  let v = web3.utils.toDecimal('0x' + sig.slice(128, 130)) + 27;
  // THESE NEXT TWO LINES MAKE THINGS WORK BUT THEY
  // ARE STINKY AND I MUST KNOW WHY THEY ARE NEEDED!!
  if(v == 54) { v = 27;}
  if(v == 55) { v = 28;}
  ret.push(message);
  ret.push(v);
  ret.push(r);
  ret.push(s);
  return ret;
}

Thanks in advance!!

I've been using ecrecover and web3.eth.sign to authorize transactions, but when I run web3.eth.sign a second time on my server, the V part of the signature is wrong(or, at least, ecrecover thinks it's wrong). I read that it should be 27 or 28, but sign() is giving me 54 and 55. However, if I changes these to 27 and 28, respectively, ecrecover works again. What's going on????

exports.sign = async(signer, nonce, amount, forUser) => {
  // pack data into the message
  let ret = [];
  let data = (nonce * 2 ** 32) + amount;
  let hexData = web3.utils.toHex(data).slice(2);
  for(let i = hexData.length; i < 16; i++) {
    hexData = "0" + hexData
  }
  let message = "0x1337beef" + forUser.slice(2) + hexData;
  // sign the message
  let sig = (await web3.eth.sign(message, signer)).slice(2);
  let r = "0x" + sig.slice(0, 64);
  let s = "0x" + sig.slice(64, 128);
  let v = web3.utils.toDecimal('0x' + sig.slice(128, 130)) + 27;
  // THESE NEXT TWO LINES MAKE THINGS WORK BUT THEY
  // ARE STINKY AND I MUST KNOW WHY THEY ARE NEEDED!!
  if(v == 54) { v = 27;}
  if(v == 55) { v = 28;}
  ret.push(message);
  ret.push(v);
  ret.push(r);
  ret.push(s);
  return ret;
}

Thanks in advance!!

I've been using ecrecover and web3.eth.sign to authorize transactions, but when I run web3.eth.sign a second time on my server, the V part of the signature is wrong(or, at least, ecrecover thinks it's wrong). I read that it should be 27 or 28, but sign() is giving me 54 and 55. However, if I changes these to 27 and 28, respectively, ecrecover works again. What's going on????

exports.sign = async(signer, nonce, amount, forUser) => {
  // pack data into the message
  let ret = [];
  let data = (nonce * 2 ** 32) + amount;
  let hexData = web3.utils.toHex(data).slice(2);
  for(let i = hexData.length; i < 16; i++) {
    hexData = "0" + hexData
  }
  let message = "0x1337beef" + forUser.slice(2) + hexData;
  // sign the message
  let sig = (await web3.eth.sign(message, signer)).slice(2);
  let r = "0x" + sig.slice(0, 64);
  let s = "0x" + sig.slice(64, 128);
  let v = web3.utils.toDecimal('0x' + sig.slice(128, 130)) + 27;
  // THESE NEXT TWO LINES MAKE THINGS WORK BUT THEY
  // ARE STINKY AND I MUST KNOW WHY THEY ARE NEEDED!!
  if(v == 54) { v = 27;}
  if(v == 55) { v = 28;}
  ret.push(message);
  ret.push(v);
  ret.push(r);
  ret.push(s);
  return ret;
}

Thanks in advance!!

edited title
Link

web3.ethseth.sign sometimes gives V parameter of 54 or 55, ecrecover then fails. What's going on?

Source Link

web3.eths.sign sometimes gives V parameter of 54 or 55, ecrecover then fails. What's going on?

I've been using ecrecover and web3.eth.sign to authorize transactions, but when I run web3.eth.sign a second time on my server, the V part of the signature is wrong(or, at least, ecrecover thinks it's wrong). I read that it should be 27 or 28, but sign() is giving me 54 and 55. However, if I changes these to 27 and 28, respectively, ecrecover works again. What's going on????

exports.sign = async(signer, nonce, amount, forUser) => {
  // pack data into the message
  let ret = [];
  let data = (nonce * 2 ** 32) + amount;
  let hexData = web3.utils.toHex(data).slice(2);
  for(let i = hexData.length; i < 16; i++) {
    hexData = "0" + hexData
  }
  let message = "0x1337beef" + forUser.slice(2) + hexData;
  // sign the message
  let sig = (await web3.eth.sign(message, signer)).slice(2);
  let r = "0x" + sig.slice(0, 64);
  let s = "0x" + sig.slice(64, 128);
  let v = web3.utils.toDecimal('0x' + sig.slice(128, 130)) + 27;
  // THESE NEXT TWO LINES MAKE THINGS WORK BUT THEY
  // ARE STINKY AND I MUST KNOW WHY THEY ARE NEEDED!!
  if(v == 54) { v = 27;}
  if(v == 55) { v = 28;}
  ret.push(message);
  ret.push(v);
  ret.push(r);
  ret.push(s);
  return ret;
}

Thanks in advance!!