Skip to content

Commit

Permalink
Make get payload rpc public
Browse files Browse the repository at this point in the history
  • Loading branch information
thebevrishot committed Nov 26, 2019
1 parent e9901f9 commit 4cd605a
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 51 deletions.
17 changes: 0 additions & 17 deletions src/Ztm.WebApi/ServicesExtensions.cs

This file was deleted.

6 changes: 3 additions & 3 deletions src/Ztm.WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IBlocksRetriever, BlocksRetriever>();
services.AddSingleton<IBlocksStorage, BlocksStorage>();

// Transaction Encoder Services.
services.AddTransactionEncoder();
// NBitcoin Services.
services.AddNBitcoin();

// Background Services.
services.AddHostedService<BlocksSynchronizer>();
Expand Down Expand Up @@ -91,7 +91,7 @@ IZcoinRpcClientFactory CreateZcoinRpcClientFactory(IServiceProvider provider)
config.Rpc.Address,
config.Network.Type,
RPCCredentialString.Parse($"{config.Rpc.UserName}:{config.Rpc.Password}"),
(ITransactionEncoder)provider.GetRequiredService(typeof(ITransactionEncoder))
provider.GetRequiredService<ITransactionEncoder>()
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Ztm.WebApi/Ztm.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ProjectReference Include="..\Ztm.Configuration\Ztm.Configuration.csproj" />
<ProjectReference Include="..\Ztm.Data.Entity\Ztm.Data.Entity.csproj" />
<ProjectReference Include="..\Ztm.Data.Entity.Postgres\Ztm.Data.Entity.Postgres.csproj" />
<ProjectReference Include="..\Ztm.Zcoin.NBitcoin\Ztm.Zcoin.NBitcoin.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Ztm.Zcoin.NBitcoin/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;
using Ztm.Zcoin.NBitcoin.Exodus;

namespace Ztm.Zcoin.NBitcoin
{
public static class ServiceCollectionExtensions
{
public static void AddNBitcoin(this IServiceCollection services)
{
// Payload Encoders
services.AddSingleton<ITransactionPayloadEncoder, SimpleSendEncoder>();

// Encoder
services.AddSingleton<ITransactionEncoder, TransactionEncoder>();
}
}
}
1 change: 1 addition & 0 deletions src/Ztm.Zcoin.NBitcoin/Ztm.Zcoin.NBitcoin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="NBitcoin" Version="4.2.16" />
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions src/Ztm.Zcoin.Rpc.Tests/ZcoinRpcClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Constructor_PassNullForServer_ShouldThrow()
{
Assert.Throws<ArgumentNullException>("server", () =>
{
new ZcoinRpcClientFactory(server: null, type: NetworkType.Regtest, credential: new RPCCredentialString(), encoder: this.encoder);
new ZcoinRpcClientFactory(server: null, type: NetworkType.Regtest, credential: new RPCCredentialString(), exodusEncoder: this.encoder);
});
}

Expand All @@ -33,16 +33,16 @@ public void Constructor_PassNullForCredential_ShouldThrow()
{
Assert.Throws<ArgumentNullException>("credential", () =>
{
new ZcoinRpcClientFactory(server: new Uri("http://127.0.0.1"), type: NetworkType.Regtest, credential: null, encoder: this.encoder);
new ZcoinRpcClientFactory(server: new Uri("http://127.0.0.1"), type: NetworkType.Regtest, credential: null, exodusEncoder: this.encoder);
});
}

[Fact]
public void Constructor_PassNullEncoder_ShouldThrow()
{
Assert.Throws<ArgumentNullException>("encoder", () =>
Assert.Throws<ArgumentNullException>("exodusEncoder", () =>
{
new ZcoinRpcClientFactory(server: new Uri("http://127.0.0.1"), type: NetworkType.Regtest, credential: new RPCCredentialString(), encoder: null);
new ZcoinRpcClientFactory(server: new Uri("http://127.0.0.1"), type: NetworkType.Regtest, credential: new RPCCredentialString(), exodusEncoder: null);
});
}

Expand Down
44 changes: 42 additions & 2 deletions src/Ztm.Zcoin.Rpc.Tests/ZcoinRpcClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using System.Threading;
using System.Threading.Tasks;
using NBitcoin;
using NBitcoin.DataEncoders;
using NBitcoin.Tests;
using NSubstitute;
using Xunit;
using Ztm.Testing;
using Ztm.Zcoin.NBitcoin;
using Ztm.Zcoin.NBitcoin.Exodus;
using Ztm.Zcoin.Testing;

Expand Down Expand Up @@ -58,7 +58,7 @@ public void Construct_WithNullArgs_ShouldThrow()
);

Assert.Throws<ArgumentNullException>(
"encoder",
"exodusEncoder",
() => new ZcoinRpcClient(this.node.CreateRPCClient(), null)
);
}
Expand Down Expand Up @@ -656,6 +656,46 @@ public async Task GetExodusTransactionAsync_WithValidExistTransaction_ShouldSucc
Assert.Equal("Create Property - Manual", infomation.Type);
}

[Fact]
public async Task GetExodusPayloadAsync_WithValidExistTransaction_ShouldSuccess()
{
// Arrange.
var owner = await this.subject.GetNewAddressAsync(CancellationToken.None);

this.node.Generate(101);
await this.subject.SendToAddressAsync(owner, Money.Coins(30), null, null, false, CancellationToken.None);
this.node.Generate(1);

var createTx = await this.subject.CreateManagedPropertyAsync(
owner,
Ecosystem.Main,
PropertyType.Indivisible,
null,
"Company",
"Private",
"Satang Corporation",
"https://satang.com",
"Provides cryptocurrency solutions.",
CancellationToken.None
);

await this.subject.SendRawTransactionAsync(createTx, CancellationToken.None);
this.node.Generate(1);

// Act.
var payload = await this.subject.GetExodusPayloadAsync(createTx.GetHash(), CancellationToken.None);

// Assert.
Assert.Equal
(
Encoders.Hex.DecodeData(
"0000003601000100000000436f6d70616e79005072697661746500536174616e6720436f72706f726174696" +
"f6e0068747470733a2f2f736174616e672e636f6d0050726f76696465732063727970746f63757272656e63" +
"7920736f6c7574696f6e732e00"),
payload
);
}

class ManagedProperty
{
readonly CoreNode node;
Expand Down
2 changes: 2 additions & 0 deletions src/Ztm.Zcoin.Rpc/IZcoinRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Task<Transaction> CreateManagedPropertyAsync(

Task<BitcoinAddress> GetNewAddressAsync(CancellationToken cancellationToken);

Task<byte[]> GetExodusPayloadAsync(uint256 transaction, CancellationToken cancellationToken);

Task<(PropertyAmount balance, PropertyAmount reserved)> GetPropertyBalanceAsync(
BitcoinAddress address, Property property, CancellationToken cancellationToken);

Expand Down
38 changes: 19 additions & 19 deletions src/Ztm.Zcoin.Rpc/ZcoinRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ namespace Ztm.Zcoin.Rpc
public sealed class ZcoinRpcClient : IZcoinRpcClient
{
readonly RPCClient client;
readonly ITransactionEncoder encoder;
readonly ITransactionEncoder exodusEncoder;

public ZcoinRpcClient(RPCClient client, ITransactionEncoder encoder)
public ZcoinRpcClient(RPCClient client, ITransactionEncoder exodusEncoder)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

if (encoder == null)
if (exodusEncoder == null)
{
throw new ArgumentNullException(nameof(encoder));
throw new ArgumentNullException(nameof(exodusEncoder));
}

this.client = client;
this.encoder = encoder;
this.exodusEncoder = exodusEncoder;
}

public void Dispose()
Expand Down Expand Up @@ -166,6 +166,12 @@ public Task<BitcoinAddress> GetNewAddressAsync(CancellationToken cancellationTok
return this.client.GetNewAddressAsync();
}

public async Task<byte[]> GetExodusPayloadAsync(uint256 transaction, CancellationToken cancellationToken)
{
var resp = await this.client.SendCommandAsync("exodus_getpayload", transaction);
return Encoders.Hex.DecodeData(resp.Result.Value<string>("payload"));
}

public async Task<(PropertyAmount balance, PropertyAmount reserved)> GetPropertyBalanceAsync(
BitcoinAddress address,
Property property,
Expand Down Expand Up @@ -403,16 +409,6 @@ static ushort ToNative(PropertyType type)
}
}

async Task<(string payload, int payloadSize)> GetPayloadAsync(Transaction transaction)
{
var resp = await this.client.SendCommandAsync("exodus_getpayload", new object[]{transaction.GetHash()});

var payload = resp.Result.Value<string>("payload");
var payloadSize = resp.Result.Value<int>("payloadsize");

return (payload: payload, payloadSize: payloadSize);
}

async Task<ExodusTransaction> TryDecodeExodusTransaction(Transaction transaction)
{
var infomation = await GetExodusTransactionAsync(transaction.GetHash(), CancellationToken.None);
Expand All @@ -421,15 +417,19 @@ async Task<ExodusTransaction> TryDecodeExodusTransaction(Transaction transaction
return null;
}

var payload = await GetPayloadAsync(transaction);
var payload = await GetExodusPayloadAsync(transaction.GetHash(), CancellationToken.None);

try
{
return this.encoder.Decode(infomation.SendingAddress, infomation.ReferenceAddress, Encoders.Hex.DecodeData(payload.payload));
return this.exodusEncoder.Decode(infomation.SendingAddress, infomation.ReferenceAddress, payload);
}
catch (TransactionFieldException)
catch (TransactionFieldException ex)
{
return null;
if (ex.Field == TransactionFieldException.TypeField)
{
return null;
}
throw;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Ztm.Zcoin.Rpc/ZcoinRpcClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public sealed class ZcoinRpcClientFactory : IZcoinRpcClientFactory
readonly Uri serverUri;
readonly NetworkType networkType;
readonly RPCCredentialString credential;
readonly ITransactionEncoder encoder;
readonly ITransactionEncoder exodusEncoder;

public ZcoinRpcClientFactory(Uri server, NetworkType type, RPCCredentialString credential, ITransactionEncoder encoder)
public ZcoinRpcClientFactory(Uri server, NetworkType type, RPCCredentialString credential, ITransactionEncoder exodusEncoder)
{
if (server == null)
{
Expand All @@ -27,15 +27,15 @@ public ZcoinRpcClientFactory(Uri server, NetworkType type, RPCCredentialString c
throw new ArgumentNullException(nameof(credential));
}

if (encoder == null)
if (exodusEncoder == null)
{
throw new ArgumentNullException(nameof(encoder));
throw new ArgumentNullException(nameof(exodusEncoder));
}

this.serverUri = server;
this.networkType = type;
this.credential = credential;
this.encoder = encoder;
this.exodusEncoder = exodusEncoder;
}

public async Task<IZcoinRpcClient> CreateRpcClientAsync(CancellationToken cancellationToken)
Expand All @@ -45,7 +45,7 @@ public async Task<IZcoinRpcClient> CreateRpcClientAsync(CancellationToken cancel

await client.ScanRPCCapabilitiesAsync();

return new ZcoinRpcClient(client, encoder);
return new ZcoinRpcClient(client, exodusEncoder);
}
}
}

0 comments on commit 4cd605a

Please sign in to comment.