Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Presentation Definition Object #249

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions bound/kt/src/main/kotlin/web5/sdk/vc/pex/PresentationDefinition.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package web5.sdk.vc.pex

import web5.sdk.rust.ConstraintsData as RustCoreConstraintsData
import web5.sdk.rust.FieldData as RustCoreFieldData
import web5.sdk.rust.FilterData as RustCoreFilterData
import web5.sdk.rust.Optionality as RustCoreOptionality
import web5.sdk.rust.InputDescriptorData as RustCoreInputDescriptorData

import web5.sdk.rust.PresentationDefinition as RustCorePresentationDefinition
import web5.sdk.rust.PresentationDefinitionData as RustCorePresentationDefinitionData

typealias ConstraintsData = RustCoreConstraintsData
typealias FieldData = RustCoreFieldData
typealias FilterData = RustCoreFilterData
typealias Optionality = RustCoreOptionality
typealias InputDescriptor = RustCoreInputDescriptorData

class PresentationDefinition {
val id: String
val name: String?
val purpose: String?
val inputDescriptors: List<InputDescriptor>

internal val rustCorePresentationDefinition: RustCorePresentationDefinition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! internal


constructor(id: String, name: String? = null, purpose: String? = null, inputDescriptors: List<InputDescriptor>) {
this.id = id
this.name = name
this.purpose = purpose
this.inputDescriptors = inputDescriptors

this.rustCorePresentationDefinition = RustCorePresentationDefinition(
RustCorePresentationDefinitionData(id, name, purpose, inputDescriptors)
)
}

constructor(rustCorePresentationDefinitionData: RustCorePresentationDefinitionData) {
this.id = rustCorePresentationDefinitionData.id
this.name = rustCorePresentationDefinitionData.name
this.purpose = rustCorePresentationDefinitionData.purpose
this.inputDescriptors = rustCorePresentationDefinitionData.inputDescriptors

this.rustCorePresentationDefinition = RustCorePresentationDefinition(rustCorePresentationDefinitionData)
}

fun selectCredentials(vcJwts: List<String>): List<String> {
return this.rustCorePresentationDefinition.selectCredentials(vcJwts)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package web5.sdk.vcs.pex

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import web5.sdk.vc.pex.*

class PresentationDefinitionTest {

@Test
fun `test basic presentation definition`() {
val inputDescriptor = InputDescriptor(
id = "test_input",
name = "Test Input",
purpose = "For testing",
constraints = ConstraintsData(
fields = listOf(
FieldData(
id = "field1",
name = "Field 1",
path = listOf("$.field1"),
purpose = "Test field",
filter = FilterData(
type = "string",
pattern = "^[a-zA-Z]+$",
constValue = null
),
optional = false,
predicate = Optionality.REQUIRED
)
)
)
)

val presentationDefinition = PresentationDefinition(
id = "test_presentation",
name = "Test Presentation",
purpose = "For testing purposes",
inputDescriptors = listOf(inputDescriptor)
)

assertEquals("test_presentation", presentationDefinition.id)
assertEquals("Test Presentation", presentationDefinition.name)
assertEquals("For testing purposes", presentationDefinition.purpose)
assertEquals(1, presentationDefinition.inputDescriptors.size)

val firstInputDescriptor = presentationDefinition.inputDescriptors[0]
assertEquals("test_input", firstInputDescriptor.id)
assertEquals("Test Input", firstInputDescriptor.name)
assertEquals("For testing", firstInputDescriptor.purpose)

val field = firstInputDescriptor.constraints.fields[0]
assertEquals("field1", field.id)
assertEquals("Field 1", field.name)
assertEquals(listOf("$.field1"), field.path)
assertEquals("Test field", field.purpose)
assertFalse(field.optional ?: true)
assertEquals(Optionality.REQUIRED, field.predicate)

val filter = field.filter
assertNotNull(filter)
assertEquals("string", filter?.type)
assertEquals("^[a-zA-Z]+$", filter?.pattern)
assertNull(filter?.constValue)
}
}
Loading