< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Outputs.ToLocalOutput
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Outputs/ToLocalOutput.cs
Tag: 30_15166811759
Line coverage
96%
Covered lines: 26
Uncovered lines: 1
Coverable lines: 27
Total lines: 81
Line coverage: 96.2%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_ScriptType()100%11100%
get_LocalDelayedPubKey()100%11100%
get_RevocationPubKey()100%11100%
get_ToSelfDelay()100%11100%
.ctor(...)100%11100%
GenerateToLocalScript(...)50%4493.75%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Outputs/ToLocalOutput.cs

#LineLine coverage
 1using NBitcoin;
 2
 3namespace NLightning.Infrastructure.Bitcoin.Outputs;
 4
 5using Domain.Money;
 6using Exceptions;
 7
 8/// <summary>
 9/// Represents a to_local output in a commitment transaction.
 10/// </summary>
 11public class ToLocalOutput : BaseOutput
 12{
 18413    public override ScriptType ScriptType => ScriptType.P2WSH;
 14
 415    public PubKey LocalDelayedPubKey { get; }
 416    public PubKey RevocationPubKey { get; }
 417    public uint ToSelfDelay { get; }
 18
 19    public ToLocalOutput(PubKey localDelayedPubKey, PubKey revocationPubKey, uint toSelfDelay, LightningMoney amount)
 18020        : base(GenerateToLocalScript(localDelayedPubKey, revocationPubKey, toSelfDelay), amount)
 21    {
 18022        ArgumentNullException.ThrowIfNull(localDelayedPubKey);
 18023        ArgumentNullException.ThrowIfNull(revocationPubKey);
 24
 18025        LocalDelayedPubKey = localDelayedPubKey;
 18026        RevocationPubKey = revocationPubKey;
 18027        ToSelfDelay = toSelfDelay;
 18028    }
 29
 30    private static Script GenerateToLocalScript(PubKey localDelayedPubKey, PubKey revocationPubKey, uint toSelfDelay)
 31    {
 32        /* The following script can be read as:
 33         ** spendingPubKey = the pubkey trying to sign this spend
 34         ** signature = the signature given by spendingPubKey
 35         ** nSequence = Provided by the spending transaction
 36         **
 37         ** if (revocationPubKey = spendingPubKey)
 38         ** { // Revocation key path
 39         **   return revocationPubKey
 40         ** }
 41         ** else
 42         ** { // Delayed key path
 43         **   if (nSequence < toSelfDelay)
 44         **   {
 45         **     exit
 46         **   }
 47         **   else
 48         **   {
 49         **     return localDelayedPubKey
 50         **   }
 51         ** }
 52         ** if (signature is valid for spendingPubKey)
 53         ** {
 54         **   return true
 55         ** }
 56         */
 57
 18058        ArgumentNullException.ThrowIfNull(localDelayedPubKey);
 18059        ArgumentNullException.ThrowIfNull(revocationPubKey);
 60
 18061        var script = new Script(
 18062            OpcodeType.OP_IF,
 18063            Op.GetPushOp(revocationPubKey.ToBytes()),
 18064            OpcodeType.OP_ELSE,
 18065            Op.GetPushOp(toSelfDelay),
 18066            OpcodeType.OP_CHECKSEQUENCEVERIFY,
 18067            OpcodeType.OP_DROP,
 18068            Op.GetPushOp(localDelayedPubKey.ToBytes()),
 18069            OpcodeType.OP_ENDIF,
 18070            OpcodeType.OP_CHECKSIG
 18071        );
 72
 73        // Check if script is correct
 18074        if (script.IsUnspendable || !script.IsValid)
 75        {
 076            throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'.");
 77        }
 78
 18079        return script;
 80    }
 81}