< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Outputs.OfferedHtlcOutput
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Outputs/OfferedHtlcOutput.cs
Tag: 36_15743069263
Line coverage
98%
Covered lines: 54
Uncovered lines: 1
Coverable lines: 55
Total lines: 98
Line coverage: 98.1%
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%
GenerateToRemoteHtlcScript(...)66.67%6697.83%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Outputs/OfferedHtlcOutput.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 an offered HTLC output in a commitment transaction.
 14/// </summary>
 15public class OfferedHtlcOutput : BaseHtlcOutput
 16{
 8017    public override ScriptType ScriptType => ScriptType.P2WPKH;
 18
 19    [SetsRequiredMembers]
 20    public OfferedHtlcOutput(LightningMoney amount, ulong cltvExpiry, bool hasAnchor, PubKey localHtlcPubKey,
 21                             ReadOnlyMemory<byte> paymentHash, PubKey remoteHtlcPubKey, PubKey revocationPubKey)
 8022        : base(amount,
 8023               GenerateToRemoteHtlcScript(hasAnchor, localHtlcPubKey, paymentHash, remoteHtlcPubKey, revocationPubKey))
 24
 25    {
 8026        RevocationPubKey = revocationPubKey;
 8027        RemoteHtlcPubKey = remoteHtlcPubKey;
 8028        LocalHtlcPubKey = localHtlcPubKey;
 8029        PaymentHash = paymentHash;
 8030        CltvExpiry = cltvExpiry;
 8031    }
 32
 33    private static Script GenerateToRemoteHtlcScript(bool hasAnchor, PubKey localHtlcPubKey,
 34                                                     ReadOnlyMemory<byte> paymentHash, PubKey remoteHtlcPubKey,
 35                                                     PubKey revocationPubKey)
 36    {
 37        // Hash the revocationPubKey
 8038        using var sha256 = new Sha256();
 8039        Span<byte> revocationPubKeySha256Hash = stackalloc byte[CryptoConstants.Sha256HashLen];
 8040        sha256.AppendData(revocationPubKey.ToBytes());
 8041        sha256.GetHashAndReset(revocationPubKeySha256Hash);
 8042        var revocationPubKeyHashRipemd160 = Ripemd160.Hash(revocationPubKeySha256Hash);
 43
 44        // Hash the paymentHash
 8045        var paymentHashRipemd160 = Ripemd160.Hash(paymentHash.Span);
 46
 8047        List<Op> ops =
 8048        [
 8049            OpcodeType.OP_DUP,
 8050            OpcodeType.OP_HASH160,
 8051            Op.GetPushOp(revocationPubKeyHashRipemd160),
 8052            OpcodeType.OP_EQUAL,
 8053            OpcodeType.OP_IF,
 8054            OpcodeType.OP_CHECKSIG,
 8055            OpcodeType.OP_ELSE,
 8056            Op.GetPushOp(remoteHtlcPubKey.ToBytes()),
 8057            OpcodeType.OP_SWAP,
 8058            OpcodeType.OP_SIZE,
 8059            Op.GetPushOp(32),
 8060            OpcodeType.OP_EQUAL,
 8061            OpcodeType.OP_NOTIF,
 8062            OpcodeType.OP_DROP,
 8063            OpcodeType.OP_2,
 8064            OpcodeType.OP_SWAP,
 8065            Op.GetPushOp(localHtlcPubKey.ToBytes()),
 8066            OpcodeType.OP_2,
 8067            OpcodeType.OP_CHECKMULTISIG,
 8068            OpcodeType.OP_ELSE,
 8069            OpcodeType.OP_HASH160,
 8070            Op.GetPushOp(paymentHashRipemd160),
 8071            OpcodeType.OP_EQUALVERIFY,
 8072            OpcodeType.OP_CHECKSIG,
 8073            OpcodeType.OP_ENDIF
 8074        ];
 75
 8076        if (hasAnchor)
 77        {
 1678            ops.AddRange([
 1679                OpcodeType.OP_1,
 1680                OpcodeType.OP_CHECKSEQUENCEVERIFY,
 1681                OpcodeType.OP_DROP
 1682            ]);
 83        }
 84
 85        // Close last IF
 8086        ops.Add(OpcodeType.OP_ENDIF);
 87
 8088        var script = new Script(ops);
 89
 90        // Check if the script is correct
 8091        if (script.IsUnspendable || !script.IsValid)
 92        {
 093            throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'.");
 94        }
 95
 8096        return script;
 8097    }
 98}