ecRecover method

Uint8List ecRecover(
  1. int recId,
  2. Uint8List messageHash,
  3. SignatureData signature, [
  4. bool isCompressed = false,
  5. bool isEthereum = false,
])

Given an arbitrary message hash and an Ethereum message signature encoded in bytes, returns the public key that was used to sign it. https://github.com/web3j/web3j/blob/c0b7b9c2769a466215d416696021aa75127c2ff1/crypto/src/main/java/org/web3j/crypto/Sign.java#L241

Implementation

Uint8List ecRecover(
  int recId,
  Uint8List messageHash,
  SignatureData signature,
  [bool isCompressed = false,
  bool isEthereum = false]
) {

  Uint8List r = padLeftUint8List(encodeBigIntAsUnsigned(signature.r));
  Uint8List s = padLeftUint8List(encodeBigIntAsUnsigned(signature.s));

  var header = recId & 0xFF;
  // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
  //                  0x1D = second key with even y, 0x1E = second key with odd y
  header = isEthereum ? header : header + magicNum;
  if (header < magicNum || header > 34) {
    throw Exception('Header byte out of range: $header');
  }

  final sig = ECSignature(signature.r, signature.s);

  recId = header - magicNum;
  Uint8List? pubKey = recoverFromSignature(recId, sig, messageHash, isCompressed);
  if (pubKey == null) {
    throw Exception('Could not recover public key from signature');
  }
  return isCompressed ? pubKey : pubKey.sublist(1);
}