diff --git a/Cargo.toml b/Cargo.toml index f60a5d1..0e1f03d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ecrecover" +name = "nifecrecover" version = "0.1.0" authors = ["John Newby "] edition = "2018" @@ -16,4 +16,4 @@ parity-bytes = "0.1.0" rustler = "0.20.0" [lib] -crate-type = ["cdylib"] \ No newline at end of file +crate-type = ["cdylib"] diff --git a/src/lib.rs b/src/lib.rs index 8460313..3954556 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,18 +7,10 @@ extern crate parity_bytes; #[macro_use] extern crate rustler; -use std::ffi::*; use ethcore_builtin::EcRecover; use crate::ethcore_builtin::Implementation; -use c_vec::{CVec}; use parity_bytes::BytesRef; use rustler::*; -use rustler::types::atom::ok; -use std::io::Write; -use std::ptr::copy_nonoverlapping; - -const INPUT_LENGTH: usize = 512; -const OUTPUT_LENGTH: usize = 32; mod atoms { rustler_atoms! { @@ -36,49 +28,18 @@ rustler_export_nifs!( #[no_mangle] -fn on_load(env: Env, _load_info: Term) -> bool { +fn on_load(_env: Env, _load_info: Term) -> bool { true } -pub fn nif_ecrecover<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult> { +pub fn nif_ecrecover<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result, Error> { let input: Binary = args[0].decode()?; let mut byte_ref = Vec::new(); let ecrecover = EcRecover { }; - let result = match ecrecover.execute(input.as_slice(), - &mut BytesRef::Flexible(&mut byte_ref)) { + let _result = match ecrecover.execute(input.as_slice(), + &mut BytesRef::Flexible(&mut byte_ref)) { Ok(_) => (), - Err(e) => return Err(rustler::Error::Atom("ecrecover failed")), + Err(_e) => return Err(rustler::Error::Atom("ecrecover failed")), }; - Ok(byte_ref.as_slice().encode(env)) -} - -/** - * C interface to the ethereum ecrecover implementation. Returns 1 on success, - * 0 on failure (in which case there are no guarantees as to what is in output. - * - * Memory allocated by caller, expected to be an array of bytes, as above-- 512 in, - * 32 out. - */ -#[no_mangle] -pub unsafe extern "C" fn ecrecover(input: *const libc::c_uchar, output: *mut libc::c_uchar) -> i16 { - let ecrecover = EcRecover { }; - let mut byte_ref = Vec::new(); - ecrecover.execute(unsafe { std::slice::from_raw_parts(input as *const u8, INPUT_LENGTH) }, - &mut BytesRef::Flexible(&mut byte_ref)); - let mut ptr: &mut[u8] = unsafe { std::slice::from_raw_parts_mut(output as *mut u8, OUTPUT_LENGTH) }; - if byte_ref.len() != OUTPUT_LENGTH { - return 0; - } - ptr.copy_from_slice(byte_ref.as_slice()); - return 1; -} - - - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } + Ok((atoms::ok(), byte_ref.as_slice()).encode(env)) } diff --git a/src/nifecrecover.erl b/src/nifecrecover.erl index 5b3765d..cedf6ea 100644 --- a/src/nifecrecover.erl +++ b/src/nifecrecover.erl @@ -23,7 +23,17 @@ ecrecover_hex(Input) -> load() -> - ok = erlang:load_nif("/home/newby/projects/ethereum/ecrecover/target/release/libecrecover", 0). + erlang:display(file:get_cwd()), + Dir = case code:priv_dir(nifecrecover) of + {error, bad_name} -> + filename:join( + filename:dirname( + filename:dirname( + code:which(?MODULE))), "priv"); + D -> D + end, + SoName = filename:join(Dir, atom_to_list(?MODULE)), + ok = erlang:load_nif(SoName, 0). not_loaded(Line) -> erlang:nif_error({error, {not_loaded, [{module, ?MODULE}, {line, Line}]}}).