Skip to content

Commit

Permalink
Add user entropy support
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapk00 committed Jun 12, 2019
1 parent ccb5774 commit 4f6afb3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
46 changes: 18 additions & 28 deletions qtlib/src/lib.rs
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);
// }
2 changes: 1 addition & 1 deletion qtlib/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using namespace std;

int main() {
char * from_rust = rust_generate_wallet(true, 1);
char * from_rust = rust_generate_wallet(true, 1, "user-provided-entropy");
auto stri = string(from_rust);
cout << stri << endl;
rust_free_string(from_rust);
Expand Down
4 changes: 2 additions & 2 deletions qtlib/src/zecpaperrust.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
extern "C"{
#endif

extern char * rust_generate_wallet(bool testnet, unsigned int count);
extern void rust_free_string(char * s);
extern char * rust_generate_wallet(bool testnet, unsigned int count, const char* entropy);
extern void rust_free_string(char* s);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 4f6afb3

Please sign in to comment.