Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/java/org/bitcoin/Secp256k1Context.java
Show All 16 Lines | |||||
package org.bitcoin; | package org.bitcoin; | ||||
/** | /** | ||||
* This class holds the context reference used in native methods | * This class holds the context reference used in native methods | ||||
* to handle ECDSA operations. | * to handle ECDSA operations. | ||||
*/ | */ | ||||
public class Secp256k1Context { | public class Secp256k1Context { | ||||
private static final boolean enabled; //true if the library is loaded | private static final boolean enabled; //true if the library is loaded | ||||
private static final long context; //ref to pointer to context obj | |||||
static { //static initializer | static { //static initializer | ||||
boolean isEnabled = true; | boolean isEnabled = true; | ||||
long contextRef = -1; | |||||
try { | try { | ||||
System.loadLibrary("secp256k1_jni"); | System.loadLibrary("secp256k1_jni"); | ||||
contextRef = secp256k1_init_context(); | |||||
} catch (UnsatisfiedLinkError e) { | } catch (UnsatisfiedLinkError e) { | ||||
System.out.println("UnsatisfiedLinkError: " + e.toString()); | System.out.println("UnsatisfiedLinkError: " + e.toString()); | ||||
isEnabled = false; | isEnabled = false; | ||||
} | } | ||||
enabled = isEnabled; | enabled = isEnabled; | ||||
context = contextRef; | |||||
} | } | ||||
public static boolean isEnabled() { | public static boolean isEnabled() { | ||||
return enabled; | return enabled; | ||||
} | } | ||||
public static long getContext() { | |||||
if(!enabled) return -1; //sanity check | |||||
return context; | |||||
} | |||||
private static native long secp256k1_init_context(); | |||||
} | } |