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 17, 2022
1 parent e6b1962 commit 58488a5
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 257 deletions.
72 changes: 0 additions & 72 deletions src/main/kotlin/com/rootsid/wal/library/DLT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,3 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

/**
* Verify issued credential
*
* @param wallet Wallet containing the credential
* @param credentialAlias Alias of Credential to verify
* @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.")
}
}

private fun VerificationResult.toMessageArray(): List<String> {
val messages = mutableListOf<String>()
for (message in this.verificationErrors) {
messages.add(message.errorMessage)
}
return messages
}

/**
* Verify imported credential
*
* @param wallet Wallet containing the credential
* @param credentialAlias Alias of credential to verify
* @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.")
}
}

/**
* Grpc config
* Done this way to allow programmatic override of the grpc config
* @constructor Create empty Grpc config
*/
class GrpcConfig {
companion object {
var host: String = System.getenv("PRISM_NODE_HOST") ?: ""
var port: String = System.getenv("PRISM_NODE_PORT") ?: "50053"
fun options() = GrpcOptions("https", host, port.toInt())
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/com/rootsid/wal/library/didcom/DIDPeer.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rootsid.wal.library.didcom

import com.rootsid.wal.library.UnpackResult
import com.rootsid.wal.library.didcom.model.UnpackResult
import com.rootsid.wal.library.didcom.storage.SecretResolver
import org.didcommx.didcomm.DIDComm
import org.didcommx.didcomm.message.Message
import org.didcommx.didcomm.model.PackEncryptedParams
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.rootsid.wal.library.didcom.model

/**
* Unpack result
*
* @property message
* @property from
* @property to
* @property res
* @constructor Create empty Unpack result
*/
data class UnpackResult(
val message: String,
val from: String?,
val to: String,
val res: org.didcommx.didcomm.model.UnpackResult
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rootsid.wal.library.didcom
package com.rootsid.wal.library.didcom.storage

import org.didcommx.didcomm.secret.Secret
import org.didcommx.didcomm.secret.SecretResolverEditable
Expand Down
82 changes: 80 additions & 2 deletions src/main/kotlin/com/rootsid/wal/library/prism/Dlt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@ 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.wallet.model.*
import io.iohk.atala.prism.api.CredentialClaim
import io.iohk.atala.prism.api.KeyGenerator
import io.iohk.atala.prism.api.VerificationResult
import io.iohk.atala.prism.api.models.AtalaOperationId
import io.iohk.atala.prism.api.models.AtalaOperationStatus
import io.iohk.atala.prism.api.node.NodeAuthApiImpl
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.ExperimentalProtoJson
import pbandk.json.encodeToJsonString

class Dlt {
Expand Down Expand Up @@ -457,7 +461,6 @@ class Dlt {
return DltDidUpdate(issueCredentialsOperationId.hexValue(), issuerDid)
}


/**
* Revoke credential
*
Expand Down Expand Up @@ -495,4 +498,79 @@ class Dlt {
credential.revoked = true
return DltDidUpdate(revokeOperationId.hexValue(), issuerDid)
}

// TODO REFACTOR PENDING ON FUNCTIONS BELOW

/**
* Verify issued credential
*
* @param wallet Wallet containing the credential
* @param credentialAlias Alias of Credential to verify
* @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.")
}
}

private fun VerificationResult.toMessageArray(): List<String> {
val messages = mutableListOf<String>()
for (message in this.verificationErrors) {
messages.add(message.errorMessage)
}
return messages
}

/**
* Verify imported credential
*
* @param wallet Wallet containing the credential
* @param credentialAlias Alias of credential to verify
* @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.")
}
}

/**
* Grpc config
* Done this way to allow programmatic override of the grpc config
* @constructor Create empty Grpc config
*/
class GrpcConfig {
companion object {
var host: String = System.getenv("PRISM_NODE_HOST") ?: ""
var port: String = System.getenv("PRISM_NODE_PORT") ?: "50053"
fun options() = GrpcOptions("https", host, port.toInt())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.rootsid.wal.library.wallet

import com.rootsid.wal.library.Config
import com.rootsid.wal.library.Wallet
import com.rootsid.wal.library.wallet.model.Wallet
import io.iohk.atala.prism.crypto.derivation.KeyDerivation
import io.iohk.atala.prism.crypto.derivation.MnemonicCode
import io.iohk.atala.prism.crypto.util.BytesOps
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.rootsid.wal.library.wallet.model

import kotlinx.serialization.Serializable

/**
* Blockchain tx log
*
* @property txId transaction id
* @property action one of ADD_KEY, REVOKE_KEY, PUBLISH_DID, ISSUE_CREDENTIAL, REVOKE_CREDENTIAL
* @property url to open the transaction on a blockchain explorer
* @constructor Create empty Blockchain tx log
*/
@Serializable
data class BlockchainTxLogEntry(
val txId: String,
val action: BlockchainTxAction,
val url: String,
val description: String?
)

// Enum for blockchain tx actions
enum class BlockchainTxAction {
ADD_KEY,
REVOKE_KEY,
PUBLISH_DID,
ISSUE_CREDENTIAL,
REVOKE_CREDENTIAL
}
32 changes: 32 additions & 0 deletions src/main/kotlin/com/rootsid/wal/library/wallet/model/Claim.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.rootsid.wal.library.wallet.model

import io.iohk.atala.prism.api.CredentialClaim
import io.iohk.atala.prism.identity.PrismDid
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

/**
* Claim
*
* @property subjectDid
* @property content
* @constructor Create empty Claim
*/
@Serializable
data class Claim(
val subjectDid: String,
val content: String
)

/**
* To credential claim
*
* Convert a Claim to PRISM CredentialClaim
*/
@OptIn(ExperimentalSerializationApi::class)
fun Claim.toCredentialClaim() = CredentialClaim(
PrismDid.fromString(this.subjectDid),
Json.decodeFromString(this.content)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.rootsid.wal.library.wallet.model

import kotlinx.serialization.Serializable

/**
* Imported credential
*
* @property alias
* @property verifiedCredential
* @constructor Create empty Imported credential
*/
@Serializable
data class ImportedCredential(
val alias: String,
// Signed VC and proof (This is the real VC)
var verifiedCredential: VerifiedCredential,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.rootsid.wal.library.wallet.model

import kotlinx.serialization.Serializable

/**
* Credential
*
* @property alias
* @property issuingDidAlias
* @property claim
* @property verifiedCredential
* @property batchId
* @property credentialHash
* @property operationHash
* @property revoked
* @constructor Create empty Credential
*/
@Serializable
data class IssuedCredential(
val alias: String,
var issuingDidAlias: String,
// Plain json claim
val claim: Claim,
// Signed VC and proof (This is the real VC)
var verifiedCredential: VerifiedCredential,
// Required for revocation
var batchId: String,
// Required for revocation
var credentialHash: String,
// Required for revocation
var operationHash: String,
var revoked: Boolean
)
Loading

0 comments on commit 58488a5

Please sign in to comment.