getPublicKey static method

ECDSAPublicKey? getPublicKey(
  1. List<int> message,
  2. List<int> signature, {
  3. bool hashMessage = true,
  4. int? payloadLength,
  5. bool useEthPrefix = false,
})

Gets the recovered ECDSAPublicKey from a message and signature.

Parameters:

  • message: The message.
  • signature: The signature bytes.
  • hashMessage: Whether to hash the message before recovering the public key (default is true).
  • payloadLength: An optional payload length to include in the message prefix.
  • useEthPrefix: Whether to use the Ethereum or Tron personal sign prefix (default is false).

Returns:

  • The recovered ECDSAPublicKey.

Implementation

static ECDSAPublicKey? getPublicKey(List<int> message, List<int> signature,
    {bool hashMessage = true,
    int? payloadLength,
    bool useEthPrefix = false}) {
  if (hashMessage) {
    String prefix = useEthPrefix
        ? ETHSignerConst.ethPersonalSignPrefix
        : TronSignerConst.tronPersonalSignPrefix;
    prefix =
        prefix + (payloadLength?.toString() ?? message.length.toString());
    final prefixBytes =
        StringUtils.encode(prefix, type: StringEncoding.ascii);
    message = QuickCrypto.keccack256Hash(<int>[...prefixBytes, ...message]);
  }

  final ethSignature = ETHSignature.fromBytes(signature);
  final toBytes = ethSignature.toBytes(false);
  final recoverId = toBytes[ETHSignerConst.ethSignatureLength];
  final signatureBytes = ECDSASignature.fromBytes(
      toBytes.sublist(0, ETHSignerConst.ethSignatureLength),
      ETHSignerConst.secp256);

  return signatureBytes.recoverPublicKey(
      message, ETHSignerConst.secp256, recoverId);
}