Skip to content

Commit

Permalink
do not load native libs in MoneroUtils until used
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jun 6, 2024
1 parent 2e1feb4 commit 9fb992c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/monero/common/MoneroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public static String getVersion() {
return "0.8.29";
}

// try to load jni bindings
static {
tryLoadNativeLibrary();
}

/**
* Try to load the native library if not already loaded.
*/
Expand All @@ -58,6 +53,9 @@ public static void tryLoadNativeLibrary() {

public static void loadNativeLibrary() {

// skip if already loaded
if (isNativeLibraryLoaded()) return;

// try to load from java library path
try {
String libName = (System.getProperty("os.name").toLowerCase().contains("windows") ? "lib" : "") + "monero-java";
Expand Down Expand Up @@ -136,7 +134,7 @@ public static void loadNativeLibrary() {
*/
public static boolean isNativeLibraryLoaded() {
try {
mapToBinary(new HashMap<>());
jsonToBinaryJni(JsonUtils.serialize(new HashMap<>()));
return true;
} catch (Exception | UnsatisfiedLinkError e) {
return false;
Expand Down Expand Up @@ -309,6 +307,7 @@ public static void validatePublicSpendKey(String publicSpendKey) {
* @return the integrated address
*/
public static MoneroIntegratedAddress getIntegratedAddress(MoneroNetworkType networkType, String standardAddress, String paymentId) {
loadNativeLibrary();
try {
return JsonUtils.deserialize(getIntegratedAddressJni(networkType.ordinal(), standardAddress, paymentId == null ? "" : paymentId), MoneroIntegratedAddress.class);
} catch (Exception err) {
Expand Down Expand Up @@ -482,17 +481,20 @@ public static <T extends MoneroTx> void mergeTx(List<T> txs, T tx) {
}

public static byte[] mapToBinary(Map<String, Object> map) {
loadNativeLibrary();
return jsonToBinaryJni(JsonUtils.serialize(map));
}

public static Map<String, Object> binaryToMap(byte[] bin) {
loadNativeLibrary();
return JsonUtils.deserialize(binaryToJsonJni(bin), new TypeReference<Map<String, Object>>(){});
}

@SuppressWarnings("unchecked")
public static Map<String, Object> binaryBlocksToMap(byte[] binBlocks) {

// convert binary blocks to json then to map
loadNativeLibrary();
Map<String, Object> map = JsonUtils.deserialize(MoneroRpcConnection.MAPPER, binaryBlocksToJsonJni(binBlocks), new TypeReference<Map<String, Object>>(){});

// parse blocks to maps
Expand Down Expand Up @@ -560,6 +562,7 @@ public static int getLogLevel() {
* @param console specifies whether or not to write to the console
*/
public static void configureNativeLogging(String path, boolean console) {
loadNativeLibrary();
configureLoggingJni(path, console);
}

Expand Down

0 comments on commit 9fb992c

Please sign in to comment.