Skip to main content

cdk_spilman/
established_channel.rs

1//! Established Spilman Channel
2//!
3//! Contains the complete channel state after funding
4
5use cashu::nuts::Proof;
6
7use super::deterministic::MintConnection;
8use super::params::ChannelParameters;
9
10/// An established Spilman payment channel
11/// Contains all channel components after funding transaction is complete
12#[derive(Debug, Clone)]
13pub struct EstablishedChannel {
14    /// Channel parameters (includes shared_secret)
15    pub params: ChannelParameters,
16    /// Locked proofs (2-of-2 multisig with expiry-based refund)
17    pub funding_proofs: Vec<Proof>,
18}
19
20impl EstablishedChannel {
21    /// Create new established channel
22    pub fn new(
23        params: ChannelParameters,
24        funding_proofs: Vec<Proof>,
25    ) -> Result<Self, anyhow::Error> {
26        // Note: This performs basic structural validation only.
27        // DLEQ proof verification (which ensures the mint actually signed these proofs)
28        // is done separately via `verify_valid_channel()` and should be called by the
29        // receiver (Charlie) when first receiving funding. The SpilmanBridge does this
30        // automatically in its `resolve_funding` step.
31
32        // Assert all proofs have the expected keyset_id from params
33        let expected_keyset_id = params.keyset_info.keyset_id;
34        for proof in &funding_proofs {
35            if proof.keyset_id != expected_keyset_id {
36                anyhow::bail!(
37                    "Funding proof has keyset_id {} but expected {} from params",
38                    proof.keyset_id,
39                    expected_keyset_id
40                );
41            }
42        }
43
44        // Assert the total value of funding proofs matches the expected funding token amount
45        let actual_funding_value: u64 = funding_proofs
46            .iter()
47            .map(|proof| u64::from(proof.amount))
48            .sum();
49        let expected_funding_value = params.get_total_funding_token_amount()?;
50
51        if actual_funding_value != expected_funding_value {
52            anyhow::bail!(
53                "Funding proofs total value {} does not match expected funding token amount {}",
54                actual_funding_value,
55                expected_funding_value
56            );
57        }
58
59        Ok(Self {
60            params,
61            funding_proofs,
62        })
63    }
64
65    /// Get the Y value for checking the funding token state
66    ///
67    /// Since all funding proofs are spent together (they're all inputs to the commitment transaction),
68    /// checking any one of them is sufficient to determine if the funding token has been spent.
69    /// This returns the Y value of the first funding proof for use with NUT-07 state checks.
70    fn get_one_funding_token_y_for_state_check(
71        &self,
72    ) -> Result<cashu::nuts::PublicKey, anyhow::Error> {
73        let proof = self
74            .funding_proofs
75            .first()
76            .ok_or_else(|| anyhow::anyhow!("No funding proofs available"))?;
77        Ok(proof.y()?)
78    }
79
80    /// Check the state of the funding token using NUT-07
81    ///
82    /// Since all funding proofs are spent together (they're all inputs to the commitment transaction),
83    /// checking any one of them is sufficient to determine if the funding token has been spent.
84    /// This method checks the first funding proof and returns its state.
85    ///
86    /// Returns the state (UNSPENT, PENDING, or SPENT) of the funding token.
87    pub async fn check_funding_token_state<M>(
88        &self,
89        mint_connection: &M,
90    ) -> Result<cashu::nuts::ProofState, anyhow::Error>
91    where
92        M: MintConnection + ?Sized,
93    {
94        let y = self.get_one_funding_token_y_for_state_check()?;
95        let response = mint_connection.check_state(vec![y]).await?;
96        response
97            .states
98            .into_iter()
99            .next()
100            .ok_or_else(|| anyhow::anyhow!("No state returned for funding token"))
101    }
102}