rustwallet/test/test.rs
cebgcontract 1ae7144573 add test
2022-09-26 11:38:47 +08:00

48 lines
1.5 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 rustywallet::{
fetch_cwallet, free_cwallet, generate_cwallet, generate_qrcode_svg, save_wallet, 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);
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 qrcode_pa = generate_qrcode_svg(wallet.public_addr);
let c_qrcode_pa = CStr::from_ptr(qrcode_pa);
println!("QR Code:\n {}", c_qrcode_pa.to_str().unwrap());
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);
}