-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccb5774
commit 4f6afb3
Showing
3 changed files
with
21 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,32 @@ | ||
use libc::{c_char}; | ||
use std::ffi::CString; | ||
use std::ffi::{CStr, CString}; | ||
use zecpaperlib::paper; | ||
|
||
/** | ||
* Call into rust to generate a paper wallet. Returns the paper wallet in JSON form. | ||
* NOTE: the returned string is owned by rust, so the caller needs to call rust_free_string with it | ||
* after using it to free it properly | ||
*/ | ||
#[no_mangle] | ||
pub extern fn rust_generate_wallet(testnet: bool, count: u32) -> *mut c_char { | ||
let c_str = CString::new(paper::generate_wallet(testnet, false, count, &[])).unwrap(); | ||
pub extern fn rust_generate_wallet(testnet: bool, count: u32, entropy: *const c_char) -> *mut c_char { | ||
let entropy_str = unsafe { | ||
assert!(!entropy.is_null()); | ||
|
||
CStr::from_ptr(entropy) | ||
}; | ||
|
||
let c_str = CString::new(paper::generate_wallet(testnet, false, count, entropy_str.to_bytes())).unwrap(); | ||
return c_str.into_raw(); | ||
} | ||
|
||
|
||
/** | ||
* Callers that recieve string return values from other functions should call this to return the string | ||
* back to rust, so it can be freed. Failure to call this function will result in a memory leak | ||
*/ | ||
#[no_mangle] | ||
pub extern fn rust_free_string(s: *mut c_char) { | ||
unsafe { | ||
if s.is_null() { return } | ||
CString::from_raw(s) | ||
}; | ||
} | ||
|
||
// #[no_mangle] | ||
// pub extern fn double_input(input: i32) -> i32 { | ||
// input * 2 | ||
// } | ||
|
||
// #[no_mangle] | ||
// pub extern fn say_hello() -> *mut c_char { | ||
// let mut hello = String::from("Hello World"); | ||
// hello.push_str(", ZecWallet!"); | ||
|
||
// let c_str_song = CString::new(hello).unwrap(); | ||
// c_str_song.into_raw() | ||
// } | ||
|
||
// #[no_mangle] | ||
// pub extern fn free_str(s: *mut c_char) { | ||
// let s = unsafe { | ||
// if s.is_null() { return } | ||
// CString::from_raw(s) | ||
// }; | ||
|
||
// println!("Freeing {:?}", s); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters