This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create model binders for the RPC controller
- Loading branch information
1 parent
efaaa1e
commit 5d45cb1
Showing
5 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
Stratis.Bitcoin/RPC/ModelBinders/DestinationModelBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters