3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 23:03:04 +00:00

Script: refactor in preparation for making it easier to manipulate and construct scripts using chunked form.

This commit is contained in:
Mike Hearn 2013-04-22 17:06:31 +02:00
parent 5ec4f43dee
commit 7837a54bc0

View File

@ -21,6 +21,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.spongycastle.crypto.digests.RIPEMD160Digest; import org.spongycastle.crypto.digests.RIPEMD160Digest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -32,6 +33,7 @@ import java.util.*;
import static com.google.bitcoin.core.ScriptOpCodes.*; import static com.google.bitcoin.core.ScriptOpCodes.*;
import static com.google.bitcoin.core.Utils.bytesToHexString; import static com.google.bitcoin.core.Utils.bytesToHexString;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
// TODO: Make this class a superclass with derived classes giving accessor methods for the various common templates. // TODO: Make this class a superclass with derived classes giving accessor methods for the various common templates.
@ -48,6 +50,7 @@ import static com.google.common.base.Preconditions.checkArgument;
*/ */
public class Script { public class Script {
private static final Logger log = LoggerFactory.getLogger(Script.class); private static final Logger log = LoggerFactory.getLogger(Script.class);
private static final long MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
/** /**
* An element that is either an opcode or a raw byte array (signature, pubkey, etc). * An element that is either an opcode or a raw byte array (signature, pubkey, etc).
@ -64,13 +67,36 @@ public class Script {
public boolean equalsOpCode(int opCode) { public boolean equalsOpCode(int opCode) {
return isOpCode && data.length == 1 && (0xFF & data[0]) == opCode; return isOpCode && data.length == 1 && (0xFF & data[0]) == opCode;
} }
public void write(OutputStream stream) throws IOException {
if (isOpCode) {
checkState(data.length == 1);
stream.write(data);
} else {
checkState(data.length <= MAX_SCRIPT_ELEMENT_SIZE);
if (data.length < OP_PUSHDATA1) {
stream.write(data.length);
} else if (data.length <= 0xFF) {
stream.write(OP_PUSHDATA1);
stream.write(data.length);
} else if (data.length <= 0xFFFF) {
stream.write(OP_PUSHDATA2);
stream.write(0xFF & data.length);
stream.write(0xFF & (data.length >> 8));
} else {
stream.write(OP_PUSHDATA4);
Utils.uint32ToByteStreamLE(data.length, stream);
}
stream.write(data);
}
}
} }
private byte[] program;
private int cursor;
// The program is a set of chunks where each element is either [opcode] or [data, data, data ...] // The program is a set of chunks where each element is either [opcode] or [data, data, data ...]
List<Chunk> chunks; protected List<Chunk> chunks;
// Unfortunately, scripts are not ever re-serialized or canonicalized when used in signature hashing. Thus we
// must preserve the exact bytes that we read off the wire, along with the parsed form.
protected byte[] program;
// Only for internal use // Only for internal use
private Script() {} private Script() {}
@ -81,7 +107,8 @@ public class Script {
* @param programBytes Array of program bytes from a transaction. * @param programBytes Array of program bytes from a transaction.
*/ */
public Script(byte[] programBytes) throws ScriptException { public Script(byte[] programBytes) throws ScriptException {
parse(programBytes, 0, programBytes.length); program = programBytes;
parse(programBytes);
} }
/** /**
@ -105,74 +132,64 @@ public class Script {
/** Returns the serialized program as a newly created byte array. */ /** Returns the serialized program as a newly created byte array. */
public byte[] getProgram() { public byte[] getProgram() {
return Arrays.copyOf(program, program.length);
}
private byte[] getData(int len) throws ScriptException {
if (len > program.length - cursor)
throw new ScriptException("Failed read of " + len + " bytes");
try { try {
byte[] buf = new byte[len]; // Don't round-trip as Satoshi's code doesn't and it would introduce a mismatch.
System.arraycopy(program, cursor, buf, 0, len); if (program != null)
cursor += len; return Arrays.copyOf(program, program.length);
return buf; ByteArrayOutputStream bos = new ByteArrayOutputStream();
} catch (ArrayIndexOutOfBoundsException e) { for (Chunk chunk : chunks) {
// We want running out of data in the array to be treated as a handleable script parsing exception, chunk.write(bos);
// not something that abnormally terminates the app. }
throw new ScriptException("Failed read of " + len + " bytes", e); return bos.toByteArray();
} catch (NegativeArraySizeException e) { } catch (IOException e) {
// We want running out of data in the array to be treated as a handleable script parsing exception, throw new RuntimeException(e); // Cannot happen.
// not something that abnormally terminates the app.
throw new ScriptException("Failed read of " + len + " bytes", e);
}
}
private int readByte() throws ScriptException {
try {
return 0xFF & program[cursor++];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ScriptException("Attempted to read outside of script boundaries");
} }
} }
/** /**
* To run a script, first we parse it which breaks it up into chunks representing pushes of * <p>To run a script, first we parse it which breaks it up into chunks representing pushes of data or logical
* data or logical opcodes. Then we can run the parsed chunks. * opcodes. Then we can run the parsed chunks.</p>
* <p/> *
* The reason for this split, instead of just interpreting directly, is to make it easier * <p>The reason for this split, instead of just interpreting directly, is to make it easier
* to reach into a programs structure and pull out bits of data without having to run it. * to reach into a programs structure and pull out bits of data without having to run it.
* This is necessary to render the to/from addresses of transactions in a user interface. * This is necessary to render the to/from addresses of transactions in a user interface.
* The official client does something similar. * The official client does something similar.</p>
*/ */
private void parse(byte[] programBytes, int offset, int length) throws ScriptException { private void parse(byte[] program) throws ScriptException {
// TODO: this is inefficient chunks = new ArrayList<Chunk>(10); // Arbitrary choice.
program = new byte[length]; ByteArrayInputStream bis = new ByteArrayInputStream(program);
System.arraycopy(programBytes, offset, program, 0, length); int initialSize = bis.available();
while (bis.available() > 0) {
int startLocationInProgram = initialSize - bis.available();
int opcode = bis.read();
offset = 0; long dataToRead = -1;
chunks = new ArrayList<Chunk>(10); // Arbitrary choice of initial size.
cursor = offset;
while (cursor < offset + length) {
int startLocationInProgram = cursor - offset;
int opcode = readByte();
if (opcode >= 0 && opcode < OP_PUSHDATA1) { if (opcode >= 0 && opcode < OP_PUSHDATA1) {
// Read some bytes of data, where how many is the opcode value itself. // Read some bytes of data, where how many is the opcode value itself.
chunks.add(new Chunk(false, getData(opcode), startLocationInProgram)); // opcode == len here. dataToRead = opcode;
} else if (opcode == OP_PUSHDATA1) { } else if (opcode == OP_PUSHDATA1) {
int len = readByte(); if (bis.available() < 1) throw new ScriptException("Unexpected end of script");
chunks.add(new Chunk(false, getData(len), startLocationInProgram)); dataToRead = bis.read();
} else if (opcode == OP_PUSHDATA2) { } else if (opcode == OP_PUSHDATA2) {
// Read a short, then read that many bytes of data. // Read a short, then read that many bytes of data.
int len = readByte() | (readByte() << 8); if (bis.available() < 2) throw new ScriptException("Unexpected end of script");
chunks.add(new Chunk(false, getData(len), startLocationInProgram)); dataToRead = bis.read() | (bis.read() << 8);
} else if (opcode == OP_PUSHDATA4) { } else if (opcode == OP_PUSHDATA4) {
// Read a uint32, then read that many bytes of data. // Read a uint32, then read that many bytes of data.
// Though this is allowed, because its value cannot be > 520, it should never actually be used // Though this is allowed, because its value cannot be > 520, it should never actually be used
long len = readByte() | (readByte() << 8) | (readByte() << 16) | (readByte() << 24); if (bis.available() < 4) throw new ScriptException("Unexpected end of script");
chunks.add(new Chunk(false, getData((int)len), startLocationInProgram)); dataToRead = bis.read() | (bis.read() << 8) | (bis.read() << 16) | (bis.read() << 24);
} else { }
if (dataToRead == -1) {
chunks.add(new Chunk(true, new byte[]{(byte) opcode}, startLocationInProgram)); chunks.add(new Chunk(true, new byte[]{(byte) opcode}, startLocationInProgram));
} else {
if (dataToRead > MAX_SCRIPT_ELEMENT_SIZE)
throw new ScriptException("Push of data element that is larger than the max element size");
byte[] data = new byte[(int)dataToRead];
if (dataToRead > 0 && bis.read(data, 0, (int)dataToRead) < dataToRead)
throw new ScriptException("Unexpected end of script");
chunks.add(new Chunk(false, data, startLocationInProgram));
} }
} }
} }
@ -184,10 +201,8 @@ public class Script {
* useful more exotic types of transaction, but today most payments are to addresses. * useful more exotic types of transaction, but today most payments are to addresses.
*/ */
public boolean isSentToRawPubKey() { public boolean isSentToRawPubKey() {
if (chunks.size() != 2) return chunks.size() == 2 && chunks.get(1).equalsOpCode(OP_CHECKSIG) &&
return false; !chunks.get(0).isOpCode && chunks.get(0).data.length > 1;
return chunks.get(1).equalsOpCode(OP_CHECKSIG) &&
!chunks.get(0).isOpCode && chunks.get(0).data.length > 1;
} }
/** /**
@ -197,8 +212,8 @@ public class Script {
* way to make payments due to the short and recognizable base58 form addresses come in. * way to make payments due to the short and recognizable base58 form addresses come in.
*/ */
public boolean isSentToAddress() { public boolean isSentToAddress() {
if (chunks.size() != 5) return false; return chunks.size() == 5 &&
return chunks.get(0).equalsOpCode(OP_DUP) && chunks.get(0).equalsOpCode(OP_DUP) &&
chunks.get(1).equalsOpCode(OP_HASH160) && chunks.get(1).equalsOpCode(OP_HASH160) &&
chunks.get(2).data.length == Address.LENGTH && chunks.get(2).data.length == Address.LENGTH &&
chunks.get(3).equalsOpCode(OP_EQUALVERIFY) && chunks.get(3).equalsOpCode(OP_EQUALVERIFY) &&
@ -424,7 +439,7 @@ public class Script {
public static int getSigOpCount(byte[] program) throws ScriptException { public static int getSigOpCount(byte[] program) throws ScriptException {
Script script = new Script(); Script script = new Script();
try { try {
script.parse(program, 0, program.length); script.parse(program);
} catch (ScriptException e) { } catch (ScriptException e) {
// Ignore errors and count up to the parse-able length // Ignore errors and count up to the parse-able length
} }
@ -437,14 +452,14 @@ public class Script {
public static long getP2SHSigOpCount(byte[] scriptSig) throws ScriptException { public static long getP2SHSigOpCount(byte[] scriptSig) throws ScriptException {
Script script = new Script(); Script script = new Script();
try { try {
script.parse(scriptSig, 0, scriptSig.length); script.parse(scriptSig);
} catch (ScriptException e) { } catch (ScriptException e) {
// Ignore errors and count up to the parse-able length // Ignore errors and count up to the parse-able length
} }
for (int i = script.chunks.size() - 1; i >= 0; i--) for (int i = script.chunks.size() - 1; i >= 0; i--)
if (!script.chunks.get(i).isOpCode) { if (!script.chunks.get(i).isOpCode) {
Script subScript = new Script(); Script subScript = new Script();
subScript.parse(script.chunks.get(i).data, 0, script.chunks.get(i).data.length); subScript.parse(script.chunks.get(i).data);
return getSigOpCount(subScript.chunks, true); return getSigOpCount(subScript.chunks, true);
} }
return 0; return 0;
@ -464,6 +479,10 @@ public class Script {
* Bitcoin system).</p> * Bitcoin system).</p>
*/ */
public boolean isPayToScriptHash() { public boolean isPayToScriptHash() {
// We have to check against the serialized form because BIP16 defines a P2SH output using an exact byte
// template, not the logical program structure. Thus you can have two programs that look identical when
// printed out but one is a P2SH script and the other isn't! :(
byte[] program = getProgram();
return program.length == 23 && return program.length == 23 &&
(program[0] & 0xff) == OP_HASH160 && (program[0] & 0xff) == OP_HASH160 &&
(program[1] & 0xff) == 0x14 && (program[1] & 0xff) == 0x14 &&
@ -581,9 +600,6 @@ public class Script {
boolean shouldExecute = !ifStack.contains(false); boolean shouldExecute = !ifStack.contains(false);
if (!chunk.isOpCode) { if (!chunk.isOpCode) {
if (chunk.data.length > 520)
throw new ScriptException("Attempted to push a data string larger than 520 bytes");
if (!shouldExecute) if (!shouldExecute)
continue; continue;
@ -1082,7 +1098,8 @@ public class Script {
byte[] pubKey = stack.pollLast(); byte[] pubKey = stack.pollLast();
byte[] sig = stack.pollLast(); byte[] sig = stack.pollLast();
byte[] connectedScript = Arrays.copyOfRange(script.program, lastCodeSepLocation, script.program.length); byte[] prog = script.getProgram();
byte[] connectedScript = Arrays.copyOfRange(prog, lastCodeSepLocation, prog.length);
UnsafeByteArrayOutputStream outStream = new UnsafeByteArrayOutputStream(sig.length + 1); UnsafeByteArrayOutputStream outStream = new UnsafeByteArrayOutputStream(sig.length + 1);
try { try {
@ -1138,7 +1155,8 @@ public class Script {
for (int i = 0; i < sigCount; i++) for (int i = 0; i < sigCount; i++)
sigs.add(stack.pollLast()); sigs.add(stack.pollLast());
byte[] connectedScript = Arrays.copyOfRange(script.program, lastCodeSepLocation, script.program.length); byte[] prog = script.getProgram();
byte[] connectedScript = Arrays.copyOfRange(prog, lastCodeSepLocation, prog.length);
for (byte[] sig : sigs) { for (byte[] sig : sigs) {
UnsafeByteArrayOutputStream outStream = new UnsafeByteArrayOutputStream(sig.length + 1); UnsafeByteArrayOutputStream outStream = new UnsafeByteArrayOutputStream(sig.length + 1);
@ -1194,7 +1212,7 @@ public class Script {
*/ */
public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey, public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey,
boolean enforceP2SH) throws ScriptException { boolean enforceP2SH) throws ScriptException {
if (program.length > 10000 || scriptPubKey.program.length > 10000) if (getProgram().length > 10000 || scriptPubKey.getProgram().length > 10000)
throw new ScriptException("Script larger than 10,000 bytes"); throw new ScriptException("Script larger than 10,000 bytes");
LinkedList<byte[]> stack = new LinkedList<byte[]>(); LinkedList<byte[]> stack = new LinkedList<byte[]>();