From 199a74185075aac0f96db7c59a53b67d0f7166dc Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 14 Nov 2014 13:57:58 +0100 Subject: [PATCH] Add an OP_RETURN convenience method on ScriptBuilder and convert unit tests to use it. --- .../main/java/org/bitcoinj/script/ScriptBuilder.java | 10 ++++++++++ core/src/test/java/org/bitcoinj/core/WalletTest.java | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java index 780c893a..2fc5fdfb 100644 --- a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java +++ b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java @@ -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(); + } } diff --git a/core/src/test/java/org/bitcoinj/core/WalletTest.java b/core/src/test/java/org/bitcoinj/core/WalletTest.java index e89fc205..a9fef68f 100644 --- a/core/src/test/java/org/bitcoinj/core/WalletTest.java +++ b/core/src/test/java/org/bitcoinj/core/WalletTest.java @@ -1650,7 +1650,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); @@ -1662,7 +1662,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); @@ -1675,7 +1675,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); @@ -1688,8 +1688,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);