[][src]Struct xaynet_core::message::MessageBuffer

pub struct MessageBuffer<T> { /* fields omitted */ }

A wrapper around a buffer that contains a Message.

It provides getters and setters to access the different fields of the message safely. A message is made of a header and a payload:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                           signature                           +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                         participant_pk                        +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                         coordinator_pk                        +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+                                                               +
|                                                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             length                            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      tag      |                    reserved                   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                                                               |
+                    payload (variable length)                  +
|                                                               |

Examples

Reading a sum message

use std::convert::TryFrom;
use xaynet_core::message::{MessageBuffer, Tag};

let mut bytes = vec![0x11; 64]; // message signature
bytes.extend(vec![0x22; 32]); // participant public signing key
bytes.extend(vec![0x33; 32]); // coordinator public encrypt key
bytes.extend(&200_u32.to_be_bytes()); // Length field
bytes.push(0x01); // tag (sum message)
bytes.extend(vec![0x00, 0x00, 0x00]); // reserved

// Payload: a sum message contains a signature and an ephemeral public key
bytes.extend(vec![0xaa; 32]); // signature
bytes.extend(vec![0xbb; 32]); // public key

let buffer = MessageBuffer::new(&bytes).unwrap();
assert_eq!(buffer.signature(), vec![0x11; 64].as_slice());
assert_eq!(buffer.participant_pk(), vec![0x22; 32].as_slice());
assert_eq!(buffer.coordinator_pk(), vec![0x33; 32].as_slice());
assert_eq!(Tag::try_from(buffer.tag()).unwrap(), Tag::Sum);
assert_eq!(
    buffer.payload(),
    [vec![0xaa; 32], vec![0xbb; 32]].concat().as_slice()
);

Writing a sum message

use std::convert::TryFrom;
use xaynet_core::message::{MessageBuffer, Tag};

let mut expected = vec![0x11; 64]; // message signature
expected.extend(vec![0x22; 32]); // participant public signing key
expected.extend(vec![0x33; 32]); // coordinator public signing key
expected.extend(&200_u32.to_be_bytes()); // length field
expected.push(0x01); // tag (sum message)
expected.extend(vec![0x00, 0x00, 0x00]); // reserved

// Payload: a sum message contains a signature and an ephemeral public key
expected.extend(vec![0xaa; 32]); // signature
expected.extend(vec![0xbb; 32]); // public key

let mut bytes = vec![0; expected.len()];
let mut buffer = MessageBuffer::new_unchecked(&mut bytes);
buffer
    .signature_mut()
    .copy_from_slice(vec![0x11; 64].as_slice());
buffer
    .participant_pk_mut()
    .copy_from_slice(vec![0x22; 32].as_slice());
buffer
    .coordinator_pk_mut()
    .copy_from_slice(vec![0x33; 32].as_slice());
buffer.set_length(200 as u32);
buffer.set_tag(Tag::Sum.into());
buffer
    .payload_mut()
    .copy_from_slice([vec![0xaa; 32], vec![0xbb; 32]].concat().as_slice());
assert_eq!(expected, bytes);

Implementations

impl<T: AsRef<[u8]>> MessageBuffer<T>[src]

pub fn inner(&self) -> &T[src]

pub fn as_ref(&self) -> MessageBuffer<&T>[src]

pub fn new(bytes: T) -> Result<Self, DecodeError>[src]

Performs bound checks for the various message fields on bytes and returns a new MessageBuffer.

Errors

Fails if the bytes are smaller than a minimal-sized message buffer.

pub fn new_unchecked(bytes: T) -> Self[src]

Returns a MessageBuffer without performing any bound checks.

This means accessing the various fields may panic if the data is invalid.

pub fn check_buffer_length(&self) -> Result<(), DecodeError>[src]

Performs bound checks to ensure the fields can be accessed without panicking.

pub fn tag(&self) -> u8[src]

Gets the tag field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn length(&self) -> u32[src]

Gets the length field

Panics

Accessing the field may panic if the buffer has not been checked before.

impl<'a, T: AsRef<[u8]> + ?Sized> MessageBuffer<&'a T>[src]

pub fn signature(&self) -> &'a [u8][src]

Gets the message signature field

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn participant_pk(&self) -> &'a [u8][src]

Gets the participant public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn coordinator_pk(&self) -> &'a [u8][src]

Gets the coordinator public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn payload(&self) -> &'a [u8][src]

Gets the rest of the message.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn check_signature(&self) -> Result<(), DecodeError>[src]

Parse the signature and public signing key, and check the message signature.

pub fn signed_data(&self) -> &'a [u8][src]

Return the portion of the message used to compute the signature, ie the entire message except the signature field itself.

Panics

Accessing the field may panic if the buffer has not been checked before.

impl<T: AsMut<[u8]> + AsRef<[u8]>> MessageBuffer<T>[src]

pub fn set_tag(&mut self, value: u8)[src]

Sets the tag field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn set_length(&mut self, value: u32)[src]

Sets the length field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn signature_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the message signature field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn participant_pk_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the participant public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn coordinator_pk_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the coordinator public key field.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn payload_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the rest of the message.

Panics

Accessing the field may panic if the buffer has not been checked before.

pub fn signed_data_mut(&mut self) -> &mut [u8][src]

Gets a mutable reference to the portion of the message used to compute the signature, ie the entire message except the signature field itself.

Panics

Accessing the field may panic if the buffer has not been checked before.

Auto Trait Implementations

impl<T> RefUnwindSafe for MessageBuffer<T> where
    T: RefUnwindSafe

impl<T> Send for MessageBuffer<T> where
    T: Send

impl<T> Sync for MessageBuffer<T> where
    T: Sync

impl<T> Unpin for MessageBuffer<T> where
    T: Unpin

impl<T> UnwindSafe for MessageBuffer<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,