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.
- Loading branch information
1 parent
f4de14f
commit 7e196fe
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
bound/kt/src/main/kotlin/web5/sdk/vc/pex/PresentationDefinition.kt
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 |
---|---|---|
@@ -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 | ||
|
||
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) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
bound/kt/src/test/kotlin/web5/sdk/vcs/pex/PresentationDefinitionTest.kt
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 |
---|---|---|
@@ -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) | ||
} | ||
} |