Skip to content

Commit

Permalink
[#6] Out of band (standard) message encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tdiesler committed Dec 21, 2022
1 parent 1458cfe commit 7b8f28a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 21 deletions.
21 changes: 18 additions & 3 deletions core/src/main/kotlin/org/nessus/didcomm/model/MessageWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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

/**
Expand All @@ -19,13 +20,27 @@ class MessageWriter {
.setPrettyPrinting()
.create()

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

fun toJson(msg: Message, pretty: Boolean = false) : String {
return toJson(msg as Any, pretty)
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)
return auxGson(pretty).toJson(obj)
}

private fun auxGson(pretty: Boolean = false): Gson {
return if (pretty) prettyGson else gson
}
}
}
62 changes: 45 additions & 17 deletions core/src/test/kotlin/org/nessus/didcomm/test/model/Fixtures.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.nessus.didcomm.test.model

import org.didcommx.didcomm.message.Attachment
import org.didcommx.didcomm.message.Message
import org.nessus.didcomm.model.OutOfBandInvitationV2
import org.nessus.didcomm.model.MessageParser

class OutOfBand {

Expand All @@ -11,20 +9,50 @@ class OutOfBand {
const val ALICE_DID = "did:example:alice"
const val FABER_DID = "did:example:faber"

private const val ID = "1234567890"
private const val TYPE = OutOfBandInvitationV2.type
val FABER_OUT_OF_BAND_INVITATION = MessageParser.fromJson("""
{
"type": "https://didcomm.org/out-of-band/2.0/invitation",
"id": "1234567890",
"from": "did:example:faber",
"body": {
"goal_code": "issue-vc",
"goal": "To issue a Faber College Graduate credential",
"accept": [
"didcomm/v2",
"didcomm/aip2;env=rfc587"
]
},
"attachments": [
{
"id": "request-0",
"media_type": "application/json",
"data": {
"json": {"protocol message": "content"}
}
}
]
}
""".trimIndent())

val OUT_OF_BAND_INVITATION = Message.builder(ID, mapOf(
"goal_code" to "issue-vc",
"goal" to "To issue a Faber College Graduate credential",
"accept" to listOf("didcomm/v2", "didcomm/aip2;env=rfc587")),
TYPE)
.from(FABER_DID)
.createdTime(1516269022)
.expiresTime(1516385931)
.attachments(listOf(
Attachment.builder(
"request-0", Attachment.Data.parse(mapOf("base64" to "qwerty"))).build()))
.build()
val ALICE_OUT_OF_BAND_INVITATION = MessageParser.fromJson("""
{
"type": "https://didcomm.org/out-of-band/2.0/invitation",
"id": "69212a3a-d068-4f9d-a2dd-4741bca89af3",
"from": "did:example:alice",
"body": {
"goal_code": "",
"goal": ""
},
"attachments": [
{
"id": "request-0",
"media_type": "application/json",
"data": {
"base64": "qwerty"
}
}
]
}
""".trimIndent())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OutOfBandInvitationTest {
@Test
fun testOutOfBandInvitation() {

val exp: Message = OutOfBand.OUT_OF_BAND_INVITATION
val exp: Message = OutOfBand.FABER_OUT_OF_BAND_INVITATION
val expBody = OutOfBandInvitationV2.fromBody(exp.body)
val expJson: String = MessageWriter.toJson(exp)
log.info("exp: {}", expJson)
Expand All @@ -31,4 +31,15 @@ class OutOfBandInvitationTest {
assertEquals(expBody, wasBody)
}

@Test
fun testOutOfBandEncoding() {
val exp: Message = OutOfBand.ALICE_OUT_OF_BAND_INVITATION
val expJson: String = MessageWriter.toJson(exp)
log.info("exp: {}", expJson)

val base64URLEncoded = MessageWriter.toBase64URL(exp)
log.info("enc: {}", base64URLEncoded)
assertEquals("eyJpZCI6IjY5MjEy", base64URLEncoded.substring(0..15))

}
}

0 comments on commit 7b8f28a

Please sign in to comment.