From 42b7ea67eb4aac6e820293a2bdba4196827510f5 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Tue, 3 Sep 2024 14:19:21 -0700 Subject: [PATCH] api/module-info,ByteArray: stop leaking HEX_FORMAT Create nested ByteArrayBase abstract class. (Will be flushed-out later with more methods) Move HEX_FORMAT inside ByteArrayBase so it can be `private`. module-info: stop exporting o.b.s.api.internal --- secp-api/src/main/java/module-info.java | 1 - .../java/org/bitcoinj/secp/api/ByteArray.java | 15 ++++++++++++--- .../org/bitcoinj/secp/bouncy/BouncyPubKey.java | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/secp-api/src/main/java/module-info.java b/secp-api/src/main/java/module-info.java index ce8736a..f460bc7 100644 --- a/secp-api/src/main/java/module-info.java +++ b/secp-api/src/main/java/module-info.java @@ -18,7 +18,6 @@ requires org.jspecify; exports org.bitcoinj.secp.api; - exports org.bitcoinj.secp.api.internal; /* TEMPORARY */ uses org.bitcoinj.secp.api.Secp256k1Provider; } diff --git a/secp-api/src/main/java/org/bitcoinj/secp/api/ByteArray.java b/secp-api/src/main/java/org/bitcoinj/secp/api/ByteArray.java index cb42758..2b97ebe 100644 --- a/secp-api/src/main/java/org/bitcoinj/secp/api/ByteArray.java +++ b/secp-api/src/main/java/org/bitcoinj/secp/api/ByteArray.java @@ -24,7 +24,6 @@ * An effectively-immutable byte array. */ public interface ByteArray extends Comparable { - HexFormat HEX_FORMAT = new HexFormat(); /** * @return the bytes as an array @@ -35,7 +34,7 @@ public interface ByteArray extends Comparable { * @return the bytes as a hex-formatted string */ default String formatHex() { - return HEX_FORMAT.formatHex(bytes()); + return toHexString(bytes()); } /** @@ -55,6 +54,16 @@ default int compareTo(ByteArray o) { * @return hex-formatted String */ static String toHexString(byte[] bytes) { - return HEX_FORMAT.formatHex(bytes); + return ByteArrayBase.HEX_FORMAT.formatHex(bytes); + } + + /** + * Abstract Base Class for creating ByteArray Implementations + */ + abstract class ByteArrayBase implements ByteArray { + private static final HexFormat HEX_FORMAT = new HexFormat(); + + @Override + public abstract byte[] bytes(); } } diff --git a/secp-bouncy/src/main/java/org/bitcoinj/secp/bouncy/BouncyPubKey.java b/secp-bouncy/src/main/java/org/bitcoinj/secp/bouncy/BouncyPubKey.java index cee78cc..719b9e0 100644 --- a/secp-bouncy/src/main/java/org/bitcoinj/secp/bouncy/BouncyPubKey.java +++ b/secp-bouncy/src/main/java/org/bitcoinj/secp/bouncy/BouncyPubKey.java @@ -50,7 +50,7 @@ public byte[] getEncoded() { @Override public String toString() { - return ByteArray.HEX_FORMAT.formatHex(bytes()); + return ByteArray.toHexString(bytes()); } @Override