Skip to content

Commit

Permalink
GetDestinationsPubKey, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Sep 28, 2014
1 parent 069d416 commit a7e94f7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
6 changes: 3 additions & 3 deletions NBitcoin.Tests/script_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,10 @@ public void CanExtractAddressesFromScript()
var payToMultiSig = new Script("1 0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27c 0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27d 2 OP_CHECKMULTISIG");

Assert.Null(payToMultiSig.GetSigner());
var destinations = payToMultiSig.GetDestinations();
var destinations = payToMultiSig.GetDestinationPublicKeys();
Assert.Equal(2, destinations.Length);
Assert.Equal(new PubKey("0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27c").ID, destinations[0]);
Assert.Equal(new PubKey("0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27d").ID, destinations[1]);
Assert.Equal("0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27c", destinations[0].ToHex());
Assert.Equal("0364bd4b02a752798342ed91c681a48793bb1c0853cbcd0b978c55e53485b8e27d", destinations[1].ToHex());

var payToScriptHash = new Script("OP_HASH160 b5b88dd9befc9236915fcdbb7fd50052df50c855 OP_EQUAL");
Assert.NotNull(payToScriptHash.GetDestination());
Expand Down
2 changes: 2 additions & 0 deletions NBitcoin/BitcoinAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public override Base58Type Type

public static BitcoinAddress Create(TxDestination id, Network network)
{
if(id == null)
throw new ArgumentNullException("id");
if(network == null)
throw new ArgumentNullException("network");
if(id is KeyId)
Expand Down
12 changes: 8 additions & 4 deletions NBitcoin/RPC/SatoshiFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ protected override void WriteTransaction(JsonTextWriter writer, Transaction tx)
WritePropertyValue(writer, "asm", txout.ScriptPubKey.ToString());
WritePropertyValue(writer, "hex", Encoders.Hex.EncodeData(txout.ScriptPubKey.ToRawScript()));

var destinations = txout.ScriptPubKey.GetDestinations().ToArray();


if(destinations.Length == 1)
var destinations = new List<TxDestination>() { txout.ScriptPubKey.GetDestination() };
if(destinations[0] == null)
{
destinations = txout.ScriptPubKey.GetDestinationPublicKeys()
.Select(p => p.ID)
.ToList<TxDestination>();
}
if(destinations.Count == 1)
{
WritePropertyValue(writer, "reqSigs", 1);
WritePropertyValue(writer, "type", GetScriptType(txout.ScriptPubKey.FindTemplate()));
Expand Down
21 changes: 5 additions & 16 deletions NBitcoin/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,25 +630,14 @@ public TxDestination GetDestination()
}

/// <summary>
/// Extract P2SH, P2PH address or multiple P2PHs addresses if this script is a multi sig from scriptPubKey
/// Extract public keys if this script is a multi sig or pay to pub key scriptPubKey
/// </summary>
/// <param name="network"></param>
/// <returns></returns>
public BitcoinAddress[] GetDestinationAddresses(Network network)
public PubKey[] GetDestinationPublicKeys()
{
return GetDestinations()
.Select(id => BitcoinAddress.Create(id, network))
.ToArray();
}
/// <summary>
/// Extract P2SH, P2PH id or multiple P2PHs ids if this script is a multi sig scriptPubKey
/// </summary>
/// <param name="network"></param>
/// <returns></returns>
public TxDestination[] GetDestinations()
{
List<TxDestination> result = new List<TxDestination>();
var single = GetDestination();
List<PubKey> result = new List<PubKey>();
var single = new PayToPubkeyTemplate().ExtractScriptPubKeyParameters(this);
if(single != null)
{
result.Add(single);
Expand All @@ -660,7 +649,7 @@ public TxDestination[] GetDestinations()
{
foreach(var key in multiSig.PubKeys)
{
result.Add(key.ID);
result.Add(key);
}
}
}
Expand Down

0 comments on commit a7e94f7

Please sign in to comment.