Skip to content

Commit

Permalink
chore: adjust function/method names, added no invitation padding pars…
Browse files Browse the repository at this point in the history
…ing test

Signed-off-by: James Ebert <jamesebert.k@gmail.com>
  • Loading branch information
JamesKEbert committed Aug 9, 2024
1 parent c979948 commit 042617d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 41 deletions.
2 changes: 1 addition & 1 deletion aries/agents/aries-vcx-agent/src/handlers/out_of_band.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<T: BaseWallet> ServiceOutOfBand<T> {
GenericOutOfBand::Sender(sender.to_owned()),
)?;

Ok(sender.to_aries_message())
Ok(sender.invitation_to_aries_message())
}

pub fn receive_invitation(&self, invitation: AriesMessage) -> AgentResult<String> {
Expand Down
85 changes: 55 additions & 30 deletions aries/aries_vcx/src/handlers/out_of_band/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,28 @@ impl OutOfBandReceiver {
}
}

pub fn from_json_string(oob_json: &str) -> VcxResult<Self> {
pub fn create_from_json_encoded_oob(oob_json: &str) -> VcxResult<Self> {
Ok(Self {
oob: from_json_string(oob_json)?,
oob: extract_encoded_invitation_from_json_string(oob_json)?,
})
}

pub fn from_base64_url(base64_url_encoded_oob: &str) -> VcxResult<Self> {
fn create_from_base64_url_encoded_oob(base64_url_encoded_oob: &str) -> VcxResult<Self> {
Ok(Self {
oob: from_json_string(&from_base64_url(base64_url_encoded_oob)?)?,
oob: extract_encoded_invitation_from_json_string(
&extract_encoded_invitation_from_base64_url(base64_url_encoded_oob)?,
)?,
})
}

pub fn from_url(oob_url_string: &str) -> VcxResult<Self> {
pub fn create_from_url_encoded_oob(oob_url_string: &str) -> VcxResult<Self> {
// TODO - URL Shortening
Ok(Self {
oob: from_json_string(&from_base64_url(&from_url(oob_url_string)?)?)?,
oob: extract_encoded_invitation_from_json_string(
&extract_encoded_invitation_from_base64_url(&extract_encoded_invitation_from_url(
oob_url_string,
)?)?,
)?,
})
}

Expand All @@ -80,37 +86,37 @@ impl OutOfBandReceiver {
}
}

pub fn to_aries_message(&self) -> AriesMessage {
pub fn invitation_to_aries_message(&self) -> AriesMessage {
self.oob.clone().into()
}

pub fn to_json_string(&self) -> String {
self.to_aries_message().to_string()
pub fn invitation_to_json_string(&self) -> String {
self.invitation_to_aries_message().to_string()
}

pub fn to_base64_url(&self) -> String {
BASE64_URL_SAFE.encode(self.to_json_string())
fn invitation_to_base64_url(&self) -> String {
BASE64_URL_SAFE.encode(self.invitation_to_json_string())
}

pub fn to_url(&self, domain_path: &str) -> VcxResult<Url> {
pub fn invitation_to_url(&self, domain_path: &str) -> VcxResult<Url> {
let mut oob_url = Url::parse(domain_path)?;
let oob_query = "oob=".to_owned() + &self.to_base64_url();
let oob_query = "oob=".to_owned() + &self.invitation_to_base64_url();
oob_url.set_query(Some(&oob_query));
Ok(oob_url)
}
}

fn from_json_string(oob_json: &str) -> VcxResult<Invitation> {
fn extract_encoded_invitation_from_json_string(oob_json: &str) -> VcxResult<Invitation> {
Ok(serde_json::from_str(oob_json)?)
}

fn from_base64_url(base64_url_encoded_oob: &str) -> VcxResult<String> {
fn extract_encoded_invitation_from_base64_url(base64_url_encoded_oob: &str) -> VcxResult<String> {
Ok(String::from_utf8(
URL_SAFE_LENIENT.decode(base64_url_encoded_oob)?,
)?)
}

fn from_url(oob_url_string: &str) -> VcxResult<String> {
fn extract_encoded_invitation_from_url(oob_url_string: &str) -> VcxResult<String> {
let oob_url = Url::parse(oob_url_string)?;
let (_oob_query, base64_url_encoded_oob) = oob_url
.query_pairs()
Expand Down Expand Up @@ -222,6 +228,7 @@ mod tests {
}"#;
const JSON_OOB_INVITE_NO_WHITESPACE: &str = r#"{"@type":"https://didcomm.org/out-of-band/1.1/invitation","@id":"69212a3a-d068-4f9d-a2dd-4741bca89af3","label":"Faber College","goal_code":"issue-vc","goal":"To issue a Faber College Graduate credential","handshake_protocols":["https://didcomm.org/didexchange/1.0","https://didcomm.org/connections/1.0"],"services":["did:sov:LjgpST2rjs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dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiI2OTIxMmEzYS1kMDY4LTRmOWQtYTJkZC00NzQxYmNhODlhZjMiLCJsYWJlbCI6IkZhYmVyIENvbGxlZ2UiLCJnb2FsX2NvZGUiOiJpc3N1ZS12YyIsImdvYWwiOiJUbyBpc3N1ZSBhIEZhYmVyIENvbGxlZ2UgR3JhZHVhdGUgY3JlZGVudGlhbCIsImhhbmRzaGFrZV9wcm90b2NvbHMiOlsiaHR0cHM6Ly9kaWRjb21tLm9yZy9kaWRleGNoYW5nZS8xLjAiLCJodHRwczovL2RpZGNvbW0ub3JnL2Nvbm5lY3Rpb25zLzEuMCJdLCJzZXJ2aWNlcyI6WyJkaWQ6c292OkxqZ3BTVDJyanNveFllZ1FEUm03RUwiXX0=";

// Params mimic example invitation in RFC 0434 - https://github.com/hyperledger/aries-rfcs/tree/main/features/0434-outofband
Expand Down Expand Up @@ -258,7 +265,7 @@ mod tests {
#[test]
fn receive_invitation_by_json() {
let base_invite = _create_invitation();
let parsed_invite = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE)
let parsed_invite = OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE)
.unwrap()
.oob;
assert_eq!(base_invite, parsed_invite);
Expand All @@ -267,52 +274,70 @@ mod tests {
#[test]
fn receive_invitation_by_json_no_whitespace() {
let base_invite = _create_invitation();
let parsed_invite = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE_NO_WHITESPACE)
.unwrap()
.oob;
let parsed_invite =
OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE_NO_WHITESPACE)
.unwrap()
.oob;
assert_eq!(base_invite, parsed_invite);
}

#[test]
fn receive_invitation_by_base64_url() {
let base_invite = _create_invitation();
let parsed_invite = OutOfBandReceiver::from_base64_url(OOB_BASE64_URL_ENCODED)
.unwrap()
.oob;
let parsed_invite =
OutOfBandReceiver::create_from_base64_url_encoded_oob(OOB_BASE64_URL_ENCODED)
.unwrap()
.oob;
assert_eq!(base_invite, parsed_invite);
}

#[test]
fn receive_invitation_by_base64_url_no_padding() {
let base_invite = _create_invitation();
let parsed_invite = OutOfBandReceiver::create_from_base64_url_encoded_oob(
OOB_BASE64_URL_ENCODED_NO_PADDING,
)
.unwrap()
.oob;
assert_eq!(base_invite, parsed_invite);
}

#[test]
fn receive_invitation_by_url() {
let base_invite = _create_invitation();
let parsed_invite = OutOfBandReceiver::from_url(OOB_URL).unwrap().oob;
let parsed_invite = OutOfBandReceiver::create_from_url_encoded_oob(OOB_URL)
.unwrap()
.oob;
assert_eq!(base_invite, parsed_invite);
}

#[test]
fn invitation_to_json() {
let out_of_band_receiver = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE).unwrap();
let out_of_band_receiver =
OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE).unwrap();

let json_invite = out_of_band_receiver.to_json_string();
let json_invite = out_of_band_receiver.invitation_to_json_string();

assert_eq!(JSON_OOB_INVITE_NO_WHITESPACE, json_invite);
}

#[test]
fn invitation_to_base64_url() {
let out_of_band_receiver = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE).unwrap();
let out_of_band_receiver =
OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE).unwrap();

let base64_url_invite = out_of_band_receiver.to_base64_url();
let base64_url_invite = out_of_band_receiver.invitation_to_base64_url();

assert_eq!(OOB_BASE64_URL_ENCODED, base64_url_invite);
}

#[test]
fn invitation_to_url() {
let out_of_band_receiver = OutOfBandReceiver::from_json_string(JSON_OOB_INVITE).unwrap();
let out_of_band_receiver =
OutOfBandReceiver::create_from_json_encoded_oob(JSON_OOB_INVITE).unwrap();

let oob_url = out_of_band_receiver
.to_url("http://example.com/ssi")
.invitation_to_url("http://example.com/ssi")
.unwrap()
.to_string();

Expand Down
20 changes: 10 additions & 10 deletions aries/aries_vcx/src/handlers/out_of_band/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,21 @@ impl OutOfBandSender {
Ok(self)
}

pub fn to_aries_message(&self) -> AriesMessage {
pub fn invitation_to_aries_message(&self) -> AriesMessage {
self.oob.clone().into()
}

pub fn to_json_string(&self) -> String {
self.to_aries_message().to_string()
pub fn invitation_to_json_string(&self) -> String {
self.invitation_to_aries_message().to_string()
}

pub fn to_base64_url(&self) -> String {
BASE64_URL_SAFE.encode(self.to_json_string())
fn invitation_to_base64_url(&self) -> String {
BASE64_URL_SAFE.encode(self.invitation_to_json_string())
}

pub fn to_url(&self, domain_path: &str) -> VcxResult<Url> {
pub fn invitation_to_url(&self, domain_path: &str) -> VcxResult<Url> {
let mut oob_url = Url::parse(domain_path)?;
let oob_query = "oob=".to_owned() + &self.to_base64_url();
let oob_query = "oob=".to_owned() + &self.invitation_to_base64_url();
oob_url.set_query(Some(&oob_query));
Ok(oob_url)
}
Expand Down Expand Up @@ -213,7 +213,7 @@ mod tests {
fn invitation_to_json() {
let out_of_band_sender = OutOfBandSender::create_from_invitation(_create_invitation());

let json_invite = out_of_band_sender.to_json_string();
let json_invite = out_of_band_sender.invitation_to_json_string();

assert_eq!(JSON_OOB_INVITE_NO_WHITESPACE, json_invite);
}
Expand All @@ -222,7 +222,7 @@ mod tests {
fn invitation_to_base64_url() {
let out_of_band_sender = OutOfBandSender::create_from_invitation(_create_invitation());

let base64_url_invite = out_of_band_sender.to_base64_url();
let base64_url_invite = out_of_band_sender.invitation_to_base64_url();

assert_eq!(OOB_BASE64_URL_ENCODED, base64_url_invite);
}
Expand All @@ -232,7 +232,7 @@ mod tests {
let out_of_band_sender = OutOfBandSender::create_from_invitation(_create_invitation());

let oob_url = out_of_band_sender
.to_url("http://example.com/ssi")
.invitation_to_url("http://example.com/ssi")
.unwrap()
.to_string();

Expand Down

0 comments on commit 042617d

Please sign in to comment.