changes for input pane

This commit is contained in:
Craig Raw 2020-04-03 16:07:03 +02:00
parent 1a0880dde7
commit e574a2bfd3
4 changed files with 95 additions and 1 deletions

View file

@ -116,6 +116,18 @@ public class ECKey {
throw new IllegalArgumentException(Hex.toHexString(encoded)); throw new IllegalArgumentException(Hex.toHexString(encoded));
} }
/**
* Returns true if the given bytes represent a public key.
*/
public static boolean isPubKey(byte[] encoded) {
if (encoded.length == 33 && (encoded[0] == 0x02 || encoded[0] == 0x03))
return true;
else if (encoded.length == 65 && encoded[0] == 0x04)
return true;
else
return false;
}
/** /**
* Creates an ECKey that cannot be used for signing, only verifying signatures, from the given encoded point. * Creates an ECKey that cannot be used for signing, only verifying signatures, from the given encoded point.
* The compression state of pub will be preserved. * The compression state of pub will be preserved.

View file

@ -106,6 +106,10 @@ public class Script {
return Hex.toHexString(getProgram()); return Hex.toHexString(getProgram());
} }
public List<ScriptChunk> getChunks() {
return Collections.unmodifiableList(chunks);
}
/** /**
* Returns true if this script has the required form to contain a destination address * Returns true if this script has the required form to contain a destination address
*/ */
@ -241,6 +245,34 @@ public class Script {
return builder.toString().trim(); return builder.toString().trim();
} }
public String toDisplayString() {
StringBuilder builder = new StringBuilder();
int signatureCount = 1;
int pubKeyCount = 1;
for(ScriptChunk chunk : chunks) {
if(chunk.isSignature()) {
builder.append("<signature").append(signatureCount++).append(">");
} else if(chunk.isScript()) {
Script nestedScript = new Script(chunk.getData());
if(ScriptPattern.isP2WPKH(nestedScript)) {
builder.append("(OP_0 <wpkh>)");
} else if(ScriptPattern.isP2WSH(nestedScript)) {
builder.append("(OP_0 <wsh>)");
} else {
builder.append("(").append(nestedScript.toDisplayString()).append(")");
}
} else if(chunk.isPubKey()) {
builder.append("<pubkey").append(pubKeyCount++).append(">");
} else {
builder.append(chunk.toString());
}
builder.append(" ");
}
return builder.toString().trim();
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View file

@ -1,6 +1,7 @@
package com.sparrowwallet.drongo.protocol; package com.sparrowwallet.drongo.protocol;
import com.sparrowwallet.drongo.Utils; import com.sparrowwallet.drongo.Utils;
import com.sparrowwallet.drongo.crypto.ECKey;
import org.bouncycastle.util.encoders.Hex; import org.bouncycastle.util.encoders.Hex;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -73,6 +74,50 @@ public class ScriptChunk {
} }
} }
public int getOpcode() {
return opcode;
}
public byte[] getData() {
return data;
}
public boolean isSignature() {
if(data == null || data.length == 0) {
return false;
}
try {
ECKey.ECDSASignature.decodeFromDER(data);
} catch(SignatureDecodeException e) {
return false;
}
return true;
}
public boolean isScript() {
if(data == null || data.length == 0) {
return false;
}
try {
new Script(data);
} catch(ProtocolException e) {
return false;
}
return true;
}
public boolean isPubKey() {
if(data == null || data.length == 0) {
return false;
}
return ECKey.isPubKey(data);
}
public byte[] toByteArray() { public byte[] toByteArray() {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
try { try {
@ -105,7 +150,7 @@ public class ScriptChunk {
return "OP_" + getOpCodeName(opcode); return "OP_" + getOpCodeName(opcode);
} }
if (data.length == 0) { if (data.length == 0) {
return "0"; return "OP_0";
} }
return Hex.toHexString(data); return Hex.toHexString(data);

View file

@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class TransactionWitness { public class TransactionWitness {
@ -18,6 +19,10 @@ public class TransactionWitness {
pushes = new ArrayList<>(Math.min(pushCount, Utils.MAX_INITIAL_ARRAY_LENGTH)); pushes = new ArrayList<>(Math.min(pushCount, Utils.MAX_INITIAL_ARRAY_LENGTH));
} }
public List<byte[]> getPushes() {
return Collections.unmodifiableList(pushes);
}
public void setPush(int i, byte[] value) { public void setPush(int i, byte[] value) {
while (i >= pushes.size()) { while (i >= pushes.size()) {
pushes.add(new byte[]{}); pushes.add(new byte[]{});