btcpayserver/BTCPayServer/ModelBinders/DerivationSchemeModelBinder.cs

58 lines
2.0 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
2018-07-26 15:32:24 +02:00
using System.Reflection;
using System.Threading.Tasks;
2020-06-28 10:55:27 +02:00
using Microsoft.AspNetCore.Mvc.ModelBinding;
2018-07-26 15:32:24 +02:00
using NBXplorer.DerivationStrategy;
namespace BTCPayServer.ModelBinders
{
public class DerivationSchemeModelBinder : IModelBinder
{
public DerivationSchemeModelBinder()
{
}
#region IModelBinder Members
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (!typeof(DerivationStrategyBase).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType))
{
return Task.CompletedTask;
}
ValueProviderResult val = bindingContext.ValueProvider.GetValue(
bindingContext.ModelName);
string key = val.FirstValue as string;
if (key == null)
{
return Task.CompletedTask;
}
var networkProvider = (BTCPayNetworkProvider)bindingContext.HttpContext.RequestServices.GetService(typeof(BTCPayNetworkProvider));
var cryptoCode = bindingContext.ValueProvider.GetValue("cryptoCode").FirstValue;
var network = networkProvider.GetNetwork<BTCPayNetwork>(cryptoCode ?? "BTC");
2018-07-26 15:32:24 +02:00
try
{
2020-06-28 10:55:27 +02:00
var data = network.NBXplorerNetwork.DerivationStrategyFactory.Parse(key);
2018-07-26 15:32:24 +02:00
if (!bindingContext.ModelType.IsInstanceOfType(data))
{
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid derivation scheme");
return Task.CompletedTask;
2018-07-26 15:32:24 +02:00
}
bindingContext.Result = ModelBindingResult.Success(data);
}
catch
{
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid derivation scheme");
}
2018-07-26 15:32:24 +02:00
return Task.CompletedTask;
}
#endregion
}
}