pub struct Message { /* private fields */ }Expand description
DIDComm message structure.
Messages are used to construct new DIDComm messages.
A common flow is
- creating a message
- setting different properties with chained setters
- serializing the message to one of the following formats:
- a plain DIDComm message
- a signed JWS envelope
- an encrypted JWE envelope
- a sealed and encrypted JWE envelope
For examples have a look here.
Implementations§
Source§impl Message
impl Message
Sourcepub fn append_attachment(&mut self, builder: AttachmentBuilder)
pub fn append_attachment(&mut self, builder: AttachmentBuilder)
Appends attachment into attachments field.
Consumes instance of AttachmentBuilder to do so.
§Parameters
builder- prepopulated instance ofAttachmentBuilder
Sourcepub fn attachment_iter(&self) -> impl DoubleEndedIterator<Item = &Attachment>
pub fn attachment_iter(&self) -> impl DoubleEndedIterator<Item = &Attachment>
Returns iterator of all attachments.
Sourcepub fn deserialize_attachments<'de, T>(&'de self, fmt: &str) -> Result<Vec<T>>where
T: Deserialize<'de>,
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
impl Message
Sourcepub fn new() -> Self
pub fn new() -> Self
Generates EMPTY default message.
Use extension messages to build final one before sending.
Sourcepub fn add_header_field(self, key: String, value: String) -> Self
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.
Sourcepub fn as_flat_jwe(
self,
alg: &CryptoAlgorithm,
recipient_public_key: Option<Vec<u8>>,
) -> Self
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.
Sourcepub fn as_flat_jws(self, alg: &SignatureAlgorithm) -> Self
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.
Sourcepub fn get_message_uri(&self) -> String
pub fn get_message_uri(&self) -> String
Shortcut to DidCommHeader::get_message_uri
Sourcepub fn reply_to(self, replying_to: &Self) -> Self
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
Sourcepub fn with_parent(self, parent: &Self) -> Self
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 threadedMessage
Sourcepub fn as_jwe(
self,
alg: &CryptoAlgorithm,
recipient_public_key: Option<Vec<u8>>,
) -> Self
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.
Sourcepub fn as_jws(self, alg: &SignatureAlgorithm) -> Self
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
Sourcepub fn body(self, body: &str) -> Result<Self>
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.
Sourcepub fn didcomm_header(self, h: DidCommHeader) -> Self
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.
Sourcepub fn get_didcomm_header(&self) -> &DidCommHeader
pub fn get_didcomm_header(&self) -> &DidCommHeader
&DidCommHeader getter.
Sourcepub fn get_jwm_header(&self) -> &JwmHeader
pub fn get_jwm_header(&self) -> &JwmHeader
&JwmCommHeader getter.
Sourcepub fn get_prior(&self) -> Result<PriorClaims>
pub fn get_prior(&self) -> Result<PriorClaims>
If message is_rotation() true - returns from_prion claims.
Errors otherwise with Error::NoRotationData
Sourcepub fn is_rotation(&self) -> bool
pub fn is_rotation(&self) -> bool
Checks if message is rotation one. Exposed for explicit checks on calling code level.
Sourcepub fn jwm_header(self, h: JwmHeader) -> Self
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.
Sourcepub fn typ(self, typ: MessageType) -> Self
pub fn typ(self, typ: MessageType) -> Self
pub fn kid(self, kid: &str) -> Self
Sourcepub fn timed(self, expires: Option<u64>) -> Self
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.
Sourcepub fn set_didcomm_header(self, h: DidCommHeader) -> Self
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.
Sourcepub fn get_application_params(&self) -> impl Iterator<Item = (&String, &String)>
pub fn get_application_params(&self) -> impl Iterator<Item = (&String, &String)>
Gets Iterator over key-value pairs of application level headers
Source§impl Message
impl Message
Sourcepub fn as_raw_json(self) -> Result<String>
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.
Sourcepub fn export_for_encryption(&self) -> Result<(Vec<u8>, Vec<u8>)>
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
Sourcepub fn seal_pre_encrypted(self, cyphertext: impl AsRef<[u8]>) -> Result<String>
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
Sourcepub 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>
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 asMessage/Jws/Jws -
encryption_recipient_private_key- recipients private key, used to decryptkekin JWE -
encryption_sender_public_key- senders public key, used to decryptkekin JWE -
signing_sender_public_key- senders public key, the JWS envelope was signed with
Sourcepub 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>
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 betoof 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
Sourcepub fn seal(
self,
sender_private_key: impl AsRef<[u8]>,
recipient_public_keys: Option<Vec<Option<Vec<u8>>>>,
) -> Result<String>
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.
impl Message
Associated functions implementations. Possibly not required as Jwe serialization covers this.
Sourcepub fn get_iv(received: &[u8]) -> Result<Vec<u8>>
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
Sourcepub fn received_as_jwe(incomming: impl AsRef<[u8]>) -> Option<Jwe>
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
Sourcepub fn receive_external_crypto(decrypted: impl AsRef<[u8]>) -> Result<Self>
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 ofas_jwefunction call output.
Sourcepub 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>
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
impl Message
Sourcepub fn encrypt(
self,
crypter: SymmetricCypherMethod,
cek: &[u8],
) -> Result<String, Error>
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
Sourcepub fn decrypt(
received_message: &[u8],
decrypter: SymmetricCypherMethod,
cek: &[u8],
) -> Result<Self, Error>
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
Sourcepub fn sign(
self,
signer: SigningMethod,
signing_sender_private_key: &[u8],
) -> Result<String, Error>
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
Sourcepub fn verify(
jws: &[u8],
signing_sender_public_key: &[u8],
) -> Result<Message, Error>
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.
Sourcepub fn verify_value(
jws: &Value,
signing_sender_public_key: &[u8],
) -> Result<Message, Error>
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 jsonValueobject -
signing_sender_public_key- optional public key used for verification, ifNoneit will try to resolve the did in thekidfield
Source§impl Message
impl Message
Sourcepub fn as_out_of_band_invitation(
self,
body: impl AsRef<[u8]>,
attachments: Option<Vec<AttachmentBuilder>>,
) -> Result<Self>
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 bodyattachments- optional set ofAttachmentBuilderto be attached