Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version = "1.63.0"
[features]
default = ["std"]
# High-level wrappers using futures traits.
async = ["std", "dep:futures"]
futures = ["std", "dep:futures"]
# High-level wrappers using tokio traits - may affect MSRV requirements.
tokio = ["std", "dep:tokio"]
std = ["alloc", "bitcoin/std", "bitcoin_hashes/std", "chacha20-poly1305/std", "rand/std", "rand/std_rng"]
Expand Down
6 changes: 3 additions & 3 deletions protocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ A BIP324 library to establish and communicate over an encrypted channel.

The library is designed with a bare `no_std` and "Sans I/O" interface to keep it as agnostic as possible to application runtimes, but higher level interfaces are exposed for ease of use.

The `async` feature includes the high-level `AsyncProcotol` type which helps create and manage an encrypted channel.
The `futures` feature includes the high-level `AsyncProcotol` type which helps create and manage an encrypted channel.

The lower-level `Handshake` and `PacketHandler` types can be directly used by applications which require more control. The handshake performs the one-and-a-half round trip dance between the peers in order to generate secret materials. A successful handshake results in a packet handler which performs the encrypt and decrypt operations for the lifetime of the channel.

## Feature Flags

* `alloc` -- Expose memory allocation dependent features.
* `std` -- Includes the `alloc` memory allocation feature as well as extra standard library dependencies for I/O and random number generators.
* `async` -- High level wrappers for asynchronous read and write runtimes using agnostic futures-rs traits.
* `tokio` -- Same wrappers as `async`, but using the popular tokio runtime's specific traits instead of futures-rs.
* `futures` -- High level wrappers for asynchronous read and write runtimes using agnostic futures-rs traits.
* `tokio` -- Same wrappers as `futures`, but using the popular tokio runtime's specific traits instead of futures-rs.

## Minimum Supported Rust Version (MSRV)

Expand Down
18 changes: 9 additions & 9 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use bitcoin_hashes::{hkdf, sha256};
use fschacha20poly1305::{FSChaCha20, FSChaCha20Poly1305};
// Default to the futures-rs traits, but can overwrite with more specific
// tokio implemenations for easier caller integration.
#[cfg(all(feature = "async", not(feature = "tokio")))]
#[cfg(all(feature = "futures", not(feature = "tokio")))]
use futures::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
#[cfg(feature = "tokio")]
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
Expand Down Expand Up @@ -1065,13 +1065,13 @@ impl fmt::Display for ProtocolError {
}

/// A protocol session with handshake and send/receive packet management.
#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
pub struct AsyncProtocol {
reader: AsyncProtocolReader,
writer: AsyncProtocolWriter,
}

#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
impl AsyncProtocol {
/// New protocol session which completes the initial handshake and returns a handler.
///
Expand Down Expand Up @@ -1206,7 +1206,7 @@ impl AsyncProtocol {
}

/// State machine of an asynchronous packet read.
#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
#[derive(Debug)]
enum DecryptState {
ReadingLength {
Expand All @@ -1219,7 +1219,7 @@ enum DecryptState {
},
}

#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
impl Default for DecryptState {
fn default() -> Self {
DecryptState::ReadingLength {
Expand All @@ -1230,13 +1230,13 @@ impl Default for DecryptState {
}

/// Manages an async buffer to automatically decrypt contents of received packets.
#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
pub struct AsyncProtocolReader {
packet_reader: PacketReader,
state: DecryptState,
}

#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
impl AsyncProtocolReader {
/// Decrypt contents of received packet from buffer.
///
Expand Down Expand Up @@ -1296,12 +1296,12 @@ impl AsyncProtocolReader {
}

/// Manages an async buffer to automatically encrypt and send contents in packets.
#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
pub struct AsyncProtocolWriter {
packet_writer: PacketWriter,
}

#[cfg(any(feature = "async", feature = "tokio"))]
#[cfg(any(feature = "futures", feature = "tokio"))]
impl AsyncProtocolWriter {
/// Encrypt contents and write packet buffer.
///
Expand Down