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

Commit

Permalink
Updated IDs in models to be able to remove all the conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
waltkb committed Nov 29, 2023
1 parent 3e287d8 commit 3a118d6
Show file tree
Hide file tree
Showing 23 changed files with 93 additions and 99 deletions.
11 changes: 6 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ dependencies {

// UUID
implementation("app.softwork:kotlinx-uuid-core:0.0.22")
implementation("app.softwork:kotlinx-uuid-exposed:0.0.22")

/* -- Security -- */
// Bouncy Castle
Expand All @@ -106,11 +107,11 @@ dependencies {
implementation("io.github.reactivecircus.cache4k:cache4k:0.11.0")

// DB
implementation("org.jetbrains.exposed:exposed-core:0.44.0")
implementation("org.jetbrains.exposed:exposed-jdbc:0.44.0")
implementation("org.jetbrains.exposed:exposed-dao:0.44.0")
implementation("org.jetbrains.exposed:exposed-java-time:0.44.0")
implementation("org.jetbrains.exposed:exposed-json:0.44.0")
implementation("org.jetbrains.exposed:exposed-core:0.44.1")
implementation("org.jetbrains.exposed:exposed-jdbc:0.44.1")
implementation("org.jetbrains.exposed:exposed-dao:0.44.1")
implementation("org.jetbrains.exposed:exposed-java-time:0.44.1")
implementation("org.jetbrains.exposed:exposed-json:0.44.1")
implementation("org.xerial:sqlite-jdbc:3.42.0.0")
implementation("org.postgresql:postgresql:42.6.0")
// migration
Expand Down
16 changes: 14 additions & 2 deletions src/main/kotlin/id/walt/db/Db.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object Db {
Accounts,
Web3Wallets
)
SchemaUtils.create(
/*SchemaUtils.create(
Web3Wallets,
Accounts,
//AccountWeb3WalletMappings,
Expand All @@ -72,7 +72,19 @@ object Db {
WalletDids,
WalletOperationHistories,
Issuers
)
)*/

SchemaUtils.create(Web3Wallets)
SchemaUtils.create(Accounts)
//AccountWeb3WalletMappings,
SchemaUtils.create(Wallets)
SchemaUtils.create(AccountWalletMappings)
SchemaUtils.create(WalletCredentials)
SchemaUtils.create(WalletKeys)
SchemaUtils.create(WalletDids)
SchemaUtils.create(WalletOperationHistories)
SchemaUtils.create(Issuers)


runBlocking {
AccountsService.register(EmailAccountRequest("Max Mustermann", "[email protected]", "string"))
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/id/walt/db/models/Accounts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import kotlinx.datetime.Instant
import kotlinx.datetime.toKotlinInstant
import kotlinx.serialization.Serializable
import kotlinx.uuid.UUID
import kotlinx.uuid.toKotlinUUID
import org.jetbrains.exposed.dao.id.UUIDTable
import kotlinx.uuid.exposed.KotlinxUUIDTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.javatime.timestamp

object Accounts : UUIDTable("accounts") {
object Accounts : KotlinxUUIDTable("accounts") {
val name = varchar("name", 128).nullable()

val email = varchar("email", 128).nullable().uniqueIndex()
val password = varchar("password", 200).nullable()

// val loginWeb3Wallet = uuid("web3wallet").nullable()
// val loginWeb3Wallet = kotlinxUUID("web3wallet").nullable()

val createdOn = timestamp("createdOn")

Expand All @@ -38,10 +37,10 @@ data class Account(
val createdOn: Instant
) {
constructor(result: ResultRow) : this(
id = result[Accounts.id].value.toKotlinUUID(),
id = result[Accounts.id].value,
name = result[Accounts.name],
email = result[Accounts.email],
//loginWeb3Wallet = result[Accounts.loginWeb3Wallet]?.toKotlinUUID(),
//loginWeb3Wallet = result[Accounts.loginWeb3Wallet]?,
createdOn = result[Accounts.createdOn].toKotlinInstant()
)
}
3 changes: 1 addition & 2 deletions src/main/kotlin/id/walt/db/models/WalletCredentials.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonObject
import kotlinx.uuid.UUID
import kotlinx.uuid.toKotlinUUID
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.javatime.timestamp
Expand Down Expand Up @@ -75,7 +74,7 @@ data class WalletCredential(


constructor(result: ResultRow) : this(
wallet = result[WalletCredentials.wallet].value.toKotlinUUID(),
wallet = result[WalletCredentials.wallet].value,
id = result[WalletCredentials.id],
document = result[WalletCredentials.document],
disclosures = result[WalletCredentials.disclosures],
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/id/walt/db/models/WalletOperationHistories.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.uuid.UUID
import kotlinx.uuid.exposed.KotlinxUUIDTable
import kotlinx.uuid.generateUUID
import kotlinx.uuid.toKotlinUUID
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.javatime.timestamp

object WalletOperationHistories : UUIDTable("wallet_operation_histories") {
object WalletOperationHistories : KotlinxUUIDTable("wallet_operation_histories") {
val account = reference("account", Accounts)
val wallet = reference("wallet", Wallets)
val timestamp = timestamp("timestamp")
Expand All @@ -32,9 +31,9 @@ data class WalletOperationHistory(
val data: String,
) {
constructor(result: ResultRow) : this(
id = result[WalletOperationHistories.id].value.toKotlinUUID(),
account = result[WalletOperationHistories.account].value.toKotlinUUID(),
wallet = result[WalletOperationHistories.wallet].value.toKotlinUUID(),
id = result[WalletOperationHistories.id].value,
account = result[WalletOperationHistories.account].value,
wallet = result[WalletOperationHistories.wallet].value,
timestamp = result[WalletOperationHistories.timestamp].toKotlinInstant(),
operation = result[WalletOperationHistories.operation],
data = result[WalletOperationHistories.data],
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/id/walt/db/models/Wallets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import kotlinx.datetime.Instant
import kotlinx.datetime.toKotlinInstant
import kotlinx.serialization.Serializable
import kotlinx.uuid.UUID
import kotlinx.uuid.toKotlinUUID
import org.jetbrains.exposed.dao.id.UUIDTable
import kotlinx.uuid.exposed.KotlinxUUIDTable
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.javatime.timestamp

object Wallets : UUIDTable("wallets") {
object Wallets : KotlinxUUIDTable("wallets") {
val name = varchar("name", 128)
val createdOn = timestamp("createdOn")
}
Expand All @@ -21,7 +20,7 @@ data class Wallet(
val createdOn: Instant
) {
constructor(result: ResultRow) : this(
id = result[Wallets.id].value.toKotlinUUID(),
id = result[Wallets.id].value,
name = result[Wallets.name],
createdOn = result[Wallets.createdOn].toKotlinInstant()
)
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/id/walt/db/models/Web3Wallets.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package id.walt.db.models

import kotlinx.uuid.exposed.autoGenerate
import kotlinx.uuid.exposed.kotlinxUUID
import org.jetbrains.exposed.sql.Table

object Web3Wallets : Table("web3wallets") {
val account = reference("account", Accounts)
val id = uuid("id").autoGenerate()
val id = kotlinxUUID("id").autoGenerate()

val address = varchar("address", 256).uniqueIndex()
val ecosystem = varchar("ecosystem", 128)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/id/walt/db/models/todo/AccountIssuers.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package id.walt.db.models.todo

import id.walt.db.models.Accounts
import org.jetbrains.exposed.dao.id.UUIDTable
import kotlinx.uuid.exposed.KotlinxUUIDTable

object AccountIssuers : UUIDTable("account_issuers") {
object AccountIssuers : KotlinxUUIDTable("account_issuers") {
val account = reference("account", Accounts)
val issuer = reference("issuer", Issuers)

Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/id/walt/db/models/todo/Issuers.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package id.walt.db.models.todo

import kotlinx.uuid.UUID
import kotlinx.uuid.toKotlinUUID
import org.jetbrains.exposed.dao.id.UUIDTable
import kotlinx.uuid.exposed.KotlinxUUIDTable
import org.jetbrains.exposed.sql.ResultRow

object Issuers : UUIDTable("issuers") {
object Issuers : KotlinxUUIDTable("issuers") {
val name = varchar("name", 128).uniqueIndex()
val description = text("description").nullable().default("no description")
val uiEndpoint = varchar("ui", 128)
Expand All @@ -20,7 +19,7 @@ data class Issuer(
val configurationEndpoint: String,
) {
constructor(result: ResultRow) : this(
id = result[Issuers.id].value.toKotlinUUID(),
id = result[Issuers.id].value,
name = result[Issuers.name],
description = result[Issuers.description],
uiEndpoint = result[Issuers.uiEndpoint],
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/id/walt/service/SSIKit2WalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
import kotlinx.uuid.UUID
import kotlinx.uuid.toJavaUUID
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
Expand Down Expand Up @@ -201,8 +200,9 @@ class SSIKit2WalletService(accountId: UUID, walletId: UUID) : WalletService(acco

println("Resolved presentation definition: ${presentationSession.authorizationRequest!!.presentationDefinition!!.toJSONString()}")

val tokenResponse = credentialWallet.processImplicitFlowAuthorization(presentationSession.authorizationRequest!!)
val resp = ktorClient.submitForm(presentationSession.authorizationRequest!!.responseUri!!,
val tokenResponse = credentialWallet.processImplicitFlowAuthorization(presentationSession.authorizationRequest)
val resp = ktorClient.submitForm(
presentationSession.authorizationRequest.responseUri!!,
parameters {
tokenResponse.toHttpParameters().forEach { entry ->
entry.value.forEach { append(entry.key, it) }
Expand Down Expand Up @@ -504,7 +504,7 @@ class SSIKit2WalletService(accountId: UUID, walletId: UUID) : WalletService(acco
transaction {
WalletKeys.insert {
it[WalletKeys.keyId] = keyId
it[wallet] = walletId.toJavaUUID()
it[wallet] = walletId
it[document] = KeySerialization.serializeKey(key)
it[createdOn] = Clock.System.now().toJavaInstant()
}
Expand Down Expand Up @@ -547,7 +547,7 @@ class SSIKit2WalletService(accountId: UUID, walletId: UUID) : WalletService(acco

override fun getHistory(limit: Int, offset: Int): List<WalletOperationHistory> =
WalletOperationHistories
.select { WalletOperationHistories.wallet eq walletId.toJavaUUID() }
.select { WalletOperationHistories.wallet eq walletId }
.orderBy(WalletOperationHistories.timestamp)
.limit(10)
.map { row ->
Expand All @@ -557,8 +557,8 @@ class SSIKit2WalletService(accountId: UUID, walletId: UUID) : WalletService(acco
override suspend fun addOperationHistory(operationHistory: WalletOperationHistory) {
transaction {
WalletOperationHistories.insert {
it[account] = operationHistory.account.toJavaUUID()
it[wallet] = operationHistory.wallet.toJavaUUID()
it[account] = operationHistory.account
it[wallet] = operationHistory.wallet
it[timestamp] = operationHistory.timestamp.toJavaInstant()
it[operation] = operationHistory.operation
it[data] = Json.encodeToString(operationHistory.data)
Expand Down
9 changes: 4 additions & 5 deletions src/main/kotlin/id/walt/service/WalletKitWalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
import kotlinx.uuid.UUID
import kotlinx.uuid.toJavaUUID
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
Expand Down Expand Up @@ -60,7 +59,7 @@ class WalletKitWalletService(accountId: UUID, walletId: UUID) : WalletService(ac

private val userEmail: String by lazy {
transaction {
Accounts.select { Accounts.id eq this@WalletKitWalletService.walletId.toJavaUUID() }
Accounts.select { Accounts.id eq this@WalletKitWalletService.walletId }
.single()[Accounts.email]
} ?: throw IllegalArgumentException("No such account: ${this.walletId}")
}
Expand Down Expand Up @@ -339,7 +338,7 @@ class WalletKitWalletService(accountId: UUID, walletId: UUID) : WalletService(ac

override fun getHistory(limit: Int, offset: Int): List<WalletOperationHistory> = transaction {
WalletOperationHistories
.select { WalletOperationHistories.account eq walletId.toJavaUUID() }
.select { WalletOperationHistories.account eq walletId }
.orderBy(WalletOperationHistories.timestamp)
.limit(10)
.map { WalletOperationHistory(it) }
Expand All @@ -348,8 +347,8 @@ class WalletKitWalletService(accountId: UUID, walletId: UUID) : WalletService(ac
override suspend fun addOperationHistory(operationHistory: WalletOperationHistory) {
transaction {
WalletOperationHistories.insert {
it[wallet] = walletId.toJavaUUID()
it[account] = accountId.toJavaUUID()
it[wallet] = walletId
it[account] = accountId
it[timestamp] = operationHistory.timestamp.toJavaInstant()
it[operation] = operationHistory.operation
it[data] = Json.encodeToString(operationHistory.data)
Expand Down
12 changes: 5 additions & 7 deletions src/main/kotlin/id/walt/service/WalletServiceManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import id.walt.service.nft.NftService
import kotlinx.datetime.Clock
import kotlinx.datetime.toJavaInstant
import kotlinx.uuid.UUID
import kotlinx.uuid.toJavaUUID
import kotlinx.uuid.toKotlinUUID
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import java.util.concurrent.ConcurrentHashMap
Expand All @@ -31,12 +29,12 @@ object WalletServiceManager {
val walletId = Wallets.insert {
it[name] = "Wallet of $accountName"
it[createdOn] = Clock.System.now().toJavaInstant()
}[Wallets.id].value.toKotlinUUID()
}[Wallets.id].value

println("Creating wallet mapping: $forAccount -> $walletId")
AccountWalletMappings.insert {
it[account] = forAccount.toJavaUUID()
it[wallet] = walletId.toJavaUUID()
it[account] = forAccount
it[wallet] = walletId
it[permissions] = AccountWalletPermissions.ADMINISTRATE
it[addedOn] = Clock.System.now().toJavaInstant()
}
Expand All @@ -46,8 +44,8 @@ object WalletServiceManager {

@Deprecated(replaceWith = ReplaceWith("AccountsService.getAccountWalletMappings(account)", "id.walt.service.account.AccountsService"), message = "depreacted")
fun listWallets(account: UUID): List<UUID> =
AccountWalletMappings.innerJoin(Wallets).select { AccountWalletMappings.account eq account.toJavaUUID() }.map {
it[Wallets.id].value.toKotlinUUID()
AccountWalletMappings.innerJoin(Wallets).select { AccountWalletMappings.account eq account }.map {
it[Wallets.id].value
}

fun getNftService(): NftService = NftKitNftService()
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/id/walt/service/Web3WalletService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import id.walt.service.dto.LinkedWalletDataTransferObject
import id.walt.service.dto.WalletDataTransferObject
import kotlinx.uuid.UUID
import kotlinx.uuid.generateUUID
import kotlinx.uuid.toJavaUUID
import kotlinx.uuid.toKotlinUUID
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction
Expand All @@ -20,8 +18,8 @@ object Web3WalletService {
*/
fun link(accountId: UUID, wallet: WalletDataTransferObject): LinkedWalletDataTransferObject =
Web3Wallets.insert {
it[account] = accountId.toJavaUUID()
it[id] = UUID.generateUUID().toJavaUUID()
it[account] = accountId
it[id] = UUID.generateUUID()
it[address] = wallet.address
it[ecosystem] = wallet.ecosystem
it[owner] = false
Expand All @@ -34,7 +32,7 @@ object Web3WalletService {
* @return true - if operation succeeded, false - otherwise
*/
fun unlink(accountId: UUID, walletId: UUID): Boolean = transaction {
Web3Wallets.deleteWhere { (account eq accountId.toJavaUUID()) and (id eq walletId.toJavaUUID()) }
Web3Wallets.deleteWhere { (account eq accountId) and (id eq walletId) }
} == 1

/**
Expand Down Expand Up @@ -71,9 +69,9 @@ object Web3WalletService {
* === 2 different accounts exist, which the user should have access to (merged) when logging in with web3-address
*/
transaction {
Web3Wallets.select { Web3Wallets.account eq accountId.toJavaUUID() }.map {
Web3Wallets.select { Web3Wallets.account eq accountId }.map {
LinkedWalletDataTransferObject(
it[Web3Wallets.id].toKotlinUUID(),
it[Web3Wallets.id],
it[Web3Wallets.address],
it[Web3Wallets.ecosystem],
it[Web3Wallets.owner]
Expand All @@ -83,7 +81,7 @@ object Web3WalletService {

private fun setIsOwner(accountId: UUID, walletId: UUID, isOwner: Boolean) = transaction {
Web3Wallets.update(
{ (Web3Wallets.account eq accountId.toJavaUUID()) and (Web3Wallets.id eq walletId.toJavaUUID()) }
{ (Web3Wallets.account eq accountId) and (Web3Wallets.id eq walletId) }
) {
it[owner] = isOwner
}
Expand Down
Loading

0 comments on commit 3a118d6

Please sign in to comment.