Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keys migration (document deserialization error) #624

Closed
wants to merge 1 commit into from
Closed
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
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 Down Expand Up @@ -40,6 +41,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)
}
}
}
}
}
Loading