< 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: 36_15743069263
Line coverage
89%
Covered lines: 53
Uncovered lines: 6
Coverable lines: 59
Total lines: 101
Line coverage: 89.8%
Branch coverage
50%
Covered branches: 3
Total branches: 6
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%
.ctor(...)100%11100%
GenerateToLocalHtlcScript(...)50%6.07687.76%

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;
 10using Infrastructure.Crypto.Hashes;
 11
 12/// <summary>
 13/// Represents a received HTLC output in a commitment transaction.
 14/// </summary>
 15public class ReceivedHtlcOutput : BaseHtlcOutput
 16{
 6817    public override ScriptType ScriptType => ScriptType.P2WSH;
 18
 19    [SetsRequiredMembers]
 20    public ReceivedHtlcOutput(LightningMoney amount, ulong cltvExpiry, bool hasAnchor, PubKey localHtlcPubKey,
 21                              ReadOnlyMemory<byte> paymentHash, PubKey remoteHtlcPubKey, PubKey revocationPubKey)
 6822        : base(amount,
 6823               GenerateToLocalHtlcScript(hasAnchor, cltvExpiry, localHtlcPubKey, paymentHash, remoteHtlcPubKey,
 6824                                         revocationPubKey))
 25    {
 6826        RevocationPubKey = revocationPubKey;
 6827        RemoteHtlcPubKey = remoteHtlcPubKey;
 6828        LocalHtlcPubKey = localHtlcPubKey;
 6829        PaymentHash = paymentHash;
 6830        CltvExpiry = cltvExpiry;
 6831    }
 32
 33    private static Script GenerateToLocalHtlcScript(bool hasAnchor, ulong cltvExpiry, PubKey localHtlcPubKey,
 34                                                    ReadOnlyMemory<byte> paymentHash, PubKey remoteHtlcPubKey,
 35                                                    PubKey revocationPubKey)
 36    {
 37        // Hash the revocationPubKey
 6838        using var sha256 = new Sha256();
 6839        Span<byte> revocationPubKeySha256Hash = stackalloc byte[CryptoConstants.Sha256HashLen];
 6840        sha256.AppendData(revocationPubKey.ToBytes());
 6841        sha256.GetHashAndReset(revocationPubKeySha256Hash);
 6842        var revocationPubKeyHashRipemd160 = Ripemd160.Hash(revocationPubKeySha256Hash);
 43
 44        // Hash the paymentHash
 6845        var paymentHashRipemd160 = Ripemd160.Hash(paymentHash.Span);
 46
 6847        List<Op> ops =
 6848        [
 6849            OpcodeType.OP_DUP,
 6850            OpcodeType.OP_HASH160,
 6851            Op.GetPushOp(revocationPubKeyHashRipemd160),
 6852            OpcodeType.OP_EQUAL,
 6853            OpcodeType.OP_IF,
 6854            OpcodeType.OP_CHECKSIG,
 6855            OpcodeType.OP_ELSE,
 6856            Op.GetPushOp(remoteHtlcPubKey.ToBytes()),
 6857            OpcodeType.OP_SWAP,
 6858            OpcodeType.OP_SIZE,
 6859            Op.GetPushOp(32),
 6860            OpcodeType.OP_EQUAL,
 6861            OpcodeType.OP_IF,
 6862            OpcodeType.OP_HASH160,
 6863            Op.GetPushOp(paymentHashRipemd160),
 6864            OpcodeType.OP_EQUALVERIFY,
 6865            OpcodeType.OP_2,
 6866            OpcodeType.OP_SWAP,
 6867            Op.GetPushOp(localHtlcPubKey.ToBytes()),
 6868            OpcodeType.OP_2,
 6869            OpcodeType.OP_CHECKMULTISIG,
 6870            OpcodeType.OP_ELSE,
 6871            OpcodeType.OP_DROP,
 6872            Op.GetPushOp((long)cltvExpiry),
 6873            OpcodeType.OP_CHECKLOCKTIMEVERIFY,
 6874            OpcodeType.OP_DROP,
 6875            OpcodeType.OP_CHECKSIG,
 6876            OpcodeType.OP_ENDIF
 6877        ];
 78
 6879        if (hasAnchor)
 80        {
 081            ops.AddRange([
 082                OpcodeType.OP_1,
 083                OpcodeType.OP_CHECKSEQUENCEVERIFY,
 084                OpcodeType.OP_DROP
 085            ]);
 86        }
 87
 88        // Close last IF
 6889        ops.Add(OpcodeType.OP_ENDIF);
 90
 6891        var script = new Script(ops);
 92
 93        // Check if script is correct
 6894        if (script.IsUnspendable || !script.IsValid)
 95        {
 096            throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'.");
 97        }
 98
 6899        return script;
 68100    }
 101}