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

Commit

Permalink
feat(incept): expose an inception endpoint to create a controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aminbenmansour committed Oct 25, 2023
1 parent 34f00e7 commit cfa84ed
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"ncount": 1,
"isith": "1",
"nsith": "1"
}
}
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}'
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()

} 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;
}
15 changes: 15 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,8 @@ 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.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 +56,18 @@ fun Application.keri() = walletRoute {

}

post("incept/{name}", {
summary = "Initialize a prefix"
}) {
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)


}

}
}

0 comments on commit cfa84ed

Please sign in to comment.