validateEvmWalletSignature method

VerifySignedMessageResult validateEvmWalletSignature(
  1. IValidateEvmWalletSignature data
)

Check if a signed message from an EVM wallet address is valid @param {IValidateWalletSignature} data - The data used to validate the EVM signature @returns {VerifySignedMessageResult}

Implementation

VerifySignedMessageResult validateEvmWalletSignature(
    IValidateEvmWalletSignature data) {
  final messageParts = data.message.split('\n');
  final messageTimestamp = messageParts.last;

  // Check if the timestamp is within the valid time range (default 10 minutes)
  if (data.timestamp != null &&
      messageTimestamp != data.timestamp.toString()) {
    throw Exception('Message does not contain a valid timestamp');
  }

  final isValidTimestamp = data.timestamp != null
      ? DateTime.now().millisecondsSinceEpoch - data.timestamp! <=
          (data.signatureValidityMinutes ?? 10) * 60000
      : true;

  // Convert message to typed message
  final List<Map<String, dynamic>> rawTypedData = [
    {"type": "string", "name": "message", "value": data.message}
  ];
  final typedData =
      rawTypedData.map((e) => EIP712TypedData.fromJson(e)).toList();

  // Recover public Key from signature
  final recovered = TypedDataUtil.recoverPublicKey(
      typedData, data.signature, TypedDataVersion.V1);

  // Get address from public key
  final address = bufferToHex(SignatureUtil.publicKeyToAddress(recovered!));

  final isValidAddress = data.walletAddress != null
      ? address.toLowerCase() == data.walletAddress?.toLowerCase()
      : true;

  return VerifySignedMessageResult(
      isValid: isValidTimestamp && isValidAddress, address: address);
}