Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Create model binders for the RPC controller
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Jul 12, 2017
1 parent efaaa1e commit 5d45cb1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Stratis.Bitcoin.IntegrationTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Program
{
public static void Main(string[] args)
{
new WalletTests().CanMineBlocks();
new WalletTests().CanSendToAddress();
}
}
}
15 changes: 15 additions & 0 deletions Stratis.Bitcoin.IntegrationTests/WalletTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public void CanMineBlocks()
}
}

[Fact]
public void CanSendToAddress()
{
using(NodeBuilder builder = NodeBuilder.Create())
{
var stratisNodeSync = builder.CreateStratisNode();
builder.StartAll();
var rpc = stratisNodeSync.CreateRPCClient();
rpc.SendCommand(NBitcoin.RPC.RPCOperations.generate, 101);
var address = new Key().PubKey.GetAddress(rpc.Network);
var tx = rpc.SendToAddress(address, Money.Coins(1.0m));
Assert.NotNull(tx);
}
}

[Fact]
public void WalletCanReorg()
{
Expand Down
65 changes: 65 additions & 0 deletions Stratis.Bitcoin/RPC/ModelBinders/DestinationModelBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using NBitcoin;
using System.Reflection;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Internal;

namespace Stratis.Bitcoin.RPC.ModelBinders
{
public class DestinationModelBinder : IModelBinder, IModelBinderProvider
{
public DestinationModelBinder()
{

}

#region IModelBinder Members

public Task BindModelAsync(ModelBindingContext bindingContext)
{
if(!SupportType(bindingContext.ModelType))
{
return TaskCache.CompletedTask;
}

ValueProviderResult val = bindingContext.ValueProvider.GetValue(
bindingContext.ModelName);
if(val == null)
{
return TaskCache.CompletedTask;
}

string key = val.FirstValue as string;
if(key == null)
{
return TaskCache.CompletedTask;
}

var network = (Network)bindingContext.HttpContext.RequestServices.GetService(typeof(Network));
//TODO: Use var data = Network.Parse(key, network); when NBitcoin is updated to latest version
var data = BitcoinAddress.Create(key, network);
if(!bindingContext.ModelType.IsInstanceOfType(data))
{
throw new FormatException("Invalid destination type");
}
bindingContext.Result = ModelBindingResult.Success(data);
return TaskCache.CompletedTask;
}

private static bool SupportType(Type type)
{
return (typeof(Base58Data).GetTypeInfo().IsAssignableFrom(type) ||
typeof(IDestination).GetTypeInfo().IsAssignableFrom(type));
}

public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if(SupportType(context.Metadata.ModelType))
return this;
return null;
}

#endregion
}
}
43 changes: 43 additions & 0 deletions Stratis.Bitcoin/RPC/ModelBinders/MoneyModelBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using NBitcoin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stratis.Bitcoin.RPC.ModelBinders
{
public class MoneyModelBinder : IModelBinder, IModelBinderProvider
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if(bindingContext.ModelType != typeof(Money))
{
return TaskCache.CompletedTask;
}

ValueProviderResult val = bindingContext.ValueProvider.GetValue(
bindingContext.ModelName);
if(val == null)
{
return TaskCache.CompletedTask;
}

string key = val.FirstValue as string;
if(key == null)
{
return TaskCache.CompletedTask;
}
return Task.FromResult(Money.Parse(key));
}

public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if(context.Metadata.ModelType == typeof(Money))
return this;
return null;
}
}
}
6 changes: 6 additions & 0 deletions Stratis.Bitcoin/RPC/WebHostExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Stratis.Bitcoin.Miner;
using Stratis.Bitcoin.RPC.ModelBinders;
using Stratis.Bitcoin.Wallet;
using System;
using System.Collections.Generic;
Expand All @@ -15,6 +16,11 @@ public static IWebHostBuilder ForFullNode(this IWebHostBuilder hostBuilder, Full
{
hostBuilder.ConfigureServices(s =>
{
s.AddMvcCore(o =>
{
o.ModelBinderProviders.Insert(0, new DestinationModelBinder());
o.ModelBinderProviders.Insert(0, new MoneyModelBinder());
});
s.AddSingleton(fullNode);
s.AddSingleton(fullNode as Builder.IFullNode);
s.AddSingleton(fullNode.Network);
Expand Down

0 comments on commit 5d45cb1

Please sign in to comment.