Browse Source

Add user entropy support

taddr
Aditya Kulkarni 5 years ago
parent
commit
4f6afb34b1
  1. 46
      qtlib/src/lib.rs
  2. 2
      qtlib/src/main.cpp
  3. 4
      qtlib/src/zecpaperrust.h

46
qtlib/src/lib.rs

@ -1,14 +1,28 @@
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 {
@ -16,27 +30,3 @@ pub extern fn rust_free_string(s: *mut c_char) {
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
qtlib/src/main.cpp

@ -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);

4
qtlib/src/zecpaperrust.h

@ -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
}

Loading…
Cancel
Save