commonware_stream/
lib.rs

1//! Exchange messages over arbitrary transport.
2
3pub mod public_key;
4pub mod utils;
5
6use bytes::Bytes;
7use prost::DecodeError;
8use std::future::Future;
9use thiserror::Error;
10
11/// Errors that can occur when interacting with a stream.
12#[derive(Error, Debug)]
13pub enum Error {
14    #[error("unexpected message")]
15    UnexpectedMessage,
16    #[error("unable to decode: {0}")]
17    UnableToDecode(DecodeError),
18    #[error("invalid ephemeral public key")]
19    InvalidEphemeralPublicKey,
20    #[error("invalid channel public key")]
21    InvalidChannelPublicKey,
22    #[error("invalid peer public key")]
23    InvalidPeerPublicKey,
24    #[error("handshake not for us")]
25    HandshakeNotForUs,
26    #[error("handshake timeout")]
27    HandshakeTimeout,
28    #[error("missing signature")]
29    MissingSignature,
30    #[error("invalid signature")]
31    InvalidSignature,
32    #[error("wrong peer")]
33    WrongPeer,
34    #[error("recv failed")]
35    RecvFailed,
36    #[error("recv too large: {0} bytes")]
37    RecvTooLarge(usize),
38    #[error("send failed")]
39    SendFailed,
40    #[error("send zero size")]
41    SendZeroSize,
42    #[error("send too large: {0} bytes")]
43    SendTooLarge(usize),
44    #[error("connection closed")]
45    StreamClosed,
46    #[error("cipher creation failed")]
47    CipherCreationFailed,
48    #[error("nonce overflow")]
49    NonceOverflow,
50    #[error("encryption failed")]
51    EncryptionFailed,
52    #[error("decryption failed")]
53    DecryptionFailed,
54    #[error("timestamp too old: {0}")]
55    InvalidTimestampOld(u64),
56    #[error("timestamp too future: {0}")]
57    InvalidTimestampFuture(u64),
58}
59
60/// A trait for sending messages.
61pub trait Sender: Sync + Send + 'static {
62    /// Send a message to the stream.
63    fn send(&mut self, msg: &[u8]) -> impl Future<Output = Result<(), Error>> + Send;
64}
65
66/// A trait for receiving messages.
67pub trait Receiver: Sync + Send + 'static {
68    /// Receive a message from the stream.
69    fn receive(&mut self) -> impl Future<Output = Result<Bytes, Error>> + Send;
70}