recoverPubKey function

String recoverPubKey({
  1. required String message,
  2. required String sig,
  3. required String coin,
  4. bool? uncompressed,
})

Implementation

String recoverPubKey({
  required String message,
  required String sig,
  required String coin,
  bool? uncompressed,
}) {
  final messageHash = _createEthStyleMessageHash(message);
  final parsedSig = _parseEthSignature(sig);
  final pubKeyUncompressed = recoverPublicKey(messageHash, parsedSig);
  if (uncompressed == true) {
    return HEX.encode(pubKeyUncompressed);
  }

  final uncompressedPrefix = [0x04];
  final pubKeyCompressed = compressPublicKey(
    Uint8List.fromList(uncompressedPrefix + pubKeyUncompressed),
  );
  String pubKeyHex = HEX.encode(pubKeyCompressed);
  if (pubKeyHex.startsWith("02")) {
    pubKeyHex = pubKeyHex.replaceFirst(
      "02",
      "04",
    ); // workaround for Safir backwards compat
  }
  return pubKeyHex;
}