//test.rs // // 测试在Rust侧生成钱包密钥对,转换成C侧的数据结构。 // 测试钱包在C侧调用接口存储和重新读出钱包密钥 // use std::ffi::CStr; use std::os::raw::c_char; //use rustylib::gen::{CWallet}; use rustwallet::{new_wallet, get_address, free_cwallet, CWallet}; fn main() { unsafe { let msg = "111"; let cstr = std::ffi::CString::new(msg).unwrap(); let wallet: CWallet = new_wallet(cstr.into_raw()); println!("---- generated a wallet to be used on C-side ----"); print_wallet(&wallet); let address = get_address(wallet); let address_str = cchar_str(address); println!("address: {}", address_str); let key0 = "b8256097c1ff2bdd483ffb53d35203a8a7ff19547d65d97f2810732a27e80c1f"; let key1 = "8e25c8b2e2c8504ea3d6d37cc268facec17ae60667048cfeb7a0092cfda8323a"; let key2 = "642630ce039174bfff6daba5b17ff1f4daf6b2b850a3407e472f9f2fd3685855"; // println!("---- saving the wallet to wallet.json ----"); // save_wallet(&wallet); // println!("---- saved! ----"); // println!("---- fetching the saved wallet to be exposed to C-side ----"); // let fetched = fetch_cwallet(); // print_wallet(&fetched); // sign(); // let sign_str = "111"; // let cstr = std::ffi::CString::new(sign_str).unwrap(); // sss_sign(cstr.into_raw()); // // free_cwallet(wallet); // 对应 generate_cwallet() // free_cwallet(fetched); // 对应 fetch_wallet() } } unsafe fn print_wallet(cwallet: &CWallet) { let msg = CStr::from_ptr(cwallet.msg_key); let pmsg = msg.to_str().unwrap(); println!("msg=> {}", pmsg); let m = CStr::from_ptr(cwallet.master_key); let pm = m.to_str().unwrap(); println!("master key=> {}", pm); let s = CStr::from_ptr(cwallet.second_key); let ps = s.to_str().unwrap(); println!("second key=> {}", ps); let b = CStr::from_ptr(cwallet.backup_key); let pb = b.to_str().unwrap(); println!("backup key=> {}", pb); } unsafe fn cchar_str(cstr: *mut c_char) -> &'static str { let msg = CStr::from_ptr(cstr); msg.to_str().unwrap() }