Files
btcpayserver/BTCPayServer/Payments/PaymentTypes.Bitcoin.cs
Andrew Camilleri 951bfeefb1 LNURL Payment Method Support (#2897)
* LNURL Payment Method Support

* Merge recent Lightning controller related changes

* Fix build

* Create separate payment settings section for stores

* Improve LNURL configuration

* Prevent duplicate array entries when merging Swagger JSON

* Fix CanSetPaymentMethodLimitsLightning

* Fix CanUsePayjoinViaUI

* Adapt test for new cancel bolt invoice feature

* rebase fixes

* Fixes after rebase

* Test fixes

* Do not turn LNURL on by default, Off-Chain payment criteria should affects both BOLT11 and LNURL, Payment criteria of unset payment method shouldn't be shown

* Send better error if payment method not found

* Revert "Prevent duplicate array entries when merging Swagger JSON"

This reverts commit 5783db9eda17c29908a60fdef2c3ebe130a8b059.

* Fix LNUrl doc

* Fix some warnings

Co-authored-by: Dennis Reimann <mail@dennisreimann.de>
Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
2021-10-25 15:18:02 +09:00

116 lines
4.8 KiB
C#

using System;
using System.Globalization;
using System.Linq;
using BTCPayServer.Payments.Bitcoin;
using BTCPayServer.Services.Invoices;
using NBitcoin;
using BTCPayServer.BIP78.Sender;
using BTCPayServer.Client.Models;
using NBitpayClient;
using Newtonsoft.Json.Linq;
using InvoiceCryptoInfo = BTCPayServer.Services.Invoices.InvoiceCryptoInfo;
namespace BTCPayServer.Payments
{
public class BitcoinPaymentType : PaymentType
{
public static BitcoinPaymentType Instance { get; } = new BitcoinPaymentType();
private BitcoinPaymentType() { }
public override string ToPrettyString() => "On-Chain";
public override string GetId() => "BTCLike";
public override string GetBadge() => "";
public override string ToStringNormalized() => "OnChain";
public override CryptoPaymentData DeserializePaymentData(BTCPayNetworkBase network, string str)
{
return ((BTCPayNetwork)network)?.ToObject<BitcoinLikePaymentData>(str);
}
public override string SerializePaymentData(BTCPayNetworkBase network, CryptoPaymentData paymentData)
{
return ((BTCPayNetwork)network).ToString(paymentData);
}
public override IPaymentMethodDetails DeserializePaymentMethodDetails(BTCPayNetworkBase network, string str)
{
return ((BTCPayNetwork)network).ToObject<BitcoinLikeOnChainPaymentMethod>(str);
}
public override string SerializePaymentMethodDetails(BTCPayNetworkBase network, IPaymentMethodDetails details)
{
return ((BTCPayNetwork)network).ToString((BitcoinLikeOnChainPaymentMethod)details);
}
public override ISupportedPaymentMethod DeserializeSupportedPaymentMethod(BTCPayNetworkBase network, JToken value)
{
if (network == null)
throw new ArgumentNullException(nameof(network));
if (value == null)
throw new ArgumentNullException(nameof(value));
var net = (BTCPayNetwork)network;
if (value is JObject jobj)
{
var scheme = net.NBXplorerNetwork.Serializer.ToObject<DerivationSchemeSettings>(jobj);
scheme.Network = net;
return scheme;
}
// Legacy
return DerivationSchemeSettings.Parse(((JValue)value).Value<string>(), net);
}
public override string GetTransactionLink(BTCPayNetworkBase network, string txId)
{
if (txId == null)
throw new ArgumentNullException(nameof(txId));
if (network?.BlockExplorerLink == null)
return null;
txId = txId.Split('-').First();
return string.Format(CultureInfo.InvariantCulture, network.BlockExplorerLink, txId);
}
public override string GetPaymentLink(BTCPayNetworkBase network, IPaymentMethodDetails paymentMethodDetails,
Money cryptoInfoDue, string serverUri)
{
if (!paymentMethodDetails.Activated)
{
return string.Empty;
}
var bip21 = ((BTCPayNetwork)network).GenerateBIP21(paymentMethodDetails.GetPaymentDestination(), cryptoInfoDue);
if ((paymentMethodDetails as BitcoinLikeOnChainPaymentMethod)?.PayjoinEnabled is true && serverUri != null)
{
bip21.QueryParams.Add(PayjoinClient.BIP21EndpointKey, $"{serverUri.WithTrailingSlash()}{network.CryptoCode}/{PayjoinClient.BIP21EndpointKey}");
}
return bip21.ToString();
}
public override string InvoiceViewPaymentPartialName { get; } = "Bitcoin/ViewBitcoinLikePaymentData";
public override object GetGreenfieldData(ISupportedPaymentMethod supportedPaymentMethod, bool canModifyStore)
{
if (supportedPaymentMethod is DerivationSchemeSettings derivationSchemeSettings)
return new OnChainPaymentMethodBaseData()
{
DerivationScheme = derivationSchemeSettings.AccountDerivation.ToString(),
AccountKeyPath = derivationSchemeSettings.GetSigningAccountKeySettings().GetRootedKeyPath(),
Label = derivationSchemeSettings.Label
};
return null;
}
public override bool IsPaymentType(string paymentType)
{
return string.IsNullOrEmpty(paymentType) || base.IsPaymentType(paymentType);
}
public override void PopulateCryptoInfo(PaymentMethod details, InvoiceCryptoInfo cryptoInfo,
string serverUrl)
{
cryptoInfo.PaymentUrls = new InvoiceCryptoInfo.InvoicePaymentUrls()
{
BIP21 = GetPaymentLink(details.Network, details.GetPaymentMethodDetails(), cryptoInfo.Due, serverUrl),
};
}
}
}