zoe_client/frb_api/
mod.rs

1use 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
10// initialize for frb
11pub fn frb_init() {
12    // Initialize Rustls crypto provider before any TLS operations
13    rustls::crypto::ring::default_provider()
14        .install_default()
15        .expect("Failed to install crypto provider");
16}
17
18// Key conversion utilities - work with hex strings instead of raw types
19#[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)) // Return the validated hex
37}
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// Relay management functions
58#[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    // Parse server key
65    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// RelayAddress creation helper
81#[frb]
82pub fn create_relay_address_with_hostname(
83    server_key_hex: String,
84    hostname: String,
85) -> Result<RelayAddress, String> {
86    // Parse server key
87    let server_key =
88        VerifyingKey::from_hex(server_key_hex).map_err(|e| format!("Invalid server key: {}", e))?;
89    // Create RelayAddress
90    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}