pub struct DeRecMessageBuilder<State, Mode> { /* private fields */ }Expand description
Builds a DeRecMessage envelope containing protocol metadata and an
encrypted payload.
The builder uses typestate and mode generics:
Statecontrols whether the payload is ready to buildModecontrols which encryption method is permitted
This ensures at compile time that:
buildcannot be called before encryption- a pairing builder cannot call channel encryption
- a channel builder cannot call pairing encryption
§Type parameters
State- encryption state marker, typicallyNotEncryptedorEncryptedMode- builder mode marker, eitherPairingModeorChannelMode
§Required fields
Before calling build, the builder must have:
channel_idtimestampmessage- the appropriate encryption method applied
§Typical usage
Pairing mode:
let envelope = DeRecMessageBuilder::new()
.channel_id(channel_id)
.timestamp(current_timestamp())
.message(&pair_request)
.encrypt_pairing(helper_public_key)?
.build()?;Channel mode:
let envelope = DeRecMessageBuilder::channel()
.channel_id(channel_id)
.timestamp(current_timestamp())
.message_body(MessageBody::VerifyShareRequest(&verify_request))
.encrypt(shared_key)?
.build()?;Implementations§
Source§impl<State, Mode> DeRecMessageBuilder<State, Mode>
impl<State, Mode> DeRecMessageBuilder<State, Mode>
Sourcepub fn channel_id(self, channel_id: ChannelId) -> Self
pub fn channel_id(self, channel_id: ChannelId) -> Self
Sourcepub fn trace_id(self, trace_id: u64) -> Self
pub fn trace_id(self, trace_id: u64) -> Self
Sets the request/response correlation token for the envelope.
On request envelopes the requester chooses any opaque u64 to identify
the in-flight call. On response envelopes the responder echoes back the
value from the request. Both happens automatically when this builder is
driven by the standard request/response producers, but the value can
also be set explicitly by callers that need to correlate from outside.
Defaults to zero when not set, which the protocol treats as “no correlation requested.”
§Arguments
trace_id- opaque correlation token
§Returns
The updated builder.
Sourcepub fn auto_trace_id(self) -> Self
pub fn auto_trace_id(self) -> Self
Sets a freshly-drawn random trace_id on the envelope.
Convenience for request producers that want a new correlation token
without managing the RNG themselves. Uses the same random source as the
rest of the library (rand::rng().next_u64()).
§Returns
The updated builder with a random trace_id.
Sourcepub fn message_body(self, body: MessageBody) -> Self
pub fn message_body(self, body: MessageBody) -> Self
Encodes the inner payload and sets the message_type discriminant from a MessageBody.
This is the canonical way to attach the inner message — prost-encodes the
payload and records the correct i32 message_type value so that
DeRecMessageBuilder::build can embed it in the outer DeRecMessage
envelope.
§Arguments
body- aMessageBodyvariant wrapping a reference to the inner protocol message
§Returns
The updated builder.
Source§impl DeRecMessageBuilder<NotEncrypted, PairingMode>
impl DeRecMessageBuilder<NotEncrypted, PairingMode>
Sourcepub fn pairing() -> Self
pub fn pairing() -> Self
Creates a new pairing-mode DeRecMessageBuilder.
This constructor is intended for flows that use pairing-envelope
encryption. Builders created with this constructor can call
encrypt_pairing but cannot call channel
encryption.
§Returns
A new empty builder in pairing mode.
§Example
let builder = DeRecMessageBuilder::new();Sourcepub fn encrypt_pairing(
self,
public_key: impl AsRef<[u8]>,
) -> Result<DeRecMessageBuilder<Encrypted, PairingMode>, DeRecMessageBuilderError>
pub fn encrypt_pairing( self, public_key: impl AsRef<[u8]>, ) -> Result<DeRecMessageBuilder<Encrypted, PairingMode>, DeRecMessageBuilderError>
Encrypts the encoded payload using pairing-envelope encryption.
This method is only available on builders created in PairingMode.
After successful encryption, the builder transitions to the Encrypted
state, enabling build.
§Arguments
public_key- recipient public key used for asymmetric pairing encryption
§Returns
On success, returns a new builder in the Encrypted state.
§Errors
Returns DeRecMessageBuilderError if:
DeRecMessageBuilderError::MissingMessageif no payload was set- the underlying pairing encryption routine fails
Source§impl DeRecMessageBuilder<NotEncrypted, ChannelMode>
impl DeRecMessageBuilder<NotEncrypted, ChannelMode>
Sourcepub fn channel() -> Self
pub fn channel() -> Self
Creates a new channel-mode DeRecMessageBuilder.
This constructor is intended for flows that use symmetric channel
encryption. Builders created with this constructor can call
encrypt but cannot call pairing encryption.
§Returns
A new empty builder in channel mode.
§Example
let builder = DeRecMessageBuilder::channel();Sourcepub fn encrypt(
self,
shared_key: &[u8; 32],
) -> Result<DeRecMessageBuilder<Encrypted, ChannelMode>, DeRecMessageBuilderError>
pub fn encrypt( self, shared_key: &[u8; 32], ) -> Result<DeRecMessageBuilder<Encrypted, ChannelMode>, DeRecMessageBuilderError>
Encrypts the encoded payload using channel encryption.
This method is only available on builders created in ChannelMode.
The nonce is derived from the channel ID by placing the big-endian
u64 channel identifier into the last 8 bytes of a 32-byte nonce.
After successful encryption, the builder transitions to the
Encrypted state, enabling build.
§Arguments
shared_key- 32-byte symmetric channel key
§Returns
On success, returns a new builder in the Encrypted state.
§Errors
Returns DeRecMessageBuilderError if:
DeRecMessageBuilderError::MissingMessageif no payload was setDeRecMessageBuilderError::MissingChannelIdifchannel_idwas not set- the underlying channel encryption routine fails
Source§impl<Mode> DeRecMessageBuilder<Encrypted, Mode>
impl<Mode> DeRecMessageBuilder<Encrypted, Mode>
Sourcepub fn build(self) -> Result<DeRecMessage, DeRecMessageBuilderError>
pub fn build(self) -> Result<DeRecMessage, DeRecMessageBuilderError>
Builds the final DeRecMessage envelope.
This method is only available after one of the permitted encryption methods has been called successfully.
§Returns
On success returns a DeRecMessage containing:
- protocol version from
ProtocolVersion::current channel_idsequenceor0if no sequence was settimestampmessagecontaining the encrypted payload bytes
§Errors
Returns DeRecMessageBuilderError if:
DeRecMessageBuilderError::MissingChannelIdifchannel_idwas not setDeRecMessageBuilderError::MissingTimestampiftimestampwas not setDeRecMessageBuilderError::MissingMessageif the encrypted payload is empty
§Notes
This function does not perform any additional cryptographic validation. It only validates that the required envelope fields are present.
Trait Implementations§
Auto Trait Implementations§
impl<State, Mode> Freeze for DeRecMessageBuilder<State, Mode>
impl<State, Mode> RefUnwindSafe for DeRecMessageBuilder<State, Mode>where
State: RefUnwindSafe,
Mode: RefUnwindSafe,
impl<State, Mode> Send for DeRecMessageBuilder<State, Mode>
impl<State, Mode> Sync for DeRecMessageBuilder<State, Mode>
impl<State, Mode> Unpin for DeRecMessageBuilder<State, Mode>
impl<State, Mode> UnsafeUnpin for DeRecMessageBuilder<State, Mode>
impl<State, Mode> UnwindSafe for DeRecMessageBuilder<State, Mode>where
State: UnwindSafe,
Mode: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more