Skip to content

Commit 1b11be2

Browse files
authored
Use correct prefix on created @type on aries messages. (#341)
* Use correct prefix on created @type on aries messages. Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Update @type in tests and mocks Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Fix nodejs wrapper test Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Agency forward messages should use legacy @type, temporarily Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Add test for @forward message type serialization Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Fix nodejs integration tests Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Bump patch version Signed-off-by: Patrik Stas <patrik.stas@absa.africa> * Fix unit tests Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
1 parent 6cec727 commit 1b11be2

File tree

16 files changed

+288
-81
lines changed

16 files changed

+288
-81
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agents/node/vcxagent-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@
6767
"winston": "^3.3.3"
6868
},
6969
"peerDependencies": {
70-
"@hyperledger/node-vcx-wrapper": "^0.20.0"
70+
"@hyperledger/node-vcx-wrapper": "^0.20.1"
7171
}
7272
}

agents/node/vcxagent-core/test/feature-discovery.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ describe('send ping, get ping', () => {
2626
expect(aliceMessages1.length).toBe(1)
2727
expect(JSON.parse(aliceMessages1[0].decryptedMsg)["@type"].match(/discover-features\/1.0\/disclose/))
2828
const disclosedProtocols = JSON.parse(aliceMessages1[0].decryptedMsg)["protocols"].map(r => r.pid)
29-
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0")
30-
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0")
31-
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/report-problem/1.0")
32-
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/present-proof/1.0")
33-
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/trust_ping/1.0")
34-
expect(disclosedProtocols).toContain("did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/discover-features/1.0")
35-
expect(disclosedProtocols).toContain( "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0")
29+
expect(disclosedProtocols).toContain("https://didcomm.org/connections/1.0")
30+
expect(disclosedProtocols).toContain("https://didcomm.org/issue-credential/1.0")
31+
expect(disclosedProtocols).toContain("https://didcomm.org/report-problem/1.0")
32+
expect(disclosedProtocols).toContain("https://didcomm.org/present-proof/1.0")
33+
expect(disclosedProtocols).toContain("https://didcomm.org/trust_ping/1.0")
34+
expect(disclosedProtocols).toContain("https://didcomm.org/discover-features/1.0")
35+
expect(disclosedProtocols).toContain( "https://didcomm.org/basicmessage/1.0")
3636

3737
await alice.updateConnection(4)
3838
const aliceMessages2 = await alice.downloadReceivedMessagesV2()

aries_vcx/src/messages/a2a/message_family.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub enum MessageFamilies {
1616
}
1717

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

2122
pub fn version(&self) -> &'static str {
2223
match self {
@@ -35,7 +36,7 @@ impl MessageFamilies {
3536
}
3637

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

4142
pub fn actors(&self) -> Option<(Actors, Actors)> {
Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1+
use regex::{Match, Regex};
12
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
23
use serde_json::Value;
34

4-
use agency_client::message_type::parse_message_type;
5+
use crate::error::{VcxError, VcxErrorKind, VcxResult};
56
use crate::messages::a2a::message_family::MessageFamilies;
67

78
#[derive(Debug, Clone, PartialEq, Default)]
89
pub struct MessageType {
9-
pub did: String,
10+
pub prefix: String,
1011
pub family: MessageFamilies,
1112
pub version: String,
12-
pub type_: String,
13+
pub msg_type: String,
1314
}
1415

1516
impl MessageType {
1617
pub fn build(family: MessageFamilies, name: &str) -> MessageType {
1718
MessageType {
18-
did: MessageFamilies::DID.to_string(),
19+
prefix: MessageFamilies::ARIES_CORE_PREFIX.to_string(),
1920
version: family.version().to_string(),
2021
family,
21-
type_: name.to_string(),
22+
msg_type: name.to_string(),
2223
}
2324
}
2425
}
2526

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

3131
match value.as_str() {
3232
Some(type_) => {
33-
let (did, family, version, type_) = parse_message_type(type_).map_err(de::Error::custom)?;
34-
Ok(MessageType {
35-
did,
36-
family: MessageFamilies::from(family),
37-
version,
38-
type_,
39-
})
33+
if let Some(msg_type) = parse_message_type_legacy(type_) {
34+
Ok(msg_type)
35+
} else if let Some(msg_type) = parse_message_type(type_) {
36+
Ok(msg_type)
37+
} else {
38+
Err(de::Error::custom("Unexpected @type field structure."))
39+
}
4040
}
4141
_ => Err(de::Error::custom("Unexpected @type field structure."))
4242
}
@@ -52,6 +52,77 @@ impl Serialize for MessageType {
5252

5353
impl std::string::ToString for MessageType {
5454
fn to_string(&self) -> String {
55-
format!("{};spec/{}/{}/{}", self.did, self.family.to_string(), self.version, self.type_)
55+
if self.family == MessageFamilies::Routing { // vcxagency-node only supports legacy format right now
56+
format!("{};spec/{}/{}/{}", MessageFamilies::DID_PREFIX.to_string(), self.family.to_string(), self.version, self.msg_type)
57+
} else {
58+
format!("{}/{}/{}/{}", self.prefix, self.family.to_string(), self.version, self.msg_type)
59+
}
60+
}
61+
}
62+
63+
64+
pub fn parse_message_type_legacy(message_type: &str) -> Option<MessageType> {
65+
lazy_static! {
66+
static ref RE: Regex = Regex::new(r"(?x)
67+
(?P<did>[\d\w:]*);
68+
(?P<spec>.*)/
69+
(?P<family>.*)/
70+
(?P<version>.*)/
71+
(?P<type>.*)").unwrap();
5672
}
73+
74+
RE.captures(message_type)
75+
.and_then(|cap| {
76+
let did = cap.name("did").as_ref().map(Match::as_str);
77+
let family = cap.name("family").as_ref().map(Match::as_str);
78+
let version = cap.name("version").as_ref().map(Match::as_str);
79+
let msg_type = cap.name("type").as_ref().map(Match::as_str);
80+
81+
match (did, family, version, msg_type) {
82+
(Some(did), Some(family), Some(version), Some(msg_type)) => {
83+
Some(MessageType {
84+
prefix: did.to_string(),
85+
family: MessageFamilies::from(family.to_string()),
86+
version: version.to_string(),
87+
msg_type: msg_type.to_string(),
88+
})
89+
}
90+
_ => {
91+
panic!("Message type regex captures, but failed to map it onto MessageType structure.")
92+
}
93+
}
94+
})
95+
}
96+
97+
pub fn parse_message_type(message_type: &str) -> Option<MessageType> {
98+
lazy_static! {
99+
static ref RE: Regex = Regex::new(r"(?x)
100+
(?P<prefix>.+)/ # https://didcomm.org/
101+
(?P<family>.+)/ # connections/
102+
(?P<version>.+)/ # 1.0/
103+
(?P<type>.+) # request
104+
").unwrap();
105+
}
106+
107+
RE.captures(message_type)
108+
.and_then(|cap| {
109+
let prefix = cap.name("prefix").as_ref().map(Match::as_str);
110+
let family = cap.name("family").as_ref().map(Match::as_str);
111+
let version = cap.name("version").as_ref().map(Match::as_str);
112+
let msg_type = cap.name("type").as_ref().map(Match::as_str);
113+
114+
match (prefix, family, version, msg_type) {
115+
(Some(prefix), Some(family), Some(version), Some(msg_type)) => {
116+
Some(MessageType {
117+
prefix: prefix.to_string(),
118+
family: MessageFamilies::from(family.to_string()),
119+
version: version.to_string(),
120+
msg_type: msg_type.to_string(),
121+
})
122+
}
123+
_ => {
124+
panic!("Message type regex captures, but failed to map it onto MessageType structure.")
125+
}
126+
}
127+
})
57128
}

0 commit comments

Comments
 (0)