< 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: 30_15166811759
Line coverage
98%
Covered lines: 54
Uncovered lines: 1
Coverable lines: 55
Total lines: 95
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.78%

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;
 10
 11/// <summary>
 12/// Represents an offered HTLC output in a commitment transaction.
 13/// </summary>
 14public class OfferedHtlcOutput : BaseHtlcOutput
 15{
 27616    public override ScriptType ScriptType => ScriptType.P2WPKH;
 17
 18    [SetsRequiredMembers]
 19    public OfferedHtlcOutput(LightningMoney anchorAmount, PubKey revocationPubKey, PubKey remoteHtlcPubKey,
 20                             PubKey localHtlcPubKey, ReadOnlyMemory<byte> paymentHash, LightningMoney amount,
 21                             ulong cltvExpiry)
 27622        : base(GenerateToRemoteHtlcScript(anchorAmount, revocationPubKey, remoteHtlcPubKey, localHtlcPubKey,
 27623                                          paymentHash),
 27624               amount)
 25    {
 27626        RevocationPubKey = revocationPubKey;
 27627        RemoteHtlcPubKey = remoteHtlcPubKey;
 27628        LocalHtlcPubKey = localHtlcPubKey;
 27629        PaymentHash = paymentHash;
 27630        CltvExpiry = cltvExpiry;
 27631    }
 32
 33    private static Script GenerateToRemoteHtlcScript(LightningMoney anchorAmount, PubKey revocationPubKey, PubKey remote
 34    {
 35        // Hash the revocationPubKey
 27636        using var sha256 = new Sha256();
 27637        Span<byte> revocationPubKeySha256Hash = stackalloc byte[CryptoConstants.SHA256_HASH_LEN];
 27638        sha256.AppendData(revocationPubKey.ToBytes());
 27639        sha256.GetHashAndReset(revocationPubKeySha256Hash);
 27640        var revocationPubKeyHashRipemd160 = Ripemd160.Hash(revocationPubKeySha256Hash);
 41
 42        // Hash the paymentHash
 27643        var paymentHashRipemd160 = Ripemd160.Hash(paymentHash.Span);
 44
 27645        List<Op> ops = [
 27646            OpcodeType.OP_DUP,
 27647            OpcodeType.OP_HASH160,
 27648            Op.GetPushOp(revocationPubKeyHashRipemd160),
 27649            OpcodeType.OP_EQUAL,
 27650            OpcodeType.OP_IF,
 27651            OpcodeType.OP_CHECKSIG,
 27652            OpcodeType.OP_ELSE,
 27653            Op.GetPushOp(remoteHtlcPubKey.ToBytes()),
 27654            OpcodeType.OP_SWAP,
 27655            OpcodeType.OP_SIZE,
 27656            Op.GetPushOp(32),
 27657            OpcodeType.OP_EQUAL,
 27658            OpcodeType.OP_NOTIF,
 27659            OpcodeType.OP_DROP,
 27660            OpcodeType.OP_2,
 27661            OpcodeType.OP_SWAP,
 27662            Op.GetPushOp(localHtlcPubKey.ToBytes()),
 27663            OpcodeType.OP_2,
 27664            OpcodeType.OP_CHECKMULTISIG,
 27665            OpcodeType.OP_ELSE,
 27666            OpcodeType.OP_HASH160,
 27667            Op.GetPushOp(paymentHashRipemd160),
 27668            OpcodeType.OP_EQUALVERIFY,
 27669            OpcodeType.OP_CHECKSIG,
 27670            OpcodeType.OP_ENDIF
 27671        ];
 72
 27673        if (!anchorAmount.IsZero)
 74        {
 9675            ops.AddRange([
 9676                OpcodeType.OP_1,
 9677                OpcodeType.OP_CHECKSEQUENCEVERIFY,
 9678                OpcodeType.OP_DROP
 9679            ]);
 80        }
 81
 82        // Close last IF
 27683        ops.Add(OpcodeType.OP_ENDIF);
 84
 27685        var script = new Script(ops);
 86
 87        // Check if the script is correct
 27688        if (script.IsUnspendable || !script.IsValid)
 89        {
 090            throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'.");
 91        }
 92
 27693        return script;
 27694    }
 95}