Skip to content

Commit

Permalink
Merge pull request #31 from roots-id/21-add-external-persistence-storage
Browse files Browse the repository at this point in the history
Refactor #21 - Add external persistence storage
  • Loading branch information
rogelio-blanco authored Jun 30, 2022
2 parents 49bb96f + abd62b6 commit bdb0d51
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 152 deletions.
130 changes: 130 additions & 0 deletions src/main/kotlin/com/rootsid/wal/library/mongoimpl/WalletDocStorage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.rootsid.wal.library.mongoimpl

import com.mongodb.client.MongoCollection
import com.mongodb.client.MongoDatabase
import com.rootsid.wal.library.mongoimpl.config.DefaultMongoDbConn
import com.rootsid.wal.library.mongoimpl.document.WalletDocument
import com.rootsid.wal.library.wallet.model.Wallet
import com.rootsid.wal.library.wallet.storage.WalletStorage
import org.litote.kmongo.*


class WalletDocStorage(db: MongoDatabase? = null, collectionName: String = "wallet") : WalletStorage {
private val walletCollection: MongoCollection<WalletDocument>

init {
val mongoConn = db ?: DefaultMongoDbConn.open()

this.walletCollection = mongoConn.getCollection<WalletDocument>(collectionName)
}

/**
* Insert wallet
*
* @param wallet Wallet data object to add into the database
* @return true if the operation was acknowledged
*/
override fun insert(wallet: Wallet): Wallet {
val result = walletCollection.insertOne(wallet as WalletDocument)

if (result.wasAcknowledged()) {
return wallet
}

throw Exception("Failed to insert wallet")
}

/**
* Update wallet
*
* @param wallet updated Wallet data object
* @return true if the operation was acknowledged
*/
override fun update(wallet: Wallet): Boolean {
val result = walletCollection.updateOne(wallet as WalletDocument, upsert())
return result.wasAcknowledged()
}

/**
* Find wallet
*
* @param name name of the wallet to find
* @return wallet data object
*/
override fun findByName(name: String): Wallet {
return walletCollection.findOne(Wallet::_id eq name) ?: throw Exception("Wallet '$name' not found.")
}

/**
* List wallets
*
* @return list of stored wallet names
*/
override fun list(): List<Wallet> {
return walletCollection.find().toList()
}

/**
* Wallet exists
*
* @param name name of the wallet to find
* @return true if the wallet was found
*/
override fun exists(name: String): Boolean {
val wallet = walletCollection.findOne("{_id:'$name'}")
return wallet != null
}

/**
* Did alias exists
*
* @param db MongoDB Client
* @param walletName name of the wallet storing the did
* @param didAlias alias of the did
* @return true if the did was found
*/
fun didAliasExists(walletName: String, didAlias: String): Boolean {
val wallet = walletCollection.findOne("{_id:'$walletName','dids':{${MongoOperator.elemMatch}: {'alias':'$didAlias'}}}")
return wallet != null
}

/**
* Key id exists
*
* @param db MongoDB Client
* @param walletName name of the wallet storing the did
* @param didAlias alias of the did
* @param keyId key identifier
* @return true if the keyId was found
*/
fun keyIdExists(walletName: String, didAlias: String, keyId: String): Boolean {
val wallet =
walletCollection.findOne("{_id:'$walletName','dids':{${MongoOperator.elemMatch}: {'alias':'$didAlias'}}, 'dids.keyPairs.keyId':'$keyId'}")
return wallet != null
}

/**
* Issued credential alias exists
*
* @param db MongoDB Client
* @param issuedCredentialAlias credential alias to find
* @return true if the did was found
*/
fun issuedCredentialAliasExists(walletName: String, issuedCredentialAlias: String): Boolean {
val wallet =
walletCollection.findOne("{_id:'$walletName','issuedCredentials':{${MongoOperator.elemMatch}: {'alias':'$issuedCredentialAlias'}}}")
return wallet != null
}

/**
* Credential alias exists
*
* @param db MongoDB Client
* @param credentialAlias credential alias to find
* @return true if the did was found
*/
fun credentialAliasExists(walletName: String, credentialAlias: String): Boolean {
val wallet = walletCollection.findOne("{_id:'$walletName','credentials':{${MongoOperator.elemMatch}: {'alias':'$credentialAlias'}}}")
return wallet != null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.rootsid.wal.library.mongoimpl.config

import com.mongodb.client.MongoClient
import com.mongodb.client.MongoDatabase
import com.rootsid.wal.library.Constant
import org.litote.kmongo.KMongo

object DefaultMongoDbConn : MongoDbConn {
private var db: MongoDatabase = openDb()

fun open(): MongoDatabase {
return db
}

override fun getDbName(): String {
return Constant.DB_NAME
}

override fun getMongoClient(): MongoClient {
return KMongo.createClient()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.rootsid.wal.library.mongoimpl.config

import com.mongodb.client.MongoClient
import com.mongodb.client.MongoDatabase

interface MongoDbConn {
fun getDbName(): String

fun getMongoClient() : MongoClient

/**
* Open db
*
* @return a MongoDatabase representing 'wal' database
*/
fun openDb(): MongoDatabase {
// TODO: make this configurable so it can use other connection settings
val client = getMongoClient() // get com.mongodb.MongoClient new instance
// TODO: make databaseName configurable
return client.getDatabase(getDbName()) // normal java driver usage
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rootsid.wal.library.wallet.storage.document
package com.rootsid.wal.library.mongoimpl.document

import com.rootsid.wal.library.dlt.model.Did
import com.rootsid.wal.library.wallet.model.ImportedCredential
Expand All @@ -9,8 +9,6 @@ import kotlinx.serialization.Serializable
@Serializable
data class WalletDocument(
override val _id: String,
// override val mnemonic: List<String>,
// override val passphrase: String,
override val seed: String,
override var dids: MutableList<Did> = mutableListOf(),
// List of imported (Issued elsewhere)
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/com/rootsid/wal/library/wallet/WalletService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.rootsid.wal.library.wallet

import com.rootsid.wal.library.Constant
import com.rootsid.wal.library.mongoimpl.document.WalletDocument
import com.rootsid.wal.library.wallet.model.Wallet
import com.rootsid.wal.library.wallet.storage.Storage
import com.rootsid.wal.library.wallet.storage.WalletStorage
import io.iohk.atala.prism.crypto.derivation.KeyDerivation
import io.iohk.atala.prism.crypto.derivation.MnemonicCode
import io.iohk.atala.prism.crypto.util.BytesOps

class WalletService(private val storage: Storage) {
class WalletService(private val walletStorage: WalletStorage) {

/**
* New wallet
Expand All @@ -17,9 +19,8 @@ class WalletService(private val storage: Storage) {
* @return a new wallet
*/
fun createWallet(name: String, mnemonic: String, passphrase: String): Wallet {
val mnemonicList = mnemonic.split(Constant.MNEMONIC_SEPARATOR).map { it.trim() }
val seed = generateSeed(mnemonic, passphrase)
return storage.createWallet(name, seed)
return walletStorage.insert(WalletDocument(name, BytesOps.bytesToHex(seed)))
}

/**
Expand All @@ -29,7 +30,7 @@ class WalletService(private val storage: Storage) {
* @param passphrase Passphrase to protect the seed with a password
* @return seed
*/
fun generateSeed(mnemonic: String, passphrase: String): ByteArray {
private fun generateSeed(mnemonic: String, passphrase: String): ByteArray {
val seed: ByteArray
val mnemonicList: MnemonicCode
if (mnemonic.trim().isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.rootsid.wal.library.wallet.storage

import com.rootsid.wal.library.wallet.model.BlockchainTxLog

interface BlockchainTxLogStorage {
fun insert(txLog: BlockchainTxLog): BlockchainTxLog

fun list(): List<BlockchainTxLog>
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.rootsid.wal.library.wallet.storage

import com.rootsid.wal.library.wallet.model.Wallet

interface WalletStorage {
fun insert(wallet: Wallet): Wallet

fun update(wallet: Wallet): Boolean

fun findByName(name: String): Wallet

fun exists(name: String): Boolean

fun list(): List<Wallet>
}
Loading

0 comments on commit bdb0d51

Please sign in to comment.