diff --git a/waltid-services/waltid-wallet-api/config/db.conf b/waltid-services/waltid-wallet-api/config/db.conf index b8239d563..03dcce751 100644 --- a/waltid-services/waltid-wallet-api/config/db.conf +++ b/waltid-services/waltid-wallet-api/config/db.conf @@ -25,4 +25,4 @@ dataSource { fullColumnNames = false } } -recreateDatabaseOnStart = true +recreateDatabaseOnStart = false diff --git a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/Main.kt b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/Main.kt index 49ed21b96..e25fe2cc1 100644 --- a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/Main.kt +++ b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/Main.kt @@ -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 @@ -40,6 +41,7 @@ suspend fun main(args: Array) { WaltidServices.minimalInit() WaltCryptoOci.init() Db.start() + Migration.Keys.execute() }, run = WebService(Application::webWalletModule).run() ) diff --git a/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/db/Migration.kt b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/db/Migration.kt new file mode 100644 index 000000000..2d3979c7c --- /dev/null +++ b/waltid-services/waltid-wallet-api/src/main/kotlin/id/walt/webwallet/db/Migration.kt @@ -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(document) + json["jwk"].takeIf { it is JsonPrimitive }?.let { + json["jwk"]?.jsonPrimitive?.content?.replace("\\", "")?.let { + buildJsonObject { + put("type", json["type"]!!) + put("jwk", Json.decodeFromString(it)) + } + }?.let { + Json.encodeToString(it) + } + } + } + } +} \ No newline at end of file