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

ScriptChunk: Add method toByteArray().

This commit is contained in:
Nicola Atzei 2018-02-21 11:27:58 +01:00 committed by Andreas Schildbach
parent b87d22e2be
commit e42e6686cc
2 changed files with 77 additions and 0 deletions

View File

@ -21,6 +21,8 @@ import org.bitcoinj.core.Utils;
import com.google.common.base.Objects;
import javax.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
@ -138,6 +140,17 @@ public class ScriptChunk {
}
}
public byte[] toByteArray() {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
write(stream);
} catch (IOException e) {
// Should not happen as ByteArrayOutputStream does not throw IOException on write
throw new RuntimeException(e);
}
return stream.toByteArray();
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder();

View File

@ -16,16 +16,24 @@
package org.bitcoinj.script;
import static org.bitcoinj.script.ScriptOpCodes.OP_0;
import static org.bitcoinj.script.ScriptOpCodes.OP_IF;
import static org.bitcoinj.script.ScriptOpCodes.OP_PUSHDATA1;
import static org.bitcoinj.script.ScriptOpCodes.OP_PUSHDATA2;
import static org.bitcoinj.script.ScriptOpCodes.OP_PUSHDATA4;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Random;
import org.junit.Test;
import org.spongycastle.util.Arrays;
public class ScriptChunkTest {
private static final Random RANDOM = new Random(42);
@Test
public void testShortestPossibleDataPush() {
assertTrue("empty push", new ScriptBuilder().data(new byte[0]).build().getChunks().get(0)
@ -46,4 +54,60 @@ public class ScriptChunkTest {
assertFalse("push of 255 bytes", new ScriptChunk(OP_PUSHDATA2, new byte[255]).isShortestPossiblePushData());
assertFalse("push of 65535 bytes", new ScriptChunk(OP_PUSHDATA4, new byte[65535]).isShortestPossiblePushData());
}
@Test
public void testToByteArray_opcode() {
byte[] expected = new byte[] { OP_IF };
byte[] actual = new ScriptChunk(OP_IF, null).toByteArray();
assertArrayEquals(expected, actual);
}
@Test
public void testToByteArray_smallNum() {
byte[] expected = new byte[] { OP_0 };
byte[] actual = new ScriptChunk(OP_0, null).toByteArray();
assertArrayEquals(expected, actual);
}
@Test
public void testToByteArray_lt_OP_PUSHDATA1() {
// < OP_PUSHDATA1
for (byte len = 1; len < OP_PUSHDATA1; len++) {
byte[] bytes = new byte[len];
RANDOM.nextBytes(bytes);
byte[] expected = Arrays.concatenate(new byte[] { len }, bytes);
byte[] actual = new ScriptChunk(len, bytes).toByteArray();
assertArrayEquals(expected, actual);
}
}
@Test
public void testToByteArray_OP_PUSHDATA1() {
// OP_PUSHDATA1
byte[] bytes = new byte[0xFF];
RANDOM.nextBytes(bytes);
byte[] expected = Arrays.concatenate(new byte[] { OP_PUSHDATA1, (byte) 0xFF }, bytes);
byte[] actual = new ScriptChunk(OP_PUSHDATA1, bytes).toByteArray();
assertArrayEquals(expected, actual);
}
@Test
public void testToByteArray_OP_PUSHDATA2() {
// OP_PUSHDATA2
byte[] bytes = new byte[0x0102];
RANDOM.nextBytes(bytes);
byte[] expected = Arrays.concatenate(new byte[] { OP_PUSHDATA2, 0x02, 0x01 }, bytes);
byte[] actual = new ScriptChunk(OP_PUSHDATA2, bytes).toByteArray();
assertArrayEquals(expected, actual);
}
@Test
public void testToByteArray_OP_PUSHDATA4() {
// OP_PUSHDATA4
byte[] bytes = new byte[0x0102];
RANDOM.nextBytes(bytes);
byte[] expected = Arrays.concatenate(new byte[] { OP_PUSHDATA4, 0x02, 0x01, 0x00, 0x00 }, bytes);
byte[] actual = new ScriptChunk(OP_PUSHDATA4, bytes).toByteArray();
assertArrayEquals(expected, actual);
}
}