Update NBitcoin, fix warnings on nullable (#2718)

This commit is contained in:
Nicolas Dorier
2021-07-29 20:29:34 +09:00
committed by GitHub
parent 499a231432
commit 6ea96efe68
22 changed files with 97 additions and 72 deletions

View File

@@ -504,11 +504,11 @@ namespace BTCPayServer.Controllers.GreenField
return Task.FromResult(GetFromActionResult(_chainPaymentMethodsController.GetOnChainPaymentMethods(storeId, enabled)));
}
public override async Task<OnChainPaymentMethodData> GetStoreOnChainPaymentMethod(string storeId,
public override Task<OnChainPaymentMethodData> GetStoreOnChainPaymentMethod(string storeId,
string cryptoCode, CancellationToken token = default)
{
return GetFromActionResult(
await _chainPaymentMethodsController.GetOnChainPaymentMethod(storeId, cryptoCode));
return Task.FromResult(GetFromActionResult(
_chainPaymentMethodsController.GetOnChainPaymentMethod(storeId, cryptoCode)));
}
public override async Task RemoveStoreOnChainPaymentMethod(string storeId, string cryptoCode,
@@ -534,13 +534,13 @@ namespace BTCPayServer.Controllers.GreenField
paymentMethod, offset, amount)));
}
public override async Task<OnChainPaymentMethodPreviewResultData> PreviewStoreOnChainPaymentMethodAddresses(
public override Task<OnChainPaymentMethodPreviewResultData> PreviewStoreOnChainPaymentMethodAddresses(
string storeId, string cryptoCode, int offset = 0, int amount = 10,
CancellationToken token = default)
{
return GetFromActionResult<OnChainPaymentMethodPreviewResultData>(
await _chainPaymentMethodsController.GetOnChainPaymentMethodPreview(storeId, cryptoCode, offset,
amount));
return Task.FromResult(GetFromActionResult<OnChainPaymentMethodPreviewResultData>(
_chainPaymentMethodsController.GetOnChainPaymentMethodPreview(storeId, cryptoCode, offset,
amount)));
}
public override Task<ApiHealthData> GetHealth(CancellationToken token = default)

View File

@@ -1,5 +1,6 @@
#nullable enable
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
@@ -122,7 +123,7 @@ namespace BTCPayServer.Controllers.GreenField
return NotFound();
}
if (string.IsNullOrEmpty(paymentMethodData?.ConnectionString))
if (string.IsNullOrEmpty(paymentMethodData.ConnectionString))
{
ModelState.AddModelError(nameof(LightningNetworkPaymentMethodData.ConnectionString),
"Missing connectionString");
@@ -131,8 +132,8 @@ namespace BTCPayServer.Controllers.GreenField
if (!ModelState.IsValid)
return this.CreateValidationError(ModelState);
LightningSupportedPaymentMethod paymentMethod = null;
if (!string.IsNullOrEmpty(paymentMethodData.ConnectionString))
LightningSupportedPaymentMethod? paymentMethod = null;
if (!string.IsNullOrEmpty(paymentMethodData!.ConnectionString))
{
if (paymentMethodData.ConnectionString == LightningSupportedPaymentMethod.InternalNode)
{
@@ -189,8 +190,8 @@ namespace BTCPayServer.Controllers.GreenField
return Ok(GetExistingLightningLikePaymentMethod(cryptoCode, store));
}
private LightningNetworkPaymentMethodData GetExistingLightningLikePaymentMethod(string cryptoCode,
StoreData store = null)
private LightningNetworkPaymentMethodData? GetExistingLightningLikePaymentMethod(string cryptoCode,
StoreData? store = null)
{
store ??= Store;
var storeBlob = store.GetStoreBlob();
@@ -201,13 +202,13 @@ namespace BTCPayServer.Controllers.GreenField
.FirstOrDefault(method => method.PaymentId == id);
var excluded = storeBlob.IsExcluded(id);
return paymentMethod == null
return paymentMethod is null
? null
: new LightningNetworkPaymentMethodData(paymentMethod.PaymentId.CryptoCode,
paymentMethod.GetDisplayableConnectionString(), !excluded);
}
private bool GetNetwork(string cryptoCode, out BTCPayNetwork network)
private bool GetNetwork(string cryptoCode, [MaybeNullWhen(false)] out BTCPayNetwork network)
{
network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
network = network?.SupportLightning is true ? network : null;

View File

@@ -73,7 +73,7 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}")]
public async Task<ActionResult<OnChainPaymentMethodData>> GetOnChainPaymentMethod(
public ActionResult<OnChainPaymentMethodData> GetOnChainPaymentMethod(
string storeId,
string cryptoCode)
{
@@ -93,7 +93,7 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/preview")]
public async Task<IActionResult> GetOnChainPaymentMethodPreview(
public IActionResult GetOnChainPaymentMethodPreview(
string storeId,
string cryptoCode,
int offset = 0, int amount = 10)

View File

@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading;
@@ -83,8 +84,8 @@ namespace BTCPayServer.Controllers.GreenField
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet")]
public async Task<IActionResult> ShowOnChainWalletOverview(string storeId, string cryptoCode)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
var wallet = _btcPayWalletProvider.GetWallet(network);
var balance = await wallet.GetBalance(derivationScheme.AccountDerivation);
@@ -102,8 +103,8 @@ namespace BTCPayServer.Controllers.GreenField
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/feerate")]
public async Task<IActionResult> GetOnChainFeeRate(string storeId, string cryptoCode, int? blockTarget = null)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
var feeRateTarget = blockTarget?? Store.GetStoreBlob().RecommendedFeeBlockTarget;
return Ok(new OnChainWalletFeeRateData()
@@ -117,8 +118,8 @@ namespace BTCPayServer.Controllers.GreenField
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address")]
public async Task<IActionResult> GetOnChainWalletReceiveAddress(string storeId, string cryptoCode, bool forceGenerate = false)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
var kpi = await _walletReceiveService.GetOrGenerate(new WalletId(storeId, cryptoCode), forceGenerate);
if (kpi is null)
@@ -145,8 +146,8 @@ namespace BTCPayServer.Controllers.GreenField
[HttpDelete("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address")]
public async Task<IActionResult> UnReserveOnChainWalletReceiveAddress(string storeId, string cryptoCode)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
var addr = await _walletReceiveService.UnReserveAddress(new WalletId(storeId, cryptoCode));
if (addr is null)
@@ -166,8 +167,8 @@ namespace BTCPayServer.Controllers.GreenField
[FromQuery] int limit = int.MaxValue
)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
var wallet = _btcPayWalletProvider.GetWallet(network);
var walletId = new WalletId(storeId, cryptoCode);
@@ -203,8 +204,8 @@ namespace BTCPayServer.Controllers.GreenField
public async Task<IActionResult> GetOnChainWalletTransaction(string storeId, string cryptoCode,
string transactionId)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
var wallet = _btcPayWalletProvider.GetWallet(network);
var tx = await wallet.FetchTransaction(derivationScheme.AccountDerivation, uint256.Parse(transactionId));
@@ -225,8 +226,8 @@ namespace BTCPayServer.Controllers.GreenField
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/utxos")]
public async Task<IActionResult> GetOnChainWalletUTXOs(string storeId, string cryptoCode)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
var wallet = _btcPayWalletProvider.GetWallet(network);
@@ -258,8 +259,8 @@ namespace BTCPayServer.Controllers.GreenField
public async Task<IActionResult> CreateOnChainTransaction(string storeId, string cryptoCode,
[FromBody] CreateOnChainTransactionRequest request)
{
if (IsInvalidWalletRequest(cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
if (IsInvalidWalletRequest(cryptoCode, out var network,
out var derivationScheme, out var actionResult)) return actionResult;
if (network.ReadonlyWallet)
{
return this.CreateAPIError("not-available",
@@ -445,8 +446,13 @@ namespace BTCPayServer.Controllers.GreenField
psbt.PSBT.RebaseKeyPaths(signingKeySettings.AccountKey, rootedKeyPath);
var accountKey = signingKey.Derive(rootedKeyPath.KeyPath);
if (signingContext?.EnforceLowR is bool v)
psbt.PSBT.Settings.SigningOptions.EnforceLowR = v;
else if (psbt.Suggestions?.ShouldEnforceLowR is bool v2)
psbt.PSBT.Settings.SigningOptions.EnforceLowR = v2;
var changed = psbt.PSBT.PSBTChanged(() => psbt.PSBT.SignAll(derivationScheme.AccountDerivation, accountKey,
rootedKeyPath, new SigningOptions() {EnforceLowR = signingContext?.EnforceLowR is bool v ? v : psbt.Suggestions.ShouldEnforceLowR }));
rootedKeyPath));
if (!changed)
{
@@ -458,7 +464,7 @@ namespace BTCPayServer.Controllers.GreenField
var transaction = psbt.PSBT.ExtractTransaction();
var transactionHash = transaction.GetHash();
BroadcastResult broadcastResult;
if (!string.IsNullOrEmpty(signingContext.PayJoinBIP21))
if (!string.IsNullOrEmpty(signingContext?.PayJoinBIP21))
{
signingContext.OriginalPSBT = psbt.PSBT.ToBase64();
try
@@ -468,8 +474,8 @@ namespace BTCPayServer.Controllers.GreenField
var payjoinPSBT = await _payjoinClient.RequestPayjoin(
new BitcoinUrlBuilder(signingContext.PayJoinBIP21, network.NBitcoinNetwork), new PayjoinWallet(derivationScheme),
psbt.PSBT, CancellationToken.None);
payjoinPSBT = psbt.PSBT.SignAll(derivationScheme.AccountDerivation, accountKey, rootedKeyPath,
new SigningOptions() {EnforceLowR = !(signingContext?.EnforceLowR is false)});
psbt.PSBT.Settings.SigningOptions = new SigningOptions() { EnforceLowR = !(signingContext?.EnforceLowR is false) };
payjoinPSBT = psbt.PSBT.SignAll(derivationScheme.AccountDerivation, accountKey, rootedKeyPath);
payjoinPSBT.Finalize();
var payjoinTransaction = payjoinPSBT.ExtractTransaction();
var hash = payjoinTransaction.GetHash();
@@ -508,11 +514,10 @@ namespace BTCPayServer.Controllers.GreenField
return await _authorizationService.CanUseHotWallet(await _settingsRepository.GetPolicies(), User);
}
private bool IsInvalidWalletRequest(string cryptoCode, out BTCPayNetwork network,
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)
private bool IsInvalidWalletRequest(string cryptoCode, [MaybeNullWhen(true)] out BTCPayNetwork network,
[MaybeNullWhen(true)] out DerivationSchemeSettings derivationScheme, [MaybeNullWhen(false)] out IActionResult actionResult)
{
derivationScheme = null;
actionResult = null;
network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
if (network is null)
{
@@ -535,6 +540,7 @@ namespace BTCPayServer.Controllers.GreenField
return true;
}
actionResult = null;
return false;
}

View File

@@ -440,9 +440,9 @@ namespace BTCPayServer.Controllers
{
//Keep compatibility with Bitpay
invoiceId = invoiceId ?? id;
id = invoiceId;
//
if (invoiceId is null)
return NotFound();
var model = await GetInvoiceModel(invoiceId, paymentMethodId == null ? null : PaymentMethodId.Parse(paymentMethodId), lang);
if (model == null)
return NotFound();
@@ -472,24 +472,24 @@ namespace BTCPayServer.Controllers
{
//Keep compatibility with Bitpay
invoiceId = invoiceId ?? id;
id = invoiceId;
//
var model = await GetInvoiceModel(invoiceId, paymentMethodId == null ? null : PaymentMethodId.Parse(paymentMethodId), lang);
if (invoiceId is null)
return NotFound();
var model = await GetInvoiceModel(invoiceId, paymentMethodId is null ? null : PaymentMethodId.Parse(paymentMethodId), lang);
if (model == null)
return NotFound();
return View(model);
}
private async Task<PaymentModel?> GetInvoiceModel(string invoiceId, PaymentMethodId paymentMethodId, string? lang)
private async Task<PaymentModel?> GetInvoiceModel(string invoiceId, PaymentMethodId? paymentMethodId, string? lang)
{
var invoice = await _InvoiceRepository.GetInvoice(invoiceId);
if (invoice == null)
return null;
var store = await _StoreRepository.FindStore(invoice.StoreId);
bool isDefaultPaymentId = false;
if (paymentMethodId == null)
if (paymentMethodId is null)
{
paymentMethodId = store.GetDefaultPaymentId(_NetworkProvider);
isDefaultPaymentId = true;
@@ -846,6 +846,11 @@ namespace BTCPayServer.Controllers
ModelState.AddModelError(nameof(model.StoreId), "You need to configure the derivation scheme in order to create an invoice");
return View(model);
}
if (model.Amount is null)
{
ModelState.AddModelError(nameof(model.Amount), "Thhe invoice amount can't be empty");
return View(model);
}
try
{
@@ -910,7 +915,7 @@ namespace BTCPayServer.Controllers
public class InvoiceStateChangeModel
{
public bool NotFound { get; set; }
public string StatusString { get; set; }
public string? StatusString { get; set; }
}
private string GetUserId()
@@ -933,7 +938,7 @@ namespace BTCPayServer.Controllers
var jObject = JObject.Parse(posData);
foreach (var item in jObject)
{
switch (item.Value.Type)
switch (item.Value?.Type)
{
case JTokenType.Array:
var items = item.Value.AsEnumerable().ToList();
@@ -945,6 +950,8 @@ namespace BTCPayServer.Controllers
case JTokenType.Object:
result.TryAdd(item.Key, ParsePosData(item.Value.ToString()));
break;
case null:
break;
default:
result.TryAdd(item.Key, item.Value.ToString());
break;

View File

@@ -335,13 +335,14 @@ namespace BTCPayServer.Controllers
derivationSchemeSettings, network, cancellationToken);
try
{
proposedPayjoin.Settings.SigningOptions = new SigningOptions()
{
EnforceLowR = !(vm.SigningContext?.EnforceLowR is false)
};
var extKey = ExtKey.Parse(vm.SigningKey, network.NBitcoinNetwork);
proposedPayjoin = proposedPayjoin.SignAll(derivationSchemeSettings.AccountDerivation,
extKey,
RootedKeyPath.Parse(vm.SigningKeyPath), new SigningOptions()
{
EnforceLowR = !(vm.SigningContext?.EnforceLowR is false)
});
RootedKeyPath.Parse(vm.SigningKeyPath));
vm.SigningContext.PSBT = proposedPayjoin.ToBase64();
vm.SigningContext.OriginalPSBT = psbt.ToBase64();
proposedPayjoin.Finalize();

View File

@@ -925,10 +925,11 @@ namespace BTCPayServer.Controllers
return View(nameof(SignWithSeed), viewModel);
}
var changed = psbt.PSBTChanged( () => psbt.SignAll(settings.AccountDerivation, signingKey, rootedKeyPath, new SigningOptions()
psbt.Settings.SigningOptions = new SigningOptions()
{
EnforceLowR = !(viewModel.SigningContext?.EnforceLowR is false)
}));
};
var changed = psbt.PSBTChanged( () => psbt.SignAll(settings.AccountDerivation, signingKey, rootedKeyPath));
if (!changed)
{
ModelState.AddModelError(nameof(viewModel.SeedOrKey), "Impossible to sign the transaction. Probable causes: Incorrect account key path in wallet settings or PSBT already signed.");