Fix NRE and do nto activate onchain if node unavailable even when lazy payments (#4291)

fixes #4289
This commit is contained in:
Andrew Camilleri 2022-11-16 01:04:51 +01:00 committed by GitHub
parent 2d23819944
commit 1d7dee8314
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -79,12 +79,6 @@ namespace BTCPayServer.Payments.Bitcoin
model.InvoiceBitcoinUrl = (cryptoInfo.PaymentUrls?.BIP21 ?? "") + lightningFallback; model.InvoiceBitcoinUrl = (cryptoInfo.PaymentUrls?.BIP21 ?? "") + lightningFallback;
model.InvoiceBitcoinUrlQR = (cryptoInfo.PaymentUrls?.BIP21 ?? "") + lightningFallback model.InvoiceBitcoinUrlQR = (cryptoInfo.PaymentUrls?.BIP21 ?? "") + lightningFallback
.Replace("LIGHTNING=", "lightning=", StringComparison.OrdinalIgnoreCase); .Replace("LIGHTNING=", "lightning=", StringComparison.OrdinalIgnoreCase);
}
else
{
model.InvoiceBitcoinUrl = "";
model.InvoiceBitcoinUrlQR = "";
}
// Most wallets still don't support BITCOIN: schema, so we're leaving this for better days // Most wallets still don't support BITCOIN: schema, so we're leaving this for better days
// Ref: https://github.com/btcpayserver/btcpayserver/pull/2060#issuecomment-723828348 // Ref: https://github.com/btcpayserver/btcpayserver/pull/2060#issuecomment-723828348
@ -100,6 +94,14 @@ namespace BTCPayServer.Payments.Bitcoin
); );
} }
} }
else
{
model.InvoiceBitcoinUrl = "";
model.InvoiceBitcoinUrlQR = "";
}
}
public override string GetCryptoImage(PaymentMethodId paymentMethodId) public override string GetCryptoImage(PaymentMethodId paymentMethodId)
{ {
@ -155,6 +157,19 @@ namespace BTCPayServer.Payments.Bitcoin
DerivationSchemeSettings supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store, DerivationSchemeSettings supportedPaymentMethod, PaymentMethod paymentMethod, StoreData store,
BTCPayNetwork network, object preparePaymentObject, IEnumerable<PaymentMethodId> invoicePaymentMethods) BTCPayNetwork network, object preparePaymentObject, IEnumerable<PaymentMethodId> invoicePaymentMethods)
{ {
if (!_ExplorerProvider.IsAvailable(network))
throw new PaymentMethodUnavailableException($"Full node not available");
if (paymentMethod.ParentEntity.Type != InvoiceType.TopUp)
{
var txOut = network.NBitcoinNetwork.Consensus.ConsensusFactory.CreateTxOut();
txOut.ScriptPubKey =
new Key().GetScriptPubKey(supportedPaymentMethod.AccountDerivation.ScriptPubKeyType());
var dust = txOut.GetDustThreshold();
var amount = paymentMethod.Calculate().Due;
if (amount < dust)
throw new PaymentMethodUnavailableException("Amount below the dust threshold. For amounts of this size, it is recommended to enable an off-chain (Lightning) payment method");
}
if (preparePaymentObject is null) if (preparePaymentObject is null)
{ {
return new BitcoinLikeOnChainPaymentMethod() return new BitcoinLikeOnChainPaymentMethod()
@ -162,8 +177,6 @@ namespace BTCPayServer.Payments.Bitcoin
Activated = false Activated = false
}; };
} }
if (!_ExplorerProvider.IsAvailable(network))
throw new PaymentMethodUnavailableException($"Full node not available");
var prepare = (Prepare)preparePaymentObject; var prepare = (Prepare)preparePaymentObject;
var onchainMethod = new BitcoinLikeOnChainPaymentMethod(); var onchainMethod = new BitcoinLikeOnChainPaymentMethod();
var blob = store.GetStoreBlob(); var blob = store.GetStoreBlob();
@ -190,15 +203,7 @@ namespace BTCPayServer.Payments.Bitcoin
} }
var reserved = await prepare.ReserveAddress; var reserved = await prepare.ReserveAddress;
if (paymentMethod.ParentEntity.Type != InvoiceType.TopUp)
{
var txOut = network.NBitcoinNetwork.Consensus.ConsensusFactory.CreateTxOut();
txOut.ScriptPubKey = reserved.Address.ScriptPubKey;
var dust = txOut.GetDustThreshold();
var amount = paymentMethod.Calculate().Due;
if (amount < dust)
throw new PaymentMethodUnavailableException("Amount below the dust threshold. For amounts of this size, it is recommended to enable an off-chain (Lightning) payment method");
}
onchainMethod.DepositAddress = reserved.Address.ToString(); onchainMethod.DepositAddress = reserved.Address.ToString();
onchainMethod.KeyPath = reserved.KeyPath; onchainMethod.KeyPath = reserved.KeyPath;
onchainMethod.PayjoinEnabled = blob.PayJoinEnabled && onchainMethod.PayjoinEnabled = blob.PayJoinEnabled &&