|
| 1 | +use crate::primitives::*; |
| 2 | +use async_trait::async_trait; |
| 3 | +use std::{marker::PhantomData, sync::Arc}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + client::{Locked, Unlockable, Unlocked}, |
| 7 | + Error, |
| 8 | +}; |
| 9 | + |
| 10 | +pub(crate) mod state { |
| 11 | + #[derive(Debug, Clone, Copy)] |
| 12 | + /// The `Locked` state of the [`crate::Adapter`]. |
| 13 | + /// See [`crate::client::Locked`] |
| 14 | + pub struct LockedState; |
| 15 | + |
| 16 | + /// The `Unlocked` state of the [`crate::Adapter`]. |
| 17 | + /// See [`crate::client::Unlocked`] |
| 18 | + #[derive(Debug, Clone, Copy)] |
| 19 | + pub struct UnlockedState; |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug)] |
| 23 | +/// The [`Adapter`] struct and it's states. |
| 24 | +/// |
| 25 | +/// Used for communication with the underlying client implementation. |
| 26 | +/// |
| 27 | +/// # Available adapters |
| 28 | +/// |
| 29 | +/// 2 Adapters are available in this crate: |
| 30 | +/// - Ethereum |
| 31 | +/// - [`crate::ethereum::LockedAdapter`] and [`crate::ethereum::UnlockedAdapter`] |
| 32 | +/// - Client implementation [`crate::Ethereum`] for chains compatible with EVM. |
| 33 | +/// - Dummy |
| 34 | +/// - [`crate::dummy::Adapter`] and it's client implementation [`crate::Dummy`] for testing. |
| 35 | +pub struct Adapter<C, S = state::LockedState> { |
| 36 | + /// client in a specific state - Locked or Unlocked |
| 37 | + pub client: Arc<C>, |
| 38 | + _state: PhantomData<S>, |
| 39 | +} |
| 40 | + |
| 41 | +impl<C, S: Clone> Clone for Adapter<C, S> { |
| 42 | + fn clone(&self) -> Self { |
| 43 | + Self { |
| 44 | + client: self.client.clone(), |
| 45 | + _state: self._state, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<C: Locked> Adapter<C> { |
| 51 | + /// Create a new [`Adapter`] in [`Locked`] state using a [`Locked`]. |
| 52 | + pub fn new(client: C) -> Adapter<C, state::LockedState> { |
| 53 | + Adapter { |
| 54 | + client: Arc::new(client), |
| 55 | + _state: PhantomData::default(), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<C: Unlocked> Adapter<C, state::LockedState> { |
| 61 | + /// Create a new [`Adapter`] in [`state::UnlockedState`] state using an [`Unlocked`] client. |
| 62 | + pub fn with_unlocked(client: C) -> Adapter<C, state::UnlockedState> { |
| 63 | + Adapter { |
| 64 | + client: Arc::new(client), |
| 65 | + _state: PhantomData::default(), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<C> Adapter<C, state::LockedState> |
| 71 | +where |
| 72 | + C: Locked + Unlockable, |
| 73 | + <C::Unlocked as Locked>::Error: Into<Error>, |
| 74 | + C::Error: Into<Error>, |
| 75 | +{ |
| 76 | + pub fn unlock(self) -> Result<Adapter<C::Unlocked, state::UnlockedState>, Error> { |
| 77 | + let unlocked = self.client.unlock().map_err(Into::into)?; |
| 78 | + |
| 79 | + Ok(Adapter { |
| 80 | + client: Arc::new(unlocked), |
| 81 | + _state: PhantomData::default(), |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[async_trait] |
| 87 | +impl<C> Unlocked for Adapter<C, state::UnlockedState> |
| 88 | +where |
| 89 | + C: Unlocked + Sync + Send, |
| 90 | + C::Error: Into<Error>, |
| 91 | +{ |
| 92 | + fn sign(&self, state_root: &str) -> Result<String, Error> { |
| 93 | + self.client.sign(state_root).map_err(Into::into) |
| 94 | + } |
| 95 | + |
| 96 | + fn get_auth(&self, intended_for: ValidatorId) -> Result<String, Error> { |
| 97 | + self.client.get_auth(intended_for).map_err(Into::into) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[async_trait] |
| 102 | +impl<C, S> Locked for Adapter<C, S> |
| 103 | +where |
| 104 | + C: Locked + Sync + Send, |
| 105 | + C::Error: Into<Error>, |
| 106 | + S: Sync + Send, |
| 107 | +{ |
| 108 | + type Error = Error; |
| 109 | + /// Get Adapter whoami |
| 110 | + fn whoami(&self) -> ValidatorId { |
| 111 | + self.client.whoami() |
| 112 | + } |
| 113 | + |
| 114 | + /// Verify, based on the signature & state_root, that the signer is the same |
| 115 | + fn verify( |
| 116 | + &self, |
| 117 | + signer: ValidatorId, |
| 118 | + state_root: &str, |
| 119 | + signature: &str, |
| 120 | + ) -> Result<bool, Error> { |
| 121 | + self.client |
| 122 | + .verify(signer, state_root, signature) |
| 123 | + .map_err(Into::into) |
| 124 | + } |
| 125 | + |
| 126 | + /// Creates a `Session` from a provided Token by calling the Contract. |
| 127 | + /// Does **not** cache the (`Token`, [`Session`]) pair. |
| 128 | + async fn session_from_token(&self, token: &str) -> Result<Session, Error> { |
| 129 | + self.client |
| 130 | + .session_from_token(token) |
| 131 | + .await |
| 132 | + .map_err(Into::into) |
| 133 | + } |
| 134 | + |
| 135 | + async fn get_deposit( |
| 136 | + &self, |
| 137 | + channel: &Channel, |
| 138 | + depositor_address: Address, |
| 139 | + ) -> Result<Deposit, Error> { |
| 140 | + self.client |
| 141 | + .get_deposit(channel, depositor_address) |
| 142 | + .await |
| 143 | + .map_err(Into::into) |
| 144 | + } |
| 145 | +} |
0 commit comments