< Summary - Combined Code Coverage

Information
Class: NLightning.Bolt11.Models.TaggedFields.FallbackAddressTaggedField
Assembly: NLightning.Bolt11
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Bolt11/Models/TaggedFields/FallbackAddressTaggedField.cs
Tag: 30_15166811759
Line coverage
100%
Covered lines: 46
Uncovered lines: 0
Coverable lines: 46
Total lines: 118
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Type()100%11100%
get_Value()100%11100%
get_Length()100%11100%
.ctor(...)100%88100%
WriteToBitWriter(...)100%11100%
IsValid()100%11100%
FromBitReader(...)100%1414100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Bolt11/Models/TaggedFields/FallbackAddressTaggedField.cs

#LineLine coverage
 1using NBitcoin;
 2
 3namespace NLightning.Bolt11.Models.TaggedFields;
 4
 5using Common.Utils;
 6using Enums;
 7using Interfaces;
 8
 9/// <summary>
 10/// Tagged field for the fallback address
 11/// </summary>
 12/// <remarks>
 13/// The fallback address is a Bitcoin address that can be used to pay the invoice on-chain if the payment fails.
 14/// </remarks>
 15/// <seealso cref="ITaggedField"/>
 16internal sealed class FallbackAddressTaggedField : ITaggedField
 17{
 18    private readonly byte[] _data;
 19
 51620    public TaggedFieldTypes Type => TaggedFieldTypes.FALLBACK_ADDRESS;
 8421    internal BitcoinAddress Value { get; }
 10822    public short Length { get; }
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="DescriptionTaggedField"/> class.
 26    /// </summary>
 27    /// <param name="value">The Description</param>
 10428    internal FallbackAddressTaggedField(BitcoinAddress value)
 29    {
 10430        Value = value;
 10431        var data = new List<byte>();
 32
 33        switch (value)
 34        {
 35            case BitcoinPubKeyAddress pubKeyAddress:
 36                // P2PKH
 3637                data.Add(17);
 3638                data.AddRange(pubKeyAddress.Hash.ToBytes());
 3639                Length = 33;
 3640                break;
 41            case BitcoinScriptAddress scriptAddress:
 42                // P2SH
 2843                data.Add(18);
 2844                data.AddRange(scriptAddress.Hash.ToBytes());
 2845                Length = 33;
 2846                break;
 47            case BitcoinWitScriptAddress witScriptAddress:
 48                // P2WSH
 2049                data.Add(0);
 2050                data.AddRange(witScriptAddress.Hash.ToBytes());
 2051                Length = 53;
 2052                break;
 53            case BitcoinWitPubKeyAddress witPubKeyAddress:
 54                // P2WPKH
 2055                data.Add(0);
 2056                data.AddRange(witPubKeyAddress.Hash.ToBytes());
 2057                Length = 33;
 58                break;
 59        }
 60
 10461        _data = [.. data];
 10462    }
 63
 64    /// <inheritdoc/>
 65    public void WriteToBitWriter(BitWriter bitWriter)
 66    {
 67        // Write Address Type
 3668        bitWriter.WriteByteAsBits(_data[0], 5);
 69
 70        // Write data
 3671        bitWriter.WriteBits(_data.AsSpan()[1..], (Length - 1) * 5);
 3672    }
 73
 74    /// <inheritdoc/>
 75    public bool IsValid()
 76    {
 2877        return true;
 78    }
 79
 80    /// <summary>
 81    /// Reads a FallbackAddressTaggedField from a BitReader
 82    /// </summary>
 83    /// <param name="bitReader">The BitReader to read from</param>
 84    /// <param name="length">The length of the field</param>
 85    /// <param name="network">The network type</param>
 86    /// <returns>The FallbackAddressTaggedField</returns>
 87    /// <exception cref="ArgumentException">Thrown when the address is unknown or invalid</exception>
 88    internal static FallbackAddressTaggedField FromBitReader(BitReader bitReader, short length, Network network)
 89    {
 90        // Get Address Type
 4891        var addressType = bitReader.ReadByteFromBits(5);
 4892        var newLength = length - 1;
 93
 94        // Read address bytes
 4895        var data = new byte[(newLength * 5 + 7) / 8];
 4896        bitReader.ReadBits(data.AsSpan(), newLength * 5);
 97
 4898        if (newLength * 5 % 8 != 0 && data[^1] == 0)
 99        {
 12100            data = data[..^1];
 101        }
 102
 48103        BitcoinAddress address = addressType switch
 48104        {
 48105            // Witness P2WPKH
 24106            0 when data.Length == 20 => new WitKeyId(data).GetAddress(network),
 48107            // Witness P2WSH
 16108            0 when data.Length == 32 => new WitScriptId(data).GetAddress(network),
 48109            // P2PKH
 12110            17 => new KeyId(data).GetAddress(network),
 48111            // P2SH
 12112            18 => new ScriptId(data).GetAddress(network),
 8113            _ => throw new ArgumentException("Address is unknown or invalid.", nameof(bitReader))
 48114        };
 115
 40116        return new FallbackAddressTaggedField(address);
 117    }
 118}