mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 18:11:36 +01:00
57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
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);
|
|
if (val == null)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
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");
|
|
try
|
|
{
|
|
var data = network.NBXplorerNetwork.DerivationStrategyFactory.Parse(key);
|
|
if (!bindingContext.ModelType.IsInstanceOfType(data))
|
|
{
|
|
throw new FormatException("Invalid destination type");
|
|
}
|
|
bindingContext.Result = ModelBindingResult.Success(data);
|
|
}
|
|
catch { throw new FormatException("Invalid derivation scheme"); }
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|