hopr_api/chain/
mod.rs

1mod accounts;
2mod channels;
3mod events;
4mod keys;
5mod tickets;
6mod values;
7
8pub use accounts::*;
9pub use channels::*;
10pub use events::*;
11pub use keys::*;
12pub use tickets::*;
13pub use values::*;
14
15/// Receipt of an on-chain operation.
16pub type ChainReceipt = hopr_crypto_types::prelude::Hash;
17
18/// Complete set of HOPR on-chain operation APIs.
19///
20/// This trait is automatically implemented for types
21/// that implement all the individual chain API traits to be implemented with the same error.
22pub trait HoprChainApi:
23    ChainReadAccountOperations<Error = Self::ChainError>
24    + ChainWriteAccountOperations<Error = Self::ChainError>
25    + ChainReadChannelOperations<Error = Self::ChainError>
26    + ChainWriteChannelOperations<Error = Self::ChainError>
27    + ChainEvents<Error = Self::ChainError>
28    + ChainKeyOperations<Error = Self::ChainError>
29    + ChainValues<Error = Self::ChainError>
30    + ChainWriteTicketOperations<Error = Self::ChainError>
31{
32    type ChainError: std::error::Error + Send + Sync + 'static;
33}
34
35impl<T, E> HoprChainApi for T
36where
37    T: ChainReadAccountOperations<Error = E>
38        + ChainWriteAccountOperations<Error = E>
39        + ChainReadChannelOperations<Error = E>
40        + ChainWriteChannelOperations<Error = E>
41        + ChainEvents<Error = E>
42        + ChainKeyOperations<Error = E>
43        + ChainValues<Error = E>
44        + ChainWriteTicketOperations<Error = E>,
45    E: std::error::Error + Send + Sync + 'static,
46{
47    type ChainError = E;
48}
49
50/// [`PathAddressResolver`] which uses the [HOPR chain API](self) to resolve addresses and channels.
51///
52/// This type implements a `From` trait for all types that implement both
53/// [`ChainKeyOperations`] and [`ChainReadChannelOperations`].
54pub struct ChainPathResolver<'a, R>(&'a R);
55
56impl<R> Clone for ChainPathResolver<'_, R> {
57    fn clone(&self) -> Self {
58        *self
59    }
60}
61
62impl<R> Copy for ChainPathResolver<'_, R> {}
63
64impl<'a, R: ChainKeyOperations + ChainReadChannelOperations> From<&'a R> for ChainPathResolver<'a, R> {
65    fn from(value: &'a R) -> Self {
66        Self(value)
67    }
68}
69
70#[async_trait::async_trait]
71impl<'c, R: ChainKeyOperations + ChainReadChannelOperations + Sync> hopr_internal_types::path::PathAddressResolver
72    for ChainPathResolver<'c, R>
73{
74    async fn resolve_transport_address(
75        &self,
76        address: &hopr_primitive_types::prelude::Address,
77    ) -> Result<Option<hopr_crypto_types::prelude::OffchainPublicKey>, hopr_internal_types::errors::PathError> {
78        self.0
79            .chain_key_to_packet_key(address)
80            .await
81            .map_err(|e| hopr_internal_types::errors::PathError::UnknownPeer(format!("{address}: {e}")))
82    }
83
84    async fn resolve_chain_address(
85        &self,
86        key: &hopr_crypto_types::prelude::OffchainPublicKey,
87    ) -> Result<Option<hopr_primitive_types::prelude::Address>, hopr_internal_types::errors::PathError> {
88        self.0
89            .packet_key_to_chain_key(key)
90            .await
91            .map_err(|e| hopr_internal_types::errors::PathError::UnknownPeer(format!("{key}: {e}")))
92    }
93
94    async fn get_channel(
95        &self,
96        src: &hopr_primitive_types::prelude::Address,
97        dst: &hopr_primitive_types::prelude::Address,
98    ) -> Result<Option<ChannelEntry>, hopr_internal_types::errors::PathError> {
99        self.0
100            .channel_by_parties(src, dst)
101            .await
102            .map_err(|e| hopr_internal_types::errors::PathError::MissingChannel(src.to_string(), format!("{dst}: {e}")))
103    }
104}