[Ethereum] What’s the best way to transform bytes of a signature into v, r, s in solidity

solidity

Given that solidity does not have any built-in string/bytes manipulation library, I was wondering what the best practice for extracting v, r, s from a signature inside solidity is.

One can encode sig as string v-r-s and use solidity-stringutils to extract them, but isn't there a better way to do this?

Best Answer

bytes sig = ...;
bytes32 r;
bytes32 s;
uint8 v;
assembly {
  r := mload(add(sig, 32))
  s := mload(add(sig, 64))
  v := and(mload(add(sig, 65)), 255)
}
if (v < 27) v += 27;
Related Topic