Add an OP_RETURN convenience method on ScriptBuilder and convert unit tests to use it.

This commit is contained in:
Mike Hearn
2014-11-14 13:57:58 +01:00
committed by Andreas Schildbach
parent 9225cf8c1f
commit ce545065d8
2 changed files with 15 additions and 5 deletions

View File

@@ -331,4 +331,14 @@ public class ScriptBuilder {
Collections.sort(pubkeys, ECKey.PUBKEY_COMPARATOR);
return ScriptBuilder.createMultiSigOutputScript(threshold, pubkeys);
}
/**
* Creates a script of the form OP_RETURN [data]. This feature allows you to attach a small piece of data (like
* a hash of something stored elsewhere) to a zero valued output which can never be spent and thus does not pollute
* the ledger.
*/
public static Script createOpReturnScript(byte[] data) {
checkArgument(data.length <= 40);
return new ScriptBuilder().op(OP_RETURN).data(data).build();
}
}

View File

@@ -1561,7 +1561,7 @@ public class WalletTest extends TestWithWallet {
receiveATransaction(wallet, myAddress);
Transaction tx = new Transaction(params);
Coin messagePrice = Coin.ZERO;
Script script = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("hello world!".getBytes()).build();
Script script = ScriptBuilder.createOpReturnScript("hello world!".getBytes());
tx.addOutput(messagePrice, script);
SendRequest request = Wallet.SendRequest.forTx(tx);
wallet.completeTx(request);
@@ -1573,7 +1573,7 @@ public class WalletTest extends TestWithWallet {
receiveATransaction(wallet, myAddress);
Transaction tx = new Transaction(params);
Coin messagePrice = CENT;
Script script = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("hello world!".getBytes()).build();
Script script = ScriptBuilder.createOpReturnScript("hello world!".getBytes());
tx.addOutput(messagePrice, script);
SendRequest request = Wallet.SendRequest.forTx(tx);
wallet.completeTx(request);
@@ -1586,7 +1586,7 @@ public class WalletTest extends TestWithWallet {
Address notMyAddr = new ECKey().toAddress(params);
Transaction tx = new Transaction(params);
Coin messagePrice = Coin.ZERO;
Script script = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("hello world!".getBytes()).build();
Script script = ScriptBuilder.createOpReturnScript("hello world!".getBytes());
tx.addOutput(CENT, notMyAddr);
tx.addOutput(messagePrice, script);
SendRequest request = Wallet.SendRequest.forTx(tx);
@@ -1599,8 +1599,8 @@ public class WalletTest extends TestWithWallet {
receiveATransaction(wallet, myAddress);
Transaction tx = new Transaction(params);
Coin messagePrice = Coin.ZERO;
Script script1 = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("hello world 1!".getBytes()).build();
Script script2 = new ScriptBuilder().op(ScriptOpCodes.OP_RETURN).data("hello world 2!".getBytes()).build();
Script script1 = ScriptBuilder.createOpReturnScript("hello world 1!".getBytes());
Script script2 = ScriptBuilder.createOpReturnScript("hello world 2!".getBytes());
tx.addOutput(messagePrice, script1);
tx.addOutput(messagePrice, script2);
SendRequest request = Wallet.SendRequest.forTx(tx);