commonware_stream/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Exchange messages over arbitrary transport.

pub mod public_key;
pub mod utils;

use bytes::Bytes;
use prost::DecodeError;
use std::future::Future;
use thiserror::Error;

/// Errors that can occur when interacting with a stream.
#[derive(Error, Debug)]
pub enum Error {
    #[error("unexpected message")]
    UnexpectedMessage,
    #[error("unable to decode: {0}")]
    UnableToDecode(DecodeError),
    #[error("invalid ephemeral public key")]
    InvalidEphemeralPublicKey,
    #[error("invalid channel public key")]
    InvalidChannelPublicKey,
    #[error("invalid peer public key")]
    InvalidPeerPublicKey,
    #[error("handshake not for us")]
    HandshakeNotForUs,
    #[error("handshake timeout")]
    HandshakeTimeout,
    #[error("missing signature")]
    MissingSignature,
    #[error("invalid signature")]
    InvalidSignature,
    #[error("wrong peer")]
    WrongPeer,
    #[error("recv failed")]
    RecvFailed,
    #[error("recv too large: {0} bytes")]
    RecvTooLarge(usize),
    #[error("send failed")]
    SendFailed,
    #[error("send zero size")]
    SendZeroSize,
    #[error("send too large: {0} bytes")]
    SendTooLarge(usize),
    #[error("connection closed")]
    StreamClosed,
    #[error("cipher creation failed")]
    CipherCreationFailed,
    #[error("nonce overflow")]
    NonceOverflow,
    #[error("encryption failed")]
    EncryptionFailed,
    #[error("decryption failed")]
    DecryptionFailed,
    #[error("timestamp too old: {0}")]
    InvalidTimestampOld(u64),
    #[error("timestamp too future: {0}")]
    InvalidTimestampFuture(u64),
}

/// A trait for sending messages.
pub trait Sender: Sync + Send + 'static {
    /// Send a message to the stream.
    fn send(&mut self, msg: &[u8]) -> impl Future<Output = Result<(), Error>> + Send;
}

/// A trait for receiving messages.
pub trait Receiver: Sync + Send + 'static {
    /// Receive a message from the stream.
    fn receive(&mut self) -> impl Future<Output = Result<Bytes, Error>> + Send;
}