Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Expose an inception endpoint for KERI #45

Merged
merged 2 commits into from
Oct 25, 2023
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 @@ -10,4 +10,4 @@
"ncount": 1,
"isith": "1",
"nsith": "1"
aminbenmansour marked this conversation as resolved.
Show resolved Hide resolved
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/id/walt/service/dto/KeriUserManagementObjects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ data class KeriCreateDbResponse (
val salt: String,
val witnesses: List<String>
)

@Serializable
data class KeriInceptionRequest (
val alias: String,
val passcode: String
)
@Serializable
data class KeriInceptionResponse (
val aid: String,
val did: String,
val publicKeys: List<String>
)
73 changes: 73 additions & 0 deletions src/main/kotlin/id/walt/service/keri/KeriInceptionService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package id.walt.service.keri

import id.walt.service.dto.KeriInceptionResponse
import id.walt.service.keri.interfaces.KeriInceptionInterface
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

class KeriInceptionService: KeriInceptionInterface {

private val CONFIG_DIR = "config"
private val INCEPTION_FILE = "inception-config.json"
override fun inceptController(name: String, alias: String, passcode: String): KeriInceptionResponse {
var prefix = ""
val publicKeys = mutableListOf<String>()

val inceptCommand: List<String> = listOf(
"kli", "incept",
"--name" ,name,
"--alias", alias,
"--passcode", passcode,
"--file", "$CONFIG_DIR/keri/$INCEPTION_FILE"
)

// kli status --name waltid --alias waltid-alias | awk '/Identifier:/ {print $2}'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a wrong passcode is provided the request can go indefinitely. We should provide a TTL.

val statusCommand: List<String> = listOf(
"kli", "status",
"--name" ,name,
"--alias", alias,
"--passcode", passcode
)

try {
val processBuilder = ProcessBuilder(inceptCommand)
val process = processBuilder.start()
process.waitFor()

val statusProcessBuilder = ProcessBuilder(statusCommand)
val statusProcess = statusProcessBuilder.start()

val prefixInputReader = BufferedReader(InputStreamReader(statusProcess.inputStream))

var line: String?
var capturingPublicKeys = false
while (prefixInputReader.readLine().also { line = it } != null) {
if (line?.contains("Identifier:") == true) {
prefix = line!!.split(" ")[1]
}

if (line?.startsWith("Public Keys:") == true) {
capturingPublicKeys = true
continue
}

if (capturingPublicKeys && line?.isNotBlank() == true) {
publicKeys.add(line!!.trim().split(" ")[1])
} else if (capturingPublicKeys && line?.isBlank() == true) {
break
}
}

statusProcess.waitFor()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Provide an alternative response in case the bash command returns an error code. Mostly related to user input errors.

} catch(e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}

return KeriInceptionResponse(prefix, "did:keri:$prefix", publicKeys)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package id.walt.service.keri.interfaces

import id.walt.service.dto.KeriInceptionResponse

interface KeriInceptionInterface {
fun inceptController(name: String, alias: String, passcode: String): KeriInceptionResponse;
}
41 changes: 41 additions & 0 deletions src/main/kotlin/id/walt/web/controllers/KeriController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package id.walt.web.controllers

import id.walt.service.dto.KeriCreateDbRequest
import id.walt.service.dto.KeriCreateDbResponse
import id.walt.service.dto.KeriInceptionRequest
import id.walt.service.dto.KeriInceptionResponse
import id.walt.service.keri.KeriInceptionService
import id.walt.service.keri.KeriInitService
import io.github.smiley4.ktorswaggerui.dsl.post
import io.github.smiley4.ktorswaggerui.dsl.route
Expand Down Expand Up @@ -54,5 +57,43 @@ fun Application.keri() = walletRoute {

}

post("incept/{name}", {
summary = "Create an inception event to initiate a controller"

request {
pathParameter<String>("name") {
description = "keystore name and file location of KERI keystore"
example = "waltid"
}

body<KeriInceptionRequest> {
description = "Required data for the inception of a controller"
example("application/json", KeriInceptionRequest(alias = "waltid-alias", passcode = "0123456789abcdefghijk"))
}

}
response {
HttpStatusCode.Created to {
body<String> {
description = "The identifiers of the controller. An AID, DID and public keys"
example("application/json", KeriInceptionResponse(aid = "EPjNBI4spoZ3lU5OCtNO4QgJmhIw7P1T-JOtwit36do4", did = "did:keri:EPjNBI4spoZ3lU5OCtNO4QgJmhIw7P1T-JOtwit36do4", publicKeys = listOf(
"DOERpUwwYCSyWH8e8yLJDKZpvEly_oJ8QJPw2wH3qRVQ"
))) {
summary = "Example of creating an inception event (First event in a Key Event Log (KEL))"
}
}
}
}
}) {
val name = call.parameters["name"] ?: return@post call.respond(HttpStatusCode.BadRequest)
val dto = call.receive<KeriInceptionRequest>()
val response = KeriInceptionService().inceptController(name, dto.alias, dto.passcode)


call.respond(HttpStatusCode.Created, response)


}

}
}
Loading