Skip to content

Commit

Permalink
Add evidence to VC (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Sep 3, 2024
1 parent caacb9a commit 138662b
Show file tree
Hide file tree
Showing 20 changed files with 1,776 additions and 90 deletions.
8 changes: 6 additions & 2 deletions bindings/web5_uniffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use web5_uniffi_wrapper::{
credentials::{
presentation_definition::PresentationDefinition,
verifiable_credential_1_1::{VerifiableCredential, VerifiableCredentialData},
verifiable_credential_1_1::{
VerifiableCredential,
VerifiableCredentialCreateOptions as VerifiableCredentialCreateOptionsData,
VerifiableCredentialData,
},
},
crypto::{
dsa::{
Expand Down Expand Up @@ -29,7 +33,7 @@ use web5_uniffi_wrapper::{
};

use web5::{
credentials::verifiable_credential_1_1::VerifiableCredentialCreateOptions as VerifiableCredentialCreateOptionsData,
credentials::CredentialSchema as CredentialSchemaData,
crypto::{dsa::Dsa, jwk::Jwk as JwkData},
dids::{
data_model::{
Expand Down
10 changes: 10 additions & 0 deletions bindings/web5_uniffi/src/web5.udl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ dictionary VerifiableCredentialCreateOptionsData {
sequence<string>? type;
timestamp? issuance_date;
timestamp? expiration_date;
CredentialSchemaData? credential_schema;
string? json_serialized_evidence;
};

interface VerifiableCredential {
Expand All @@ -266,6 +268,7 @@ interface VerifiableCredential {
string json_serialized_credential_subject,
VerifiableCredentialCreateOptionsData? options
);
[Throws=Web5Error]
VerifiableCredentialData get_data();
[Throws=Web5Error, Name=from_vc_jwt]
constructor(string vc_jwt, boolean verify);
Expand All @@ -281,4 +284,11 @@ dictionary VerifiableCredentialData {
string json_serialized_credential_subject;
timestamp issuance_date;
timestamp? expiration_date;
CredentialSchemaData? credential_schema;
string? json_serialized_evidence;
};

dictionary CredentialSchemaData {
string id;
string type;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ use std::{sync::Arc, time::SystemTime};
use web5::{
credentials::{
verifiable_credential_1_1::{
VerifiableCredential as InnerVerifiableCredential, VerifiableCredentialCreateOptions,
VerifiableCredential as InnerVerifiableCredential,
VerifiableCredentialCreateOptions as InnerVerifiableCredentialCreateOptions,
},
CredentialSubject, Issuer,
CredentialSchema, CredentialSubject, Issuer,
},
json::FromJson as _,
json::{FromJson as _, JsonObject},
};

#[derive(Default)]
pub struct VerifiableCredentialCreateOptions {
pub id: Option<String>,
pub context: Option<Vec<String>>,
pub r#type: Option<Vec<String>>,
pub issuance_date: Option<SystemTime>,
pub expiration_date: Option<SystemTime>,
pub credential_schema: Option<CredentialSchema>,
pub json_serialized_evidence: Option<String>,
}

pub struct VerifiableCredential {
inner_vc: InnerVerifiableCredential,
json_serialized_issuer: String,
Expand All @@ -26,7 +38,25 @@ impl VerifiableCredential {
let credential_subject =
CredentialSubject::from_json_string(&json_serialized_credential_subject)?;

let inner_vc = InnerVerifiableCredential::create(issuer, credential_subject, options)?;
let options = options.unwrap_or_default();
let evidence = match options.json_serialized_evidence {
Some(evidence_string) => {
Some(serde_json::from_str::<Vec<JsonObject>>(&evidence_string)?)
}
None => None,
};
let inner_options = InnerVerifiableCredentialCreateOptions {
id: options.id,
context: options.context,
r#type: options.r#type,
issuance_date: options.issuance_date,
expiration_date: options.expiration_date,
credential_schema: options.credential_schema,
evidence,
};

let inner_vc =
InnerVerifiableCredential::create(issuer, credential_subject, Some(inner_options))?;

Ok(Self {
inner_vc,
Expand All @@ -35,16 +65,23 @@ impl VerifiableCredential {
})
}

pub fn get_data(&self) -> VerifiableCredentialData {
VerifiableCredentialData {
pub fn get_data(&self) -> Result<VerifiableCredentialData> {
let json_serialized_evidence = match &self.inner_vc.evidence {
Some(e) => Some(serde_json::to_string(e)?),
None => None,
};

Ok(VerifiableCredentialData {
context: self.inner_vc.context.clone(),
id: self.inner_vc.id.clone(),
r#type: self.inner_vc.r#type.clone(),
json_serialized_issuer: self.json_serialized_issuer.clone(),
json_serialized_credential_subject: self.json_serialized_credential_subject.clone(),
issuance_date: self.inner_vc.issuance_date,
expiration_date: self.inner_vc.expiration_date,
}
credential_schema: self.inner_vc.credential_schema.clone(),
json_serialized_evidence,
})
}

pub fn from_vc_jwt(vc_jwt: String, verify: bool) -> Result<Self> {
Expand Down Expand Up @@ -79,4 +116,6 @@ pub struct VerifiableCredentialData {
pub json_serialized_credential_subject: String,
pub issuance_date: SystemTime,
pub expiration_date: Option<SystemTime>,
pub credential_schema: Option<CredentialSchema>,
pub json_serialized_evidence: Option<String>,
}
1 change: 1 addition & 0 deletions bindings/web5_uniffi_wrapper/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl From<Web5Error> for InnerWeb5Error {

match variant.as_str() {
"Json" => InnerWeb5Error::Json(msg),
"JsonSchema" => InnerWeb5Error::JsonSchema(msg),
"Parameter" => InnerWeb5Error::Parameter(msg),
"DataMember" => InnerWeb5Error::DataMember(msg),
"NotFound" => InnerWeb5Error::NotFound(msg),
Expand Down
89 changes: 82 additions & 7 deletions bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) {
if (lib.uniffi_web5_uniffi_checksum_method_signer_sign() != 5738.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_web5_uniffi_checksum_method_verifiablecredential_get_data() != 34047.toShort()) {
if (lib.uniffi_web5_uniffi_checksum_method_verifiablecredential_get_data() != 24514.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_web5_uniffi_checksum_method_verifiablecredential_sign() != 21705.toShort()) {
Expand Down Expand Up @@ -5332,10 +5332,11 @@ open class VerifiableCredential: Disposable, AutoCloseable, VerifiableCredential
}
}

override fun `getData`(): VerifiableCredentialData {

@Throws(Web5Exception::class)override fun `getData`(): VerifiableCredentialData {
return FfiConverterTypeVerifiableCredentialData.lift(
callWithPointer {
uniffiRustCall() { _status ->
uniffiRustCallWithError(Web5Exception) { _status ->
UniffiLib.INSTANCE.uniffi_web5_uniffi_fn_method_verifiablecredential_get_data(
it, _status)
}
Expand Down Expand Up @@ -5730,6 +5731,35 @@ public object FfiConverterTypeBearerDidData: FfiConverterRustBuffer<BearerDidDat



data class CredentialSchemaData (
var `id`: kotlin.String,
var `type`: kotlin.String
) {

companion object
}

public object FfiConverterTypeCredentialSchemaData: FfiConverterRustBuffer<CredentialSchemaData> {
override fun read(buf: ByteBuffer): CredentialSchemaData {
return CredentialSchemaData(
FfiConverterString.read(buf),
FfiConverterString.read(buf),
)
}

override fun allocationSize(value: CredentialSchemaData) = (
FfiConverterString.allocationSize(value.`id`) +
FfiConverterString.allocationSize(value.`type`)
)

override fun write(value: CredentialSchemaData, buf: ByteBuffer) {
FfiConverterString.write(value.`id`, buf)
FfiConverterString.write(value.`type`, buf)
}
}



data class DidData (
var `uri`: kotlin.String,
var `url`: kotlin.String,
Expand Down Expand Up @@ -6231,7 +6261,9 @@ data class VerifiableCredentialCreateOptionsData (
var `context`: List<kotlin.String>?,
var `type`: List<kotlin.String>?,
var `issuanceDate`: java.time.Instant?,
var `expirationDate`: java.time.Instant?
var `expirationDate`: java.time.Instant?,
var `credentialSchema`: CredentialSchemaData?,
var `jsonSerializedEvidence`: kotlin.String?
) {

companion object
Expand All @@ -6245,6 +6277,8 @@ public object FfiConverterTypeVerifiableCredentialCreateOptionsData: FfiConverte
FfiConverterOptionalSequenceString.read(buf),
FfiConverterOptionalTimestamp.read(buf),
FfiConverterOptionalTimestamp.read(buf),
FfiConverterOptionalTypeCredentialSchemaData.read(buf),
FfiConverterOptionalString.read(buf),
)
}

Expand All @@ -6253,7 +6287,9 @@ public object FfiConverterTypeVerifiableCredentialCreateOptionsData: FfiConverte
FfiConverterOptionalSequenceString.allocationSize(value.`context`) +
FfiConverterOptionalSequenceString.allocationSize(value.`type`) +
FfiConverterOptionalTimestamp.allocationSize(value.`issuanceDate`) +
FfiConverterOptionalTimestamp.allocationSize(value.`expirationDate`)
FfiConverterOptionalTimestamp.allocationSize(value.`expirationDate`) +
FfiConverterOptionalTypeCredentialSchemaData.allocationSize(value.`credentialSchema`) +
FfiConverterOptionalString.allocationSize(value.`jsonSerializedEvidence`)
)

override fun write(value: VerifiableCredentialCreateOptionsData, buf: ByteBuffer) {
Expand All @@ -6262,6 +6298,8 @@ public object FfiConverterTypeVerifiableCredentialCreateOptionsData: FfiConverte
FfiConverterOptionalSequenceString.write(value.`type`, buf)
FfiConverterOptionalTimestamp.write(value.`issuanceDate`, buf)
FfiConverterOptionalTimestamp.write(value.`expirationDate`, buf)
FfiConverterOptionalTypeCredentialSchemaData.write(value.`credentialSchema`, buf)
FfiConverterOptionalString.write(value.`jsonSerializedEvidence`, buf)
}
}

Expand All @@ -6274,7 +6312,9 @@ data class VerifiableCredentialData (
var `jsonSerializedIssuer`: kotlin.String,
var `jsonSerializedCredentialSubject`: kotlin.String,
var `issuanceDate`: java.time.Instant,
var `expirationDate`: java.time.Instant?
var `expirationDate`: java.time.Instant?,
var `credentialSchema`: CredentialSchemaData?,
var `jsonSerializedEvidence`: kotlin.String?
) {

companion object
Expand All @@ -6290,6 +6330,8 @@ public object FfiConverterTypeVerifiableCredentialData: FfiConverterRustBuffer<V
FfiConverterString.read(buf),
FfiConverterTimestamp.read(buf),
FfiConverterOptionalTimestamp.read(buf),
FfiConverterOptionalTypeCredentialSchemaData.read(buf),
FfiConverterOptionalString.read(buf),
)
}

Expand All @@ -6300,7 +6342,9 @@ public object FfiConverterTypeVerifiableCredentialData: FfiConverterRustBuffer<V
FfiConverterString.allocationSize(value.`jsonSerializedIssuer`) +
FfiConverterString.allocationSize(value.`jsonSerializedCredentialSubject`) +
FfiConverterTimestamp.allocationSize(value.`issuanceDate`) +
FfiConverterOptionalTimestamp.allocationSize(value.`expirationDate`)
FfiConverterOptionalTimestamp.allocationSize(value.`expirationDate`) +
FfiConverterOptionalTypeCredentialSchemaData.allocationSize(value.`credentialSchema`) +
FfiConverterOptionalString.allocationSize(value.`jsonSerializedEvidence`)
)

override fun write(value: VerifiableCredentialData, buf: ByteBuffer) {
Expand All @@ -6311,6 +6355,8 @@ public object FfiConverterTypeVerifiableCredentialData: FfiConverterRustBuffer<V
FfiConverterString.write(value.`jsonSerializedCredentialSubject`, buf)
FfiConverterTimestamp.write(value.`issuanceDate`, buf)
FfiConverterOptionalTimestamp.write(value.`expirationDate`, buf)
FfiConverterOptionalTypeCredentialSchemaData.write(value.`credentialSchema`, buf)
FfiConverterOptionalString.write(value.`jsonSerializedEvidence`, buf)
}
}

Expand Down Expand Up @@ -6596,6 +6642,35 @@ public object FfiConverterOptionalTypeKeyManager: FfiConverterRustBuffer<KeyMana



public object FfiConverterOptionalTypeCredentialSchemaData: FfiConverterRustBuffer<CredentialSchemaData?> {
override fun read(buf: ByteBuffer): CredentialSchemaData? {
if (buf.get().toInt() == 0) {
return null
}
return FfiConverterTypeCredentialSchemaData.read(buf)
}

override fun allocationSize(value: CredentialSchemaData?): ULong {
if (value == null) {
return 1UL
} else {
return 1UL + FfiConverterTypeCredentialSchemaData.allocationSize(value)
}
}

override fun write(value: CredentialSchemaData?, buf: ByteBuffer) {
if (value == null) {
buf.put(0)
} else {
buf.put(1)
FfiConverterTypeCredentialSchemaData.write(value, buf)
}
}
}




public object FfiConverterOptionalTypeDidDhtCreateOptions: FfiConverterRustBuffer<DidDhtCreateOptions?> {
override fun read(buf: ByteBuffer): DidDhtCreateOptions? {
if (buf.get().toInt() == 0) {
Expand Down
Loading

0 comments on commit 138662b

Please sign in to comment.