Skip to content

Commit

Permalink
Fix bug for ListUnspent (scriptPubKey uncorrectly parsed) + RPCClient…
Browse files Browse the repository at this point in the history
… improvement ListSecrets, ListAddressGrouping, ListAccounts
  • Loading branch information
NicolasDorier committed Nov 11, 2014
1 parent b4f313a commit 0c01561
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
105 changes: 105 additions & 0 deletions NBitcoin/RPC/RPCClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,63 @@

namespace NBitcoin.RPC
{
public class RPCAccount
{
public Money Amount
{
get;
set;
}
public String AccountName
{
get;
set;
}
}

public class ChangeAddress
{
public Money Amount
{
get;
set;
}
public BitcoinAddress Address
{
get;
set;
}
}

public class AddressGrouping
{
public AddressGrouping()
{
ChangeAddresses = new List<ChangeAddress>();
}
public BitcoinAddress PublicAddress
{
get;
set;
}
public Money Amount
{
get;
set;
}
public string Account
{
get;
set;
}

public List<ChangeAddress> ChangeAddresses
{
get;
set;
}
}

public class RPCClient
{
private readonly NetworkCredential _Credentials;
Expand Down Expand Up @@ -432,5 +489,53 @@ public async Task<uint256[]> GetRawMempoolAsync()
var array = (JArray)result.Result;
return array.Select(o => (string)o).Select(s => new uint256(s)).ToArray();
}

public IEnumerable<BitcoinSecret> ListSecrets()
{
foreach(var grouping in ListAddressGroupings())
{
yield return DumpPrivKey(grouping.PublicAddress);
foreach(var change in grouping.ChangeAddresses)
yield return DumpPrivKey(change.Address);
}
}

public IEnumerable<AddressGrouping> ListAddressGroupings()
{
var result = SendCommand(RPCOperations.listaddressgroupings);
var array = (JArray)result.Result;
foreach(var group in array.Children<JArray>())
{
var grouping = new AddressGrouping();
grouping.PublicAddress = BitcoinAddress.Create(group[0][0].ToString());
grouping.Amount = Money.Coins(group[0][1].Value<decimal>());
grouping.Account = group[0][2].ToString();

foreach(var subgroup in group.Skip(1))
{
var change = new ChangeAddress();
change.Address = BitcoinAddress.Create(subgroup[0].ToString());
change.Amount = Money.Coins(subgroup[1].Value<decimal>());
grouping.ChangeAddresses.Add(change);
}

yield return grouping;
}
}


public IEnumerable<RPCAccount> ListAccounts()
{
var result = SendCommand(RPCOperations.listaccounts);
var obj = (JObject)result.Result;
foreach(var prop in obj.Properties())
{
yield return new RPCAccount()
{
AccountName = prop.Name,
Amount = Money.Coins((decimal)prop.Value)
};
}
}
}
}
5 changes: 3 additions & 2 deletions NBitcoin/RPC/UnspentCoin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using NBitcoin.DataEncoders;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,7 +15,7 @@ public UnspentCoin(JObject unspent)
OutPoint = new OutPoint(new uint256((string)unspent["txid"]), (uint)unspent["vout"]);
Address = Network.CreateFromBase58Data<BitcoinAddress>((string)unspent["address"]);
Account = (string)unspent["account"];
ScriptPubKey = new Script((string)unspent["scriptPubKey"]);
ScriptPubKey = new Script(Encoders.Hex.DecodeData((string)unspent["scriptPubKey"]));
var amount = (decimal)unspent["amount"];
Amount = new Money((long)(amount * Money.COIN));
Confirmations = (uint)unspent["confirmations"];
Expand Down

0 comments on commit 0c01561

Please sign in to comment.