Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct prefix on created @type on aries messages. #341

Merged
merged 8 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agents/node/vcxagent-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@
"winston": "^3.3.3"
},
"peerDependencies": {
"@hyperledger/node-vcx-wrapper": "^0.20.0"
"@hyperledger/node-vcx-wrapper": "^0.20.1"
}
}
14 changes: 7 additions & 7 deletions agents/node/vcxagent-core/test/feature-discovery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ describe('send ping, get ping', () => {
expect(aliceMessages1.length).toBe(1)
expect(JSON.parse(aliceMessages1[0].decryptedMsg)["@type"].match(/discover-features\/1.0\/disclose/))
const disclosedProtocols = JSON.parse(aliceMessages1[0].decryptedMsg)["protocols"].map(r => r.pid)
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0")
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0")
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/report-problem/1.0")
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/present-proof/1.0")
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/trust_ping/1.0")
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/discover-features/1.0")
expect(disclosedProtocols).toContain( "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0")
expect(disclosedProtocols).toContain("https://didcomm.org/connections/1.0")
expect(disclosedProtocols).toContain("https://didcomm.org/issue-credential/1.0")
expect(disclosedProtocols).toContain("https://didcomm.org/report-problem/1.0")
expect(disclosedProtocols).toContain("https://didcomm.org/present-proof/1.0")
expect(disclosedProtocols).toContain("https://didcomm.org/trust_ping/1.0")
expect(disclosedProtocols).toContain("https://didcomm.org/discover-features/1.0")
expect(disclosedProtocols).toContain( "https://didcomm.org/basicmessage/1.0")

await alice.updateConnection(4)
const aliceMessages2 = await alice.downloadReceivedMessagesV2()
Expand Down
5 changes: 3 additions & 2 deletions aries_vcx/src/messages/a2a/message_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub enum MessageFamilies {
}

impl MessageFamilies {
pub const DID: &'static str = "did:sov:BzCbsNYhMrjHiqZDTUASHg";
pub const ARIES_CORE_PREFIX: &'static str = "https://didcomm.org";
pub const DID_PREFIX: &'static str = "did:sov:BzCbsNYhMrjHiqZDTUASHg";

pub fn version(&self) -> &'static str {
match self {
Expand All @@ -35,7 +36,7 @@ impl MessageFamilies {
}

pub fn id(&self) -> String {
format!("{};spec/{}/{}", Self::DID, self.to_string(), self.version().to_string())
format!("{}/{}/{}", Self::ARIES_CORE_PREFIX, self.to_string(), self.version().to_string())
}

pub fn actors(&self) -> Option<(Actors, Actors)> {
Expand Down
99 changes: 85 additions & 14 deletions aries_vcx/src/messages/a2a/message_type.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
use regex::{Match, Regex};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;

use agency_client::message_type::parse_message_type;
use crate::error::{VcxError, VcxErrorKind, VcxResult};
use crate::messages::a2a::message_family::MessageFamilies;

#[derive(Debug, Clone, PartialEq, Default)]
pub struct MessageType {
pub did: String,
pub prefix: String,
pub family: MessageFamilies,
pub version: String,
pub type_: String,
pub msg_type: String,
}

impl MessageType {
pub fn build(family: MessageFamilies, name: &str) -> MessageType {
MessageType {
did: MessageFamilies::DID.to_string(),
prefix: MessageFamilies::ARIES_CORE_PREFIX.to_string(),
version: family.version().to_string(),
family,
type_: name.to_string(),
msg_type: name.to_string(),
}
}
}


impl<'de> Deserialize<'de> for MessageType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
let value = Value::deserialize(deserializer).map_err(de::Error::custom)?;

match value.as_str() {
Some(type_) => {
let (did, family, version, type_) = parse_message_type(type_).map_err(de::Error::custom)?;
Ok(MessageType {
did,
family: MessageFamilies::from(family),
version,
type_,
})
if let Some(msg_type) = parse_message_type_legacy(type_) {
Ok(msg_type)
} else if let Some(msg_type) = parse_message_type(type_) {
Ok(msg_type)
} else {
Err(de::Error::custom("Unexpected @type field structure."))
}
}
_ => Err(de::Error::custom("Unexpected @type field structure."))
}
Expand All @@ -52,6 +52,77 @@ impl Serialize for MessageType {

impl std::string::ToString for MessageType {
fn to_string(&self) -> String {
format!("{};spec/{}/{}/{}", self.did, self.family.to_string(), self.version, self.type_)
if self.family == MessageFamilies::Routing { // vcxagency-node only supports legacy format right now
format!("{};spec/{}/{}/{}", MessageFamilies::DID_PREFIX.to_string(), self.family.to_string(), self.version, self.msg_type)
} else {
format!("{}/{}/{}/{}", self.prefix, self.family.to_string(), self.version, self.msg_type)
}
}
}


pub fn parse_message_type_legacy(message_type: &str) -> Option<MessageType> {
lazy_static! {
static ref RE: Regex = Regex::new(r"(?x)
(?P<did>[\d\w:]*);
(?P<spec>.*)/
(?P<family>.*)/
(?P<version>.*)/
(?P<type>.*)").unwrap();
}

RE.captures(message_type)
.and_then(|cap| {
let did = cap.name("did").as_ref().map(Match::as_str);
let family = cap.name("family").as_ref().map(Match::as_str);
let version = cap.name("version").as_ref().map(Match::as_str);
let msg_type = cap.name("type").as_ref().map(Match::as_str);

match (did, family, version, msg_type) {
(Some(did), Some(family), Some(version), Some(msg_type)) => {
Some(MessageType {
prefix: did.to_string(),
family: MessageFamilies::from(family.to_string()),
version: version.to_string(),
msg_type: msg_type.to_string(),
})
}
_ => {
panic!("Message type regex captures, but failed to map it onto MessageType structure.")
}
}
})
}

pub fn parse_message_type(message_type: &str) -> Option<MessageType> {
lazy_static! {
static ref RE: Regex = Regex::new(r"(?x)
(?P<prefix>.+)/ # https://didcomm.org/
(?P<family>.+)/ # connections/
(?P<version>.+)/ # 1.0/
(?P<type>.+) # request
").unwrap();
}

RE.captures(message_type)
.and_then(|cap| {
let prefix = cap.name("prefix").as_ref().map(Match::as_str);
let family = cap.name("family").as_ref().map(Match::as_str);
let version = cap.name("version").as_ref().map(Match::as_str);
let msg_type = cap.name("type").as_ref().map(Match::as_str);

match (prefix, family, version, msg_type) {
(Some(prefix), Some(family), Some(version), Some(msg_type)) => {
Some(MessageType {
prefix: prefix.to_string(),
family: MessageFamilies::from(family.to_string()),
version: version.to_string(),
msg_type: msg_type.to_string(),
})
}
_ => {
panic!("Message type regex captures, but failed to map it onto MessageType structure.")
}
}
})
}
Loading