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

fix: restore process duplicates did peers #173

Merged
merged 3 commits into from
Jul 15, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@file:Suppress("ktlint:standard:import-ordering")

package org.hyperledger.identus.walletsdk.pluto

import java.util.*
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
Expand Down Expand Up @@ -32,11 +35,12 @@ import org.hyperledger.identus.walletsdk.domain.models.keyManagement.IndexKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.JWK
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.PrivateKey
import org.hyperledger.identus.walletsdk.domain.models.keyManagement.StorableKey
import org.hyperledger.identus.walletsdk.pluto.PlutoRestoreTask.BackUpMessage.JsonAsStringSerializer.descriptor
import org.hyperledger.identus.walletsdk.pluto.PlutoRestoreTask.BackUpMessage.JsonAsStringSerializer.deserialize
import org.hyperledger.identus.walletsdk.pluto.PlutoRestoreTask.BackUpMessage.JsonAsStringSerializer.serialize
import org.hyperledger.identus.walletsdk.pluto.backup.models.BackupV0_0_1
import org.hyperledger.identus.walletsdk.pollux.models.AnonCredential
import org.hyperledger.identus.walletsdk.pollux.models.JWTCredential
import java.util.*
import kotlin.jvm.Throws
import kotlin.time.DurationUnit
import kotlin.time.toDuration

Expand All @@ -57,12 +61,12 @@ open class PlutoRestoreTask(
*/
fun run() {
restoreCredentials()
restoreDids()
restoreDidPairs()
restoreKeys()
restoreLinkSecret()
restoreMessages()
restoreMediators()
restoreDids()
}

/**
Expand Down Expand Up @@ -169,14 +173,21 @@ open class PlutoRestoreTask(
}.forEach {
if (it.third is DID) {
if (it.third.toString().contains("peer")) {
val metaId = (it.third as DID).toString()
val origDid = (it.third as DID)
val did = if (origDid.toString().contains("#")) {
val splits = origDid.toString().split("#")
DID(splits[0])
} else {
DID(origDid.toString())
}
val keyId = origDid.toString()
pluto.storePrivateKeys(
it.first as StorableKey,
it.third as DID,
did,
(it.first.keySpecification[IndexKey().property])?.toInt(),
metaId
keyId
)
pluto.storePeerDID(it.third as DID)
// pluto.storePeerDID(did)
} else {
pluto.storePrismDIDAndPrivateKeys(
it.third as DID,
Expand Down Expand Up @@ -271,14 +282,13 @@ open class PlutoRestoreTask(
* @throws IndexOutOfBoundsException If the input string is not in the required format `x:y:z`
*/
@Throws(IndexOutOfBoundsException::class)
private fun String.toDID(alias: String? = null): DID {
return DID(
private fun String.toDID(alias: String? = null): DID =
DID(
DID.getSchemaFromString(this),
DID.getMethodFromString(this),
DID.getMethodIdFromString(this),
alias
)
}

/**
* Represents the various types of backup restoration IDs that can be used.
Expand All @@ -295,13 +305,12 @@ open class PlutoRestoreTask(
*
* @return The corresponding RestorationID object based on the current BackUpRestorationId.
*/
fun toRestorationId(): RestorationID {
return when (this) {
fun toRestorationId(): RestorationID =
when (this) {
JWT -> RestorationID.JWT
ANONCRED -> RestorationID.ANONCRED
W3C -> RestorationID.W3C
}
}
}

/**
Expand Down Expand Up @@ -520,8 +529,8 @@ open class PlutoRestoreTask(
*
* @return The converted Message object.
*/
fun toMessage(): Message {
return Message(
fun toMessage(): Message =
Message(
id,
piuri,
from,
Expand All @@ -537,7 +546,6 @@ open class PlutoRestoreTask(
ack,
direction
)
}

/**
* Serializer for the [Message.Direction] enum class.
Expand Down Expand Up @@ -571,9 +579,8 @@ open class PlutoRestoreTask(
* @param decoder The decoder used for deserialization.
* @return The deserialized Message.Direction value.
*/
override fun deserialize(decoder: Decoder): Message.Direction {
return Message.Direction.fromValue(decoder.decodeInt())
}
override fun deserialize(decoder: Decoder): Message.Direction =
Message.Direction.fromValue(decoder.decodeInt())
}

/**
Expand Down Expand Up @@ -722,9 +729,8 @@ open class PlutoRestoreTask(
* @param decoder The Decoder object used for deserialization.
* @return A new instance of DID.
*/
override fun deserialize(decoder: Decoder): DID {
return DID(decoder.decodeString())
}
override fun deserialize(decoder: Decoder): DID =
DID(decoder.decodeString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,10 @@ class BackupRestorationTests {
val revoked: Boolean
)

fun String.toRestorationId(): RestorationID {
return RestorationID.entries.first {
fun String.toRestorationId(): RestorationID =
RestorationID.entries.first {
it.value == this
}
}

val credentials = Json.decodeFromString<List<CredentialMock>>(getCredentials).map {
val currentCredential = when (it.restorationId.toRestorationId()) {
Expand Down Expand Up @@ -310,8 +309,8 @@ class BackupRestorationTests {

// Validates DIDs are backed up and restored properly, and passed to pluto for storage
val didCaptor = argumentCaptor<DID>()
verify(edgeAgent.pluto, times(9)).storePeerDID(didCaptor.capture())
assertEquals(9, didCaptor.allValues.size)
verify(edgeAgent.pluto, times(5)).storePeerDID(didCaptor.capture())
assertEquals(5, didCaptor.allValues.size)

assertTrue {
didCaptor.allValues.contains(DID("did:peer:2.Ez6LSok96TA4orHQXSMHZj3mqyUuVLMfLfGGqj27i1giErbXL.Vz6Mku5mY1GuJ9AN2vvDwjMv5QUC2zqKVRPCcbmJVYTFTCFmr"))
Expand All @@ -328,18 +327,6 @@ class BackupRestorationTests {
assertTrue {
didCaptor.allValues.contains(DID("did:prism:0a4b552169e3158781741fbbeffe81212784d32d90cf8f2622923f11f6ecd966:CoUBCoIBEjsKB21hc3RlcjAQAUouCglzZWNwMjU2azESIQLgzhsuOqhAyImy-c8o9ZmIJ4iY_Gc8tvNIT3l1w58f2BJDCg9hdXRoZW50aWNhdGlvbjAQBEouCglzZWNwMjU2azESIQLgzhsuOqhAyImy-c8o9ZmIJ4iY_Gc8tvNIT3l1w58f2A"))
}
assertTrue {
didCaptor.allValues.contains(DID("did:peer:2.Ez6LSok96TA4orHQXSMHZj3mqyUuVLMfLfGGqj27i1giErbXL.Vz6Mku5mY1GuJ9AN2vvDwjMv5QUC2zqKVRPCcbmJVYTFTCFmr#key-2"))
}
assertTrue {
didCaptor.allValues.contains(DID("did:peer:2.Ez6LSok96TA4orHQXSMHZj3mqyUuVLMfLfGGqj27i1giErbXL.Vz6Mku5mY1GuJ9AN2vvDwjMv5QUC2zqKVRPCcbmJVYTFTCFmr#key-1"))
}
assertTrue {
didCaptor.allValues.contains(DID("did:peer:2.Ez6LSqWfJdwLMDmpew7Yd8AQS2MxDwvSCNpjaZ7EQVst8rkfA.Vz6MknpCj4WomXhC2eur8nf4wnoFzHpCA6EAFU6afEmmrUVRA.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6ImRpZDpwZWVyOjIuRXo2TFNnaHdTRTQzN3duREUxcHQzWDZoVkRVUXpTanNIemlucFgzWEZ2TWpSQW03eS5WejZNa2hoMWU1Q0VZWXE2SkJVY1RaNkNwMnJhbkNXUnJ2N1lheDNMZTRONTlSNmRkLlNleUowSWpvaVpHMGlMQ0p6SWpwN0luVnlhU0k2SW1oMGRIQnpPaTh2YzJsMExYQnlhWE50TFcxbFpHbGhkRzl5TG1GMFlXeGhjSEpwYzIwdWFXOGlMQ0poSWpwYkltUnBaR052YlcwdmRqSWlYWDE5LlNleUowSWpvaVpHMGlMQ0p6SWpwN0luVnlhU0k2SW5kemN6b3ZMM05wZEMxd2NtbHpiUzF0WldScFlYUnZjaTVoZEdGc1lYQnlhWE50TG1sdkwzZHpJaXdpWVNJNld5SmthV1JqYjIxdEwzWXlJbDE5ZlEiLCJyIjpbXSwiYSI6W119fQ#key-2"))
}
assertTrue {
didCaptor.allValues.contains(DID("did:peer:2.Ez6LSqWfJdwLMDmpew7Yd8AQS2MxDwvSCNpjaZ7EQVst8rkfA.Vz6MknpCj4WomXhC2eur8nf4wnoFzHpCA6EAFU6afEmmrUVRA.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6ImRpZDpwZWVyOjIuRXo2TFNnaHdTRTQzN3duREUxcHQzWDZoVkRVUXpTanNIemlucFgzWEZ2TWpSQW03eS5WejZNa2hoMWU1Q0VZWXE2SkJVY1RaNkNwMnJhbkNXUnJ2N1lheDNMZTRONTlSNmRkLlNleUowSWpvaVpHMGlMQ0p6SWpwN0luVnlhU0k2SW1oMGRIQnpPaTh2YzJsMExYQnlhWE50TFcxbFpHbGhkRzl5TG1GMFlXeGhjSEpwYzIwdWFXOGlMQ0poSWpwYkltUnBaR052YlcwdmRqSWlYWDE5LlNleUowSWpvaVpHMGlMQ0p6SWpwN0luVnlhU0k2SW5kemN6b3ZMM05wZEMxd2NtbHpiUzF0WldScFlYUnZjaTVoZEdGc1lYQnlhWE50TG1sdkwzZHpJaXdpWVNJNld5SmthV1JqYjIxdEwzWXlJbDE5ZlEiLCJyIjpbXSwiYSI6W119fQ#key-1"))
}

// Validates DID pairs are backed up and restored properly, and passed to pluto for storage
val didPairsHostCaptor = argumentCaptor<DID>()
Expand Down Expand Up @@ -371,7 +358,7 @@ class BackupRestorationTests {
)
// 9 because of already ran items and items in the key backup
val keysDidCaptor2 = argumentCaptor<DID>()
verify(edgeAgent.pluto, times(9)).storePeerDID(keysDidCaptor2.capture())
verify(edgeAgent.pluto, times(5)).storePeerDID(keysDidCaptor2.capture())

val keysCaptor = argumentCaptor<StorableKey>()
val keysDidCaptor3 = argumentCaptor<DID>()
Expand Down

Large diffs are not rendered by default.

Loading