< 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: 36_15743069263
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{
 10813    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(LightningMoney amount, PubKey localDelayedPubKey, PubKey revocationPubKey, uint toSelfDelay)
 10420        : base(amount, GenerateToLocalScript(localDelayedPubKey, revocationPubKey, toSelfDelay))
 21    {
 10422        ArgumentNullException.ThrowIfNull(localDelayedPubKey);
 10423        ArgumentNullException.ThrowIfNull(revocationPubKey);
 24
 10425        LocalDelayedPubKey = localDelayedPubKey;
 10426        RevocationPubKey = revocationPubKey;
 10427        ToSelfDelay = toSelfDelay;
 10428    }
 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
 10458        ArgumentNullException.ThrowIfNull(localDelayedPubKey);
 10459        ArgumentNullException.ThrowIfNull(revocationPubKey);
 60
 10461        var script = new Script(
 10462            OpcodeType.OP_IF,
 10463            Op.GetPushOp(revocationPubKey.ToBytes()),
 10464            OpcodeType.OP_ELSE,
 10465            Op.GetPushOp(toSelfDelay),
 10466            OpcodeType.OP_CHECKSEQUENCEVERIFY,
 10467            OpcodeType.OP_DROP,
 10468            Op.GetPushOp(localDelayedPubKey.ToBytes()),
 10469            OpcodeType.OP_ENDIF,
 10470            OpcodeType.OP_CHECKSIG
 10471        );
 72
 73        // Check if the script is correct
 10474        if (script.IsUnspendable || !script.IsValid)
 75        {
 076            throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'.");
 77        }
 78
 10479        return script;
 80    }
 81}