From 4d875f5ad08d728dd6959772210f1675d14fca82 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 3 Apr 2020 17:14:10 +0200 Subject: [PATCH] witness as display string --- .../sparrowwallet/drongo/protocol/Script.java | 6 +++++- .../drongo/protocol/ScriptChunk.java | 10 +++++++++ .../drongo/protocol/TransactionWitness.java | 21 +++++++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Script.java b/src/main/java/com/sparrowwallet/drongo/protocol/Script.java index 8b6e77c..08b174f 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Script.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Script.java @@ -246,10 +246,14 @@ public class Script { } public String toDisplayString() { + return toDisplayString(chunks); + } + + static String toDisplayString(List scriptChunks) { StringBuilder builder = new StringBuilder(); int signatureCount = 1; int pubKeyCount = 1; - for(ScriptChunk chunk : chunks) { + for(ScriptChunk chunk : scriptChunks) { if(chunk.isSignature()) { builder.append(""); } else if(chunk.isScript()) { diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java b/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java index 71a184d..e899fcb 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/ScriptChunk.java @@ -145,6 +145,16 @@ public class ScriptChunk { return opcodeLength + pushDataSizeLength + dataLength; } + static int getOpcodeForLength(int length) { + if(length <= 0xFF) { + return OP_PUSHDATA1; + } + if(length <= 0xFFFF) { + return OP_PUSHDATA2; + } + return OP_PUSHDATA4; + } + public String toString() { if (data == null) { return "OP_" + getOpCodeName(opcode); diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java index 160c308..d1ca1c3 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionWitness.java @@ -56,19 +56,28 @@ public class TransactionWitness { @Override public String toString() { - StringBuffer buffer = new StringBuffer(); + StringBuilder builder = new StringBuilder(); for (byte[] push : pushes) { if (push == null) { - buffer.append("NULL"); + builder.append("NULL"); } else if (push.length == 0) { - buffer.append("EMPTY"); + builder.append("EMPTY"); } else { - buffer.append(Hex.toHexString(push)); + builder.append(Hex.toHexString(push)); } - buffer.append(" "); + builder.append(" "); } - return buffer.toString().trim(); + return builder.toString().trim(); + } + + public String toDisplayString() { + List scriptChunks = new ArrayList<>(pushes.size()); + for(byte[] push : pushes) { + scriptChunks.add(new ScriptChunk(ScriptChunk.getOpcodeForLength(push.length), push)); + } + + return Script.toDisplayString(scriptChunks); } @Override