zoe_client/frb_api/
mod.rs1use crate::Client;
2use flutter_rust_bridge::frb;
3use zoe_app_primitives::RelayAddress;
4use zoe_wire_protocol::VerifyingKey;
5
6mod for_std;
7#[allow(unused_imports)]
8pub use for_std::*;
9
10pub fn frb_init() {
12 rustls::crypto::ring::default_provider()
14 .install_default()
15 .expect("Failed to install crypto provider");
16}
17
18#[frb]
20pub fn create_signing_key_random() -> String {
21 use ed25519_dalek::SigningKey;
22 use rand::rngs::OsRng;
23 let key = SigningKey::generate(&mut OsRng);
24 hex::encode(key.to_bytes())
25}
26
27#[frb]
28pub fn signing_key_from_hex(hex: String) -> Result<String, String> {
29 use ed25519_dalek::SigningKey;
30 let bytes = hex::decode(hex).map_err(|e| format!("Invalid hex: {}", e))?;
31 if bytes.len() != 32 {
32 return Err("SigningKey must be exactly 32 bytes".to_string());
33 }
34 let array: [u8; 32] = bytes.try_into().map_err(|_| "Invalid byte array")?;
35 let _key = SigningKey::from_bytes(&array);
36 Ok(hex::encode(array)) }
38
39#[frb]
40pub fn signing_key_to_verifying_key(signing_key_hex: String) -> Result<String, String> {
41 use ed25519_dalek::SigningKey;
42 let bytes = hex::decode(signing_key_hex).map_err(|e| format!("Invalid hex: {}", e))?;
43 if bytes.len() != 32 {
44 return Err("SigningKey must be exactly 32 bytes".to_string());
45 }
46 let array: [u8; 32] = bytes.try_into().map_err(|_| "Invalid byte array")?;
47 let signing_key = SigningKey::from_bytes(&array);
48 let verifying_key = signing_key.verifying_key();
49 Ok(hex::encode(verifying_key.to_bytes()))
50}
51
52#[frb]
53pub fn verifying_key_from_hex(hex: String) -> Result<VerifyingKey, String> {
54 VerifyingKey::from_hex(hex)
55}
56
57#[frb]
59pub async fn prepare_client_for_systems_test(
60 client: &Client,
61 server_address: String,
62 server_key_hex: String,
63) -> Result<bool, String> {
64 let server_key =
66 VerifyingKey::from_hex(server_key_hex).map_err(|e| format!("Invalid server key: {}", e))?;
67
68 let relay_address = RelayAddress::new(server_key).with_address_str(server_address);
69
70 client.close().await;
71
72 client
73 .add_relay(relay_address)
74 .await
75 .map_err(|e| format!("Failed to add relay: {}", e))?;
76
77 Ok(true)
78}
79
80#[frb]
82pub fn create_relay_address_with_hostname(
83 server_key_hex: String,
84 hostname: String,
85) -> Result<RelayAddress, String> {
86 let server_key =
88 VerifyingKey::from_hex(server_key_hex).map_err(|e| format!("Invalid server key: {}", e))?;
89 let relay_address = RelayAddress::new(server_key)
91 .with_address_str(hostname)
92 .with_name("Default Server".to_string());
93
94 Ok(relay_address)
95}