commonware_stream/
lib.rs

1//! Exchange messages over arbitrary transport.
2
3#![doc(
4    html_logo_url = "https://commonware.xyz/imgs/rustdoc_logo.svg",
5    html_favicon_url = "https://commonware.xyz/favicon.ico"
6)]
7
8pub mod public_key;
9pub mod utils;
10
11use bytes::Bytes;
12use commonware_codec::Error as CodecError;
13use commonware_runtime::Error as RuntimeError;
14use std::future::Future;
15use thiserror::Error;
16
17/// Errors that can occur when interacting with a stream.
18#[derive(Error, Debug)]
19pub enum Error {
20    // Handshake errors
21    #[error("handshake timeout")]
22    HandshakeTimeout,
23
24    // Hello errors
25    #[error("hello not for us")]
26    HelloNotForUs,
27    #[error("hello uses our public key")]
28    HelloUsesOurKey,
29    #[error("invalid signature")]
30    InvalidSignature,
31    #[error("timestamp too old: {0}")]
32    InvalidTimestampOld(u64),
33    #[error("timestamp too future: {0}")]
34    InvalidTimestampFuture(u64),
35    #[error("info continuation tag was invalid")]
36    InvalidInfoContinuationTag,
37
38    // Confirmation errors
39    #[error("shared secret was not contributory")]
40    SharedSecretNotContributory,
41    #[error("cipher creation failed")]
42    CipherCreation,
43    #[error("HKDF expansion failed")]
44    HKDFExpansion,
45    #[error("key confirmation failed")]
46    ConfirmationFailed,
47    #[error("invalid key confirmation")]
48    InvalidConfirmation,
49
50    // Connection errors
51    #[error("cannot dial self")]
52    DialSelf,
53    #[error("wrong peer")]
54    WrongPeer,
55    #[error("recv failed")]
56    RecvFailed(RuntimeError),
57    #[error("recv too large: {0} bytes")]
58    RecvTooLarge(usize),
59    #[error("send failed")]
60    SendFailed(RuntimeError),
61    #[error("send zero size")]
62    SendZeroSize,
63    #[error("send too large: {0} bytes")]
64    SendTooLarge(usize),
65    #[error("connection closed")]
66    StreamClosed,
67
68    // Encryption errors
69    #[error("nonce overflow")]
70    NonceOverflow,
71    #[error("encryption failed")]
72    EncryptionFailed,
73    #[error("decryption failed")]
74    DecryptionFailed,
75
76    // Codec errors
77    #[error("unable to decode: {0}")]
78    UnableToDecode(CodecError),
79}
80
81/// A trait for sending messages.
82pub trait Sender: Sync + Send + 'static {
83    /// Send a message to the stream.
84    fn send(&mut self, msg: &[u8]) -> impl Future<Output = Result<(), Error>> + Send;
85}
86
87/// A trait for receiving messages.
88pub trait Receiver: Sync + Send + 'static {
89    /// Receive a message from the stream.
90    fn receive(&mut self) -> impl Future<Output = Result<Bytes, Error>> + Send;
91}