commonware_stream/
lib.rs

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