Skip to content

Commit

Permalink
Add to_message on envelopes & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
DeppLearning committed Apr 1, 2021
1 parent 628e7be commit e63a4db
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 309 deletions.
16 changes: 8 additions & 8 deletions identity-comm/src/envelope/plaintext.rs
@@ -1,11 +1,11 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::envelope::EnvelopeExt;
use crate::error::Result;
use identity_core::convert::ToJson as _;
use serde::Serialize;
use serde::Deserialize;

use crate::{envelope::EnvelopeExt, error::Result, error::Error};
use serde::Serialize;

/// A DIDComm Plaintext Message
///
Expand All @@ -21,12 +21,12 @@ impl Envelope {
pub fn from_message<T: Serialize>(message: &T) -> Result<Self> {
message.to_json().map_err(Into::into).map(Self)
}
pub fn to_message<'a, T>(&'a self) -> Result<T>
where
T: Deserialize<'a> {
serde_json::from_str(&self.0).map_err(Error::from)
pub fn to_message<T>(&self) -> Result<T>
where
for<'a> T: Deserialize<'a>,
{
serde_json::from_str(&self.0).map_err(Into::into)
}

}

impl EnvelopeExt for Envelope {
Expand Down
40 changes: 25 additions & 15 deletions identity-comm/src/envelope/signed.rs
@@ -1,19 +1,20 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity_core::{crypto::KeyPair, utils::encode_b58};
use libjose::{
jose::JoseTokenType,
jws::{Encoder, JwsAlgorithm, JwsFormat, JwsHeader,Decoder},
};
use serde::Serialize;
use crate::envelope::EnvelopeExt;
use crate::envelope::Plaintext;
use crate::error::Result;
use identity_core::crypto::KeyPair;
use identity_core::crypto::PublicKey;
use identity_core::utils::encode_b58;
use libjose::jose::JoseTokenType;
use libjose::jws::Decoder;
use libjose::jws::Encoder;
use libjose::jws::JwsAlgorithm;
use libjose::jws::JwsFormat;
use libjose::jws::JwsHeader;
use serde::Deserialize;


use crate::{
envelope::{EnvelopeExt, Plaintext},
error::Result,
};
use serde::Serialize;

/// Supported digital signature algorithms
///
Expand Down Expand Up @@ -65,9 +66,18 @@ impl Envelope {
.map_err(Into::into)
.map(Self)
}
pub fn to_message<'a, T>(&'a self, algorithm: Algorithm, keypair: &KeyPair) -> Result<T>
where T: Deserialize<'a> {
Decoder::new(keypair.public().as_bytes()).format(JwsFormat::Compact).algorithm(algorithm).decode(self.as_bytes()).map_err(Into::into)

pub fn to_message<T>(&self, algorithm: Algorithm, public: &PublicKey) -> Result<T>
where
for<'a> T: Deserialize<'a>,
{
let token = Decoder::new(public.as_ref())
.key_id(encode_b58(public))
.format(JwsFormat::Compact)
.algorithm(algorithm.into())
.decode(self.as_bytes())?;

serde_json::from_slice(&token.claims.to_vec()).map_err(Into::into)
}
}

Expand Down
20 changes: 10 additions & 10 deletions identity-comm/src/error.rs
Expand Up @@ -5,14 +5,14 @@ pub type Result<T, E = Error> = core::result::Result<T, E>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("IOTA Error: {0}")]
IotaError(#[from] identity_iota::error::Error),
#[error("IOTA Core Error: {0}")]
CoreError(#[from] identity_core::error::Error),
#[error("JOSE Error: {0}")]
JoseError(#[from] libjose::error::Error),
#[error("DID Document Error: {0}")]
DocumentError(#[from] did_doc::Error),
#[error("JSON Error: {0}")]
JsonError(#[from] serde_json::error::Error)
#[error("IOTA Error: {0}")]
IotaError(#[from] identity_iota::error::Error),
#[error("IOTA Core Error: {0}")]
CoreError(#[from] identity_core::error::Error),
#[error("JOSE Error: {0}")]
JoseError(#[from] libjose::error::Error),
#[error("DID Document Error: {0}")]
DocumentError(#[from] did_doc::Error),
#[error("JSON Error: {0}")]
JsonError(#[from] serde_json::error::Error),
}
2 changes: 0 additions & 2 deletions identity-comm/src/lib.rs
Expand Up @@ -8,5 +8,3 @@ pub mod envelope;
pub mod error;
pub mod message;
pub mod utils;


4 changes: 0 additions & 4 deletions identity-comm/src/message/authentication.rs
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use crate::message::Timing;
use crate::message::Message;
use did_doc::{url::Url, Signature};
use identity_iota::did::DID;
use serde::Serialize;
Expand Down Expand Up @@ -146,6 +145,3 @@ impl AuthenticationResponse {
self.signature = signature;
}
}

impl Message for AuthenticationRequest {}
impl Message for AuthenticationResponse {}
96 changes: 0 additions & 96 deletions identity-comm/src/message/builder.rs

This file was deleted.

55 changes: 28 additions & 27 deletions identity-comm/src/message/discovery.rs
@@ -1,5 +1,7 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use crate::message::Timing;
use crate::message::Message;
use did_doc::url::Url;
use identity_iota::did::DID;

Expand All @@ -15,31 +17,7 @@ pub struct DidRequest {
#[serde(skip_serializing_if = "Option::is_none")]
timing: Option<Timing>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct DidResponse {
id: DID,
}

impl DidResponse {
pub fn new(id: DID) -> Self {
Self { id }
}

/// Get a mutable reference to the did response's id.
pub fn id_mut(&mut self) -> &mut DID {
&mut self.id
}

/// Get a reference to the did response's id.
pub fn id(&self) -> &DID {
&self.id
}

/// Set the did response's id.
pub fn set_id(&mut self, id: DID) {
self.id = id;
}
}
impl DidRequest {
pub fn new(callback_url: Url) -> Self {
Self {
Expand Down Expand Up @@ -127,5 +105,28 @@ impl DidRequest {
}
}

impl Message for DidRequest {}
impl Message for DidResponse {}
#[derive(Debug, Deserialize, Serialize)]
pub struct DidResponse {
id: DID,
}

impl DidResponse {
pub fn new(id: DID) -> Self {
Self { id }
}

/// Get a mutable reference to the did response's id.
pub fn id_mut(&mut self) -> &mut DID {
&mut self.id
}

/// Get a reference to the did response's id.
pub fn id(&self) -> &DID {
&self.id
}

/// Set the did response's id.
pub fn set_id(&mut self, id: DID) {
self.id = id;
}
}
47 changes: 10 additions & 37 deletions identity-comm/src/message/message.rs
@@ -1,43 +1,16 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use did_doc::{url::Url, Document, Signature};

use identity_core::crypto::{KeyPair, PublicKey};
use identity_iota::did::DID;
use crate::envelope::Encrypted;
use crate::envelope::EncryptionAlgorithm;
use crate::envelope::Plaintext;
use crate::envelope::SignatureAlgorithm;
use crate::envelope::Signed;
use crate::error::Result;
use identity_core::crypto::KeyPair;
use identity_core::crypto::PublicKey;
use serde::Serialize;

use crate::{
envelope::{Encrypted, EncryptionAlgorithm, Plaintext, SignatureAlgorithm, Signed},
error::Result,
message::Timing
};

#[derive(Debug, Deserialize, Serialize, Default)]
pub struct CustomMessage {
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) callback_url: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) response_requested: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) context: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) id: Option<DID>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) did_document: Option<Document>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) thread: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) challenge: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) signature: Option<Signature>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) timing: Option<Timing>,
}
pub trait Message: Serialize {}


pub trait AsEnvelope {
pub trait Message {
fn pack_plain(&self) -> Result<Plaintext>;
fn pack_auth(&self, algorithm: EncryptionAlgorithm, recipients: &[PublicKey], sender: &KeyPair) -> Result<Encrypted>;
fn pack_auth_non_repudiable(
Expand All @@ -51,7 +24,7 @@ pub trait AsEnvelope {
fn pack_non_repudiable(&self, algorithm: SignatureAlgorithm, sender: &KeyPair) -> Result<Signed>;
}

impl<T: Serialize> AsEnvelope for T {
impl<T: Serialize> Message for T {
fn pack_plain(&self) -> Result<Plaintext> {
Plaintext::from_message(self)
}
Expand Down
9 changes: 4 additions & 5 deletions identity-comm/src/message/mod.rs
Expand Up @@ -2,12 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::module_inception)]
mod builder;
mod authentication;
mod discovery;
mod message;
mod resolution;
mod timing;
mod trustping;
mod discovery;
mod resolution;
mod authentication;

pub use self::{builder::*, message::*,timing::*,trustping::*,discovery::*,resolution::*,authentication::*};
pub use self::{authentication::*, discovery::*, message::*, resolution::*, timing::*, trustping::*};

0 comments on commit e63a4db

Please sign in to comment.