mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-11-02 18:26:43 +00:00
script convenience methods
This commit is contained in:
parent
4d875f5ad0
commit
d28186f8c9
3 changed files with 20 additions and 3 deletions
|
@ -106,6 +106,10 @@ public class Script {
|
|||
return Hex.toHexString(getProgram());
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return chunks.isEmpty();
|
||||
}
|
||||
|
||||
public List<ScriptChunk> 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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -71,13 +71,13 @@ public class TransactionWitness {
|
|||
return builder.toString().trim();
|
||||
}
|
||||
|
||||
public String toDisplayString() {
|
||||
public List<ScriptChunk> asScriptChunks() {
|
||||
List<ScriptChunk> 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
|
||||
|
|
Loading…
Reference in a new issue