Skip to content

Commit

Permalink
implement holder udl
Browse files Browse the repository at this point in the history
  • Loading branch information
swaptr committed Oct 18, 2023
1 parent 2e56921 commit 90b0318
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 65 deletions.
162 changes: 97 additions & 65 deletions uniffi_aries_vcx/core/src/handlers/holder.rs
Original file line number Diff line number Diff line change
@@ -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<VcxHolder>,
}

// initializers
#[derive(Debug, PartialEq, Eq)]
enum HolderState {
Initial,
ProposalSet,
OfferReceived,
RequestSet,
Finished,
Failed,
}

impl From<VcxHolderState> 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<Arc<Holder>> {
block_on(async {
Expand All @@ -31,20 +39,20 @@ pub fn create(source_id: String) -> VcxUniFFIResult<Arc<Holder>> {
})
}

pub fn create_from_offer(
source_id: String,
cred: OfferCredentialV1,
) -> VcxUniFFIResult<Arc<Holder>> {
pub fn create_from_offer(source_id: String, offer_message: String) -> VcxUniFFIResult<Arc<Holder>> {
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<Arc<Holder>> {
let propose_credential = serde_json::from_str(&propose_credential)?;

block_on(async {
let handler = Mutex::new(VcxHolder::create_with_proposal(
&source_id,
Expand All @@ -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<RequestCredentialV1> {
Ok(self.handler.lock()?.clone().get_msg_credential_request()?)
pub fn get_msg_credential_request(&self) -> VcxUniFFIResult<String> {
Ok(serde_json::to_string(
&self.handler.lock()?.clone().get_msg_credential_request()?,
)?)
}

pub fn decline_offer(&self) -> VcxUniFFIResult<ProblemReport> {
Ok(self
.handler
.lock()?
.clone()
.decline_offer(Some(&String::new()))?)
pub fn decline_offer(&self) -> VcxUniFFIResult<String> {
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?)
})
}

Expand All @@ -110,15 +125,16 @@ impl Holder {
}

pub fn get_state(&self) -> VcxUniFFIResult<HolderState> {
Ok(self.handler.lock()?.get_state())
Ok(HolderState::from(self.handler.lock()?.get_state()))
}

pub fn get_source_id(&self) -> VcxUniFFIResult<String> {
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<String> {
let credential = self.handler.lock()?.get_credential()?;
Ok(credential.0)
}

pub fn get_attributes(&self) -> VcxUniFFIResult<String> {
Expand All @@ -129,8 +145,8 @@ impl Holder {
Ok(self.handler.lock()?.get_attachment()?)
}

pub fn get_offer(&self) -> VcxUniFFIResult<OfferCredentialV1> {
Ok(self.handler.lock()?.get_offer()?)
pub fn get_offer(&self) -> VcxUniFFIResult<String> {
Ok(serde_json::to_string(&(self.handler.lock()?.get_offer()?))?)
}

pub fn get_tails_location(&self) -> VcxUniFFIResult<String> {
Expand All @@ -153,27 +169,43 @@ impl Holder {
Ok(self.handler.lock()?.get_thread_id()?)
}

pub async fn is_revokable(&self, ledger: &impl AnoncredsLedgerRead) -> VcxUniFFIResult<bool> {
block_on(async { Ok(self.handler.lock()?.is_revokable(ledger).await?) })
pub async fn is_revokable(&self, profile: ProfileHolder) -> VcxUniFFIResult<bool> {
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<bool> {
Ok(self.handler.lock()?.is_revoked(ledger, anoncreds).await?)
pub async fn is_revoked(&self, profile: ProfileHolder) -> VcxUniFFIResult<bool> {
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<String> {
block_on(async { Ok(self.handler.lock()?.get_cred_rev_id(anoncreds).await?) })
pub fn get_cred_rev_id(&self, profile: ProfileHolder) -> VcxUniFFIResult<String> {
block_on(async {
Ok(self
.handler
.lock()?
.get_cred_rev_id(profile.inner.anoncreds())
.await?)
})
}

pub fn get_problem_report(&self) -> VcxUniFFIResult<ProblemReport> {
Ok(self.handler.lock()?.get_problem_report()?)
pub fn get_problem_report(&self) -> VcxUniFFIResult<String> {
Ok(serde_json::to_string(
&self.handler.lock()?.get_problem_report()?,
)?)
}

pub fn get_final_message(&self) -> VcxUniFFIResult<Option<AriesMessage>> {
Ok(self.handler.lock()?.get_final_message()?)
pub fn get_final_message(&self) -> VcxUniFFIResult<Option<String>> {
Ok(Some(serde_json::to_string(
&self.handler.lock()?.get_final_message()?,
)?))
}
}
86 changes: 86 additions & 0 deletions uniffi_aries_vcx/core/src/vcx.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
};

0 comments on commit 90b0318

Please sign in to comment.