package com.bitbill.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Base64; import java.util.zip.Deflater; import java.util.zip.Inflater; public class BBSignEncoding { public static byte[] zlibCompress(byte[] data) { byte[] output; //LEVEL 5 to make it the same as iOS COMPRESSION_ZLIB Deflater compresser = new Deflater(5,true); compresser.reset(); compresser.setInput(data); compresser.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); try { byte[] buf = new byte[1024]; while (!compresser.finished()) { int i = compresser.deflate(buf); bos.write(buf, 0, i); } output = bos.toByteArray(); } catch (Exception e) { output = data; e.printStackTrace(); } finally { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } compresser.end(); return output; } public static byte[] zlibDecompress(byte[] data) { byte[] output; Inflater decompresser = new Inflater(true); decompresser.reset(); decompresser.setInput(data); ByteArrayOutputStream o = new ByteArrayOutputStream(data.length); try { byte[] buf = new byte[1024]; while (!decompresser.finished()) { int i = decompresser.inflate(buf); o.write(buf, 0, i); } output = o.toByteArray(); } catch (Exception e) { output = data; e.printStackTrace(); } finally { try { o.close(); } catch (IOException e) { e.printStackTrace(); } } decompresser.end(); return output; } public static void main(String[] args) throws Exception { //test qrcode String qrCodeStr = "bb_tx:jVRNc9RIDP0rKZ+pbH9/cAuh2LBFDqRmd0MYakrdrU4MY/eU7UkmUPnvyO0AgeKAfXG/J6mlJ0tfjo4a6Mq+n5qj50cNZ/VpnhE8Yp9OSz9BnF6nyk6lm6Cv5DRAPxLTln51v8NKry4uK3eDh9WhIl/WzXR4/XLdPF83IdpgQlIuoVcuR8aNSuCc5xJS0Eb6nEBA9uAykzFkBBkcZ1qI7BmydfNs3Qxwt0kwAUWk2JGyGygJOr2n4w4G6HDCYWFvYbvH5XOpkL4f66NI5a7HYQMpDTiONUHFo5YyehQ+SBGzDRKztwDSMB8tjwKzzsF7X1OZyi/eFhkmZaTWFj2GgJErAzx7Djlh1GBySDHwdfMwu5Nqm/2wrc7z4fi6lOstwq4dj2Pp1n/thjKVWLbHq1nrjMPp93K/BajOv6E/zFJh3oRtiZ824X7CJUnPnV10/E7ewHhTuSSFlB5NpKQ1uFjt8LBrB5i7PItnmdfKWGMWCae2w3GCbvfIUe2ezdzDk05t6G+o8RkwMd8vBHO/3qVYYJl+gkgaSk5KWeYY58JIBiJZZb1lRgukuzO91kSjDV1pvJWESsKSyJZZYbJVdJoR6pdWhHCDVhpjtBVqxtEuqLSKCykYcP3nreditv7TVnMXmVMyWZZYTFn5PNdXlb1txzZs5w5m2I5IyNhe9zDthxl7T/NiLTcmkEYqREYzwMnZexkdhKxs4o5pzaWBADwahU7klJzmKXJnEIyyMpC2SmKUChU3yVFRNlFYL2i+XACwOWuVmHJSZMUES46qQR5lyomfrpsPD8smuO9C2f404wN2MHyqUD3H0v5YBEI/2kRsb/HnHTLe4DDcLzukLCHPVx/38a2VWYp3d+ry4P7ueDrjXXo1vPrY+zehWj8O2+Kyurs9edFeXp1//u/iSrz553D2f/l3f9hfXF68deefX5TqkhGfbLUKUXfajgSvuKlQX/q42C0m0+GMZuJpactcnTxNgGR4+Ao="; String str = qrCodeStr.substring(6); //remove bb_tx: prefix //decompress byte[] decodedBytes = Base64.getDecoder().decode(str.getBytes("UTF-8")); String qrstrDecoded = new String(zlibDecompress(decodedBytes)); System.out.println(qrstrDecoded); //print //{ "amount" : "1000000", "sendContactId" : "tomtan", "transactionType" : "TRX", "hexTx" : "{\"txID\":\"bc7b6bd48de948fc0164da88913adb5639fda2af9a8f03cbfea3b810522f90e0\",\"raw_data\":{\"contract\":[{\"parameter\":{\"value\":{\"amount\":1000000,\"owner_address\":\"41c533c9e29b32cf7b3ef97aa3609c71c2ef5fb999\",\"to_address\":\"417e0ed463557e9ebbec146a1f91afdec5a6fbdcb1\"},\"type_url\":\"type.googleapis.com\/protocol.TransferContract\"},\"type\":\"TransferContract\"}],\"ref_block_bytes\":\"9187\",\"ref_block_hash\":\"d32339e6c46a5a8c\",\"expiration\":1709546766000,\"timestamp\":1709463906000},\"raw_data_hex\":\"0a0291872208d32339e6c46a5a8c40b0fdb5c7e0315a67080112630a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412320a1541c533c9e29b32cf7b3ef97aa3609c71c2ef5fb9991215417e0ed463557e9ebbec146a1f91afdec5a6fbdcb118c0843d70d0cdf49fe031\",\"visible\":false,\"signature\":[\"b77166b6c44bc02f91fe0993c8abf47d18055136aba1c64e82fdd851dc186ea6473b5a843ec34e416d83ef7d771923cb8baa7ff54d04832f4020d89ebe1c3dfd1C\"]}", "symbol" : "TRX", "remark" : "", "coinType" : "25", "receiveContactId" : "sherry", "to" : "TMTjucQ73f32Yw4Xx8Gm1dH1mdFrFjn9Lb", "address" : "TTwvABiXZMzVRZ2LJxHWoUuxuRXRQ8MzBo", "fee" : "100000", "decimals" : "6", "nonce" : "0", "txHash" : "", "contractAddress" : "TRX"} //compress //byte[] encodedBytes = Base64.encode(StringUtils.zlibCompress(jsonstr.getBytes("utf-8")), Base64.NO_WRAP); //mQrcodeMvpPresenter.createQrcode(AppConstants.WatchWallet.PREFIX_SIGN + new String(encodedBytes)); } }