2022-10-23 08:58:24 +08:00

78 lines
2.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//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, restore_wallet, free_cwallet, CWallet};
macro_rules! print_cchar{
($p1:expr, $p2:expr) => (
let s = CStr::from_ptr($p2);
let ps = s.to_str().unwrap();
println!(">>>>>>>>>>>>>>macro_rules print_cchar");
println!("{:?} {:?}",
$p1,
ps)
)
}
macro_rules! cchar_to_str{
($p1:expr) => {
{
let s = CStr::from_ptr($p1);
let ps = s.to_str().unwrap();
ps
}
}
}
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_to_str!(address);
println!("address: {}", address_str);
let key0 = "3da2dfc54de71230f639f37de61d8f8c4699f75e783dc54353146a4b73250366";
let key1 = "a1a3ed90e41a37096f07957b9888ffa7a74406f3a3e8c77686d92f2ba7c22d58";
let key2 = "27216cbe70021d5d1d018da5ab548b8b45dee4fd9ec0b33ab1dd264cf2ff249f";
// 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 pmsg = cchar_to_str!(cwallet.msg_key);
println!("msg=> {}", pmsg);
print_cchar!("msg=> ", cwallet.msg_key);
let pm = cchar_to_str!(cwallet.master_key);
println!("master key=> {}", pm);
let ps = cchar_to_str!(cwallet.second_key);
println!("second key=> {}", ps);
let pb = cchar_to_str!(cwallet.backup_key);
println!("backup key=> {}", pb);
}