Skip to content

Commit

Permalink
Use p2sh for multi sig stealth address payment
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Nov 3, 2014
1 parent a6aef9b commit 8c9a3b6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
10 changes: 2 additions & 8 deletions NBitcoin.Tests/transaction_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,6 @@ private void AssertHasAsset(Transaction tx, ColoredTransaction colored, ColoredE
[Fact]
[Trait("UnitTest", "UnitTest")]
public void CanBuildStealthTransaction()
{
CanBuildStealthTransactionCore(false);
CanBuildStealthTransactionCore(true);
}

void CanBuildStealthTransactionCore(bool p2sh)
{
var stealthKeys = Enumerable.Range(0, 3).Select(_ => new Key()).ToArray();
var scanKey = new Key();
Expand All @@ -339,12 +333,12 @@ void CanBuildStealthTransactionCore(bool p2sh)
builder
.AddCoins(coins)
.AddKeys(bob)
.Send(darkSatoshi, "1.00", p2sh: p2sh)
.Send(darkSatoshi, "1.00")
.BuildTransaction(true);
Assert.True(builder.Verify(tx));

//Satoshi scans a StealthCoin in the transaction with his scan key
var stealthCoin = StealthCoin.Find(tx, darkSatoshi, scanKey, allowP2sh: p2sh);
var stealthCoin = StealthCoin.Find(tx, darkSatoshi, scanKey);
Assert.NotNull(stealthCoin);

//Satoshi sends back the money to Bob
Expand Down
5 changes: 2 additions & 3 deletions NBitcoin/Coin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,10 @@ public Script Redeem
/// <param name="tx">The transaction to scan</param>
/// <param name="address">The stealth address</param>
/// <param name="scan">The scan private key</param>
/// <param name="allowP2sh">Support P2SH stealth payment, this weaken the scanner to DDOS attacks</param>
/// <returns></returns>
public static StealthCoin Find(Transaction tx, BitcoinStealthAddress address, Key scan, bool allowP2sh = false)
public static StealthCoin Find(Transaction tx, BitcoinStealthAddress address, Key scan)
{
var payment = address.GetPayments(tx, scan, allowP2sh).FirstOrDefault();
var payment = address.GetPayments(tx, scan).FirstOrDefault();
if(payment == null)
return null;
var txId = tx.GetHash();
Expand Down
2 changes: 1 addition & 1 deletion NBitcoin/Scanning/StealthPaymentScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Key Scan

public override Coins ScanCoins(uint256 txId, Transaction tx, int height)
{
var payments = StealthPayment.GetPayments(tx, Address, Scan, false);
var payments = StealthPayment.GetPayments(tx, Address, Scan);
return new Coins(tx, txout => Match(txout, payments), height);
}

Expand Down
13 changes: 6 additions & 7 deletions NBitcoin/Stealth/BitcoinStealthAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public bool Match(StealthMetadata metadata)
return Match(metadata.BitField);
}

public StealthPayment[] GetPayments(Transaction transaction, bool allowP2sh = false)
public StealthPayment[] GetPayments(Transaction transaction)
{
return StealthPayment.GetPayments(transaction, null, null, allowP2sh);
return StealthPayment.GetPayments(transaction, null, null);
}
}
public class BitcoinStealthAddress : Base58Data
Expand Down Expand Up @@ -235,20 +235,19 @@ public override Base58Type Type
/// <param name="tx">The transaction to scan</param>
/// <param name="address">The stealth address</param>
/// <param name="scan">The scan private key</param>
/// <param name="allowP2sh">Support P2SH stealth payment, this weaken the scanner to DDOS attacks</param>
/// <returns></returns>
public StealthPayment[] GetPayments(Transaction transaction, Key scanKey, bool allowP2sh = false)
public StealthPayment[] GetPayments(Transaction transaction, Key scanKey)
{
return StealthPayment.GetPayments(transaction, this, scanKey, allowP2sh);
return StealthPayment.GetPayments(transaction, this, scanKey);
}

public StealthPayment CreatePayment(Key ephemKey = null, bool p2sh = false)
public StealthPayment CreatePayment(Key ephemKey = null)
{
if(ephemKey == null)
ephemKey = new Key();

var metadata = StealthMetadata.CreateMetadata(ephemKey, this.Prefix);
return new StealthPayment(this, ephemKey, metadata, p2sh);
return new StealthPayment(this, ephemKey, metadata);
}


Expand Down
9 changes: 4 additions & 5 deletions NBitcoin/Stealth/StealthPayment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public BitcoinAddress GetAddress(Network network)

public class StealthPayment
{
public StealthPayment(BitcoinStealthAddress address, Key ephemKey, StealthMetadata metadata, bool p2sh)
public StealthPayment(BitcoinStealthAddress address, Key ephemKey, StealthMetadata metadata)
{
Metadata = metadata;
ScriptPubKey = CreatePaymentScript(address.SignatureCount, address.SpendPubKeys, ephemKey, address.ScanPubKey);
if(p2sh)

if(address.SignatureCount > 1)
{
Redeem = ScriptPubKey;
ScriptPubKey = ScriptPubKey.ID.CreateScriptPubKey();
Expand Down Expand Up @@ -140,7 +141,7 @@ public void AddToTransaction(Transaction transaction, Money value)
transaction.Outputs.Add(new TxOut(value, ScriptPubKey));
}

public static StealthPayment[] GetPayments(Transaction transaction, BitcoinStealthAddress address, Key scan, bool allowP2sh = false)
public static StealthPayment[] GetPayments(Transaction transaction, BitcoinStealthAddress address, Key scan)
{
List<StealthPayment> result = new List<StealthPayment>();
for(int i = 0 ; i < transaction.Outputs.Count - 1 ; i++)
Expand All @@ -155,8 +156,6 @@ public static StealthPayment[] GetPayments(Transaction transaction, BitcoinSteal

if(scriptId != null)
{
if(!allowP2sh)
continue;
if(address == null)
throw new ArgumentNullException("address");
redeem = CreatePaymentScript(address, metadata.EphemKey, scan);
Expand Down
4 changes: 2 additions & 2 deletions NBitcoin/TransactionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ private void AssertOpReturn(string name)
}
}

public TransactionBuilder Send(BitcoinStealthAddress address, Money money, Key ephemKey = null, bool p2sh = false)
public TransactionBuilder Send(BitcoinStealthAddress address, Money money, Key ephemKey = null)
{
if(_OpReturnUser == null)
_OpReturnUser = "Stealth Payment";
Expand All @@ -474,7 +474,7 @@ public TransactionBuilder Send(BitcoinStealthAddress address, Money money, Key e

CurrentGroup.Builders.Add(ctx =>
{
var payment = address.CreatePayment(ephemKey, p2sh);
var payment = address.CreatePayment(ephemKey);
payment.AddToTransaction(ctx.Transaction, money);
return money;
});
Expand Down

0 comments on commit 8c9a3b6

Please sign in to comment.