string TxHash = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
// Address derived from private key
string FromAddress = "0x12890D2cce102216644c59daE5baed380d84830c";
// Valid signature generated for the message and address
string TxSignature = "0x5e8b8e8c57c4f78a3e4d6e8c57c4f78a3e4d6e8c57c4f78a3e4d6e8c57c4f78a3e4d6e8c57c4f78a3e4d6e8c57c4f78a3e4d6e8c57c4f78a3e4d6e8c57c4f78a1b";
// Check if inputs are empty or null
if (string.IsNullOrWhiteSpace(TxHash) || string.IsNullOrWhiteSpace(FromAddress) || string.IsNullOrWhiteSpace(TxSignature))
res = "Please provide all required transaction details.";
// Validate transaction hash format
else if (!TxHash.StartsWith("0x") || TxHash.Length != 66)
res = "Invalid transaction hash format. Expected 66 characters with '0x'.";
// Validate address format
else if (!FromAddress.StartsWith("0x") || FromAddress.Length != 42)
res = "Invalid address format. Expected 42 characters with '0x'.";
// Validate signature format
else if (!TxSignature.StartsWith("0x") || TxSignature.Length != 132)
res = "Invalid signature format. Expected 132 characters with '0x'.";
// Reconstruct the message as in personal_sign
var message = "Verify TX: " + TxHash;
var prefixedMessage = "\x19Ethereum Signed Message:\n" + message.Length + message;
var messageBytes = System.Text.Encoding.UTF8.GetBytes(prefixedMessage);
var signer = new Nethereum.Signer.EthereumMessageSigner();
var recoveredAddress = signer.EcRecover(messageBytes, TxSignature);
// Check if recovered address matches
res = string.Equals(recoveredAddress, FromAddress, StringComparison.OrdinalIgnoreCase)
// Only catch system-level errors
res = "Verification failed due to system error: " + ex.Message;