Commit e92ed397 authored by Marek Kotewicz's avatar Marek Kotewicz
Browse files

jni properly linked

parent 5d21c6d7
......@@ -10,6 +10,10 @@ import com.facebook.react.bridge.Promise;
*/
public class EthkeyBridge extends ReactContextBaseJavaModule {
static {
System.loadLibrary("signer");
}
@Override
public String getName() {
return "EthkeyBridge";
......@@ -21,14 +25,32 @@ public class EthkeyBridge extends ReactContextBaseJavaModule {
@ReactMethod
public void brainWalletAddress(String seed, Promise promise) {
int a = hello();
promise.resolve("hello from java " + a + " sa");
promise.resolve(ethkeyBrainwalletAddress(seed));
}
@ReactMethod
public void brainWalletSecret(String seed, Promise promise) {
promise.resolve("hello from java");
promise.resolve(ethkeyBrainwalletSecret(seed));
}
@ReactMethod
public void brainWalletSign(String seed, String message, Promise promise) {
promise.resolve(ethkeyBrainwalletSign(seed, message));
}
@ReactMethod
public void rlpItem(String rlp, int position, Promise promise) {
promise.resolve(ethkeyRlpItem(rlp, position));
}
@ReactMethod
public void keccak(String data, Promise promise) {
promise.resolve(ethkeyKeccak(data));
}
private static native int hello();
private static native String ethkeyBrainwalletAddress(String seed);
private static native String ethkeyBrainwalletSecret(String seed);
private static native String ethkeyBrainwalletSign(String seed, String message);
private static native String ethkeyRlpItem(String data, int position);
private static native String ethkeyKeccak(String data);
}
......@@ -42,7 +42,6 @@ public class MainApplication extends Application implements ReactApplication {
@Override
public void onCreate() {
super.onCreate();
System.loadLibrary("signer");
SoLoader.init(this, /* native exopackage */ false);
}
}
......@@ -10,7 +10,7 @@ ethkey = { git = "https://github.com/ethcore/parity" }
rlp = { git = "https://github.com/ethcore/parity" }
tiny-keccak = "1.1"
#[target.'cfg(target_os="android")'.dependencies]
[target.'cfg(target_os="android")'.dependencies]
jni = { git = "https://github.com/prevoty/jni-rs", default-features = false }
[lib]
......
......@@ -7,6 +7,7 @@ all: ios android
ios: $(LIB)
android: $(ARCHS_ANDROID)
sh copy_android.sh
.PHONY: $(ARCHS_IOS)
$(ARCHS_IOS): %:
......
......@@ -94,24 +94,65 @@ pub unsafe extern fn keccak256(data: *mut StringPtr) -> *mut String {
Box::into_raw(Box::new(res.to_hex()))
}
extern crate jni;
use self::jni::sys::jint;
#[no_mangle]
#[allow(non_snake_case)]
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_hello() -> jint {
4
}
#[cfg(target_os = "android")]
#[allow(non_snake_case)]
pub mod android {
extern crate jni;
use self::jni::sys::jint;
use super::*;
use self::jni::JNIEnv;
use self::jni::objects::{JClass, JString};
use self::jni::sys::{jint, jstring};
#[no_mangle]
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_ethkeyBrainwalletAddress(env: JNIEnv, _: JClass, seed: JString) -> jstring {
let seed: String = env.get_string(seed).expect("Invalid seed").into();
let keypair = Brain::new(seed).generate().unwrap();
let java_address = env.new_string(format!("{:?}", keypair.address())).expect("Could not create java string");
java_address.into_inner()
}
#[no_mangle]
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_ethkeyBrainwalletSecret(env: JNIEnv, _: JClass, seed: JString) -> jstring {
let seed: String = env.get_string(seed).expect("Invalid seed").into();
let keypair = Brain::new(seed).generate().unwrap();
let java_secret = env.new_string(format!("{:?}", keypair.secret())).expect("Could not create java string");
java_secret.into_inner()
}
#[no_mangle]
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_ethkeyBrainwalletSign(env: JNIEnv, _: JClass, seed: JString, message: JString) -> jstring {
let seed: String = env.get_string(seed).expect("Invalid seed").into();
let message: String = env.get_string(message).expect("Invalid message").into();
let keypair = Brain::new(seed).generate().unwrap();
let message: Message = message.parse().unwrap();
let signature = sign(keypair.secret(), &message).unwrap();
let java_signature = env.new_string(format!("{}", signature)).expect("Could not create java string");
java_signature.into_inner()
}
#[no_mangle]
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_ethkeyRlpItem(env: JNIEnv, _: JClass, data: JString, position: jint) -> jstring {
let data: String = env.get_string(data).expect("Invalid seed").into();
match safe_rlp_item(&data, position as u32) {
Ok(result) => env.new_string(result).expect("Could not create java string").into_inner(),
Err(_) => {
let res = env.new_string("").expect("").into_inner();
env.throw(res.into());
res
},
}
}
#[no_mangle]
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_helloFromRust() -> jint {
5
pub unsafe extern fn Java_com_nativesigner_EthkeyBridge_ethkeyKeccak(env: JNIEnv, _: JClass, data: JString) -> jstring {
let data: String = env.get_string(data).expect("Invalid seed").into();
let hex = data.from_hex().unwrap();
let mut res: [u8; 32] = [0; 32];
let mut keccak = Keccak::new_keccak256();
keccak.update(&hex);
keccak.finalize(&mut res);
env.new_string(res.to_hex()).expect("Could not create java string").into_inner()
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment