Skip to content

Commit

Permalink
fix: keys migration (document deserialization error)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplotean committed Aug 14, 2024
1 parent e1c7454 commit 0b5f6ac
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion waltid-services/waltid-wallet-api/config/db.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ dataSource {
fullColumnNames = false
}
}
recreateDatabaseOnStart = true
recreateDatabaseOnStart = false
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import id.walt.commons.web.WebService
import id.walt.crypto.keys.oci.WaltCryptoOci
import id.walt.did.helpers.WaltidServices
import id.walt.webwallet.db.Db
import id.walt.webwallet.db.Migration
import id.walt.webwallet.web.Administration.configureAdministration
import id.walt.webwallet.web.controllers.*
import id.walt.webwallet.web.controllers.NotificationController.notifications
Expand All @@ -35,6 +36,7 @@ suspend fun main(args: Array<String>) {
WaltidServices.minimalInit()
WaltCryptoOci.init()
Db.start()
Migration.Keys.execute()
},
run = WebService(Application::webWalletModule).run()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package id.walt.webwallet.db

import id.walt.webwallet.db.models.WalletKey
import id.walt.webwallet.db.models.WalletKeys
import kotlinx.datetime.toJavaInstant
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.batchUpsert
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction

object Migration {
/**
* Migrate from jwk key string representation to json object
*/
object Keys {
fun execute() = transaction {
val conversion = WalletKeys.selectAll().mapNotNull { row ->
val wallet = row[WalletKeys.wallet].value
val key = WalletKey(row)
convertDocument(key.document)?.let {
Pair(
wallet, key.copy(
document = it
)
)
}
}
WalletKeys.batchUpsert(conversion) {
this[WalletKeys.document] = it.second.document
this[WalletKeys.keyId] = it.second.keyId
this[WalletKeys.wallet] = it.first
this[WalletKeys.createdOn] = it.second.createdOn.toJavaInstant()
}
}

private fun convertDocument(document: String): String? = let {
val json = Json.decodeFromString<JsonObject>(document)
json["jwk"].takeIf { it is JsonPrimitive }?.let {
json["jwk"]?.jsonPrimitive?.content?.replace("\\", "")?.let {
buildJsonObject {
put("type", json["type"]!!)
put("jwk", Json.decodeFromString<JsonObject>(it))
}
}?.let {
Json.encodeToString(it)
}
}
}
}
}

0 comments on commit 0b5f6ac

Please sign in to comment.