generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add APID VC implementations for sign & verify, add JSON serialization…
… for VC at UniFFI layer (#251)
- Loading branch information
1 parent
1155fed
commit cfe6f16
Showing
10 changed files
with
619 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 91 additions & 15 deletions
106
bindings/web5_uniffi_wrapper/src/credentials/verifiable_credential_1_1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,111 @@ | ||
use crate::{ | ||
dids::bearer_did::BearerDid, | ||
dsa::{Signer, Verifier}, | ||
errors::Result, | ||
errors::{Result, RustCoreError}, | ||
}; | ||
use std::sync::Arc; | ||
use std::sync::{Arc, RwLock}; | ||
use web5::apid::credentials::verifiable_credential_1_1::VerifiableCredential as InnerVerifiableCredential; | ||
|
||
pub struct VerifiableCredential(pub InnerVerifiableCredential); | ||
pub struct VerifiableCredential(pub Arc<RwLock<InnerVerifiableCredential>>); | ||
|
||
impl VerifiableCredential { | ||
pub fn new(verifiable_credential: InnerVerifiableCredential) -> Self { | ||
Self(verifiable_credential) | ||
pub fn new(verifiable_credential: data::VerifiableCredential) -> Result<Self> { | ||
let inner_verifiable_credential = verifiable_credential.to_inner()?; | ||
|
||
Ok(Self(Arc::new(RwLock::new(inner_verifiable_credential)))) | ||
} | ||
|
||
pub fn verify(vcjwt: &str) -> Result<Self> { | ||
let vc = InnerVerifiableCredential::verify(vcjwt).map_err(|e| Arc::new(e.into()))?; | ||
Ok(Self(vc)) | ||
let inner_verifiable_credential = | ||
InnerVerifiableCredential::verify(vcjwt).map_err(|e| Arc::new(e.into()))?; | ||
|
||
Ok(Self(Arc::new(RwLock::new(inner_verifiable_credential)))) | ||
} | ||
|
||
pub fn verify_with_verifier(vcjwt: &str, verifier: Arc<dyn Verifier>) -> Result<Self> { | ||
let vc = InnerVerifiableCredential::verify_with_verifier(vcjwt, verifier.to_inner()) | ||
.map_err(|e| Arc::new(e.into()))?; | ||
Ok(Self(vc)) | ||
let inner_verifiable_credential = | ||
InnerVerifiableCredential::verify_with_verifier(vcjwt, verifier.to_inner()) | ||
.map_err(|e| Arc::new(e.into()))?; | ||
|
||
Ok(Self(Arc::new(RwLock::new(inner_verifiable_credential)))) | ||
} | ||
|
||
pub fn sign(&self, bearer_did: Arc<BearerDid>) -> Result<String> { | ||
let inner_verifiable_credential = self | ||
.0 | ||
.read() | ||
.map_err(|e| RustCoreError::from_poison_error(e, "RwLockReadError"))?; | ||
|
||
inner_verifiable_credential | ||
.sign(&bearer_did.0) | ||
.map_err(|e| Arc::new(e.into())) | ||
} | ||
|
||
pub fn sign(&self, signer: Arc<dyn Signer>) -> Result<String> { | ||
self.0 | ||
.sign(signer.to_inner()) | ||
pub fn sign_with_signer(&self, key_id: &str, signer: Arc<dyn Signer>) -> Result<String> { | ||
let inner_verifiable_credential = self | ||
.0 | ||
.read() | ||
.map_err(|e| RustCoreError::from_poison_error(e, "RwLockReadError"))?; | ||
|
||
inner_verifiable_credential | ||
.sign_with_signer(key_id, signer.to_inner()) | ||
.map_err(|e| Arc::new(e.into())) | ||
} | ||
|
||
pub fn get_data(&self) -> InnerVerifiableCredential { | ||
self.0.clone() | ||
pub fn get_data(&self) -> Result<data::VerifiableCredential> { | ||
let inner_verifiable_credential = self | ||
.0 | ||
.read() | ||
.map_err(|e| RustCoreError::from_poison_error(e, "RwLockReadError"))?; | ||
|
||
data::VerifiableCredential::from_inner(inner_verifiable_credential.clone()) | ||
} | ||
} | ||
|
||
pub mod data { | ||
use super::*; | ||
use std::time::SystemTime; | ||
|
||
#[derive(Clone)] | ||
pub struct VerifiableCredential { | ||
pub context: Vec<String>, | ||
pub id: String, | ||
pub r#type: Vec<String>, | ||
pub json_serialized_issuer: String, // JSON serialized | ||
pub issuance_date: SystemTime, | ||
pub expiration_date: Option<SystemTime>, | ||
pub json_serialized_credential_subject: String, // JSON serialized | ||
} | ||
|
||
impl VerifiableCredential { | ||
pub fn from_inner(inner_verifiable_credential: InnerVerifiableCredential) -> Result<Self> { | ||
Ok(Self { | ||
context: inner_verifiable_credential.context.clone(), | ||
id: inner_verifiable_credential.id.clone(), | ||
r#type: inner_verifiable_credential.r#type.clone(), | ||
json_serialized_issuer: serde_json::to_string(&inner_verifiable_credential.issuer) | ||
.map_err(|e| Arc::new(e.into()))?, | ||
issuance_date: inner_verifiable_credential.issuance_date, | ||
expiration_date: inner_verifiable_credential.expiration_date, | ||
json_serialized_credential_subject: serde_json::to_string( | ||
&inner_verifiable_credential.credential_subject, | ||
) | ||
.map_err(|e| Arc::new(e.into()))?, | ||
}) | ||
} | ||
|
||
pub fn to_inner(&self) -> Result<InnerVerifiableCredential> { | ||
Ok(InnerVerifiableCredential { | ||
context: self.context.clone(), | ||
id: self.id.clone(), | ||
r#type: self.r#type.clone(), | ||
issuer: serde_json::from_str(&self.json_serialized_issuer) | ||
.map_err(|e| Arc::new(e.into()))?, | ||
issuance_date: self.issuance_date, | ||
expiration_date: self.expiration_date, | ||
credential_subject: serde_json::from_str(&self.json_serialized_credential_subject) | ||
.map_err(|e| Arc::new(e.into()))?, | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.