1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use crate::state::State;
use crate::{Initiator, Responder, XXVault};
use ockam_core::{async_trait, compat::boxed::Box};
use ockam_core::{AsyncTryClone, Result};
use ockam_key_exchange_core::NewKeyExchanger;

/// Represents an XX NewKeyExchanger
pub struct XXNewKeyExchanger<V: XXVault> {
    vault: V,
}

impl<V: XXVault> XXNewKeyExchanger<V> {
    /// Create a new XXNewKeyExchanger
    pub fn new(vault: V) -> Self {
        Self { vault }
    }
}

#[async_trait]
impl<V: XXVault> AsyncTryClone for XXNewKeyExchanger<V> {
    async fn async_try_clone(&self) -> Result<Self> {
        Ok(Self {
            vault: self.vault.async_try_clone().await?,
        })
    }
}

#[async_trait]
impl<V: XXVault> NewKeyExchanger for XXNewKeyExchanger<V> {
    type Initiator = Initiator<V>;
    type Responder = Responder<V>;
    /// Create a new initiator using the provided backing vault
    async fn initiator(&self) -> Result<Initiator<V>> {
        let ss = State::new(&self.vault).await?;
        Ok(Initiator::new(ss))
    }

    /// Create a new responder using the provided backing vault
    async fn responder(&self) -> Result<Responder<V>> {
        let ss = State::new(&self.vault).await?;
        Ok(Responder::new(ss))
    }
}