diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Script.java b/src/main/java/com/sparrowwallet/drongo/protocol/Script.java index 08b174f..f03e053 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Script.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Script.java @@ -106,6 +106,10 @@ public class Script { return Hex.toHexString(getProgram()); } + public boolean isEmpty() { + return chunks.isEmpty(); + } + public List getChunks() { 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()); } + public Script getFirstNestedScript() { + for(ScriptChunk chunk : chunks) { + if(chunk.isScript()) { + return new Script(chunk.getData()); + } + } + + return null; + } + public static int decodeFromOpN(int opcode) { if((opcode != OP_0 && opcode != OP_1NEGATE) && (opcode < OP_1 || opcode > OP_16)) { throw new ProtocolException("decodeFromOpN called on non OP_N opcode: " + opcode); diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java b/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java index e899fcb..09fb56a 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java @@ -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) */ public boolean isOpCode() { - return opcode > OP_PUSHDATA4; + return opcode == ScriptOpCodes.OP_0 || opcode > OP_PUSHDATA4; } public void write(OutputStream stream) throws IOException { @@ -146,6 +146,9 @@ public class ScriptChunk { } static int getOpcodeForLength(int length) { + if(length == 0) { + return OP_0; + } if(length <= 0xFF) { return OP_PUSHDATA1; } diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java index d1ca1c3..b3f8a7e 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java @@ -71,13 +71,13 @@ public class TransactionWitness { return builder.toString().trim(); } - public String toDisplayString() { + public List asScriptChunks() { List scriptChunks = new ArrayList<>(pushes.size()); for(byte[] push : pushes) { scriptChunks.add(new ScriptChunk(ScriptChunk.getOpcodeForLength(push.length), push)); } - return Script.toDisplayString(scriptChunks); + return scriptChunks; } @Override