-
Notifications
You must be signed in to change notification settings - Fork 1
/
PayTo.cs
95 lines (74 loc) · 3.07 KB
/
PayTo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
namespace payto;
public enum DestinationType
{
/// <summary>
/// general case of [email protected]
/// May be LNURL address (http based) or BIP353 address (DNS based)
/// </summary>
ADDRESS,
/// <summary>
/// libhtning address based on BIP 353: DNS Payment Instructions - https://github.com/bitcoin/bips/pull/1551
/// </summary>
ADDRESS_DNS_BIP353,
/// <summary>
/// lightning address based on Http and LNURLP https://github.com/andrerfneves/lightning-address/blob/master/README.md
/// </summary>
ADDRESS_LNURL,
/// <summary>
/// LNURL PayRequest encoded as bech32 lnurlxxx (LUD01) or lnurlp://xxx (LUD17)
/// </summary>
LNURL_PAYREQUEST,
/// <summary>
/// lno1... offer to fetch invoice
/// </summary>
BOLT12_OFFER,
//BOLT12_INVOICE,
//BOLT11_INVOICE,
}
internal class PayTo
{
public static DestinationType WhatIsIt(string destination)
{
if (destination.Contains("@"))
{
/// can be DNS_BIP353 or LNURL_LIGHTNING_ADDRESS
/// BIP353 says to start with ₿ symbol
if (destination.StartsWith("₿"))
return DestinationType.ADDRESS_DNS_BIP353;
/// realistically let's not expect the ₿ symbol, but still first try check the DNS instead of HTTP for better privacy
if (PayToDns.TryQueryDnsInfo(destination))
return DestinationType.ADDRESS_DNS_BIP353;
/// all other cases assume LNURL address
return DestinationType.ADDRESS_LNURL;
}
if (destination.StartsWith("lno1", StringComparison.InvariantCultureIgnoreCase))
{
/// BOLT12_OFFER
return DestinationType.BOLT12_OFFER;
}
if (destination.StartsWith("lnurl1", StringComparison.InvariantCultureIgnoreCase)
|| destination.StartsWith("lnurlp", StringComparison.InvariantCultureIgnoreCase))
{
/// LNURL
return DestinationType.LNURL_PAYREQUEST;
}
throw new ArgumentException("The destination is not recognized as valid target. Must be [email protected] or bolt12 offer or LNURLP ");
}
internal static async Task PayToDestinationAsync(string destination)
{
// trim and remove "lightning:"
destination = destination.Trim()
.Replace("lightning://", "", StringComparison.InvariantCultureIgnoreCase)
.Replace("lightning:", "", StringComparison.InvariantCultureIgnoreCase);
// determine type of destination
var destinationType = PayTo.WhatIsIt(destination);
if (destinationType == DestinationType.BOLT12_OFFER)
PayToBolt12.PayToOffer(destination);
if (destinationType == DestinationType.ADDRESS_DNS_BIP353)
PayToDns.PayToAddress(destination);
if (destinationType == DestinationType.ADDRESS_LNURL)
await PayToLNURL.PayToAddressAsync(destination);
if (destinationType == DestinationType.LNURL_PAYREQUEST)
await PayToLNURL.PayToLNURLAsync(destination);
}
}