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

ci: update package.json and update docs #12

Merged
merged 1 commit into from
Jan 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ const val SERVICE_DIDCOMM_MESSAGING = "DIDCommMessaging"
const val SERVICE_ROUTING_KEYS = "routingKeys"
const val SERVICE_ACCEPT = "accept"

/**
* Represents a PeerDID DID Document.
*
* @property did The DID identifier.
* @property authentication The list of authentication verification methods.
* @property keyAgreement The list of key agreement verification methods.
* @property service The list of service endpoints.
*/
@Serializable
data class DIDDocPeerDID
@JvmOverloads
Expand All @@ -23,11 +31,27 @@ constructor(
val keyAgreement: List<VerificationMethodPeerDID> = emptyList(),
val service: List<Service>? = null
) {
/**
* Retrieves a list of IDs from the authentication list.
*
* @return The list of authentication kid IDs.
*/
val authenticationKids
get() = authentication.map { it.id }

/**
* Represents the agreementKids property in the DIDDocPeerDID class.
* It is a read-only property that returns a list of IDs of key agreements in the DID document.
*
* @return The list of IDs of key agreements.
*/
val agreementKids
get() = keyAgreement.map { it.id }

/**
* Converts the DID document to a dictionary representation.
* @return The dictionary representation of the DID document.
*/
fun toDict(): Map<String, Any> {
val res =
mutableMapOf(
Expand All @@ -49,6 +73,11 @@ constructor(
return res
}

/**
* Converts the object to its JSON representation as a String.
*
* @return The JSON representation of the object as a String.
*/
fun toJson(): String {
return toDict().toJsonElement().toString()
}
Expand All @@ -61,6 +90,7 @@ constructor(
* @throws MalformedPeerDIDDOcException if the input DID Doc JSON is not a valid peerdid DID Doc
* @return [DIDDocPeerDID] instance
*/
@Throws(MalformedPeerDIDDOcException::class)
fun fromJson(value: JSON): DIDDocPeerDID {
try {
// Two ways
Expand All @@ -72,19 +102,36 @@ constructor(
}
}

/**
* Represents a verification method used in PeerDID.
*
* @property id The ID of the verification method.
* @property controller The controller of the verification method.
* @property verMaterial The verification material of the verification method.
*/
@Serializable
data class VerificationMethodPeerDID(
val id: String,
val controller: String,
val verMaterial: VerificationMaterialPeerDID<out VerificationMethodTypePeerDID>
) {
/**
* Returns the appropriate public key field based on the verification material format.
*
* @return The public key field.
*/
private fun publicKeyField() =
when (verMaterial.format) {
VerificationMaterialFormatPeerDID.BASE58 -> PublicKeyField.BASE58
VerificationMaterialFormatPeerDID.JWK -> PublicKeyField.JWK
VerificationMaterialFormatPeerDID.MULTIBASE -> PublicKeyField.MULTIBASE
}

/**
* Converts the VerificationMethodPeerDID to a dictionary representation.
*
* @return The dictionary representation of the VerificationMethodPeerDID.
*/
fun toDict() =
mapOf(
"id" to id,
Expand All @@ -94,17 +141,38 @@ data class VerificationMethodPeerDID(
)
}

/**
* Represents a service.
*/
sealed interface Service

/**
* Represents a service provided by a DID document.
*
* @property data The data of the service.
*/
data class OtherService(val data: Map<String, Any>) : Service

/**
* Represents a DIDComm service peer DID.
* @property id The ID of the service.
* @property type The type of the service.
* @property serviceEndpoint The service endpoint.
* @property routingKeys The list of routing keys.
* @property accept The list of accepted message types.
*/
data class DIDCommServicePeerDID(
val id: String,
val type: String,
val serviceEndpoint: String,
val routingKeys: List<String>,
val accept: List<String>
) : Service {
/**
* Converts the DIDCommServicePeerDID object to a mutable map representation.
*
* @return The mutable map representation of the DIDCommServicePeerDID object.
*/
fun toDict(): MutableMap<String, Any> {
val res =
mutableMapOf<String, Any>(
Expand All @@ -118,6 +186,11 @@ data class DIDCommServicePeerDID(
}
}

/**
* Represents the different types of public key fields.
*
* @property value The string value of the public key field.
*/
enum class PublicKeyField(val value: String) {
BASE58("publicKeyBase58"),
MULTIBASE("publicKeyMultibase"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ fun createPeerDIDNumalgo2(

val encodedEncryptionKeysStr =
encryptionKeys
.map { createMultibaseEncnumbasis(it) }
.map { ".${Numalgo2Prefix.KEY_AGREEMENT.prefix}$it" }
.joinToString("")
.map { createMultibaseEncnumbasis(it) }.joinToString("") { ".${Numalgo2Prefix.KEY_AGREEMENT.prefix}$it" }
val encodedSigningKeysStr =
signingKeys
.map { createMultibaseEncnumbasis(it) }
.map { ".${Numalgo2Prefix.AUTHENTICATION.prefix}$it" }
.joinToString("")
.map { createMultibaseEncnumbasis(it) }.joinToString("") { ".${Numalgo2Prefix.AUTHENTICATION.prefix}$it" }
val encodedService = if (service.isNullOrEmpty()) "" else encodeService(service)

return "did:peer:2$encodedEncryptionKeysStr$encodedSigningKeysStr$encodedService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlin.jvm.JvmOverloads
* @return resolved [DIDDocPeerDID] as JSON string
*/
@JvmOverloads
@Throws(IllegalArgumentException::class)
fun resolvePeerDID(
peerDID: PeerDID,
format: VerificationMaterialFormatPeerDID = VerificationMaterialFormatPeerDID.MULTIBASE
Expand All @@ -37,6 +38,13 @@ fun resolvePeerDID(
return didDoc.toJson()
}

/**
* Builds a PeerDID DID Document for the numalgo0 format.
*
* @param peerDID The PeerDID.
* @param format The format of verification material in the DID Document.
* @return The built DID Document.
*/
private fun buildDIDDocNumalgo0(peerDID: PeerDID, format: VerificationMaterialFormatPeerDID): DIDDocPeerDID {
val inceptionKey = peerDID.substring(10)
val decodedEncumbasis = decodeMultibaseEncnumbasisAuth(inceptionKey, format)
Expand All @@ -46,6 +54,15 @@ private fun buildDIDDocNumalgo0(peerDID: PeerDID, format: VerificationMaterialFo
)
}

/**
* Builds a PeerDID DID Document using the Numalgo2 algorithm.
*
* @param peerDID The PeerDID identifier.
* @param format The format of the verification material.
* @return The built PeerDID DID Document.
* @throws IllegalArgumentException if the transform part of the PeerDID is unsupported.
*/
@Throws(IllegalArgumentException::class)
private fun buildDIDDocNumalgo2(peerDID: PeerDID, format: VerificationMaterialFormatPeerDID): DIDDocPeerDID {
val keys = peerDID.drop(11)

Expand Down Expand Up @@ -87,6 +104,15 @@ private fun buildDIDDocNumalgo2(peerDID: PeerDID, format: VerificationMaterialFo
)
}

/**
* Decodes a multibase-encoded encnumbasis with a given verification material format.
*
* @param multibase The multibase-encoded encnumbasis to decode.
* @param format The verification material format.
* @throws MalformedPeerDIDException if the multibase is invalid.
* @return The decoded encnumbasis as verification material.
*/
@Throws(MalformedPeerDIDException::class)
private fun decodeMultibaseEncnumbasisAuth(
multibase: String,
format: VerificationMaterialFormatPeerDID
Expand All @@ -100,6 +126,15 @@ private fun decodeMultibaseEncnumbasisAuth(
}
}

/**
* Decodes a multibase encoded number basis agreement to a verification material for DID DOC.
*
* @param multibase The multibase string to decode.
* @param format The format of public keys in the DID DOC.
* @throws MalformedPeerDIDException If the multibase string is invalid.
* @return The decoded encnumbasis as verification material for DID DOC.
*/
@Throws(MalformedPeerDIDException::class)
private fun decodeMultibaseEncnumbasisAgreement(
multibase: String,
format: VerificationMaterialFormatPeerDID
Expand All @@ -113,6 +148,15 @@ private fun decodeMultibaseEncnumbasisAgreement(
}
}

/**
* Decodes the provided list of service JSON objects according to the PeerDID spec.
*
* @param service The list of JSON objects representing services.
* @param peerDID The PeerDID used as an ID.
* @return The decoded list of services.
* @throws MalformedPeerDIDException If the service is not correctly decoded.
*/
@Throws(MalformedPeerDIDException::class)
private fun doDecodeService(service: List<JSON>, peerDID: String): List<Service>? {
try {
return decodeService(service, peerDID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ package io.iohk.atala.prism.didcomm.didpeer
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable

/**
* An enumeration that represents the format of verification material in a PeerDID.
*/
enum class VerificationMaterialFormatPeerDID {
JWK,
BASE58,
MULTIBASE
}

/**
* Represents the types of verification methods for PeerDID.
*
* @param value The string value associated with the verification method type.
*/
@Serializable
sealed class VerificationMethodTypePeerDID(val value: String)

/**
* A sealed class representing the types of verification methods for agreements.
*
* @property value The value associated with the verification method type.
*/
sealed class VerificationMethodTypeAgreement(value: String) : VerificationMethodTypePeerDID(value) {
object JsonWebKey2020 : VerificationMethodTypeAgreement("JsonWebKey2020")

Expand All @@ -20,6 +33,11 @@ sealed class VerificationMethodTypeAgreement(value: String) : VerificationMethod
object X25519KeyAgreementKey2020 : VerificationMethodTypeAgreement("X25519KeyAgreementKey2020")
}

/**
* Represents the different types of authentication methods for verification.
*
* @param value The string value representing the authentication method type.
*/
sealed class VerificationMethodTypeAuthentication(value: String) : VerificationMethodTypePeerDID(value) {
object JsonWebKey2020 : VerificationMethodTypeAuthentication("JsonWebKey2020")

Expand All @@ -28,15 +46,37 @@ sealed class VerificationMethodTypeAuthentication(value: String) : VerificationM
object ED25519VerificationKey2020 : VerificationMethodTypeAuthentication("Ed25519VerificationKey2020")
}

/**
* Represents the verification material used in a PeerDID.
*
* @param T The type of the verification method.
* @property format The format of the verification material.
* @property value The value of the verification material.
* @property type The type of the verification method.
*/
@Serializable
data class VerificationMaterialPeerDID<T : VerificationMethodTypePeerDID>(
val format: VerificationMaterialFormatPeerDID,
@Contextual val value: Any,
val type: T
)

/**
* Alias for [VerificationMaterialPeerDID] with [VerificationMethodTypeAgreement] type parameter.
*/
typealias VerificationMaterialAgreement = VerificationMaterialPeerDID<VerificationMethodTypeAgreement>

/**
* Alias for [VerificationMaterialPeerDID] with [VerificationMethodTypeAuthentication] type parameter.
*/
typealias VerificationMaterialAuthentication = VerificationMaterialPeerDID<VerificationMethodTypeAuthentication>

/**
* Type alias for JSON strings.
*/
typealias JSON = String

/**
* Defines the type PeerDID, which is an alias for a String.
*/
typealias PeerDID = String
Loading
Loading