| | 1 | | using System.Security.Cryptography; |
| | 2 | | using System.Text; |
| | 3 | |
|
| | 4 | | namespace EF.Blockchain.Domain; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Represents a transaction output in the blockchain. |
| | 8 | | /// Contains information about the receiver, the amount, and the parent transaction hash. |
| | 9 | | /// </summary> |
| | 10 | | public class TransactionOutput |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Gets the address of the receiver. |
| | 14 | | /// </summary> |
| 4680 | 15 | | public string ToAddress { get; private set; } |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Gets the amount being transferred. |
| | 19 | | /// </summary> |
| 4792 | 20 | | public int Amount { get; private set; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Gets or sets the transaction hash this output belongs to. |
| | 24 | | /// </summary> |
| 3112 | 25 | | public string? Tx { get; private set; } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the <see cref="TransactionOutput"/> class. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="toAddress">The destination address.</param> |
| | 31 | | /// <param name="amount">The amount to be transferred.</param> |
| | 32 | | /// <param name="tx">Optional parent transaction hash.</param> |
| 976 | 33 | | public TransactionOutput( |
| 976 | 34 | | string toAddress = "", |
| 976 | 35 | | int amount = 0, |
| 976 | 36 | | string? tx = "") |
| 976 | 37 | | { |
| 976 | 38 | | ToAddress = toAddress.Trim(); |
| 976 | 39 | | Amount = amount; |
| 976 | 40 | | Tx = tx; |
| 976 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Validates the transaction output fields. |
| | 45 | | /// </summary> |
| | 46 | | /// <returns>A <see cref="Validation"/> result indicating if the output is valid.</returns> |
| | 47 | | public Validation IsValid() |
| 824 | 48 | | { |
| 824 | 49 | | if (string.IsNullOrWhiteSpace(ToAddress)) |
| 8 | 50 | | return new Validation(false, "Missing address"); |
| | 51 | |
|
| 816 | 52 | | if (Amount < 1) |
| 8 | 53 | | return new Validation(false, "Negative amount"); |
| | 54 | |
|
| 808 | 55 | | return new Validation(true); |
| 824 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Generates a SHA256 hash based on the output's address and amount. |
| | 60 | | /// </summary> |
| | 61 | | /// <returns>A lowercase hexadecimal string representing the hash.</returns> |
| | 62 | | public string GetHash() |
| 2288 | 63 | | { |
| 2288 | 64 | | var input = $"{ToAddress}{Amount}"; |
| 2288 | 65 | | using var sha = SHA256.Create(); |
| 2288 | 66 | | var hashBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(input)); |
| 2288 | 67 | | return Convert.ToHexString(hashBytes).ToLower(); |
| 2288 | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Sets the parent transaction hash for this output. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="txHash">The transaction hash to associate.</param> |
| | 74 | | public void SetTx(string txHash) |
| 1472 | 75 | | { |
| 1472 | 76 | | Tx = txHash; |
| 1472 | 77 | | } |
| | 78 | | } |