use crate::auth::factors::AuthFactors;
use crate::error::Result;
use crate::id::{AccountId, ProfileIx};
use crate::wallet::summary::SpendSummary;
#[non_exhaustive]
pub struct UnlockRequest {
pub account: AccountId,
pub reason: Option<String>,
}
impl UnlockRequest {
pub fn new(account: AccountId) -> Self {
Self {
account,
reason: None,
}
}
}
#[non_exhaustive]
pub struct SpendConfirmRequest {
pub account: AccountId,
pub profile: ProfileIx,
pub summary: SpendSummary,
}
impl SpendConfirmRequest {
pub fn new(account: AccountId, profile: ProfileIx, summary: SpendSummary) -> Self {
Self {
account,
profile,
summary,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SpendDecision {
Approve,
Decline(Option<String>),
}
#[async_trait::async_trait]
pub trait AuthProvider: Send + Sync {
async fn collect_factors(&self, request: UnlockRequest) -> Result<AuthFactors>;
async fn confirm_spend(&self, request: SpendConfirmRequest) -> Result<SpendDecision>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_bare_unlock_request_carries_the_account_and_no_reason() {
let req = UnlockRequest::new(AccountId::new("acct"));
assert_eq!(req.account, AccountId::new("acct"));
assert!(req.reason.is_none());
}
#[test]
fn spend_confirm_request_holds_its_context() {
use crate::wallet::summary::{SpendDestination, SpendRecipient, SpendTier};
let summary = SpendSummary::new(
SpendTier::Confirm,
vec![SpendRecipient {
address: "xch1recipient".into(),
amount_mojos: 1_000_000_000_000,
asset_id: None,
destination: SpendDestination::Address,
}],
0,
);
let req = SpendConfirmRequest::new(AccountId::new("acct"), ProfileIx::ROOT, summary);
assert_eq!(req.profile, ProfileIx::ROOT);
assert_eq!(req.summary.recipients.len(), 1);
assert!(req.summary.to_string().contains("XCH"));
}
#[test]
fn spend_decision_distinguishes_approve_from_decline() {
assert_ne!(SpendDecision::Approve, SpendDecision::Decline(None));
assert_eq!(
SpendDecision::Decline(Some("too large".into())),
SpendDecision::Decline(Some("too large".into())),
);
}
}