Skip to content

Commit

Permalink
[#6] Out of band didcomm message encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tdiesler committed Dec 22, 2022
1 parent 1458cfe commit 1c1618e
Show file tree
Hide file tree
Showing 36 changed files with 1,246 additions and 275 deletions.
4 changes: 0 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>com.google.crypto.tink</groupId>
<artifactId>tink</artifactId>
Expand Down
102 changes: 0 additions & 102 deletions core/src/main/kotlin/org/nessus/didcomm/aries/AriesAgent.kt

This file was deleted.

30 changes: 0 additions & 30 deletions core/src/main/kotlin/org/nessus/didcomm/model/MessageParser.kt

This file was deleted.

32 changes: 32 additions & 0 deletions core/src/main/kotlin/org/nessus/didcomm/model/MessageReader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.nessus.didcomm.model

import com.google.gson.Gson
import org.didcommx.didcomm.message.Message

/**
* Parses a JSON string to a DIDComm Message
*/
object MessageReader {

private val gson = Gson()

fun fromJson(json: String) : Message {
val jsonMap: MutableMap<String, Any> = mutableMapOf()
gson.fromJson(json, Map::class.java).forEach { en ->
val enval = en.value!!
when(val key: String = en.key.toString()) {
"created_time" -> jsonMap[key] = (enval as Double).toLong()
"expires_time" -> jsonMap[key] = (enval as Double).toLong()
"custom_headers" -> if (enval is Map<*, *> && enval.isNotEmpty()) {
jsonMap[key] = enval
}
else -> jsonMap[key] = enval
}
}
return Message.parse(jsonMap)
}

fun <T> fromJson(json: String, type: Class<T>) : T {
return gson.fromJson(json, type)
}
}
14 changes: 14 additions & 0 deletions core/src/main/kotlin/org/nessus/didcomm/model/MessageType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.nessus.didcomm.model

abstract class MessageType (

/**
* The header conveying the DIDComm Message Type URI.
* REQUIRED
*/
val type: String
) {
companion object {
const val OUT_OF_BAND_INVITATION = "https://didcomm.org/out-of-band/2.0/invitation"
}
}
54 changes: 38 additions & 16 deletions core/src/main/kotlin/org/nessus/didcomm/model/MessageWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,51 @@ package org.nessus.didcomm.model
import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.nimbusds.jose.util.Base64URL
import org.didcommx.didcomm.message.Message

/**
* Serializes a DIDComm Message to JSON
*/
class MessageWriter {

companion object {
private val gson: Gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()
private val prettyGson: Gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.create()

fun toJson(msg: Message, pretty: Boolean = false) : String {
return toJson(msg as Any, pretty)
object MessageWriter {

private val gson: Gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()
private val prettyGson: Gson = GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.create()

fun toBase64URL(msg: Message): String {
return Base64URL.encode(toJson(msg)).toString()
}

fun toJson(msg: Message, pretty: Boolean = false) : String {
val jsonObj = gson.toJsonTree(msg).asJsonObject
// Remove empty 'custom_headers'
// [TODO] we may have to remove emtpty content for other headers too
val customHeaders = jsonObj.getAsJsonObject("custom_headers")
if (customHeaders.entrySet().isEmpty()) {
jsonObj.remove("custom_headers")
}
return auxGson(pretty).toJson(jsonObj)
}

fun toJson(obj: Any, pretty: Boolean = false) : String {
val gson = if (pretty) prettyGson else gson
return gson.toJson(obj)
fun toJson(obj: Any, pretty: Boolean = false) : String {
return auxGson(pretty).toJson(obj)
}

fun toMutableMap(obj: Any) : MutableMap<String, Any> {
val result: MutableMap<String, Any> = mutableMapOf()
val input: String = if (obj is String) obj else gson.toJson(obj)
gson.fromJson(input, MutableMap::class.java).forEach {
en -> result[en.key as String] = en.value!!
}
return result
}

private fun auxGson(pretty: Boolean = false): Gson {
return if (pretty) prettyGson else gson
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import com.google.gson.Gson
* ]
* }
*/
data class OutOfBandInvitationV2(
data class OutOfBandInvitation(

/**
* Message ID. The id attribute value MUST be unique to the sender, across all messages they send.
Expand Down Expand Up @@ -69,19 +69,12 @@ data class OutOfBandInvitationV2(
* OPTIONAL
*/
val attachments: List<Any>?,
) {
) : MessageType(OUT_OF_BAND_INVITATION) {

companion object {

/**
* The header conveying the DIDComm MTURI.
* REQUIRED
*/
const val type: String = "https://didcomm.org/out-of-band/2.0/invitation"

fun fromBody(body: Map<String, Any?>): OutOfBandInvitationV2 {
fun fromBody(body: Map<String, Any?>): OutOfBandInvitation {
val gson = Gson()
return gson.fromJson(gson.toJson(body), OutOfBandInvitationV2::class.java)
return gson.fromJson(gson.toJson(body), OutOfBandInvitation::class.java)
}
}
}
19 changes: 19 additions & 0 deletions core/src/main/kotlin/org/nessus/didcomm/service/AgentService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.nessus.didcomm.service

import org.didcommx.didcomm.message.Message
import org.nessus.didcomm.wallet.NessusWallet

/**
* An Agent can create, send, receive DIDComMessages
*/
interface AgentService : Service {

companion object {
val type: Class<AgentService> = AgentService::class.java
}

override val type: Class<AgentService>
get() = Companion.type

fun createMessage(wallet: NessusWallet, type: String, body: Map<String, Any> = mapOf()) : Message
}
20 changes: 20 additions & 0 deletions core/src/main/kotlin/org/nessus/didcomm/service/ServiceRegistry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.nessus.didcomm.service

// [TODO] document all services
interface Service {
val type: Class<out Service>
}

// [TODO] document ServiceRegistry
object ServiceRegistry {

private val registry : MutableMap<String, Service> = mutableMapOf()

fun <T : Service> getService(type : Class<T>) : T {
return registry[type.name] as T
}

fun <T : Service> addService(service: T) {
registry[service.type.name] = service
}
}
34 changes: 34 additions & 0 deletions core/src/main/kotlin/org/nessus/didcomm/service/WalletService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.nessus.didcomm.service

import org.nessus.didcomm.wallet.NessusWallet
import org.nessus.didcomm.wallet.WalletException
import org.nessus.didcomm.wallet.WalletRegistry

interface WalletService : Service {

companion object {
val type: Class<WalletService> = WalletService::class.java
val registry = WalletRegistry()
}

override val type: Class<WalletService>
get() = Companion.type

fun assertConfigValue(config: Map<String, Any?>, key: String) : Any {
return config[key] ?: throw WalletException("No config value for: $key")
}

fun getConfigValue(config: Map<String, Any?>, key: String) : Any? {
return config[key]
}

fun hasConfigValue(config: Map<String, Any?>, key: String) : Boolean {
return config[key] != null
}

fun createWallet(config: Map<String, Any?>): NessusWallet

fun publicDid(wallet: NessusWallet): String?

fun closeAndRemove(wallet: NessusWallet?)
}
18 changes: 9 additions & 9 deletions core/src/main/kotlin/org/nessus/didcomm/wallet/Did.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import id.walt.crypto.KeyAlgorithm

val DEFAULT_KEY_ALGORITHM = KeyAlgorithm.EdDSA_Ed25519

enum class DidMethod(mname : String) {
enum class DidMethod(val mname : String) {
KEY("key"),
SOV("sov");

Expand All @@ -13,11 +13,11 @@ enum class DidMethod(mname : String) {
}
}

open class Did(method: DidMethod) {
}

class DidKey() : Did(DidMethod.KEY) {
}

class DidSov() : Did(DidMethod.SOV) {
}
//open class Did(method: DidMethod) {
//}
//
//class DidKey() : Did(DidMethod.KEY) {
//}
//
//class DidSov() : Did(DidMethod.SOV) {
//}
Loading

0 comments on commit 1c1618e

Please sign in to comment.