Skip to main content

Message

Struct Message 

Source
pub struct Message { /* private fields */ }
Expand description

DIDComm message structure.

Messages are used to construct new DIDComm messages.

A common flow is

For examples have a look here.

Specification

Implementations§

Source§

impl Message

Source

pub fn append_attachment(&mut self, builder: AttachmentBuilder)

Appends attachment into attachments field. Consumes instance of AttachmentBuilder to do so.

§Parameters
  • builder - prepopulated instance of AttachmentBuilder
Source

pub fn attachment_iter(&self) -> impl DoubleEndedIterator<Item = &Attachment>

Returns iterator of all attachments.

Source

pub fn deserialize_attachments<'de, T>(&'de self, fmt: &str) -> Result<Vec<T>>
where T: Deserialize<'de>,

Deserializes a the attachements with media-type fmt into Vec<T>.

§Error:

It returns an error if media type is not application/json or if the media is invalid JSON document.

Source§

impl Message

Source

pub fn new() -> Self

Generates EMPTY default message. Use extension messages to build final one before sending.

Source

pub fn add_header_field(self, key: String, value: String) -> Self

Adds (or updates) custom unique header key-value pair to the header. This portion of header is not sent as JOSE header.

Source

pub fn as_flat_jwe( self, alg: &CryptoAlgorithm, recipient_public_key: Option<Vec<u8>>, ) -> Self

Sets message to be serialized as flat JWE JSON. If this message has multiple targets, sealing it will result in an Error.

Source

pub fn as_flat_jws(self, alg: &SignatureAlgorithm) -> Self

Sets message to be serialized as flat JWS JSON and then calls as_jws. If this message has multiple targets, sealing it will result in an Error.

Source

pub fn get_message_uri(&self) -> String

Shortcut to DidCommHeader::get_message_uri

Source

pub fn reply_to(self, replying_to: &Self) -> Self

Sets thid and pthid same as those in replying_to Shortcut to DidCommHeader::reply_to method

  • replying_to - ref to message we’re replying to
Source

pub fn with_parent(self, parent: &Self) -> Self

Sets pthid to the parent’s thid. It defaults to id if thid is missing.

§Parameters
  • parent - ref to a parent threaded Message
Source

pub fn as_jwe( self, alg: &CryptoAlgorithm, recipient_public_key: Option<Vec<u8>>, ) -> Self

Setter of from header Helper method.

For resolve feature will set kid header automatically based on the did document resolved.

Source

pub fn as_jws(self, alg: &SignatureAlgorithm) -> Self

Creates set of JWM related headers for the JWE Modifies JWM related header portion to match encryption implementation and leaves other parts unchanged. TODO + FIXME: complete implementation

Source

pub fn body(self, body: &str) -> Result<Self>

Setter of the body. Note, that given text has to be a valid JSON string to be a valid body value.

Source

pub fn didcomm_header(self, h: DidCommHeader) -> Self

Setter of didcomm_header. Replaces existing one with provided by consuming both values. Returns modified instance of Self.

Source

pub fn from(self, from: &str) -> Self

Setter of from header.

Source

pub fn get_body(&self) -> Result<String>

Getter of the body as String.

Source

pub fn get_didcomm_header(&self) -> &DidCommHeader

&DidCommHeader getter.

Source

pub fn get_jwm_header(&self) -> &JwmHeader

&JwmCommHeader getter.

Source

pub fn get_prior(&self) -> Result<PriorClaims>

If message is_rotation() true - returns from_prion claims. Errors otherwise with Error::NoRotationData

Source

pub fn is_rotation(&self) -> bool

Checks if message is rotation one. Exposed for explicit checks on calling code level.

Source

pub fn jwm_header(self, h: JwmHeader) -> Self

Setter of jwm_header. Replaces existing one with provided by consuming both values. Returns modified instance of Self.

Source

pub fn m_type(self, m_type: &str) -> Self

Setter of m_type @type header

Source

pub fn typ(self, typ: MessageType) -> Self

Setter of typ header property.

§Parameters
  • typ - MessageType to be set for typ property
Source

pub fn kid(self, kid: &str) -> Self

Source

pub fn timed(self, expires: Option<u64>) -> Self

Sets times of creation as now and, optional, expires time.

§Arguments
  • expires - time in seconds since Unix Epoch when message is considered to be invalid.
Source

pub fn to(self, to: &[&str]) -> Self

Setter of to header

Source

pub fn set_didcomm_header(self, h: DidCommHeader) -> Self

Setter of didcomm_header. Replaces existing one with provided by consuming both values. Returns modified instance of Self.

Source

pub fn get_application_params(&self) -> impl Iterator<Item = (&String, &String)>

Gets Iterator over key-value pairs of application level headers

Source

pub fn thid(self, thid: &str) -> Self

Setter of thid header

Source

pub fn pthid(self, pthid: &str) -> Self

Setter of pthid header

Source§

impl Message

Source

pub fn as_raw_json(self) -> Result<String>

Serializes current state of the message into json. Consumes original message - use as raw sealing of envelope.

Source

pub fn export_for_encryption(&self) -> Result<(Vec<u8>, Vec<u8>)>

Presents IV and Payload to be externally encrypted and then sealed with seal_pre_encrypted method.

§Returns

Tuple of bytes where .0 is IV and .1 is payload for encryption

Source

pub fn seal_pre_encrypted(self, cyphertext: impl AsRef<[u8]>) -> Result<String>

Builds JWE from current message and it’s pre-encrypted payload: expert_for_encryption should be used prior to this call and it’s output provided as payload.

§Parameters

ciphertext - encrypted output of export_for_encryption as JWE payload

Returns serialized JSON JWE message, which is ready to be sent to receipent

Source

pub fn receive( incoming: &str, encryption_recipient_private_key: Option<&[u8]>, encryption_sender_public_key: Option<Vec<u8>>, signing_sender_public_key: Option<&[u8]>, ) -> Result<Self>

Construct a message from received data. Raw, JWS or JWE payload is accepted.

§Arguments
  • incoming - serialized message as Message/Jws/Jws

  • encryption_recipient_private_key - recipients private key, used to decrypt kek in JWE

  • encryption_sender_public_key - senders public key, used to decrypt kek in JWE

  • signing_sender_public_key - senders public key, the JWS envelope was signed with

Source

pub fn routed_by( self, sender_private_key: &[u8], recipient_public_keys: Option<Vec<Option<Vec<u8>>>>, mediator_did: &str, mediator_public_key: Option<Vec<u8>>, ) -> Result<String>

Wrap self to be mediated by some mediator. Warning: Should be called on a Message instance which is ready to be sent! If message is not properly set up for crypto - this method will propagate error from called .seal() method. Takes one mediator at a time to make sure that mediated chain preserves unchanged. This method can be chained any number of times to match all the mediators in the chain.

§Arguments
  • sender_private_key - encryption key for inner message payload JWE encryption

  • recipient_public_keys - keys used to encrypt content encryption key for recipient; can be provided if key should not be resolved via recipients DID

  • mediator_did - DID of message mediator, will be to of mediated envelope

  • mediator_public_key - key used to encrypt content encryption key for mediator; can be provided if key should not be resolved via mediators DID

Source

pub fn seal( self, sender_private_key: impl AsRef<[u8]>, recipient_public_keys: Option<Vec<Option<Vec<u8>>>>, ) -> Result<String>

Seals (encrypts) self and returns ready to send JWE

§Arguments
  • sender_private_key - encryption key for inner message payload JWE encryption

  • recipient_public_keys - keys used to encrypt content encryption key for recipient; can be provided if key should not be resolved via recipients DID

Source§

impl Message

Associated functions implementations. Possibly not required as Jwe serialization covers this.

Source

pub fn get_iv(received: &[u8]) -> Result<Vec<u8>>

Parses iv value as Vec<u8> from public header. Both regular JSON and Compact representations are accepted. Returns Error on failure. TODO: Add examples

Source

pub fn received_as_jwe(incomming: impl AsRef<[u8]>) -> Option<Jwe>

Transforms incomming into Jwe if it is one Also checks if skid field is present or returns None othervise Key resolution and validation fall onto caller of this method

§Parameters
  • incomming - incomming message

Returns Option<Jwe> where .header.skid is skid and .payload() is cyphertext

Source

pub fn receive_external_crypto(decrypted: impl AsRef<[u8]>) -> Result<Self>

Transforms decrypted Jwe into Message

§Parameters
  • decrypted - result of decrypting of Jwe payload retreived after decrypting content of as_jwe function call output.
Source

pub fn seal_signed( self, encryption_sender_private_key: &[u8], encryption_recipient_public_keys: Option<Vec<Option<Vec<u8>>>>, signing_algorithm: SignatureAlgorithm, signing_sender_private_key: &[u8], ) -> Result<String>

Signs raw message and then packs it to encrypted envelope Spec

§Arguments
  • encryption_sender_private_key - encryption key for inner message payload JWE encryption

  • encryption_recipient_public_keys - keys used to encrypt content encryption key for recipient with; can be provided if key should not be resolved via recipients DID

  • signing_algorithm - encryption algorithm used

  • signing_sender_private_key - signing key for enveloped message JWS encryption

Source§

impl Message

Source

pub fn encrypt( self, crypter: SymmetricCypherMethod, cek: &[u8], ) -> Result<String, Error>

Encrypts current message by consuming it. Uses provided cryptography function to perform the encryption. Agnostic of actual algorithm used. Consuming is to make sure no changes are possible post packaging / sending. Returns (JwmHeader, Vec<u8>) to be sent to recipient.

§Arguments
  • crypter - encryptor that should be used

  • cek - content encryption key to encrypt message with

Source

pub fn decrypt( received_message: &[u8], decrypter: SymmetricCypherMethod, cek: &[u8], ) -> Result<Self, Error>

Decrypts received cypher into instance of Message. Received message should be encrypted with our pub key. Returns Ok(Message) if decryption / deserialization succeeded. Error otherwise.

§Arguments
  • received_message - received message as byte array

  • decrypter - decrypter that should be used

  • cek - content encryption key to decrypt message with

Source

pub fn sign( self, signer: SigningMethod, signing_sender_private_key: &[u8], ) -> Result<String, Error>

Signs message and turns it into Jws envelope. Err is returned if message is not properly prepared or data is malformed. Jws enveloped payload is base64_url encoded

Source

pub fn verify( jws: &[u8], signing_sender_public_key: &[u8], ) -> Result<Message, Error>

Verifies signature and returns payload message on verification success. Err return if signature invalid or data is malformed. Expects Jws’s payload to be a valid serialized Message and base64_url encoded.

Source

pub fn verify_value( jws: &Value, signing_sender_public_key: &[u8], ) -> Result<Message, Error>

Verifies signature and returns payload message on verification success. Err return if signature invalid or data is malformed. Expects Jws’s payload to be a valid serialized Message and base64_url encoded.

§Arguments
  • jws - to be verified jws message as json Value object

  • signing_sender_public_key - optional public key used for verification, if None it will try to resolve the did in the kid field

Source§

impl Message

Source

pub fn as_out_of_band_invitation( self, body: impl AsRef<[u8]>, attachments: Option<Vec<AttachmentBuilder>>, ) -> Result<Self>

Transforms given Message into out_of_band invitation with given body and optional attachments.

§Parameters
  • body - bytes of JSON serialized message body
  • attachments - optional set of AttachmentBuilder to be attached

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Message

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Message

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Message

Source§

fn eq(&self, other: &Message) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Message

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Message

Source§

impl StructuralPartialEq for Message

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,