hopr_api/chain/
mod.rs

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