| | 1 | | using NBitcoin; |
| | 2 | | using NBitcoin.Crypto; |
| | 3 | | using System.Security.Cryptography; |
| | 4 | | using System.Text; |
| | 5 | | using System.Text.Json.Serialization; |
| | 6 | |
|
| | 7 | | namespace EF.Blockchain.Domain; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Represents an input in a blockchain transaction, including signature validation and ownership of funds. |
| | 11 | | /// </summary> |
| | 12 | | public class TransactionInput |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Public key (hex) of the sender. |
| | 16 | | /// </summary> |
| 1704 | 17 | | public string FromAddress { get; private set; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Amount of coins being transferred from this input. |
| | 21 | | /// </summary> |
| 1848 | 22 | | public int Amount { get; private set; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Signature proving ownership of the referenced output. |
| | 26 | | /// </summary> |
| 3008 | 27 | | public string Signature { get; private set; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// The hash of the previous transaction output being referenced. |
| | 31 | | /// </summary> |
| 1968 | 32 | | public string PreviousTx { get; private set; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Constructor used for deserialization. |
| | 36 | | /// </summary> |
| | 37 | | [JsonConstructor] |
| 56 | 38 | | public TransactionInput(string fromAddress, int amount, string signature, string previousTx) |
| 56 | 39 | | { |
| 56 | 40 | | FromAddress = fromAddress; |
| 56 | 41 | | Amount = amount; |
| 56 | 42 | | Signature = signature; |
| 56 | 43 | | PreviousTx = previousTx; |
| 56 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Creates a new transaction input with optional values. |
| | 48 | | /// </summary> |
| 656 | 49 | | public TransactionInput(string? fromAddress = null, int? amount = null, string? signature = null, string? previousTx |
| 656 | 50 | | { |
| 656 | 51 | | FromAddress = fromAddress ?? string.Empty; |
| 656 | 52 | | Amount = amount ?? 0; |
| 656 | 53 | | Signature = signature ?? string.Empty; |
| 656 | 54 | | PreviousTx = previousTx ?? string.Empty; |
| 656 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Computes a SHA-256 hash of this transaction input. |
| | 59 | | /// </summary> |
| | 60 | | /// <returns>The lowercase hex hash string.</returns> |
| | 61 | | public string GetHash() |
| 680 | 62 | | { |
| 680 | 63 | | var raw = $"{PreviousTx}{FromAddress}{Amount}"; |
| 680 | 64 | | using var sha256 = SHA256.Create(); |
| 680 | 65 | | var bytes = Encoding.UTF8.GetBytes(raw); |
| 680 | 66 | | var hashBytes = sha256.ComputeHash(bytes); |
| 680 | 67 | | return Convert.ToHexString(hashBytes).ToLower(); |
| 680 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Signs this transaction input using the sender's private key. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="privateKeyHex">The sender's private key in hex format.</param> |
| | 74 | | public void Sign(string privateKeyHex) |
| 512 | 75 | | { |
| 512 | 76 | | var privateKeyBytes = Convert.FromHexString(privateKeyHex); |
| 512 | 77 | | var key = new Key(privateKeyBytes); |
| | 78 | |
|
| 512 | 79 | | var hashHex = GetHash(); |
| 512 | 80 | | var hashBytes = Convert.FromHexString(hashHex); |
| 512 | 81 | | var signature = key.Sign(new uint256(hashBytes)); |
| | 82 | |
|
| 512 | 83 | | Signature = Convert.ToHexString(signature.ToDER()); |
| 512 | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Validates the transaction input by checking the digital signature. |
| | 88 | | /// </summary> |
| | 89 | | /// <returns>A <see cref="Validation"/> result indicating success or failure.</returns> |
| | 90 | | public Validation IsValid() |
| 480 | 91 | | { |
| 480 | 92 | | if (string.IsNullOrWhiteSpace(Signature) || string.IsNullOrWhiteSpace(PreviousTx)) |
| 288 | 93 | | return new Validation(false, "Signature and previous TX are required"); |
| | 94 | |
|
| 192 | 95 | | if (Amount < 1) |
| 16 | 96 | | return new Validation(false, "Amount must be greater than zero"); |
| | 97 | |
|
| | 98 | | try |
| 176 | 99 | | { |
| 176 | 100 | | var pubKeyBytes = Convert.FromHexString(FromAddress); |
| 168 | 101 | | var pubKey = new PubKey(pubKeyBytes); |
| | 102 | |
|
| 168 | 103 | | var hashBytes = Convert.FromHexString(GetHash()); |
| 168 | 104 | | var signatureBytes = Convert.FromHexString(Signature); |
| 168 | 105 | | var ecdsaSig = ECDSASignature.FromDER(signatureBytes); |
| | 106 | |
|
| 168 | 107 | | bool isValid = pubKey.Verify(new uint256(hashBytes), ecdsaSig); |
| | 108 | |
|
| 168 | 109 | | return isValid ? new Validation() : new Validation(false, "Invalid tx input signature"); |
| | 110 | | } |
| 8 | 111 | | catch |
| 8 | 112 | | { |
| 8 | 113 | | return new Validation(false, "Error verifying signature"); |
| | 114 | | } |
| 480 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Creates a new <see cref="TransactionInput"/> from a given <see cref="TransactionOutput"/>. |
| | 119 | | /// </summary> |
| | 120 | | /// <param name="txo">The transaction output to reference.</param> |
| | 121 | | /// <returns>A new input referencing the output.</returns> |
| | 122 | | public static TransactionInput FromTxo(TransactionOutput txo) |
| 8 | 123 | | { |
| 8 | 124 | | return new TransactionInput( |
| 8 | 125 | | fromAddress: txo.ToAddress, |
| 8 | 126 | | amount: txo.Amount, |
| 8 | 127 | | previousTx: txo.Tx |
| 8 | 128 | | ); |
| 8 | 129 | | } |
| | 130 | | } |