Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 create a log for blockchain transactions #6

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ publishing {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/roots-id/wal-library")
credentials {
username = System.getenv("ROOTSID_USER")
password = System.getenv("ROOTSID_PASSWORD")
username = System.getenv("GITHUB_USER")
password = System.getenv("GITHUB_TOKEN")
}
}
}
Expand Down
54 changes: 48 additions & 6 deletions src/main/kotlin/com/rootsid/wal/library/DLT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,46 @@ private fun transactionId(oid: AtalaOperationId): String {
return response.transactionId
}

/**
* Wait for submission
*
* @param nodePublicApi PRISM node
* @param operationId operation Identifier
* @param action action associated with the operation (for traceability)
* @return Log Entry
*/
private fun waitForSubmission(nodePublicApi: NodePublicApi, operationId: AtalaOperationId, action: BlockchainTxAction): BlockchainTxLogEntry {
var status = runBlocking {
nodePublicApi.getOperationInfo(operationId).status
}

while (status == AtalaOperationStatus.PENDING_SUBMISSION) {
println("Current operation status: ${AtalaOperationStatus.asString(status)}\n")
Thread.sleep(10000)
status = runBlocking {
nodePublicApi.getOperationInfo(operationId).status
}
}
val tid = transactionId(operationId)
println("Track the transaction in:\n- ${Config.TESTNET_URL}$tid\n")
return BlockchainTxLogEntry(tid, action, "${Config.TESTNET_URL}$tid")
}

/**
* Wait until confirmed
*
* @param nodePublicApi PRISM node
* @param operationId operation Identifier
*/
private fun waitUntilConfirmed(nodePublicApi: NodePublicApi, operationId: AtalaOperationId) {
var tid = ""

var status = runBlocking {
nodePublicApi.getOperationInfo(operationId).status
}

while (status != AtalaOperationStatus.CONFIRMED_AND_APPLIED &&
status != AtalaOperationStatus.CONFIRMED_AND_REJECTED
) {
if (status == AtalaOperationStatus.AWAIT_CONFIRMATION && tid.isEmpty()) {
tid = transactionId(operationId)
println("Track the transaction in:\n- ${Config.TESTNET_URL}$tid\n")
}
println("Current operation status: ${AtalaOperationStatus.asString(status)}\n")
Thread.sleep(10000)
status = runBlocking {
Expand Down Expand Up @@ -205,7 +227,7 @@ fun getDidDocument(wallet: Wallet, didAlias: String): PrismDidDataModel {
/**
* Publish did
*
* @param wallet Wallet contining the DID
* @param wallet Wallet containing the DID
* @param didAlias Alias of the DID
* @return updated wallet
*/
Expand Down Expand Up @@ -238,6 +260,10 @@ fun publishDid(wallet: Wallet, didAlias: String): Wallet {
PrismDid.DEFAULT_MASTER_KEY_ID
)
}

wallet.addBlockchainTxLog(
waitForSubmission(nodeAuthApi, createDidOperationId, BlockchainTxAction.PUBLISH_DID)
)
waitUntilConfirmed(nodeAuthApi, createDidOperationId)

val response = runBlocking { nodeAuthApi.getOperationInfo(createDidOperationId) }
Expand Down Expand Up @@ -310,6 +336,10 @@ fun addKey(wallet: Wallet, didAlias: String, keyId: String, keyTypeValue: Int):
keysToRevoke = arrayOf()
)
}

wallet.addBlockchainTxLog(
waitForSubmission(nodeAuthApi, updateDidOperationId, BlockchainTxAction.ADD_KEY)
)
waitUntilConfirmed(nodeAuthApi, updateDidOperationId)

val response = runBlocking { nodeAuthApi.getOperationInfo(updateDidOperationId) }
Expand Down Expand Up @@ -365,6 +395,10 @@ fun revokeKey(wallet: Wallet, didAlias: String, keyId: String): Wallet {
keysToRevoke = arrayOf(keyId)
)
}

wallet.addBlockchainTxLog(
waitForSubmission(nodeAuthApi, updateDidOperationId, BlockchainTxAction.REVOKE_KEY)
)
waitUntilConfirmed(nodeAuthApi, updateDidOperationId)

val response = runBlocking { nodeAuthApi.getOperationInfo(updateDidOperationId) }
Expand Down Expand Up @@ -430,6 +464,10 @@ fun issueCredential(wallet: Wallet, didAlias: String, issuedCredential: IssuedCr
credentialsInfo.merkleRoot
)
}

wallet.addBlockchainTxLog(
waitForSubmission(nodeAuthApi, issueCredentialsOperationId, BlockchainTxAction.ISSUE_CREDENTIAL)
)
waitUntilConfirmed(nodeAuthApi, issueCredentialsOperationId)

val response = runBlocking { nodeAuthApi.getOperationInfo(issueCredentialsOperationId) }
Expand Down Expand Up @@ -484,6 +522,10 @@ fun revokeCredential(wallet: Wallet, credentialAlias: String): Wallet {
arrayOf(Sha256Digest.fromHex(credential.credentialHash))
)
}

wallet.addBlockchainTxLog(
waitForSubmission(nodeAuthApi, revokeOperationId, BlockchainTxAction.REVOKE_CREDENTIAL)
)
waitUntilConfirmed(nodeAuthApi, revokeOperationId)

val status = runBlocking { nodeAuthApi.getOperationInfo(revokeOperationId).status }
Expand Down
36 changes: 35 additions & 1 deletion src/main/kotlin/com/rootsid/wal/library/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ data class Wallet(
// List of imported (Issued elsewhere)
var importedCredentials: MutableList<ImportedCredential> = mutableListOf(),
// List of credentials issued by a DID from this wallet
var issuedCredentials: MutableList<IssuedCredential> = mutableListOf()
var issuedCredentials: MutableList<IssuedCredential> = mutableListOf(),
var blockchainTxLogEntry: MutableList<BlockchainTxLogEntry> = mutableListOf()
)

/**
* Add blockchain tx log
*
* @param entry blockchain transaction log entry
*/
fun Wallet.addBlockchainTxLog(entry: BlockchainTxLogEntry) {
blockchainTxLogEntry.add(entry)
}

/**
* D i d
*
Expand Down Expand Up @@ -75,6 +85,30 @@ data class KeyPair(
var revoked: Boolean = false
)

/**
* 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
)

// Enum for blockchain tx actions
enum class BlockchainTxAction {
ADD_KEY,
REVOKE_KEY,
PUBLISH_DID,
ISSUE_CREDENTIAL,
REVOKE_CREDENTIAL
}

/**
* Verified credential
*
Expand Down