mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-26 01:56:44 +00:00
support detection of utf8 string script chunks
This commit is contained in:
parent
883b1c95b4
commit
0002d6bae6
2 changed files with 34 additions and 2 deletions
|
@ -13,6 +13,7 @@ import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.CharsetDecoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -31,6 +32,16 @@ public class Utils {
|
||||||
return s.matches(BASE64_REGEX);
|
return s.matches(BASE64_REGEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isUtf8(byte[] bytes) {
|
||||||
|
try {
|
||||||
|
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
|
||||||
|
decoder.decode(java.nio.ByteBuffer.wrap(bytes));
|
||||||
|
return true;
|
||||||
|
} catch(Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static String bytesToHex(byte[] bytes) {
|
public static String bytesToHex(byte[] bytes) {
|
||||||
char[] hexChars = new char[bytes.length * 2];
|
char[] hexChars = new char[bytes.length * 2];
|
||||||
for ( int j = 0; j < bytes.length; j++ ) {
|
for ( int j = 0; j < bytes.length; j++ ) {
|
||||||
|
|
|
@ -7,6 +7,8 @@ import org.bouncycastle.util.encoders.Hex;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -28,7 +30,11 @@ public class ScriptChunk {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ScriptChunk fromOpcode(int opcode) {
|
public static ScriptChunk fromOpcode(int opcode) {
|
||||||
return new ScriptChunk(opcode, null);
|
return new ScriptChunk(opcode, opcode == ScriptOpCodes.OP_0 ? new byte[0] : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ScriptChunk fromString(String strData, Charset charset) {
|
||||||
|
return fromData(strData.getBytes(charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ScriptChunk fromData(byte[] data) {
|
public static ScriptChunk fromData(byte[] data) {
|
||||||
|
@ -68,7 +74,7 @@ public class ScriptChunk {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(OutputStream stream) throws IOException {
|
public void write(OutputStream stream) throws IOException {
|
||||||
if (isOpCode()) {
|
if (isOpCode() && opcode != ScriptOpCodes.OP_0) {
|
||||||
if(data != null) throw new IllegalStateException("Data must be null for opcode chunk");
|
if(data != null) throw new IllegalStateException("Data must be null for opcode chunk");
|
||||||
stream.write(opcode);
|
stream.write(opcode);
|
||||||
} else if (data != null) {
|
} else if (data != null) {
|
||||||
|
@ -126,6 +132,18 @@ public class ScriptChunk {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isString() {
|
||||||
|
if(data == null || data.length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isSignature() || isPubKey()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Utils.isUtf8(data);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isScript() {
|
public boolean isScript() {
|
||||||
if(data == null || data.length == 0) {
|
if(data == null || data.length == 0) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -213,6 +231,9 @@ public class ScriptChunk {
|
||||||
if (data.length == 0) {
|
if (data.length == 0) {
|
||||||
return "OP_0";
|
return "OP_0";
|
||||||
}
|
}
|
||||||
|
if(Utils.isUtf8(data)) {
|
||||||
|
return new String(data, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
return Hex.toHexString(data);
|
return Hex.toHexString(data);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue