Skip to content

Commit

Permalink
full impl
Browse files Browse the repository at this point in the history
  • Loading branch information
nitro-neal committed Sep 10, 2024
1 parent 8db0f98 commit 5269665
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 18 deletions.
6 changes: 2 additions & 4 deletions bindings/web5_uniffi/src/web5.udl
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,10 @@ interface PresentationDefinition {
string get_json_serialized_presentation_definition();
[Throws=Web5Error]
sequence<string> select_credentials([ByRef] sequence<string> vc_jwts);
[Throws=Web5Error]
string create_presentation_from_credentials([ByRef] sequence<string> vc_jwts);
};





dictionary VerifiableCredentialCreateOptionsData {
string? id;
sequence<string>? context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ impl PresentationDefinition {
Ok(self.0.select_credentials(vc_jwts)?)
}

pub fn create_presentation_from_credentials(&self, vc_jwts: &Vec<String>) -> Result<String> {
let presentation_result = self.0.create_presentation_from_credentials(vc_jwts)?;
let json_serialized_presentation_result = serde_json::to_string(&presentation_result)?;

Ok(json_serialized_presentation_result)
}

pub fn get_json_serialized_presentation_definition(&self) -> Result<String> {
let json_serialized_presentation_definition = serde_json::to_string(&self.0)?;

Expand Down
24 changes: 24 additions & 0 deletions bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,8 @@ internal open class UniffiVTableCallbackInterfaceVerifier(








Expand Down Expand Up @@ -1056,6 +1058,8 @@ internal interface UniffiLib : Library {
): Unit
fun uniffi_web5_uniffi_fn_constructor_presentationdefinition_new(`jsonSerializedPresentationDefinition`: RustBuffer.ByValue,uniffi_out_err: UniffiRustCallStatus,
): Pointer
fun uniffi_web5_uniffi_fn_method_presentationdefinition_create_presentation_from_credentials(`ptr`: Pointer,`vcJwts`: RustBuffer.ByValue,uniffi_out_err: UniffiRustCallStatus,
): RustBuffer.ByValue
fun uniffi_web5_uniffi_fn_method_presentationdefinition_get_json_serialized_presentation_definition(`ptr`: Pointer,uniffi_out_err: UniffiRustCallStatus,
): RustBuffer.ByValue
fun uniffi_web5_uniffi_fn_method_presentationdefinition_select_credentials(`ptr`: Pointer,`vcJwts`: RustBuffer.ByValue,uniffi_out_err: UniffiRustCallStatus,
Expand Down Expand Up @@ -1300,6 +1304,8 @@ internal interface UniffiLib : Library {
): Short
fun uniffi_web5_uniffi_checksum_method_portabledid_to_json_string(
): Short
fun uniffi_web5_uniffi_checksum_method_presentationdefinition_create_presentation_from_credentials(
): Short
fun uniffi_web5_uniffi_checksum_method_presentationdefinition_get_json_serialized_presentation_definition(
): Short
fun uniffi_web5_uniffi_checksum_method_presentationdefinition_select_credentials(
Expand Down Expand Up @@ -1456,6 +1462,9 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) {
if (lib.uniffi_web5_uniffi_checksum_method_portabledid_to_json_string() != 53673.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_web5_uniffi_checksum_method_presentationdefinition_create_presentation_from_credentials() != 27580.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_web5_uniffi_checksum_method_presentationdefinition_get_json_serialized_presentation_definition() != 52261.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
Expand Down Expand Up @@ -4548,6 +4557,8 @@ public object FfiConverterTypePortableDid: FfiConverter<PortableDid, Pointer> {

public interface PresentationDefinitionInterface {

fun `createPresentationFromCredentials`(`vcJwts`: List<kotlin.String>): kotlin.String

fun `getJsonSerializedPresentationDefinition`(): kotlin.String

fun `selectCredentials`(`vcJwts`: List<kotlin.String>): List<kotlin.String>
Expand Down Expand Up @@ -4644,6 +4655,19 @@ open class PresentationDefinition: Disposable, AutoCloseable, PresentationDefini
}


@Throws(Web5Exception::class)override fun `createPresentationFromCredentials`(`vcJwts`: List<kotlin.String>): kotlin.String {
return FfiConverterString.lift(
callWithPointer {
uniffiRustCallWithError(Web5Exception) { _status ->
UniffiLib.INSTANCE.uniffi_web5_uniffi_fn_method_presentationdefinition_create_presentation_from_credentials(
it, FfiConverterSequenceString.lower(`vcJwts`),_status)
}
}
)
}



@Throws(Web5Exception::class)override fun `getJsonSerializedPresentationDefinition`(): kotlin.String {
return FfiConverterString.lift(
callWithPointer {
Expand Down
17 changes: 14 additions & 3 deletions bound/kt/src/main/kotlin/web5/sdk/vc/VerifiablePresentation.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package web5.sdk.vc

import com.fasterxml.jackson.module.kotlin.readValue
import web5.sdk.Json
import java.util.Date
import web5.sdk.Web5Exception
import web5.sdk.dids.BearerDid
Expand All @@ -12,7 +14,8 @@ data class VerifiablePresentationCreateOptions(
val context: List<String>? = null,
val type: List<String>? = null,
val issuanceDate: Date? = null,
val expirationDate: Date? = null
val expirationDate: Date? = null,
val additionalProperties: Map<String, Any>? = null
)

data class VerifiablePresentation private constructor(
Expand All @@ -23,6 +26,7 @@ data class VerifiablePresentation private constructor(
val issuanceDate: Date,
val expirationDate: Date? = null,
val verifiableCredential: List<String>,
val additionalProperties: Map<String, Any>?,
internal val rustCoreVerifiablePresentation: RustCoreVerifiablePresentation,
) {
companion object {
Expand All @@ -32,6 +36,8 @@ data class VerifiablePresentation private constructor(
options: VerifiablePresentationCreateOptions? = null
): VerifiablePresentation {
try {
val jsonSerializedAdditionalProperties = options?.additionalProperties?.let { Json.stringify(it) }

val rustCoreVerifiablePresentation = RustCoreVerifiablePresentation.create(
holder,
verifiableCredential,
Expand All @@ -40,7 +46,8 @@ data class VerifiablePresentation private constructor(
options?.context,
options?.type,
options?.issuanceDate?.toInstant(),
options?.expirationDate?.toInstant()
options?.expirationDate?.toInstant(),
jsonSerializedAdditionalProperties
)
)

Expand All @@ -54,7 +61,8 @@ data class VerifiablePresentation private constructor(
Date.from(data.issuanceDate),
data.expirationDate?.let { Date.from(it) },
data.verifiableCredential,
rustCoreVerifiablePresentation
options?.additionalProperties,
rustCoreVerifiablePresentation,
)
} catch (e: RustCoreException) {
throw Web5Exception.fromRustCore(e)
Expand All @@ -66,6 +74,8 @@ data class VerifiablePresentation private constructor(
val rustCoreVerifiablePresentation = RustCoreVerifiablePresentation.fromVpJwt(vpJwt, verify)
val data = rustCoreVerifiablePresentation.getData()

val additionalProperties = data.jsonSerializedAdditionalData?.let { Json.jsonMapper.readValue<Map<String, Any>>(it) }

return VerifiablePresentation(
data.context,
data.type,
Expand All @@ -74,6 +84,7 @@ data class VerifiablePresentation private constructor(
Date.from(data.issuanceDate),
data.expirationDate?.let { Date.from(it) },
data.verifiableCredential,
additionalProperties,
rustCoreVerifiablePresentation
)
} catch (e: RustCoreException) {
Expand Down
48 changes: 45 additions & 3 deletions bound/kt/src/main/kotlin/web5/sdk/vc/pex/PresentationDefinition.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package web5.sdk.vc.pex

import com.fasterxml.jackson.annotation.JsonProperty
import web5.sdk.Json
import web5.sdk.Web5Exception
import web5.sdk.rust.PresentationDefinition as RustCorePresentationDefinition
Expand All @@ -10,8 +9,8 @@ data class PresentationDefinition(
val id: String,
val name: String?,
val purpose: String?,
@JsonProperty("input_descriptors")
val inputDescriptors: List<InputDescriptor>
val inputDescriptors: List<InputDescriptor>,
val submissionRequirements: List<SubmissionRequirement>? = null
) {
internal val rustCorePresentationDefinition = RustCorePresentationDefinition(
Json.stringify(this)
Expand All @@ -24,6 +23,15 @@ data class PresentationDefinition(
throw Web5Exception.fromRustCore(e)
}
}

fun createPresentationFromCredentials(vcJwts: List<String>): PresentationResult {
try {
val rustCoreJsonSerializedPresentationResult = this.rustCorePresentationDefinition.createPresentationFromCredentials(vcJwts)
return Json.jsonMapper.readValue(rustCoreJsonSerializedPresentationResult, PresentationResult::class.java)
} catch (e: RustCoreException) {
throw Web5Exception.fromRustCore(e)
}
}
}

data class InputDescriptor(
Expand Down Expand Up @@ -57,4 +65,38 @@ data class Filter(
val pattern: String? = null,
val const: String? = null,
val contains: Filter? = null
)

data class SubmissionRequirement(
val rule: SubmissionRequirementRule,
val from: String? = null,
val fromNested: List<SubmissionRequirement>? = null,
val name: String? = null,
val purpose: String? = null,
val count: Int? = null,
val min: Int? = null,
val max: Int? = null
)

enum class SubmissionRequirementRule {
All,
Pick
}

data class PresentationResult(
val presentationSubmission: PresentationSubmission,
val matchedVcJwts: List<String>
)

data class PresentationSubmission(
val id: String,
val definitionId: String,
val descriptorMap: List<InputDescriptorMapping>
)

data class InputDescriptorMapping(
val id: String,
val format: String,
val path: String,
val pathNested: InputDescriptorMapping? = null
)
39 changes: 39 additions & 0 deletions bound/kt/src/test/kotlin/web5/sdk/vc/VerifiablePresentationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,43 @@ class VerifiablePresentationTest {
assertEquals(vp.type, decodedVp.type)
assertEquals(vp.verifiableCredential, decodedVp.verifiableCredential)
}

@Test
fun test_create_verifiable_presentation_with_additional_properties() {
val holder = HOLDER_DID_URI
val verifiableCredential = listOf(VERIFIABLE_CREDENTIAL)

// Define additional properties to test
val additionalProperties = mapOf(
"customProperty1" to "customValue1",
"customProperty2" to 12345,
"customProperty3" to true
)

val options = VerifiablePresentationCreateOptions(
id = "urn:uuid:12345678-1234-5678-1234-567812345678",
context = listOf("https://www.w3.org/2018/credentials/v1"),
type = listOf("VerifiablePresentation"),
issuanceDate = Date(),
expirationDate = Date(System.currentTimeMillis() + 86400000), // 1 day later
additionalProperties = additionalProperties
)

val vp = VerifiablePresentation.create(holder, verifiableCredential, options)

// Assertions
assertEquals(holder, vp.holder)
assertEquals(options.id, vp.id)
assertEquals(options.context, vp.context)
assertEquals(options.type, vp.type)
assertEquals(verifiableCredential, vp.verifiableCredential)
assertNotNull(vp.issuanceDate)
assertNotNull(vp.expirationDate)
assertNotNull(vp.additionalProperties)

// Check that the additional properties match
assertEquals("customValue1", vp.additionalProperties?.get("customProperty1"))
assertEquals(12345, vp.additionalProperties?.get("customProperty2"))
assertEquals(true, vp.additionalProperties?.get("customProperty3"))
}
}
Loading

0 comments on commit 5269665

Please sign in to comment.