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

another take on simpler way of adding a service #313

Closed
wants to merge 4 commits into from
Closed
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
36 changes: 36 additions & 0 deletions dids/src/main/kotlin/web5/sdk/dids/did/BearerDid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Comment on lines +65 to +76
Copy link
Contributor

Choose a reason for hiding this comment

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

would it make more sense for addService to be a method on DidDocument? that's what we've done in go and dart.

this method is helpful for building did docs in general. examples here and here. same usage exists in go as well

Copy link
Author

Choose a reason for hiding this comment

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

that would only work if the service was mutable (like it is in other places) - otherwise currently, has to return a new did (with it added to the doc) OR you need to return a new doc and then update the doc in the did (which returns a new did) if sticking with immutable approach in kotlin (correct me if wrong).

Copy link
Author

@michaelneale michaelneale May 17, 2024

Choose a reason for hiding this comment

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

ie, you would do blah.document.addService()... but it wouldn't change document in place but return a new document as List is immutable (can extend to other things that may want to mutate).

image

Copy link
Author

Choose a reason for hiding this comment

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

so - we could change them to be mutable lists?



/**
* 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<Service>): 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.
Expand Down
16 changes: 16 additions & 0 deletions dids/src/test/kotlin/web5/sdk/dids/did/BearerDidTest.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
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
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
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())
Expand Down
Loading