3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

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
parent f65da9c201
commit 199a741850
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

@ -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);