diff --git a/dids/src/main/kotlin/web5/sdk/dids/did/BearerDid.kt b/dids/src/main/kotlin/web5/sdk/dids/did/BearerDid.kt index 7a5658c70..7433ae156 100644 --- a/dids/src/main/kotlin/web5/sdk/dids/did/BearerDid.kt +++ b/dids/src/main/kotlin/web5/sdk/dids/did/BearerDid.kt @@ -7,6 +7,7 @@ import web5.sdk.crypto.KeyManager import web5.sdk.crypto.jwk.Jwk import web5.sdk.dids.didcore.DidDocument import web5.sdk.dids.didcore.Did +import web5.sdk.dids.didcore.Service import web5.sdk.dids.didcore.VMSelector import web5.sdk.dids.didcore.VerificationMethod @@ -61,6 +62,41 @@ public class BearerDid( return Pair(signer, verificationMethod) } + /** + * Adds a new service to the DID Document and returns a new `BearerDid` instance with the updated document. + * + * @param service The service to add to the DID Document. + * @return A new `BearerDid` instance with the updated DID Document. + */ + public fun addService(service: Service): BearerDid { + val updatedServices = document.service?.toMutableList() ?: mutableListOf() + updatedServices.add(service) + val updatedDocument = createUpdatedDocument(updatedServices) + return BearerDid(uri, did, keyManager, updatedDocument) + } + + + /** + * Creates a new `DidDocument` instance with the updated services. + * + * @param updatedServices The updated list of services to include in the DID Document. + * @return A new `DidDocument` instance with the updated services. + */ + private fun createUpdatedDocument(updatedServices: List): DidDocument { + return DidDocument( + id = document.id, + verificationMethod = document.verificationMethod, + service = updatedServices, + authentication = document.authentication, + assertionMethod = document.assertionMethod, + keyAgreement = document.keyAgreement, + capabilityInvocation = document.capabilityInvocation, + capabilityDelegation = document.capabilityDelegation, + controller = document.controller, + alsoKnownAs = document.alsoKnownAs + ) + } + /** * Converts a `BearerDid` object to a portable format containing the URI and verification methods * associated with the DID. diff --git a/dids/src/test/kotlin/web5/sdk/dids/did/BearerDidTest.kt b/dids/src/test/kotlin/web5/sdk/dids/did/BearerDidTest.kt index d0965d2f5..a11cbb56a 100644 --- a/dids/src/test/kotlin/web5/sdk/dids/did/BearerDidTest.kt +++ b/dids/src/test/kotlin/web5/sdk/dids/did/BearerDidTest.kt @@ -1,6 +1,7 @@ package web5.sdk.dids.did import org.junit.jupiter.api.Assertions.assertArrayEquals +import org.junit.jupiter.api.Assertions.assertTrue import org.mockito.kotlin.any import org.mockito.kotlin.doReturn import org.mockito.kotlin.eq @@ -8,6 +9,8 @@ import org.mockito.kotlin.spy import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import web5.sdk.crypto.InMemoryKeyManager +import web5.sdk.dids.didcore.Service +import web5.sdk.dids.methods.dht.DidDht import web5.sdk.dids.methods.jwk.DidJwk import kotlin.test.Test import kotlin.test.assertEquals @@ -15,6 +18,19 @@ import kotlin.test.assertNotNull class BearerDidTest { + @Test + fun `addService should add a new service to the DID Document`() { + val bearerDid = DidDht.create(InMemoryKeyManager()) + + val newService = Service(id = "service3", type = "ServiceType3", serviceEndpoint = listOf("https://endpoint3")) + val updatedBearerDid = bearerDid.addService(newService) + + assertEquals(1, updatedBearerDid.document.service?.size) + assertTrue(updatedBearerDid.document.service?.any { it.id == "service3" } == true) + } + + + @Test fun `getSigner should return a signer and verification method`() { val keyManager = spy(InMemoryKeyManager())