This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
38 changed files
with
945 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,18 @@ import id.walt.config.ConfigManager | |
import id.walt.config.DatabaseConfiguration | ||
import id.walt.config.DatasourceConfiguration | ||
import id.walt.db.models.* | ||
import id.walt.db.repositories.DbCredential | ||
import id.walt.db.repositories.DbDid | ||
import id.walt.db.repositories.DbKey | ||
import id.walt.service.Did | ||
import id.walt.service.account.AccountsService | ||
import id.walt.service.credentials.CredentialsService | ||
import id.walt.service.dids.DidInsertDataObject | ||
import id.walt.service.dids.DidsService | ||
import id.walt.service.keys.KeysService | ||
import id.walt.web.model.EmailLoginRequest | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.flywaydb.core.Flyway | ||
import org.jetbrains.exposed.sql.Database | ||
import org.jetbrains.exposed.sql.SchemaUtils | ||
import org.jetbrains.exposed.sql.StdOutSqlLogger | ||
|
@@ -15,6 +24,7 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager | |
import org.jetbrains.exposed.sql.transactions.transaction | ||
import org.slf4j.bridge.SLF4JBridgeHandler | ||
import java.sql.Connection | ||
import java.util.* | ||
|
||
object Db { | ||
|
||
|
@@ -25,11 +35,11 @@ object Db { | |
val databaseConfig = ConfigManager.getConfig<DatabaseConfiguration>() | ||
|
||
//migrate | ||
/*Flyway.configure() | ||
Flyway.configure() | ||
.locations(databaseConfig.database.replace(".", "/")) | ||
.dataSource(datasourceConfig.hikariDataSource) | ||
.load() | ||
.migrate()*/ | ||
.migrate() | ||
|
||
// connect | ||
log.info { "Connecting to database at \"${datasourceConfig.hikariDataSource.jdbcUrl}\"..." } | ||
|
@@ -57,42 +67,43 @@ object Db { | |
"TRANSACTION_SERIALIZABLE" -> Connection.TRANSACTION_SERIALIZABLE | ||
else -> Connection.TRANSACTION_SERIALIZABLE | ||
} | ||
|
||
suspend fun init() { | ||
val databaseConfig = ConfigManager.getConfig<DatabaseConfiguration>() | ||
transaction { | ||
if (databaseConfig.recreate_schema) { | ||
println("DROP SCHEMA") | ||
SchemaUtils.drop( | ||
WalletOperationHistories, | ||
WalletKeys, | ||
WalletDids, | ||
WalletCredentials, | ||
AccountWallets, | ||
Accounts, | ||
Emails, | ||
Wallets | ||
) | ||
} | ||
println("CREATE SCHEMA IF NOT EXISTING") | ||
SchemaUtils.create( | ||
Wallets, | ||
Emails, | ||
Accounts, | ||
AccountWallets, | ||
WalletCredentials, | ||
WalletDids, | ||
WalletKeys, | ||
WalletOperationHistories | ||
) | ||
} | ||
|
||
if (databaseConfig.recreate_schema) { | ||
val accountId = AccountsService.register(EmailLoginRequest("[email protected]", "password")).getOrThrow().id | ||
println("CREATED ACCOUNT: $accountId") | ||
} | ||
/** moved to [AccountsService.register] **/ | ||
// val did = WalletServiceManager.getWalletService(accountId).createDid("key") | ||
// println("CREATED DID: $did") | ||
} | ||
} | ||
fun main(){ | ||
ConfigManager.loadConfigs(emptyArray()) | ||
Db.start() | ||
// val account = AccountsService.register(EmailLoginRequest("username", "password")).getOrThrow().id | ||
val account = UUID.fromString("04e595ac-7c48-4482-9ea6-8ae0981251c8") | ||
println(account) | ||
val key = KeysService.add(account, DbKey(keyId = "keyId", document = "document")) | ||
println(key) | ||
val did = DidsService.add( | ||
account, DidInsertDataObject( | ||
key = key, | ||
did = Did(did = "did", document = "document") | ||
) | ||
) | ||
println(did) | ||
val cid = CredentialsService.add(account, DbCredential( | ||
credentialId = "credentialId", | ||
document = "document" | ||
)) | ||
println(cid) | ||
// ConfigManager.loadConfigs(emptyArray()) | ||
// val datasourceConfig = ConfigManager.getConfig<DatasourceConfiguration>() | ||
// Database.connect(datasourceConfig.hikariDataSource) | ||
// transaction { | ||
// SchemaUtils.create( | ||
// Accounts, | ||
// Emails, | ||
// Wallets, | ||
// AccountWallets, | ||
// WalletOperationHistories, | ||
// Keys, | ||
// Dids, | ||
// Credentials, | ||
// AccountKeys, | ||
// AccountDids, | ||
// AccountCredentials, | ||
// ) | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package id.walt.db.models | ||
|
||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
|
||
object AccountCredentials : UUIDTable("account_credentials") { | ||
val account = reference("account", Accounts) | ||
val credential = reference("credential", Credentials) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package id.walt.db.models | ||
|
||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
|
||
object AccountDids: UUIDTable("account_dids") { | ||
val account = reference("account", Accounts) | ||
val did = reference("did", Dids) | ||
val alias = varchar("alias", 1024) | ||
val default = bool("default").default(false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package id.walt.db.models | ||
|
||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
|
||
object AccountKeys : UUIDTable("account_keys") { | ||
val account = reference("account", Accounts) | ||
val key = reference("key", Keys) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package id.walt.db.models | ||
|
||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
|
||
object Credentials : UUIDTable() { | ||
val credentialId = varchar("cid", 256).uniqueIndex() | ||
val document = text("document") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package id.walt.db.models | ||
|
||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
|
||
object Dids : UUIDTable() { | ||
val did = varchar("did", 1024).uniqueIndex() | ||
val document = text("document") | ||
val key = reference("key", Keys) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package id.walt.db.models | ||
|
||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
|
||
object Keys : UUIDTable() { | ||
val keyId = varchar("kid", 512).uniqueIndex() | ||
val document = text("document") | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
24 changes: 2 additions & 22 deletions
24
src/main/kotlin/id/walt/db/models/WalletOperationHistories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,11 @@ | ||
package id.walt.db.models | ||
|
||
import id.walt.service.WalletService | ||
import id.walt.utils.JsonUtils.toJsonPrimitives | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.JsonElement | ||
import org.jetbrains.exposed.dao.id.UUIDTable | ||
import org.jetbrains.exposed.sql.javatime.timestamp | ||
|
||
object WalletOperationHistories : UUIDTable() { | ||
val account = reference("account", Accounts.id) | ||
val account = reference("account", Accounts) | ||
val timestamp = timestamp("timestamp") | ||
val operation = varchar("operation", 48) | ||
val data = text("data") | ||
} | ||
|
||
@Serializable | ||
data class WalletOperationHistory( | ||
val accountId: String, | ||
//val walletId: String, | ||
val timestamp: Instant, | ||
val operation: String, | ||
val data: Map<String, JsonElement> | ||
) { | ||
companion object { | ||
fun new(wallet: WalletService, operation: String, data: Map<String, Any?>) = | ||
WalletOperationHistory(wallet.accountId.toString(), Clock.System.now(), operation, data.toJsonPrimitives()) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/id/walt/db/repositories/AccountCredentialsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package id.walt.db.repositories | ||
|
||
import id.walt.db.models.AccountCredentials | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
import org.jetbrains.exposed.sql.ResultRow | ||
import org.jetbrains.exposed.sql.statements.InsertStatement | ||
import java.util.* | ||
|
||
object AccountCredentialsRepository : RepositoryBase<DbAccountCredentials>(AccountCredentials) { | ||
override fun ResultRow.fromRow(): DbAccountCredentials = DbAccountCredentials( | ||
id = this[AccountCredentials.id].value, | ||
account = this[AccountCredentials.account].value, | ||
credential = this[AccountCredentials.credential].value, | ||
) | ||
|
||
override fun DbAccountCredentials.toRow(insertStatement: InsertStatement<EntityID<UUID>>): InsertStatement<EntityID<UUID>> = | ||
let { | ||
insertStatement[AccountCredentials.account] = it.account | ||
insertStatement[AccountCredentials.credential] = it.credential | ||
insertStatement | ||
} | ||
} | ||
|
||
data class DbAccountCredentials( | ||
override val id: UUID? = null, | ||
val account: UUID, | ||
val credential: UUID, | ||
) : DbEntity() |
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/id/walt/db/repositories/AccountDidsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package id.walt.db.repositories | ||
|
||
import id.walt.db.models.AccountDids | ||
import org.jetbrains.exposed.dao.id.EntityID | ||
import org.jetbrains.exposed.sql.Column | ||
import org.jetbrains.exposed.sql.ResultRow | ||
import org.jetbrains.exposed.sql.select | ||
import org.jetbrains.exposed.sql.statements.InsertStatement | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import java.util.* | ||
|
||
object AccountDidsRepository : RepositoryBase<DbAccountDids>(AccountDids) { | ||
override fun ResultRow.fromRow(): DbAccountDids = DbAccountDids( | ||
id = this[AccountDids.id].value, | ||
account = this[AccountDids.account].value, | ||
did = this[AccountDids.did].value, | ||
alias = this[AccountDids.alias], | ||
default = this[AccountDids.default], | ||
) | ||
|
||
override fun DbAccountDids.toRow(insertStatement: InsertStatement<EntityID<UUID>>): InsertStatement<EntityID<UUID>> = | ||
let { | ||
insertStatement[AccountDids.account] = it.account | ||
insertStatement[AccountDids.did] = it.did | ||
insertStatement[AccountDids.alias] = it.alias | ||
insertStatement[AccountDids.default] = it.default | ||
insertStatement | ||
} | ||
} | ||
|
||
data class DbAccountDids( | ||
override val id: UUID? = null, | ||
val account: UUID, | ||
val did: UUID, | ||
val alias: String, | ||
val default: Boolean, | ||
) : DbEntity() |
Oops, something went wrong.