Skip to content

Commit

Permalink
fix: anoncred verification and breaking changes missing (#196)
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian G <cristian.castro@iohk.io>
  • Loading branch information
cristianIOHK authored Sep 9, 2024
1 parent eed1736 commit 778cb91
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The complete platform is separated into multiple repositories:
* [edge-agent-sdk-swift](https://github.com/hyperledger/identus-edge-agent-sdk-swift/) - Repo that implements Edge Agent for Apple platforms in Swift.
* [edge-agent-sdk-ts](https://github.com/hyperledger/identus-edge-agent-sdk-ts/) - Repo that implements Edge Agent for Browser and Node.js platforms in Typescript.
* [identus-cloud-agent](https://github.com/hyperledger/identus-cloud-agent/) - Repo that contains the platform Building Blocks.
* [mediator](https://github.com/hyperledger/identus-mediator/) - Repo for DIDComm V2 Mediator
* [mediator](https://github.com/hyperledger/identus-mediator/) - Repo for DIDComm V2 Mediator.

### Modules / APIs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,7 @@ open class EdgeAgent {
KeyCurve(Curve.SECP256K1, privateKeyKeyPath)
)
val offerDataString = offer.attachments.firstNotNullOf {
when (it.data) {
is AttachmentJsonData -> it.data.data
else -> null
}
it.data.getDataAsJsonString()
}
val offerJsonObject = Json.parseToJsonElement(offerDataString).jsonObject
val jwtString =
Expand Down Expand Up @@ -1057,12 +1054,9 @@ open class EdgeAgent {
if (format != CredentialType.ANONCREDS_PROOF_REQUEST) {
throw EdgeAgentError.InvalidCredentialFormatError(CredentialType.ANONCREDS_PROOF_REQUEST)
}
val requestData = request.attachments.mapNotNull {
when (it.data) {
is AttachmentJsonData -> it.data.data
else -> null
}
}.first()
val requestData = request.attachments.firstNotNullOf {
it.data.getDataAsJsonString()
}
val linkSecret = getLinkSecret()
try {
presentationString = credential.presentation(
Expand Down Expand Up @@ -1093,10 +1087,7 @@ open class EdgeAgent {
KeyCurve(Curve.SECP256K1, privateKeyKeyPath)
)
val requestData = request.attachments.firstNotNullOf {
when (it.data) {
is AttachmentJsonData -> it.data.data
else -> null
}
it.data.getDataAsJsonString()
}
try {
presentationString = credential.presentation(
Expand All @@ -1113,15 +1104,12 @@ open class EdgeAgent {
}

CredentialType.SDJWT.type -> {
val requestData = request.attachments.mapNotNull {
when (it.data) {
is AttachmentJsonData -> it.data.data
else -> null
}
}.first().encodeToByteArray()
val requestData = request.attachments.firstNotNullOf {
it.data.getDataAsJsonString()
}
try {
presentationString = credential.presentation(
requestData,
requestData.encodeToByteArray(),
listOf(CredentialOperationsOptions.DisclosingClaims(listOf(credential.claims.toString())))
)
} catch (e: Exception) {
Expand Down Expand Up @@ -1194,7 +1182,7 @@ open class EdgeAgent {
type = type,
presentationClaims = presentationClaims,
options = AnoncredsPresentationOptions(
nonce = generateNonce()
nonce = generateNumericNonce()
)
)
attachmentDescriptor = AttachmentDescriptor(
Expand Down Expand Up @@ -1244,6 +1232,7 @@ open class EdgeAgent {
?: throw EdgeAgentError.CannotFindDIDPrivateKey(didString)
val privateKey =
apollo.restorePrivateKey(storablePrivateKey.restorationIdentifier, storablePrivateKey.data)

val presentationSubmissionProof = pollux.createJWTPresentationSubmission(
presentationDefinitionRequest = presentationDefinitionRequestString,
credential = credential,
Expand All @@ -1266,6 +1255,7 @@ open class EdgeAgent {
)
} else {
val linkSecret = getLinkSecret()

val presentationSubmissionProof = pollux.createAnoncredsPresentationSubmission(
presentationDefinitionRequest = presentationDefinitionRequestString,
credential = credential,
Expand Down Expand Up @@ -1490,11 +1480,16 @@ open class EdgeAgent {
}
}

private fun generateNonce(size: Int = 16): String {
private fun generateNumericNonce(size: Int = 16): String {
val random = SecureRandom()
val nonce = ByteArray(size)
random.nextBytes(nonce)
return Base64.getUrlEncoder().withoutPadding().encodeToString(nonce)
val nonce = StringBuilder(size)

repeat(size) {
val digit = random.nextInt(10) // Generates a number between 0 and 9
nonce.append(digit)
}

return nonce.toString()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,7 @@ class BasicMediatorHandler(
return flow {
val message = mercury.sendMessageParseResponse(requestMessage)
message?.let {
try {
emit(PickupRunner(message, mercury).run())
} catch (e: Exception) {
println("Message of type ${message.piuri} cannot be sent to PickupRunner")
e.printStackTrace()
}
emit(PickupRunner(message, mercury).run())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.hyperledger.identus.walletsdk.edgeagent.protocols.pickup

import org.hyperledger.identus.apollo.base64.base64UrlDecoded
import org.hyperledger.identus.walletsdk.domain.buildingblocks.Mercury
import org.hyperledger.identus.walletsdk.domain.models.AttachmentData.AttachmentBase64
import org.hyperledger.identus.walletsdk.domain.models.AttachmentData.AttachmentJsonData
import org.hyperledger.identus.walletsdk.domain.models.AttachmentDescriptor
import org.hyperledger.identus.walletsdk.domain.models.Message
import org.hyperledger.identus.walletsdk.edgeagent.EdgeAgentError
Expand Down Expand Up @@ -97,22 +94,12 @@ class PickupRunner(message: Message, private val mercury: Mercury) {
* @return The PickupAttachment object if the attachment data is of type AttachmentBase64 or AttachmentJsonData, otherwise null.
*/
private fun processAttachment(attachment: AttachmentDescriptor): PickupAttachment? {
return when (attachment.data) {
is AttachmentBase64 -> {
PickupAttachment(
attachmentId = attachment.id,
data = attachment.data.base64.base64UrlDecoded
)
}

is AttachmentJsonData -> {
PickupAttachment(
attachmentId = attachment.id,
data = attachment.data.data
)
}
val data = attachment.data.getDataAsJsonString()
val id = attachment.id

else -> null
}
return PickupAttachment(
attachmentId = id,
data = data
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,12 @@ data class AnonCredential(
throw UnknownError.SomethingWentWrongError()
}

val presentationRequest = PresentationRequest(request.toString())
val decodedRequest = request.decodeToString()
val presentationRequest = PresentationRequest(decodedRequest)
val cred = anoncreds_uniffi.Credential(this.id)

val requestedAttributes = extractRequestedAttributes(request.toString())
val requestedPredicates = extractRequestedPredicatesKeys(request.toString())
val requestedAttributes = extractRequestedAttributes(decodedRequest)
val requestedPredicates = extractRequestedPredicatesKeys(decodedRequest)

val credentialRequests = anoncreds_uniffi.RequestedCredential(
cred = cred,
Expand Down

0 comments on commit 778cb91

Please sign in to comment.