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).

Implementation

static ECDSAPublicKey getPublicKey(
  List<int> message,
  List<int> signature, {
  bool hashMessage = true,
  int? payloadLength,
  bool useEthPrefix = false,
}) {
  if (hashMessage) {
    String prefix =
        useEthPrefix
            ? CryptoSignerConst.ethPersonalSignPrefix
            : CryptoSignerConst.tronSignMessagePrefix;
    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[CryptoSignerConst.ecdsaSignatureLength];
  final signatureBytes = ECDSASignature.fromBytes(
    toBytes.sublist(0, CryptoSignerConst.ecdsaSignatureLength),
    CryptoSignerConst.generatorSecp256k1,
  );

  return signatureBytes.recoverPublicKey(
    message,
    CryptoSignerConst.generatorSecp256k1,
    recoverId,
  );
}