support loading libsecp256k1 from jvm lib folder

This commit is contained in:
Craig Raw 2026-03-13 07:58:32 +02:00
parent f5a52f9eae
commit dad9fe2fcc
2 changed files with 33 additions and 15 deletions

View File

@ -42,11 +42,4 @@ test {
jvmArgs = ["--enable-native-access=ALL-UNNAMED"]
}
processResources {
doLast {
delete fileTree("$buildDir/resources/main/native").matching {
exclude "${osName}/**"
}
}
}

View File

@ -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);
}