Skip to content

Commit

Permalink
Clarify TxOut.IsTo method, 1.0.5.29
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Nov 4, 2014
1 parent 6485bde commit 4cdd3f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
19 changes: 19 additions & 0 deletions NBitcoin.Tests/transaction_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ namespace NBitcoin.Tests
{
public class transaction_tests
{
[Fact]
[Trait("UnitTest", "UnitTest")]
public void CanExtractTxOutDestinationEasily()
{
var secret = new BitcoinSecret("KyJTjvFpPF6DDX4fnT56d2eATPfxjdUPXFFUb85psnCdh34iyXRQ");

var tx = new Transaction();
var p2pkh = new TxOut(new Money((UInt64)45000000), secret.GetAddress());
var p2pk = new TxOut(new Money((UInt64)80000000), secret.Key.PubKey);

tx.AddOutput(p2pkh);
tx.AddOutput(p2pk);

Assert.False(p2pkh.IsTo(secret.Key.PubKey));
Assert.True(p2pkh.IsTo(secret.GetAddress()));
Assert.True(p2pk.IsTo(secret.Key.PubKey));
Assert.False(p2pk.IsTo(secret.GetAddress()));
}

[Fact]
[Trait("UnitTest", "UnitTest")]
public void CanSignTransaction()
Expand Down
2 changes: 1 addition & 1 deletion NBitcoin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.5.28")]
[assembly: AssemblyVersion("1.0.5.29")]
[assembly: AssemblyFileVersion("1.0.0.0")]
26 changes: 22 additions & 4 deletions NBitcoin/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,21 +591,39 @@ public void ReadWrite(BitcoinStream stream)

#endregion

/// <summary>
/// Verify belongs to the given address
/// </summary>
/// <param name="address">P2PKH or P2SH address</param>
/// <returns></returns>
public bool IsTo(BitcoinAddress address)
{
if(address == null)
throw new ArgumentNullException("address");
return IsTo((KeyId)address.ID);
return IsTo(address.ID);
}

private bool IsTo(KeyId keyId)
/// <summary>
/// Verify belongs to the given address
/// </summary>
/// <param name="address">P2PKH or P2SH address</param>
/// <returns></returns>
public bool IsTo(TxDestination destination)
{
return ScriptPubKey.GetDestination() == keyId;
return ScriptPubKey.GetDestination() == destination;
}

/// <summary>
/// Verify is a P2PK of the given public key
/// </summary>
/// <param name="pubkey"></param>
/// <returns></returns>
public bool IsTo(PubKey pubkey)
{
return IsTo(pubkey.ID);
var owners = ScriptPubKey.GetDestinationPublicKeys();
if(owners.Length != 1)
return false;
return owners[0] == pubkey;
}


Expand Down

0 comments on commit 4cdd3f2

Please sign in to comment.