2022-09-26 11:21:33 +00:00

46 lines
1.3 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 rustylib::gen::{CWallet};
use rustwallet::{
fetch_cwallet, free_cwallet, generate_cwallet, save_wallet, sign, CWallet,
};
fn main() {
unsafe {
let wallet: CWallet = generate_cwallet();
println!("---- generated a wallet to be used on C-side ----");
print_wallet(&wallet);
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();
free_cwallet(wallet); // 对应 generate_cwallet()
free_cwallet(fetched); // 对应 fetch_wallet()
}
}
unsafe fn print_wallet(wallet: &CWallet) {
let a = CStr::from_ptr(wallet.public_addr);
let pa = a.to_str().unwrap();
println!("public address=> {}", pa);
let pk = CStr::from_ptr(wallet.public_key);
let ppk = pk.to_str().unwrap();
println!("public key=> {}", ppk);
let sk = CStr::from_ptr(wallet.private_key);
let psk = sk.to_str().unwrap();
println!("private key=> {}", psk);
}