| | | 1 | | using NBitcoin; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | | 4 | | |
| | | 5 | | using Domain.Protocol.ValueObjects; |
| | | 6 | | using Domain.Utils; |
| | | 7 | | using Enums; |
| | | 8 | | using Interfaces; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Tagged field for the fallback address |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// The fallback address is a Bitcoin address that can be used to pay the invoice on-chain if the payment fails. |
| | | 15 | | /// </remarks> |
| | | 16 | | /// <seealso cref="ITaggedField"/> |
| | | 17 | | internal sealed class FallbackAddressTaggedField : ITaggedField |
| | | 18 | | { |
| | | 19 | | private readonly byte[] _data; |
| | | 20 | | |
| | 592 | 21 | | public TaggedFieldTypes Type => TaggedFieldTypes.FallbackAddress; |
| | 84 | 22 | | internal BitcoinAddress Value { get; } |
| | 108 | 23 | | public short Length { get; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="DescriptionTaggedField"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="value">The Description</param> |
| | 104 | 29 | | internal FallbackAddressTaggedField(BitcoinAddress value) |
| | | 30 | | { |
| | 104 | 31 | | Value = value; |
| | 104 | 32 | | var data = new List<byte>(); |
| | | 33 | | |
| | | 34 | | switch (value) |
| | | 35 | | { |
| | | 36 | | case BitcoinPubKeyAddress pubKeyAddress: |
| | | 37 | | // P2PKH |
| | 36 | 38 | | data.Add(17); |
| | 36 | 39 | | data.AddRange(pubKeyAddress.Hash.ToBytes()); |
| | 36 | 40 | | Length = 33; |
| | 36 | 41 | | break; |
| | | 42 | | case BitcoinScriptAddress scriptAddress: |
| | | 43 | | // P2SH |
| | 28 | 44 | | data.Add(18); |
| | 28 | 45 | | data.AddRange(scriptAddress.Hash.ToBytes()); |
| | 28 | 46 | | Length = 33; |
| | 28 | 47 | | break; |
| | | 48 | | case BitcoinWitScriptAddress witScriptAddress: |
| | | 49 | | // P2WSH |
| | 20 | 50 | | data.Add(0); |
| | 20 | 51 | | data.AddRange(witScriptAddress.Hash.ToBytes()); |
| | 20 | 52 | | Length = 53; |
| | 20 | 53 | | break; |
| | | 54 | | case BitcoinWitPubKeyAddress witPubKeyAddress: |
| | | 55 | | // P2WPKH |
| | 20 | 56 | | data.Add(0); |
| | 20 | 57 | | data.AddRange(witPubKeyAddress.Hash.ToBytes()); |
| | 20 | 58 | | Length = 33; |
| | | 59 | | break; |
| | | 60 | | } |
| | | 61 | | |
| | 104 | 62 | | _data = [.. data]; |
| | 104 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc/> |
| | | 66 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | | 67 | | { |
| | | 68 | | // Write Address Type |
| | 36 | 69 | | bitWriter.WriteByteAsBits(_data[0], 5); |
| | | 70 | | |
| | | 71 | | // Write data |
| | 36 | 72 | | bitWriter.WriteBits(_data.AsSpan()[1..], (Length - 1) * 5); |
| | 36 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc/> |
| | | 76 | | public bool IsValid() |
| | | 77 | | { |
| | 56 | 78 | | return true; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Reads a FallbackAddressTaggedField from a BitReader |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="bitReader">The BitReader to read from</param> |
| | | 85 | | /// <param name="length">The length of the field</param> |
| | | 86 | | /// <param name="bitcoinNetwork">The network type</param> |
| | | 87 | | /// <returns>The FallbackAddressTaggedField</returns> |
| | | 88 | | /// <exception cref="ArgumentException">Thrown when the address is unknown or invalid</exception> |
| | | 89 | | internal static FallbackAddressTaggedField FromBitReader(BitReader bitReader, short length, |
| | | 90 | | BitcoinNetwork bitcoinNetwork) |
| | | 91 | | { |
| | | 92 | | // Get Address Type |
| | 48 | 93 | | var addressType = bitReader.ReadByteFromBits(5); |
| | 48 | 94 | | var newLength = length - 1; |
| | | 95 | | |
| | | 96 | | // Read address bytes |
| | 48 | 97 | | var data = new byte[(newLength * 5 + 7) / 8]; |
| | 48 | 98 | | bitReader.ReadBits(data.AsSpan(), newLength * 5); |
| | | 99 | | |
| | 48 | 100 | | if (newLength * 5 % 8 != 0 && data[^1] == 0) |
| | 12 | 101 | | data = data[..^1]; |
| | | 102 | | |
| | 48 | 103 | | var network = Network.GetNetwork(bitcoinNetwork) ?? |
| | 48 | 104 | | throw new ArgumentException("Network is unknown or invalid.", nameof(bitcoinNetwork)); |
| | 48 | 105 | | BitcoinAddress address = addressType switch |
| | 48 | 106 | | { |
| | 48 | 107 | | // Witness P2WPKH |
| | 24 | 108 | | 0 when data.Length == 20 => new WitKeyId(data).GetAddress(network), |
| | 48 | 109 | | // Witness P2WSH |
| | 16 | 110 | | 0 when data.Length == 32 => new WitScriptId(data).GetAddress(network), |
| | 48 | 111 | | // P2PKH |
| | 12 | 112 | | 17 => new KeyId(data).GetAddress(network), |
| | 48 | 113 | | // P2SH |
| | 12 | 114 | | 18 => new ScriptId(data).GetAddress(network), |
| | 8 | 115 | | _ => throw new ArgumentException("Address is unknown or invalid.", nameof(bitReader)) |
| | 48 | 116 | | }; |
| | | 117 | | |
| | 40 | 118 | | return new FallbackAddressTaggedField(address); |
| | | 119 | | } |
| | | 120 | | } |