| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | 4 | |
|
| | 5 | | using Common.Utils; |
| | 6 | | using Enums; |
| | 7 | | using 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"/> |
| | 16 | | internal sealed class FallbackAddressTaggedField : ITaggedField |
| | 17 | | { |
| | 18 | | private readonly byte[] _data; |
| | 19 | |
|
| 516 | 20 | | public TaggedFieldTypes Type => TaggedFieldTypes.FALLBACK_ADDRESS; |
| 84 | 21 | | internal BitcoinAddress Value { get; } |
| 108 | 22 | | 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> |
| 104 | 28 | | internal FallbackAddressTaggedField(BitcoinAddress value) |
| | 29 | | { |
| 104 | 30 | | Value = value; |
| 104 | 31 | | var data = new List<byte>(); |
| | 32 | |
|
| | 33 | | switch (value) |
| | 34 | | { |
| | 35 | | case BitcoinPubKeyAddress pubKeyAddress: |
| | 36 | | // P2PKH |
| 36 | 37 | | data.Add(17); |
| 36 | 38 | | data.AddRange(pubKeyAddress.Hash.ToBytes()); |
| 36 | 39 | | Length = 33; |
| 36 | 40 | | break; |
| | 41 | | case BitcoinScriptAddress scriptAddress: |
| | 42 | | // P2SH |
| 28 | 43 | | data.Add(18); |
| 28 | 44 | | data.AddRange(scriptAddress.Hash.ToBytes()); |
| 28 | 45 | | Length = 33; |
| 28 | 46 | | break; |
| | 47 | | case BitcoinWitScriptAddress witScriptAddress: |
| | 48 | | // P2WSH |
| 20 | 49 | | data.Add(0); |
| 20 | 50 | | data.AddRange(witScriptAddress.Hash.ToBytes()); |
| 20 | 51 | | Length = 53; |
| 20 | 52 | | break; |
| | 53 | | case BitcoinWitPubKeyAddress witPubKeyAddress: |
| | 54 | | // P2WPKH |
| 20 | 55 | | data.Add(0); |
| 20 | 56 | | data.AddRange(witPubKeyAddress.Hash.ToBytes()); |
| 20 | 57 | | Length = 33; |
| | 58 | | break; |
| | 59 | | } |
| | 60 | |
|
| 104 | 61 | | _data = [.. data]; |
| 104 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <inheritdoc/> |
| | 65 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | 66 | | { |
| | 67 | | // Write Address Type |
| 36 | 68 | | bitWriter.WriteByteAsBits(_data[0], 5); |
| | 69 | |
|
| | 70 | | // Write data |
| 36 | 71 | | bitWriter.WriteBits(_data.AsSpan()[1..], (Length - 1) * 5); |
| 36 | 72 | | } |
| | 73 | |
|
| | 74 | | /// <inheritdoc/> |
| | 75 | | public bool IsValid() |
| | 76 | | { |
| 28 | 77 | | 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 |
| 48 | 91 | | var addressType = bitReader.ReadByteFromBits(5); |
| 48 | 92 | | var newLength = length - 1; |
| | 93 | |
|
| | 94 | | // Read address bytes |
| 48 | 95 | | var data = new byte[(newLength * 5 + 7) / 8]; |
| 48 | 96 | | bitReader.ReadBits(data.AsSpan(), newLength * 5); |
| | 97 | |
|
| 48 | 98 | | if (newLength * 5 % 8 != 0 && data[^1] == 0) |
| | 99 | | { |
| 12 | 100 | | data = data[..^1]; |
| | 101 | | } |
| | 102 | |
|
| 48 | 103 | | BitcoinAddress address = addressType switch |
| 48 | 104 | | { |
| 48 | 105 | | // Witness P2WPKH |
| 24 | 106 | | 0 when data.Length == 20 => new WitKeyId(data).GetAddress(network), |
| 48 | 107 | | // Witness P2WSH |
| 16 | 108 | | 0 when data.Length == 32 => new WitScriptId(data).GetAddress(network), |
| 48 | 109 | | // P2PKH |
| 12 | 110 | | 17 => new KeyId(data).GetAddress(network), |
| 48 | 111 | | // P2SH |
| 12 | 112 | | 18 => new ScriptId(data).GetAddress(network), |
| 8 | 113 | | _ => throw new ArgumentException("Address is unknown or invalid.", nameof(bitReader)) |
| 48 | 114 | | }; |
| | 115 | |
|
| 40 | 116 | | return new FallbackAddressTaggedField(address); |
| | 117 | | } |
| | 118 | | } |