< Summary - Combined Code Coverage

Information
Class: NLightning.Domain.Protocol.Models.CommitmentNumber
Assembly: NLightning.Domain
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Domain/Protocol/Models/CommitmentNumber.cs
Tag: 36_15743069263
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 89
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
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_Value()100%11100%
get_ObscuringFactor()100%11100%
get_ObscuredValue()100%11100%
.ctor(...)100%11100%
Increment()100%11100%
CalculateLockTime()100%11100%
CalculateSequence()100%11100%
CalculateObscuringFactor(...)100%22100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Domain/Protocol/Models/CommitmentNumber.cs

#LineLine coverage
 1namespace NLightning.Domain.Protocol.Models;
 2
 3using Bitcoin.ValueObjects;
 4using Crypto.Hashes;
 5using Crypto.ValueObjects;
 6
 7/// <summary>
 8/// Manages Lightning Network commitment numbers and their obscuring as defined in BOLT3.
 9/// </summary>
 10public class CommitmentNumber
 11{
 12    /// <summary>
 13    /// Gets the commitment number value.
 14    /// </summary>
 39215    public ulong Value { get; private set; }
 16
 17    /// <summary>
 18    /// Gets the obscuring factor derived from payment basepoints.
 19    /// </summary>
 15620    public ulong ObscuringFactor { get; }
 21
 22    /// <summary>
 23    /// Gets the obscured commitment number (value XOR obscuring factor).
 24    /// </summary>
 14825    public ulong ObscuredValue => Value ^ ObscuringFactor;
 26
 27    /// <summary>
 28    /// Represents a commitment number in the Lightning Network.
 29    /// </summary>
 22830    public CommitmentNumber(CompactPubKey localPaymentBasepoint, CompactPubKey remotePaymentBasepoint, ISha256 sha256,
 22831                            ulong initialValue = 0)
 32    {
 22833        Value = initialValue;
 22834        ObscuringFactor = CalculateObscuringFactor(localPaymentBasepoint, remotePaymentBasepoint, sha256);
 22835    }
 36
 37    /// <summary>
 38    /// Increments the commitment number.
 39    /// </summary>
 40    /// <returns>This instance for chaining.</returns>
 41    public CommitmentNumber Increment()
 42    {
 443        Value++;
 444        return this;
 45    }
 46
 47    /// <summary>
 48    /// Calculates the transaction locktime value using the obscured commitment number.
 49    /// </summary>
 50    /// <returns>The transaction locktime.</returns>
 51    public BitcoinLockTime CalculateLockTime()
 52    {
 7253        return new BitcoinLockTime((0x20 << 24) | (uint)(ObscuredValue & 0xFFFFFF));
 54    }
 55
 56    /// <summary>
 57    /// Calculates the transaction sequence value using the obscured commitment number.
 58    /// </summary>
 59    /// <returns>The transaction sequence.</returns>
 60    public BitcoinSequence CalculateSequence()
 61    {
 7262        return new BitcoinSequence((uint)((0x80UL << 24) | ((ObscuredValue >> 24) & 0xFFFFFF)));
 63    }
 64
 65    /// <summary>
 66    /// Calculates the 48-bit obscuring factor by hashing the concatenation of payment basepoints.
 67    /// </summary>
 68    /// <param name="localBasepoint">The local payment basepoint.</param>
 69    /// <param name="remoteBasepoint">The remote payment basepoint.</param>
 70    /// <param name="sha256">The SHA256 hash function instance.</param>
 71    /// <returns>The 48-bit obscuring factor as ulong.</returns>
 72    private static ulong CalculateObscuringFactor(CompactPubKey localBasepoint, CompactPubKey remoteBasepoint,
 73                                                  ISha256 sha256)
 74    {
 75        // Hash the concatenation of payment basepoints
 22876        sha256.AppendData(localBasepoint);
 22877        sha256.AppendData(remoteBasepoint);
 78
 22879        Span<byte> hashResult = stackalloc byte[32];
 22880        sha256.GetHashAndReset(hashResult);
 81
 82        // Extract the lower 48 bits (6 bytes) of the hash
 22883        ulong obscuringFactor = 0;
 319284        for (var i = 26; i < 32; i++) // Last 6 bytes of the 32-byte hash
 136885            obscuringFactor = (obscuringFactor << 8) | hashResult[i];
 86
 22887        return obscuringFactor;
 88    }
 89}