From 7b0dfd66a7ff44f70b0d6434c5fbde8c76b5d585 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 3 May 2024 11:46:33 +0200 Subject: [PATCH] fix premature decompression of bbqr zlib parts --- .../sparrow/io/bbqr/BBQRDecoder.java | 1 + .../sparrow/io/bbqr/BBQREncoder.java | 2 +- .../sparrow/io/bbqr/BBQREncoding.java | 22 +++++++++++++++++-- .../sparrow/io/bbqr/BBQRHeader.java | 4 ++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRDecoder.java b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRDecoder.java index 2a92ca12..5572edfc 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRDecoder.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRDecoder.java @@ -37,6 +37,7 @@ public class BBQRDecoder { if(receivedParts.size() == totalParts) { byte[] data = concatParts(); + data = header.inflate(data); if(type == BBQRType.PSBT) { result = new Result(new PSBT(data)); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoder.java b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoder.java index 591f46f1..9ae25a40 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoder.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoder.java @@ -35,7 +35,7 @@ public class BBQREncoder { BBQREncoding encoding = desiredEncoding; try { - encoded = encoding.encode(data); + encoded = encoding.encode(encoding.deflate(data)); if(encoding == BBQREncoding.ZLIB) { String uncompressed = BBQREncoding.BASE32.encode(data); if(encoded.length() > uncompressed.length()) { diff --git a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoding.java b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoding.java index 293fa183..fec1adcb 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoding.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQREncoding.java @@ -42,6 +42,11 @@ public enum BBQREncoding { }, ZLIB("Z") { @Override public String encode(byte[] data) throws BBQREncodingException { + return BASE32.encode(data); + } + + @Override + public byte[] deflate(byte[] data) throws BBQREncodingException { try { Deflater deflater = new Deflater(JZlib.Z_BEST_COMPRESSION, 10, true); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -49,7 +54,7 @@ public enum BBQREncoding { zOut.write(data); zOut.close(); - return BASE32.encode(out.toByteArray()); + return out.toByteArray(); } catch(Exception e) { throw new BBQREncodingException("Error deflating with zlib", e); } @@ -57,9 +62,14 @@ public enum BBQREncoding { @Override public byte[] decode(String part) throws BBQREncodingException { + return BASE32.decode(part); + } + + @Override + public byte[] inflate(byte[] data) throws BBQREncodingException { try { Inflater inflater = new Inflater(10, true); - ByteArrayInputStream in = new ByteArrayInputStream(BASE32.decode(part)); + ByteArrayInputStream in = new ByteArrayInputStream(data); InflaterInputStream zIn = new InflaterInputStream(in, inflater); byte[] decoded = zIn.readAllBytes(); zIn.close(); @@ -96,6 +106,14 @@ public enum BBQREncoding { return code; } + public byte[] deflate(byte[] data) throws BBQREncodingException { + return data; + } + + public byte[] inflate(byte[] data) throws BBQREncodingException { + return data; + } + public abstract String encode(byte[] data) throws BBQREncodingException; public abstract byte[] decode(String part) throws BBQREncodingException; diff --git a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRHeader.java b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRHeader.java index 4689ba91..4a18813e 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRHeader.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/bbqr/BBQRHeader.java @@ -13,6 +13,10 @@ public record BBQRHeader(BBQREncoding encoding, BBQRType type, int seqTotal, int return encoding.decode(part.substring(8)); } + public byte[] inflate(byte[] data) { + return encoding.inflate(data); + } + public static BBQRHeader fromString(String part) { if(part.length() < 8) { throw new IllegalArgumentException("Part too short");