3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 18:25:51 +00:00

ScriptBuilder: Add methods opTrue() and opFalse().

This commit is contained in:
Nicola Atzei 2018-01-02 13:26:04 +01:00 committed by Andreas Schildbach
parent e9469b3a72
commit 3b80b707a5
2 changed files with 53 additions and 1 deletions

View File

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

View File

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