use chia_protocol::CoinSpend;
use crate::auth::provider::{AuthProvider, SpendConfirmRequest, SpendDecision};
use crate::error::{AccountError, Result};
use crate::id::{AccountId, ProfileIx};
use crate::wallet::policy::CustodyScope;
use crate::wallet::summary::SpendSummary;
struct AuthorizedSpend {
coin_spends: Vec<CoinSpend>,
summary: SpendSummary,
scope: CustodyScope,
}
pub struct SpendApproval {
inner: AuthorizedSpend,
}
impl SpendApproval {
pub(crate) fn new(
coin_spends: Vec<CoinSpend>,
summary: SpendSummary,
scope: CustodyScope,
) -> Self {
Self {
inner: AuthorizedSpend {
coin_spends,
summary,
scope,
},
}
}
pub fn summary(&self) -> &SpendSummary {
&self.inner.summary
}
pub fn coin_spends(&self) -> &[CoinSpend] {
&self.inner.coin_spends
}
pub(crate) fn scope(&self) -> &CustodyScope {
&self.inner.scope
}
}
pub struct PendingApproval {
inner: AuthorizedSpend,
}
impl PendingApproval {
pub(crate) fn new(
coin_spends: Vec<CoinSpend>,
summary: SpendSummary,
scope: CustodyScope,
) -> Self {
Self {
inner: AuthorizedSpend {
coin_spends,
summary,
scope,
},
}
}
pub fn summary(&self) -> &SpendSummary {
&self.inner.summary
}
pub async fn confirm_with(
self,
provider: &dyn AuthProvider,
account: AccountId,
profile: ProfileIx,
) -> Result<SpendApproval> {
let request = SpendConfirmRequest::new(account, profile, self.inner.summary.clone());
let decision = provider.confirm_spend(request).await?;
self.decided(decision)
}
fn decided(self, decision: SpendDecision) -> Result<SpendApproval> {
match decision {
SpendDecision::Approve => Ok(SpendApproval { inner: self.inner }),
SpendDecision::Decline(reason) => Err(AccountError::UserDeclined(format!(
"the user declined this spend{}",
reason.map(|r| format!(": {r:?}")).unwrap_or_default()
))),
}
}
}
pub enum SpendRuling {
Approved(SpendApproval),
RequiresConfirmation(PendingApproval),
}
#[cfg(test)]
mod tests {
use super::*;
use crate::wallet::summary::{SpendDestination, SpendRecipient, SpendTier};
fn scope() -> CustodyScope {
CustodyScope::new(
ProfileIx::ROOT,
&crate::wallet::policy::CustodyPolicy::Hot(Default::default()),
chia_protocol::Bytes32::new([0u8; 32]),
)
}
fn summary() -> SpendSummary {
SpendSummary::new(
SpendTier::Confirm,
vec![SpendRecipient {
address: "xch1abc".into(),
amount_mojos: 42,
asset_id: None,
destination: SpendDestination::Address,
}],
7,
)
}
fn denial(result: Result<SpendApproval>) -> AccountError {
match result {
Ok(_) => panic!("expected a denial, got a signable approval"),
Err(e) => e,
}
}
fn coin_spend(amount: u64) -> CoinSpend {
use chia_protocol::{Bytes32, Coin, Program};
CoinSpend::new(
Coin::new(Bytes32::new([1u8; 32]), Bytes32::new([2u8; 32]), amount),
Program::from(vec![1u8]),
Program::from(vec![1u8]),
)
}
#[test]
fn an_approval_exposes_the_spends_and_the_summary_it_was_built_from() {
let approval = SpendApproval::new(vec![coin_spend(99)], summary(), scope());
assert_eq!(approval.coin_spends(), &[coin_spend(99)]);
assert_eq!(approval.summary().recipients[0].amount_mojos, 42);
assert_eq!(approval.summary().fee, 7);
}
#[test]
fn confirming_a_pending_approval_yields_an_approval_over_the_very_same_spends() {
let pending = PendingApproval::new(vec![coin_spend(1234)], summary(), scope());
assert_eq!(pending.summary().recipients[0].amount_mojos, 42);
let approval = pending.decided(SpendDecision::Approve).unwrap();
assert_eq!(approval.coin_spends(), &[coin_spend(1234)]);
assert_eq!(approval.summary(), &summary());
}
#[test]
fn a_declined_ceremony_denies_outright_and_never_yields_an_approval() {
let pending = PendingApproval::new(vec![coin_spend(1)], summary(), scope());
let err = denial(pending.decided(SpendDecision::Decline(Some("not mine".into()))));
assert!(
matches!(&err, AccountError::UserDeclined(m) if m.contains("declined") && m.contains("not mine")),
"{err:?}"
);
}
#[test]
fn a_reasonless_decline_is_still_denied() {
let pending = PendingApproval::new(vec![coin_spend(1)], summary(), scope());
let err = denial(pending.decided(SpendDecision::Decline(None)));
assert!(matches!(err, AccountError::UserDeclined(_)), "{err:?}");
}
}