androidkotlinbitcoinj

How to use NativeSecp256k1 wrapper from Bitcoinj in Android?


As I understand bitcoinj library contains wrapper for bitcoin-core functions - NativeSecp256k1. I am trying to call one of methods from this wrapper:

NativeSecp256k1.secKeyVerify(byteArrayOf(...))

But I got a crash:

java.lang.UnsatisfiedLinkError: No implementation found for int org.bitcoin.NativeSecp256k1.secp256k1_ec_seckey_verify(java.nio.ByteBuffer, long) (tried Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1seckey_1verify and Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1seckey_1verify__Ljava_nio_ByteBuffer_2J) at org.bitcoin.NativeSecp256k1.secp256k1_ec_seckey_verify(Native Method) at org.bitcoin.NativeSecp256k1.secKeyVerify(NativeSecp256k1.java:134) at com.my.app.MainActivity.onCreate(MainActivity.kt:15) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

It looks like bitcoinj library contains the wrapper but doesn't contains native library which used in this wrapper. How to fix this issue?


Solution

  • Bitcoinj uses JNI to load a C library for secp256k1 curve operations. If the lib is imported correctly... and since you are trying to access it directly, you might be missing the System.loadLibrary('secp256k1') call

    Check here : https://github.com/bitcoinj/bitcoinj/blob/2ec193f8479425c3a66bebf5f2d3493e39e88f7c/core/src/main/java/org/bitcoin/Secp256k1Context.java

    To build the compiles sources for the lib, its describes in the comment block of the NativeSecp256k1 class, here : https://github.com/bitcoinj/bitcoinj/blob/2ec193f8479425c3a66bebf5f2d3493e39e88f7c/core/src/main/java/org/bitcoin/NativeSecp256k1.java#L34