Skip to content

Commit

Permalink
refactor: model, package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Essbante committed Jun 20, 2022
1 parent 58488a5 commit afde645
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package com.rootsid.wal.library
*
* @constructor Create empty Constant
*/
object Config {
object Constant {
const val MNEMONIC_SEPARATOR = ","
const val TESTNET_URL = "https://explorer.cardano-testnet.iohkdev.io/en/transaction?id="
var DB_NAME = "wal"
const val DB_NAME = "wal"
}
11 changes: 0 additions & 11 deletions src/main/kotlin/com/rootsid/wal/library/DLT.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.rootsid.wal.library.prism
package com.rootsid.wal.library.dlt

import com.rootsid.wal.library.*
import com.rootsid.wal.library.prism.model.Did
import com.rootsid.wal.library.prism.model.DltDidUpdate
import com.rootsid.wal.library.prism.model.KeyPath
import com.rootsid.wal.library.dlt.model.*
import com.rootsid.wal.library.dlt.model.Did
import com.rootsid.wal.library.wallet.model.*
import io.iohk.atala.prism.api.CredentialClaim
import io.iohk.atala.prism.api.KeyGenerator
Expand All @@ -15,45 +14,42 @@ import io.iohk.atala.prism.api.node.NodePayloadGenerator
import io.iohk.atala.prism.api.node.NodePublicApi
import io.iohk.atala.prism.api.node.PrismDidState
import io.iohk.atala.prism.common.PrismSdkInternal
import io.iohk.atala.prism.credentials.json.JsonBasedCredential
import io.iohk.atala.prism.crypto.EC
import io.iohk.atala.prism.crypto.MerkleInclusionProof
import io.iohk.atala.prism.crypto.Sha256Digest
import io.iohk.atala.prism.crypto.keys.ECKeyPair
import io.iohk.atala.prism.identity.*
import io.iohk.atala.prism.protos.*
import io.ipfs.multibase.Base58
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
import pbandk.ByteArr
import pbandk.json.encodeToJsonString

class Dlt {

/**
* Transaction id
*
* @param oid operation identifier
* @return transaction Id
* Use this function to get the blockchain transaction ID associated with an operationID
* @param operationId operation identifier
* @return blockchain Tx identifier
*/
@OptIn(PrismSdkInternal::class)
private fun transactionId(oid: AtalaOperationId): String {
private fun transactionId(operationId: AtalaOperationId): String {
val node = NodeServiceCoroutine.Client(GrpcClient(GrpcConfig.options()))
val response = runBlocking {
node.GetOperationInfo(GetOperationInfoRequest(ByteArr(oid.value())))
node.GetOperationInfo(GetOperationInfoRequest(ByteArr(operationId.value())))
}
return response.transactionId
}

/**
* Wait for submission
* Waits until the operationId status changes from [AtalaOperationStatus.PENDING_SUBMISSION] to [AtalaOperationStatus.AWAIT_CONFIRMATION]
*
* @param nodePublicApi PRISM node
* @param nodePublicApi PRISM node request handler
* @param operationId operation Identifier
* @param action action associated with the operation (for traceability)
* @return Log Entry
* @param description additional details
* @return Log Entry containing details of the operation and the blockchain transaction
*/
private fun waitForSubmission(nodePublicApi: NodePublicApi, operationId: AtalaOperationId, action: BlockchainTxAction, description: String): BlockchainTxLogEntry {
var status = runBlocking {
Expand All @@ -68,14 +64,14 @@ class Dlt {
}
}
val tid = transactionId(operationId)
println("Track the transaction in:\n- ${Config.TESTNET_URL}$tid\n")
return BlockchainTxLogEntry(tid, action, "${Config.TESTNET_URL}$tid", description)
println("Track the transaction in:\n- ${Constant.TESTNET_URL}$tid\n")
return BlockchainTxLogEntry(tid, action, "${Constant.TESTNET_URL}$tid", description)
}

/**
* Wait until confirmed
* Waits until the operationId status changes to [AtalaOperationStatus.CONFIRMED_AND_REJECTED] or [AtalaOperationStatus.CONFIRMED_AND_APPLIED]
*
* @param nodePublicApi PRISM node
* @param nodePublicApi PRISM node request handler
* @param operationId operation Identifier
*/
private fun waitUntilConfirmed(nodePublicApi: NodePublicApi, operationId: AtalaOperationId) {
Expand All @@ -96,16 +92,16 @@ class Dlt {
}

/**
* Derive key pair
* Derive a key pair using a seed and a [KeyPath]
*
* @param keyPairs List containing key path information
* @param keyPaths List containing key path information
* @param seed seed
* @param keyId Id of the key to derive
* @return Key pair
*/
@OptIn(PrismSdkInternal::class)
private fun deriveKeyPair(keyPairs: MutableList<KeyPath>, seed: ByteArray, keyId: String): ECKeyPair {
val keyPathList = keyPairs.filter { it.keyId == keyId }
private fun deriveKeyPair(keyPaths: MutableList<KeyPath>, seed: ByteArray, keyId: String): ECKeyPair {
val keyPathList = keyPaths.filter { it.keyId == keyId }
if (keyPathList.isNotEmpty()) {
val keyPath = keyPathList[0]
return KeyGenerator.deriveKeyFromFullPath(
Expand Down Expand Up @@ -499,7 +495,7 @@ class Dlt {
return DltDidUpdate(revokeOperationId.hexValue(), issuerDid)
}

// TODO REFACTOR PENDING ON FUNCTIONS BELOW
// TODO: REFACTOR PENDING ON FUNCTIONS BELOW

/**
* Verify issued credential
Expand All @@ -509,23 +505,23 @@ class Dlt {
* @return Verification result
*/
// TODO: refactor to a single verifyCredential function
fun verifyIssuedCredential(wallet: Wallet, credentialAlias: String): List<String> {
val credentials = wallet.issuedCredentials.filter { it.alias == credentialAlias }
if (credentials.isNotEmpty()) {
val credential = credentials[0]
val nodeAuthApi = NodeAuthApiImpl(GrpcConfig.options())
val signed = JsonBasedCredential.fromString(credential.verifiedCredential.encodedSignedCredential)
// Use encodeDefaults to generate empty siblings field on proof
val format = Json { encodeDefaults = true }
val proof = MerkleInclusionProof.decode(format.encodeToString(credential.verifiedCredential.proof))

return runBlocking {
nodeAuthApi.verify(signed, proof).toMessageArray()
}
} else {
throw Exception("Credential '$credentialAlias' not found.")
}
}
// fun verifyIssuedCredential(wallet: Wallet, credentialAlias: String): List<String> {
// val credentials = wallet.issuedCredentials.filter { it.alias == credentialAlias }
// if (credentials.isNotEmpty()) {
// val credential = credentials[0]
// val nodeAuthApi = NodeAuthApiImpl(GrpcConfig.options())
// val signed = JsonBasedCredential.fromString(credential.verifiedCredential.encodedSignedCredential)
// // Use encodeDefaults to generate empty siblings field on proof
// val format = Json { encodeDefaults = true }
// val proof = MerkleInclusionProof.decode(format.encodeToString(credential.verifiedCredential.proof))
//
// return runBlocking {
// nodeAuthApi.verify(signed, proof).toMessageArray()
// }
// } else {
// throw Exception("Credential '$credentialAlias' not found.")
// }
// }

private fun VerificationResult.toMessageArray(): List<String> {
val messages = mutableListOf<String>()
Expand All @@ -543,23 +539,23 @@ class Dlt {
* @return Verification result
*/
// TODO: refactor to a single verifyCredential function
fun verifyImportedCredential(wallet: Wallet, credentialAlias: String): List<String> {
val credentials = wallet.importedCredentials.filter { it.alias == credentialAlias }
if (credentials.isNotEmpty()) {
val credential = credentials[0]
val nodeAuthApi = NodeAuthApiImpl(GrpcConfig.options())
val signed = JsonBasedCredential.fromString(credential.verifiedCredential.encodedSignedCredential)
// Use encodeDefaults to generate empty siblings field on proof
val format = Json { encodeDefaults = true }
val proof = MerkleInclusionProof.decode(format.encodeToString(credential.verifiedCredential.proof))

return runBlocking {
nodeAuthApi.verify(signed, proof).toMessageArray()
}
} else {
throw Exception("Credential '$credentialAlias' not found.")
}
}
// fun verifyImportedCredential(wallet: Wallet, credentialAlias: String): List<String> {
// val credentials = wallet.importedCredentials.filter { it.alias == credentialAlias }
// if (credentials.isNotEmpty()) {
// val credential = credentials[0]
// val nodeAuthApi = NodeAuthApiImpl(GrpcConfig.options())
// val signed = JsonBasedCredential.fromString(credential.verifiedCredential.encodedSignedCredential)
// // Use encodeDefaults to generate empty siblings field on proof
// val format = Json { encodeDefaults = true }
// val proof = MerkleInclusionProof.decode(format.encodeToString(credential.verifiedCredential.proof))
//
// return runBlocking {
// nodeAuthApi.verify(signed, proof).toMessageArray()
// }
// } else {
// throw Exception("Credential '$credentialAlias' not found.")
// }
// }

/**
* Grpc config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rootsid.wal.library.wallet.model
package com.rootsid.wal.library.dlt.model

import io.iohk.atala.prism.api.CredentialClaim
import io.iohk.atala.prism.identity.PrismDid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rootsid.wal.library.prism.model
package com.rootsid.wal.library.dlt.model

import kotlinx.serialization.Serializable

Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/com/rootsid/wal/library/dlt/model/DltDidUpdate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rootsid.wal.library.dlt.model

import kotlinx.serialization.Serializable

/**
* Dlt did update
*
* @property operationId operation id
* @property did updated did
* @constructor Create Dlt did update
*/
@Serializable
data class DltDidUpdate(
val operationId: String?,
val did: Did?
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rootsid.wal.library.prism.model
package com.rootsid.wal.library.dlt.model

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rootsid.wal.library.wallet.model
package com.rootsid.wal.library.dlt.model

import kotlinx.serialization.Serializable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rootsid.wal.library.wallet.model
package com.rootsid.wal.library.dlt.model

import kotlinx.serialization.Serializable

Expand Down

This file was deleted.

15 changes: 7 additions & 8 deletions src/main/kotlin/com/rootsid/wal/library/wallet/WalletService.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.rootsid.wal.library.wallet

import com.rootsid.wal.library.Config
import com.rootsid.wal.library.Constant
import com.rootsid.wal.library.wallet.model.Wallet
import com.rootsid.wal.library.wallet.storage.Storage
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 {
class WalletService(private val storage: Storage) {

/**
* New wallet
Expand All @@ -16,11 +16,10 @@ class WalletService {
* @param passphrase passphrase
* @return a new wallet
*/
fun newWallet(name: String, mnemonic: String, passphrase: String): Wallet {
val mnemonicList = mnemonic.split(Config.MNEMONIC_SEPARATOR).map { it.trim() }
fun createWallet(name: String, mnemonic: String, passphrase: String): Wallet {
val mnemonicList = mnemonic.split(Constant.MNEMONIC_SEPARATOR).map { it.trim() }
val seed = generateSeed(mnemonic, passphrase)
// TODO: add wallet to storage
return Wallet(name, mnemonicList, passphrase, BytesOps.bytesToHex(seed))
return storage.createWallet(name, seed)
}

/**
Expand All @@ -38,7 +37,7 @@ class WalletService {
seed = KeyDerivation.binarySeed(mnemonicList, passphrase)
} else {
try {
mnemonicList = MnemonicCode(mnemonic.split(Config.MNEMONIC_SEPARATOR).map { it.trim() })
mnemonicList = MnemonicCode(mnemonic.split(Constant.MNEMONIC_SEPARATOR).map { it.trim() })
seed = KeyDerivation.binarySeed(mnemonicList, passphrase)
} catch (e: Exception) {
throw Exception("Invalid mnemonic phrase")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rootsid.wal.library.wallet.model

interface BlockchainTxLog {
val _id: String
val walletId: String
var logEntries: MutableList<BlockchainTxLogEntry>
}

/**
* Add blockchain tx log
*
* @param entry blockchain transaction log entry
*/
fun BlockchainTxLog.addBlockchainTxLog(entry: BlockchainTxLogEntry) {
logEntries.add(entry)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rootsid.wal.library.wallet.model

import com.rootsid.wal.library.dlt.model.VerifiedCredential
import kotlinx.serialization.Serializable

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.rootsid.wal.library.wallet.model

import com.rootsid.wal.library.dlt.model.Claim
import com.rootsid.wal.library.dlt.model.VerifiedCredential
import kotlinx.serialization.Serializable

/**
Expand Down
Loading

0 comments on commit afde645

Please sign in to comment.