diff --git a/build.gradle b/build.gradle index e0a7365..45751ee 100644 --- a/build.gradle +++ b/build.gradle @@ -42,11 +42,4 @@ test { jvmArgs = ["--enable-native-access=ALL-UNNAMED"] } -processResources { - doLast { - delete fileTree("$buildDir/resources/main/native").matching { - exclude "${osName}/**" - } - } -} diff --git a/src/main/java/org/bitcoin/Secp256k1Context.java b/src/main/java/org/bitcoin/Secp256k1Context.java index c5276d9..e5b45d5 100644 --- a/src/main/java/org/bitcoin/Secp256k1Context.java +++ b/src/main/java/org/bitcoin/Secp256k1Context.java @@ -4,6 +4,7 @@ import com.sparrowwallet.drongo.NativeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; import java.io.IOException; public class Secp256k1Context { @@ -33,23 +34,47 @@ public class Secp256k1Context { } private static boolean loadLibrary() { + String osName = System.getProperty("os.name"); + String osArch = System.getProperty("os.arch"); + String libName; + if(osName.startsWith("Windows")) { + libName = "libsecp256k1-0.dll"; + } else if(osName.startsWith("Mac")) { + libName = "libsecp256k1.dylib"; + } else { + libName = "libsecp256k1.so"; + } + + // Try loading from the application image lib/ directory + String javaHome = System.getProperty("java.home"); + if(javaHome != null) { + File libFile = new File(javaHome, "lib" + java.io.File.separator + libName); + if(libFile.exists()) { + try { + System.load(libFile.getAbsolutePath()); + return true; + } catch(UnsatisfiedLinkError e) { + log.debug("Could not load libsecp256k1 from java.home, falling back to JAR extraction", e); + } + } + } + + // Fallback: extract from JAR try { - String osName = System.getProperty("os.name"); - String osArch = System.getProperty("os.arch"); if(osName.startsWith("Mac") && osArch.equals("aarch64")) { - NativeUtils.loadLibraryFromJar("/native/osx/aarch64/libsecp256k1.dylib"); + NativeUtils.loadLibraryFromJar("/native/osx/aarch64/" + libName); } else if(osName.startsWith("Mac")) { - NativeUtils.loadLibraryFromJar("/native/osx/x64/libsecp256k1.dylib"); + NativeUtils.loadLibraryFromJar("/native/osx/x64/" + libName); } else if(osName.startsWith("Windows")) { - NativeUtils.loadLibraryFromJar("/native/windows/x64/libsecp256k1-0.dll"); + NativeUtils.loadLibraryFromJar("/native/windows/x64/" + libName); } else if(osArch.equals("aarch64")) { - NativeUtils.loadLibraryFromJar("/native/linux/aarch64/libsecp256k1.so"); + NativeUtils.loadLibraryFromJar("/native/linux/aarch64/" + libName); } else { - NativeUtils.loadLibraryFromJar("/native/linux/x64/libsecp256k1.so"); + NativeUtils.loadLibraryFromJar("/native/linux/x64/" + libName); } return true; - } catch(IOException e) { + } catch(UnsatisfiedLinkError | IOException e) { log.error("Error loading libsecp256k1 library", e); }