| | 1 | | using System.Text; |
| | 2 | | using NBitcoin.DataEncoders; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Bitcoin.Encoders; |
| | 5 | |
|
| | 6 | | using Domain.Constants; |
| | 7 | | using Domain.Utils; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Bech32 encoder for lightning invoices |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="hrp">The Human Readable Part</param> |
| 156 | 13 | | internal sealed class Bech32Encoder(string? hrp = null) : NBitcoin.DataEncoders.Bech32Encoder(hrp is null ? Encoding.UTF |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Encode the lightning invoice into a bech32 string |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="bitWriter">Bit writer to write to</param> |
| | 19 | | /// <param name="signature">Signature to encode</param> |
| | 20 | | /// <returns>String representing the invoice</returns> |
| | 21 | | internal string EncodeLightningInvoice(BitWriter bitWriter, byte[] signature) |
| | 22 | | { |
| | 23 | | // Convert to 5 bits per byte |
| 72 | 24 | | var convertedSignature = ConvertBits(signature.AsReadOnly(), 8, 5); |
| 72 | 25 | | var convertedData = ConvertBits(bitWriter.ToArray().AsReadOnly(), 8, 5); |
| | 26 | |
|
| | 27 | | // Check for padding |
| 72 | 28 | | var expectedLength = bitWriter.TotalBits / 5; |
| 72 | 29 | | if (convertedData.Length != expectedLength) |
| | 30 | | { |
| 72 | 31 | | convertedData = convertedData[..expectedLength]; |
| | 32 | | } |
| | 33 | |
|
| 72 | 34 | | var invoiceData = new byte[104 + convertedData.Length]; |
| 72 | 35 | | convertedData.CopyTo(invoiceData, 0); |
| 72 | 36 | | convertedSignature.CopyTo(invoiceData, convertedData.Length); |
| | 37 | |
|
| 72 | 38 | | return EncodeData(invoiceData, Bech32EncodingType.BECH32); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Decode the lightning invoice from a bech32 string |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="invoiceString">String representing the invoice</param> |
| | 45 | | /// <param name="data">Data part of the invoice</param> |
| | 46 | | /// <param name="signature">Signature part of the invoice</param> |
| | 47 | | /// <param name="hrp">Human Readable Part of the invoice</param> |
| | 48 | | internal static void DecodeLightningInvoice(string invoiceString, out byte[] data, out byte[] signature, out string |
| | 49 | | { |
| | 50 | | // Be lenient and covert it all to lower case |
| 92 | 51 | | invoiceString = invoiceString.ToLowerInvariant(); |
| | 52 | |
|
| | 53 | | // Validate the prefix |
| 92 | 54 | | if (!invoiceString.StartsWith(InvoiceConstants.Prefix)) |
| | 55 | | { |
| 4 | 56 | | throw new ArgumentException("Missing prefix in invoice", nameof(invoiceString)); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | // Extract human readable part |
| 88 | 60 | | var separatorIndex = invoiceString.LastIndexOf(InvoiceConstants.Separator); |
| | 61 | | switch (separatorIndex) |
| | 62 | | { |
| | 63 | | case -1: |
| 4 | 64 | | throw new ArgumentException("Invalid invoice format", nameof(invoiceString)); |
| | 65 | | case 0: |
| 0 | 66 | | throw new ArgumentException("Missing human readable part in invoice", nameof(invoiceString)); |
| | 67 | | case > 21: |
| 0 | 68 | | throw new ArgumentException("Human readable part too long in invoice", nameof(invoiceString)); |
| | 69 | | } |
| | 70 | |
|
| 84 | 71 | | hrp = invoiceString[..separatorIndex]; |
| 84 | 72 | | var bech32 = new Bech32Encoder(invoiceString[..separatorIndex]) |
| 84 | 73 | | { |
| 84 | 74 | | StrictLength = false |
| 84 | 75 | | }; |
| 84 | 76 | | var invoiceData = bech32.DecodeDataRaw(invoiceString, out var encodingType); |
| | 77 | |
|
| 80 | 78 | | if (encodingType != Bech32EncodingType.BECH32) |
| | 79 | | { |
| 0 | 80 | | throw new ArgumentException("Invalid encoding type in invoice", nameof(invoiceString)); |
| | 81 | | } |
| | 82 | |
|
| 80 | 83 | | signature = bech32.ConvertBits(invoiceData.AsSpan()[(invoiceData.Length - 104)..], 5, 8); |
| 76 | 84 | | data = bech32.ConvertBits(invoiceData.AsSpan()[..(invoiceData.Length - 104)], 5, 8); |
| 76 | 85 | | } |
| | 86 | | } |