< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Outputs.ReceivedHtlcOutput
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Outputs/ReceivedHtlcOutput.cs
Tag: 30_15166811759
Line coverage
98%
Covered lines: 57
Uncovered lines: 1
Coverable lines: 58
Total lines: 100
Line coverage: 98.2%
Branch coverage
66%
Covered branches: 4
Total branches: 6
Branch coverage: 66.6%
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%
.ctor(...)100%11100%
GenerateToLocalHtlcScript(...)66.67%6697.92%

File(s)

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

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using NBitcoin;
 3
 4namespace NLightning.Infrastructure.Bitcoin.Outputs;
 5
 6using Crypto.Hashes;
 7using Domain.Crypto.Constants;
 8using Domain.Money;
 9using Exceptions;
 10
 11/// <summary>
 12/// Represents a received HTLC output in a commitment transaction.
 13/// </summary>
 14public class ReceivedHtlcOutput : BaseHtlcOutput
 15{
 19616    public override ScriptType ScriptType => ScriptType.P2WSH;
 17
 18    [SetsRequiredMembers]
 19    public ReceivedHtlcOutput(LightningMoney anchorAmount, PubKey revocationPubKey, PubKey remoteHtlcPubKey,
 20                              PubKey localHtlcPubKey, ReadOnlyMemory<byte> paymentHash, LightningMoney amount,
 21                              ulong cltvExpiry)
 19622        : base(GenerateToLocalHtlcScript(anchorAmount, revocationPubKey, remoteHtlcPubKey, localHtlcPubKey, paymentHash,
 19623                                         cltvExpiry),
 19624               amount)
 25    {
 19626        RevocationPubKey = revocationPubKey;
 19627        RemoteHtlcPubKey = remoteHtlcPubKey;
 19628        LocalHtlcPubKey = localHtlcPubKey;
 19629        PaymentHash = paymentHash;
 19630        CltvExpiry = cltvExpiry;
 19631    }
 32
 33    private static Script GenerateToLocalHtlcScript(LightningMoney anchorAmount, PubKey revocationPubKey,
 34                                                    PubKey remoteHtlcPubKey, PubKey localHtlcPubKey,
 35                                                    ReadOnlyMemory<byte> paymentHash, ulong cltvExpiry)
 36    {
 37        // Hash the revocationPubKey
 19638        using var sha256 = new Sha256();
 19639        Span<byte> revocationPubKeySha256Hash = stackalloc byte[CryptoConstants.SHA256_HASH_LEN];
 19640        sha256.AppendData(revocationPubKey.ToBytes());
 19641        sha256.GetHashAndReset(revocationPubKeySha256Hash);
 19642        var revocationPubKeyHashRipemd160 = Ripemd160.Hash(revocationPubKeySha256Hash);
 43
 44        // Hash the paymentHash
 19645        var paymentHashRipemd160 = Ripemd160.Hash(paymentHash.Span);
 46
 19647        List<Op> ops = [
 19648            OpcodeType.OP_DUP,
 19649            OpcodeType.OP_HASH160,
 19650            Op.GetPushOp(revocationPubKeyHashRipemd160),
 19651            OpcodeType.OP_EQUAL,
 19652            OpcodeType.OP_IF,
 19653            OpcodeType.OP_CHECKSIG,
 19654            OpcodeType.OP_ELSE,
 19655            Op.GetPushOp(remoteHtlcPubKey.ToBytes()),
 19656            OpcodeType.OP_SWAP,
 19657            OpcodeType.OP_SIZE,
 19658            Op.GetPushOp(32),
 19659            OpcodeType.OP_EQUAL,
 19660            OpcodeType.OP_IF,
 19661            OpcodeType.OP_HASH160,
 19662            Op.GetPushOp(paymentHashRipemd160),
 19663            OpcodeType.OP_EQUALVERIFY,
 19664            OpcodeType.OP_2,
 19665            OpcodeType.OP_SWAP,
 19666            Op.GetPushOp(localHtlcPubKey.ToBytes()),
 19667            OpcodeType.OP_2,
 19668            OpcodeType.OP_CHECKMULTISIG,
 19669            OpcodeType.OP_ELSE,
 19670            OpcodeType.OP_DROP,
 19671            Op.GetPushOp((long)cltvExpiry),
 19672            OpcodeType.OP_CHECKLOCKTIMEVERIFY,
 19673            OpcodeType.OP_DROP,
 19674            OpcodeType.OP_CHECKSIG,
 19675            OpcodeType.OP_ENDIF
 19676        ];
 77
 19678        if (!anchorAmount.IsZero)
 79        {
 6080            ops.AddRange([
 6081                OpcodeType.OP_1,
 6082                OpcodeType.OP_CHECKSEQUENCEVERIFY,
 6083                OpcodeType.OP_DROP
 6084            ]);
 85        }
 86
 87        // Close last IF
 19688        ops.Add(OpcodeType.OP_ENDIF);
 89
 19690        var script = new Script(ops);
 91
 92        // Check if script is correct
 19693        if (script.IsUnspendable || !script.IsValid)
 94        {
 095            throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'.");
 96        }
 97
 19698        return script;
 19699    }
 100}