< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Protocol.Models.CommitmentNumber
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Protocol/Models/CommitmentNumber.cs
Tag: 30_15166811759
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 94
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.Infrastructure/Protocol/Models/CommitmentNumber.cs

#LineLine coverage
 1using NBitcoin;
 2
 3namespace NLightning.Infrastructure.Protocol.Models;
 4
 5using Crypto.Hashes;
 6
 7/// <summary>
 8/// Manages Lightning Network commitment numbers and their obscuring as defined in BOLT3.
 9/// </summary>
 10public sealed class CommitmentNumber
 11{
 12    /// <summary>
 13    /// Gets the commitment number value.
 14    /// </summary>
 55615    public ulong Value { get; private set; }
 16
 17    /// <summary>
 18    /// Gets the obscuring factor derived from payment basepoints.
 19    /// </summary>
 29620    public ulong ObscuringFactor { get; }
 21
 22    /// <summary>
 23    /// Gets the obscured commitment number (value XOR obscuring factor).
 24    /// </summary>
 28825    public ulong ObscuredValue => Value ^ ObscuringFactor;
 26
 27    /// <summary>
 28    /// Represents a commitment number in the Lightning Network.
 29    /// </summary>
 25230    public CommitmentNumber(PubKey localPaymentBasepoint, PubKey remotePaymentBasepoint,
 25231                            ulong initialValue = 0)
 32    {
 25233        ArgumentNullException.ThrowIfNull(localPaymentBasepoint);
 25234        ArgumentNullException.ThrowIfNull(remotePaymentBasepoint);
 35
 25236        Value = initialValue;
 25237        ObscuringFactor = CalculateObscuringFactor(localPaymentBasepoint, remotePaymentBasepoint);
 25238    }
 39
 40    /// <summary>
 41    /// Increments the commitment number.
 42    /// </summary>
 43    /// <returns>This instance for chaining.</returns>
 44    public CommitmentNumber Increment()
 45    {
 446        Value++;
 447        return this;
 48    }
 49
 50    /// <summary>
 51    /// Calculates the transaction locktime value using the obscured commitment number.
 52    /// </summary>
 53    /// <returns>The transaction locktime.</returns>
 54    public LockTime CalculateLockTime()
 55    {
 14056        return new LockTime((0x20 << 24) | (uint)(ObscuredValue & 0xFFFFFF));
 57    }
 58
 59    /// <summary>
 60    /// Calculates the transaction sequence value using the obscured commitment number.
 61    /// </summary>
 62    /// <returns>The transaction sequence.</returns>
 63    public Sequence CalculateSequence()
 64    {
 14465        return new Sequence((uint)((0x80UL << 24) | ((ObscuredValue >> 24) & 0xFFFFFF)));
 66    }
 67
 68    /// <summary>
 69    /// Calculates the 48-bit obscuring factor by hashing the concatenation of payment basepoints.
 70    /// </summary>
 71    /// <param name="localBasepoint">The local payment basepoint.</param>
 72    /// <param name="remoteBasepoint">The remote payment basepoint.</param>
 73    /// <returns>The 48-bit obscuring factor as ulong.</returns>
 74    private static ulong CalculateObscuringFactor(PubKey localBasepoint, PubKey remoteBasepoint)
 75    {
 25276        using var sha256 = new Sha256();
 77
 78        // Hash the concatenation of payment basepoints
 25279        sha256.AppendData(localBasepoint.ToBytes());
 25280        sha256.AppendData(remoteBasepoint.ToBytes());
 81
 25282        Span<byte> hashResult = stackalloc byte[32];
 25283        sha256.GetHashAndReset(hashResult);
 84
 85        // Extract the lower 48 bits (6 bytes) of the hash
 25286        ulong obscuringFactor = 0;
 352887        for (var i = 26; i < 32; i++) // Last 6 bytes of the 32-byte hash
 88        {
 151289            obscuringFactor = (obscuringFactor << 8) | hashResult[i];
 90        }
 91
 25292        return obscuringFactor;
 25293    }
 94}