From 3b80b707a53b318695e81afd0934101aec7311c4 Mon Sep 17 00:00:00 2001 From: Nicola Atzei Date: Tue, 2 Jan 2018 13:26:04 +0100 Subject: [PATCH] ScriptBuilder: Add methods opTrue() and opFalse(). --- .../org/bitcoinj/script/ScriptBuilder.java | 35 +++++++++++++++++++ .../bitcoinj/script/ScriptBuilderTest.java | 19 +++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java index abe5d586..efca88cb 100644 --- a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java +++ b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java @@ -1,5 +1,6 @@ /* * Copyright 2013 Google Inc. + * Copyright 2018 Nicola Atzei * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -207,6 +208,40 @@ public class ScriptBuilder { return addChunk(index, new ScriptChunk(data.length, data)); } + /** + * Adds true to the end of the program. + * @return this + */ + public ScriptBuilder opTrue() { + return number(1); // it push OP_1/OP_TRUE + } + + /** + * Adds true to the given index in the program. + * @param index at which insert true + * @return this + */ + public ScriptBuilder opTrue(int index) { + return number(index, 1); // push OP_1/OP_TRUE + } + + /** + * Adds false to the end of the program. + * @return this + */ + public ScriptBuilder opFalse() { + return number(0); // push OP_0/OP_FALSE + } + + /** + * Adds false to the given index in the program. + * @param index at which insert true + * @return this + */ + public ScriptBuilder opFalse(int index) { + return number(index, 0); // push OP_0/OP_FALSE + } + /** Creates a new immutable Script based on the state of the builder. */ public Script build() { return new Script(chunks); diff --git a/core/src/test/java/org/bitcoinj/script/ScriptBuilderTest.java b/core/src/test/java/org/bitcoinj/script/ScriptBuilderTest.java index 6721e4e5..8be1faf1 100644 --- a/core/src/test/java/org/bitcoinj/script/ScriptBuilderTest.java +++ b/core/src/test/java/org/bitcoinj/script/ScriptBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Nicola Atzei + * Copyright 2018 Nicola Atzei * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,9 @@ package org.bitcoinj.script; +import static org.bitcoinj.script.ScriptOpCodes.OP_FALSE; +import static org.bitcoinj.script.ScriptOpCodes.OP_TRUE; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -31,4 +34,18 @@ public class ScriptBuilderTest { } } } + + @Test + public void testOpTrue() { + byte[] expected = new byte[] { OP_TRUE }; + byte[] s = new ScriptBuilder().opTrue().build().getProgram(); + assertArrayEquals(expected, s); + } + + @Test + public void testOpFalse() { + byte[] expected = new byte[] { OP_FALSE }; + byte[] s = new ScriptBuilder().opFalse().build().getProgram(); + assertArrayEquals(expected, s); + } }