use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use crate::wab_client::types::LinkedMethod;
use crate::wallet::privileged::PrivilegedKeyManager;
use crate::WalletError;
use bsv::wallet::interfaces::WalletInterface;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AuthState {
Unauthenticated,
Authenticating,
Authenticated,
Failed(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StateSnapshot {
pub presentation_key: Option<String>,
pub auth_state: AuthState,
pub profile: Option<Profile>,
pub is_new_user: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Profile {
pub user_id: String,
pub identity_key: String,
pub presentation_key: String,
pub linked_methods: Vec<LinkedMethod>,
}
pub type WalletBuilderFn = Box<
dyn Fn(
Vec<u8>,
Arc<dyn PrivilegedKeyManager>,
) -> Pin<
Box<
dyn Future<Output = Result<Arc<dyn WalletInterface + Send + Sync>, WalletError>>
+ Send,
>,
> + Send
+ Sync,
>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_state_serialization() {
let states = vec![
AuthState::Unauthenticated,
AuthState::Authenticating,
AuthState::Authenticated,
AuthState::Failed("test error".to_string()),
];
for state in &states {
let json = serde_json::to_string(state).expect("serialize AuthState");
let back: AuthState = serde_json::from_str(&json).expect("deserialize AuthState");
assert_eq!(&back, state, "AuthState round-trip failed for {:?}", state);
}
}
#[test]
fn test_state_snapshot_serialization() {
let snapshot = StateSnapshot {
presentation_key: Some("abcd1234".to_string()),
auth_state: AuthState::Authenticated,
profile: Some(Profile {
user_id: "user-1".to_string(),
identity_key: "deadbeef".to_string(),
presentation_key: "abcd1234".to_string(),
linked_methods: vec![],
}),
is_new_user: Some(false),
};
let json = serde_json::to_vec(&snapshot).expect("serialize StateSnapshot");
let back: StateSnapshot = serde_json::from_slice(&json).expect("deserialize StateSnapshot");
assert_eq!(back.auth_state, AuthState::Authenticated);
assert_eq!(back.presentation_key.as_deref(), Some("abcd1234"));
assert_eq!(back.is_new_user, Some(false));
assert!(back.profile.is_some());
let prof = back.profile.unwrap();
assert_eq!(prof.user_id, "user-1");
assert_eq!(prof.identity_key, "deadbeef");
}
}