script convenience methods

This commit is contained in:
Craig Raw 2020-04-04 15:13:13 +02:00
parent 4d875f5ad0
commit d28186f8c9
3 changed files with 20 additions and 3 deletions

View file

@ -106,6 +106,10 @@ public class Script {
return Hex.toHexString(getProgram()); return Hex.toHexString(getProgram());
} }
public boolean isEmpty() {
return chunks.isEmpty();
}
public List<ScriptChunk> getChunks() { public List<ScriptChunk> getChunks() {
return Collections.unmodifiableList(chunks); return Collections.unmodifiableList(chunks);
} }
@ -165,6 +169,16 @@ public class Script {
throw new NonStandardScriptException("Cannot find number of required signatures for non standard script: " + toString()); throw new NonStandardScriptException("Cannot find number of required signatures for non standard script: " + toString());
} }
public Script getFirstNestedScript() {
for(ScriptChunk chunk : chunks) {
if(chunk.isScript()) {
return new Script(chunk.getData());
}
}
return null;
}
public static int decodeFromOpN(int opcode) { public static int decodeFromOpN(int opcode) {
if((opcode != OP_0 && opcode != OP_1NEGATE) && (opcode < OP_1 || opcode > OP_16)) { if((opcode != OP_0 && opcode != OP_1NEGATE) && (opcode < OP_1 || opcode > OP_16)) {
throw new ProtocolException("decodeFromOpN called on non OP_N opcode: " + opcode); throw new ProtocolException("decodeFromOpN called on non OP_N opcode: " + opcode);

View file

@ -42,7 +42,7 @@ public class ScriptChunk {
* If this chunk is a single byte of non-pushdata content (could be OP_RESERVED or some invalid Opcode) * If this chunk is a single byte of non-pushdata content (could be OP_RESERVED or some invalid Opcode)
*/ */
public boolean isOpCode() { public boolean isOpCode() {
return opcode > OP_PUSHDATA4; return opcode == ScriptOpCodes.OP_0 || opcode > OP_PUSHDATA4;
} }
public void write(OutputStream stream) throws IOException { public void write(OutputStream stream) throws IOException {
@ -146,6 +146,9 @@ public class ScriptChunk {
} }
static int getOpcodeForLength(int length) { static int getOpcodeForLength(int length) {
if(length == 0) {
return OP_0;
}
if(length <= 0xFF) { if(length <= 0xFF) {
return OP_PUSHDATA1; return OP_PUSHDATA1;
} }

View file

@ -71,13 +71,13 @@ public class TransactionWitness {
return builder.toString().trim(); return builder.toString().trim();
} }
public String toDisplayString() { public List<ScriptChunk> asScriptChunks() {
List<ScriptChunk> scriptChunks = new ArrayList<>(pushes.size()); List<ScriptChunk> scriptChunks = new ArrayList<>(pushes.size());
for(byte[] push : pushes) { for(byte[] push : pushes) {
scriptChunks.add(new ScriptChunk(ScriptChunk.getOpcodeForLength(push.length), push)); scriptChunks.add(new ScriptChunk(ScriptChunk.getOpcodeForLength(push.length), push));
} }
return Script.toDisplayString(scriptChunks); return scriptChunks;
} }
@Override @Override