From 90b03184c5e95c7c6bf2cee361d5c53e58927162 Mon Sep 17 00:00:00 2001 From: Swapnil Tripathi Date: Tue, 17 Oct 2023 11:09:58 +0530 Subject: [PATCH] implement holder udl --- uniffi_aries_vcx/core/src/handlers/holder.rs | 162 +++++++++++-------- uniffi_aries_vcx/core/src/vcx.udl | 86 ++++++++++ 2 files changed, 183 insertions(+), 65 deletions(-) diff --git a/uniffi_aries_vcx/core/src/handlers/holder.rs b/uniffi_aries_vcx/core/src/handlers/holder.rs index db65f3f337..0fdc4c6df5 100644 --- a/uniffi_aries_vcx/core/src/handlers/holder.rs +++ b/uniffi_aries_vcx/core/src/handlers/holder.rs @@ -1,28 +1,36 @@ use aries_vcx::{ - aries_vcx_core::{ - anoncreds::base_anoncreds::BaseAnonCreds, ledger::base_ledger::AnoncredsLedgerRead, - }, - handlers::issuance::holder::Holder as VcxHolder, - messages::{ - msg_fields::protocols::{ - cred_issuance::v1::{ - issue_credential::IssueCredentialV1, offer_credential::OfferCredentialV1, - propose_credential::ProposeCredentialV1, request_credential::RequestCredentialV1, - }, - report_problem::ProblemReport, - }, - AriesMessage, - }, - protocols::issuance::holder::state_machine::HolderState, + core::profile::profile::Profile, handlers::issuance::holder::Holder as VcxHolder, + protocols::issuance::holder::state_machine::HolderState as VcxHolderState, }; use std::sync::{Arc, Mutex}; -use crate::{errors::error::VcxUniFFIResult, runtime::block_on}; +use crate::{core::profile::ProfileHolder, errors::error::VcxUniFFIResult, runtime::block_on}; pub struct Holder { handler: Mutex, } -// initializers +#[derive(Debug, PartialEq, Eq)] +enum HolderState { + Initial, + ProposalSet, + OfferReceived, + RequestSet, + Finished, + Failed, +} + +impl From for HolderState { + fn from(x: VcxHolderState) -> Self { + match x { + VcxHolderState::Initial => HolderState::Initial, + VcxHolderState::ProposalSet => HolderState::ProposalSet, + VcxHolderState::OfferReceived => HolderState::OfferReceived, + VcxHolderState::RequestSet => HolderState::RequestSet, + VcxHolderState::Finished => HolderState::Finished, + VcxHolderState::Failed => HolderState::Failed, + } + } +} pub fn create(source_id: String) -> VcxUniFFIResult> { block_on(async { @@ -31,20 +39,20 @@ pub fn create(source_id: String) -> VcxUniFFIResult> { }) } -pub fn create_from_offer( - source_id: String, - cred: OfferCredentialV1, -) -> VcxUniFFIResult> { +pub fn create_from_offer(source_id: String, offer_message: String) -> VcxUniFFIResult> { + let offer_message = serde_json::from_str(&offer_message)?; block_on(async { - let handler = Mutex::new(VcxHolder::create_from_offer(&source_id, cred)?); + let handler = Mutex::new(VcxHolder::create_from_offer(&source_id, offer_message)?); Ok(Arc::new(Holder { handler })) }) } pub fn create_with_proposal( source_id: String, - propose_credential: ProposeCredentialV1, + propose_credential: String, ) -> VcxUniFFIResult> { + let propose_credential = serde_json::from_str(&propose_credential)?; + block_on(async { let handler = Mutex::new(VcxHolder::create_with_proposal( &source_id, @@ -55,53 +63,60 @@ pub fn create_with_proposal( } impl Holder { - pub fn set_proposal( - &mut self, - credential_proposal: ProposeCredentialV1, - ) -> VcxUniFFIResult<()> { - self.handler.lock()?.set_proposal(credential_proposal)?; + pub fn set_proposal(&mut self, credential_proposal: String) -> VcxUniFFIResult<()> { + self.handler + .lock()? + .set_proposal(serde_json::from_str(&credential_proposal)?)?; Ok(()) } pub fn prepare_credential_request( &mut self, - ledger: &impl AnoncredsLedgerRead, - anoncreds: &impl BaseAnonCreds, + profile: ProfileHolder, my_pw_did: String, ) -> VcxUniFFIResult<()> { block_on(async { self.handler .lock()? - .prepare_credential_request(ledger, anoncreds, my_pw_did) + .prepare_credential_request( + profile.inner.ledger_read(), + profile.inner.anoncreds(), + my_pw_did, + ) .await?; Ok(()) }) } - pub fn get_msg_credential_request(&self) -> VcxUniFFIResult { - Ok(self.handler.lock()?.clone().get_msg_credential_request()?) + pub fn get_msg_credential_request(&self) -> VcxUniFFIResult { + Ok(serde_json::to_string( + &self.handler.lock()?.clone().get_msg_credential_request()?, + )?) } - pub fn decline_offer(&self) -> VcxUniFFIResult { - Ok(self - .handler - .lock()? - .clone() - .decline_offer(Some(&String::new()))?) + pub fn decline_offer(&self) -> VcxUniFFIResult { + Ok(serde_json::to_string( + &self.handler.lock()?.clone().decline_offer(Some(""))?, + )?) } pub async fn process_credential( &mut self, - ledger: &impl AnoncredsLedgerRead, - anoncreds: &impl BaseAnonCreds, - credential: IssueCredentialV1, + profile: ProfileHolder, + credential: String, ) -> VcxUniFFIResult<()> { + let credential = serde_json::from_str(&credential)?; + block_on(async { - self.handler + Ok(self + .handler .lock()? - .process_credential(ledger, anoncreds, credential) - .await?; - Ok(()) + .process_credential( + profile.inner.ledger_read(), + profile.inner.anoncreds(), + credential, + ) + .await?) }) } @@ -110,15 +125,16 @@ impl Holder { } pub fn get_state(&self) -> VcxUniFFIResult { - Ok(self.handler.lock()?.get_state()) + Ok(HolderState::from(self.handler.lock()?.get_state())) } pub fn get_source_id(&self) -> VcxUniFFIResult { Ok(self.handler.lock()?.get_source_id()) } - pub fn get_credential(&self) -> VcxUniFFIResult<(String, AriesMessage)> { - Ok(self.handler.lock()?.get_credential()?) + pub fn get_credential(&self) -> VcxUniFFIResult { + let credential = self.handler.lock()?.get_credential()?; + Ok(credential.0) } pub fn get_attributes(&self) -> VcxUniFFIResult { @@ -129,8 +145,8 @@ impl Holder { Ok(self.handler.lock()?.get_attachment()?) } - pub fn get_offer(&self) -> VcxUniFFIResult { - Ok(self.handler.lock()?.get_offer()?) + pub fn get_offer(&self) -> VcxUniFFIResult { + Ok(serde_json::to_string(&(self.handler.lock()?.get_offer()?))?) } pub fn get_tails_location(&self) -> VcxUniFFIResult { @@ -153,27 +169,43 @@ impl Holder { Ok(self.handler.lock()?.get_thread_id()?) } - pub async fn is_revokable(&self, ledger: &impl AnoncredsLedgerRead) -> VcxUniFFIResult { - block_on(async { Ok(self.handler.lock()?.is_revokable(ledger).await?) }) + pub async fn is_revokable(&self, profile: ProfileHolder) -> VcxUniFFIResult { + block_on(async { + Ok(self + .handler + .lock()? + .is_revokable(profile.inner.ledger_read()) + .await?) + }) } - pub async fn is_revoked( - &self, - ledger: &impl AnoncredsLedgerRead, - anoncreds: &impl BaseAnonCreds, - ) -> VcxUniFFIResult { - Ok(self.handler.lock()?.is_revoked(ledger, anoncreds).await?) + pub async fn is_revoked(&self, profile: ProfileHolder) -> VcxUniFFIResult { + Ok(self + .handler + .lock()? + .is_revoked(profile.inner.ledger_read(), profile.inner.anoncreds()) + .await?) } - pub fn get_cred_rev_id(&self, anoncreds: &impl BaseAnonCreds) -> VcxUniFFIResult { - block_on(async { Ok(self.handler.lock()?.get_cred_rev_id(anoncreds).await?) }) + pub fn get_cred_rev_id(&self, profile: ProfileHolder) -> VcxUniFFIResult { + block_on(async { + Ok(self + .handler + .lock()? + .get_cred_rev_id(profile.inner.anoncreds()) + .await?) + }) } - pub fn get_problem_report(&self) -> VcxUniFFIResult { - Ok(self.handler.lock()?.get_problem_report()?) + pub fn get_problem_report(&self) -> VcxUniFFIResult { + Ok(serde_json::to_string( + &self.handler.lock()?.get_problem_report()?, + )?) } - pub fn get_final_message(&self) -> VcxUniFFIResult> { - Ok(self.handler.lock()?.get_final_message()?) + pub fn get_final_message(&self) -> VcxUniFFIResult> { + Ok(Some(serde_json::to_string( + &self.handler.lock()?.get_final_message()?, + )?)) } } diff --git a/uniffi_aries_vcx/core/src/vcx.udl b/uniffi_aries_vcx/core/src/vcx.udl index 412455643e..bc8ac6179e 100644 --- a/uniffi_aries_vcx/core/src/vcx.udl +++ b/uniffi_aries_vcx/core/src/vcx.udl @@ -68,6 +68,83 @@ interface Connection { void send_ack(ProfileHolder profile); }; +interface Holder { + [Throws=VcxUniFFIError] + void set_proposal(string credential_proposal); + + [Throws=VcxUniFFIError] + void prepare_credential_request(ProfileHolder profile, string my_pw_did); + + [Throws=VcxUniFFIError] + string get_msg_credential_request(); + + [Throws=VcxUniFFIError] + string decline_offer(); + + [Throws=VcxUniFFIError] + void process_credential(ProfileHolder profile, string credential); + + [Throws=VcxUniFFIError] + boolean is_terminal_state(); + + [Throws=VcxUniFFIError] + HolderState get_state(); + + [Throws=VcxUniFFIError] + string get_source_id(); + + [Throws=VcxUniFFIError] + string get_credential(); + + [Throws=VcxUniFFIError] + string get_attributes(); + + [Throws=VcxUniFFIError] + string get_attachment(); + + [Throws=VcxUniFFIError] + string get_offer(); + + [Throws=VcxUniFFIError] + string get_tails_location(); + + [Throws=VcxUniFFIError] + string get_tails_hash(); + + [Throws=VcxUniFFIError] + string get_rev_reg_id(); + + [Throws=VcxUniFFIError] + string get_cred_id(); + + [Throws=VcxUniFFIError] + string get_thread_id(); + + [Throws=VcxUniFFIError] + boolean is_revokable(ProfileHolder profile); + + [Throws=VcxUniFFIError] + boolean is_revoked(ProfileHolder profile); + + [Throws=VcxUniFFIError] + string get_cred_rev_id(ProfileHolder profile); + + [Throws=VcxUniFFIError] + string get_problem_report(); + + [Throws=VcxUniFFIError] + string? get_final_message(); +}; + +enum HolderState { + "Initial", + "ProposalSet", + "OfferReceived", + "RequestSet", + "Finished", + "Failed" +}; + [Error] enum VcxUniFFIError { "AriesVcxError", @@ -88,4 +165,13 @@ namespace vcx { [Throws=VcxUniFFIError] UnpackMessage unpack_message(ProfileHolder profile, string packed_msg); + + [Throws=VcxUniFFIError] + Holder create(string source_id); + + [Throws=VcxUniFFIError] + Holder create_from_offer(string source_id, string offer_message); + + [Throws=VcxUniFFIError] + Holder create_with_proposal(string source_id, string propose_credential); };