< Summary

Information
Class: EF.Blockchain.Domain.TransactionOutput
Assembly: EF.Blockchain.Domain
File(s): C:\dev\@web3\web3-001-ef-blockchain\backend\EF.Blockchain\src\EF.Blockchain.Domain\TransactionOutput.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 78
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_ToAddress()100%11100%
get_Amount()100%11100%
get_Tx()100%11100%
.ctor(...)100%11100%
IsValid()100%44100%
GetHash()100%11100%
SetTx(...)100%11100%

File(s)

C:\dev\@web3\web3-001-ef-blockchain\backend\EF.Blockchain\src\EF.Blockchain.Domain\TransactionOutput.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3
 4namespace 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>
 10public class TransactionOutput
 11{
 12    /// <summary>
 13    /// Gets the address of the receiver.
 14    /// </summary>
 468015    public string ToAddress { get; private set; }
 16
 17    /// <summary>
 18    /// Gets the amount being transferred.
 19    /// </summary>
 479220    public int Amount { get; private set; }
 21
 22    /// <summary>
 23    /// Gets or sets the transaction hash this output belongs to.
 24    /// </summary>
 311225    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>
 97633    public TransactionOutput(
 97634        string toAddress = "",
 97635        int amount = 0,
 97636        string? tx = "")
 97637    {
 97638        ToAddress = toAddress.Trim();
 97639        Amount = amount;
 97640        Tx = tx;
 97641    }
 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()
 82448    {
 82449        if (string.IsNullOrWhiteSpace(ToAddress))
 850            return new Validation(false, "Missing address");
 51
 81652        if (Amount < 1)
 853            return new Validation(false, "Negative amount");
 54
 80855        return new Validation(true);
 82456    }
 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()
 228863    {
 228864        var input = $"{ToAddress}{Amount}";
 228865        using var sha = SHA256.Create();
 228866        var hashBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
 228867        return Convert.ToHexString(hashBytes).ToLower();
 228868    }
 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)
 147275    {
 147276        Tx = txHash;
 147277    }
 78}