From 7a8b4365a163d2e810ffd936a32fea78d37f2cc8 Mon Sep 17 00:00:00 2001 From: conanoc Date: Mon, 17 Jun 2024 15:59:04 +0900 Subject: [PATCH] Fix DID Exchange protocol (#30) Signed-off-by: conanoc --- .../connection/ConnectionCommand.kt | 23 ++++++++++++++----- .../connection/PeerDIDService.kt | 2 +- .../ariesframework/ledger/LedgerService.kt | 9 +++----- .../ariesframework/oob/OutOfBandCommand.kt | 23 ++++++++++++++++--- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/ConnectionCommand.kt b/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/ConnectionCommand.kt index 4058a1f..9d7059c 100644 --- a/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/ConnectionCommand.kt +++ b/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/ConnectionCommand.kt @@ -19,6 +19,7 @@ import org.hyperledger.ariesframework.connection.messages.DidExchangeResponseMes import org.hyperledger.ariesframework.connection.messages.TrustPingMessage import org.hyperledger.ariesframework.connection.repository.ConnectionRecord import org.hyperledger.ariesframework.oob.messages.OutOfBandInvitation +import org.hyperledger.ariesframework.oob.models.HandshakeProtocol import org.hyperledger.ariesframework.oob.models.ReceiveOutOfBandInvitationConfig import org.hyperledger.ariesframework.oob.repository.OutOfBandRecord import org.slf4j.LoggerFactory @@ -143,11 +144,13 @@ class ConnectionCommand(val agent: Agent, private val dispatcher: Dispatcher) { * Accept a connection invitation as invitee (by sending a connection request message) for the connection with the specified connection id. * This is not needed when auto accepting of connections is enabled. * @param outOfBandRecord out of band record containing the invitation to accept. + * @param handshakeProtocol handshake protocol to use for accepting the invitation. * @param config optional config for accepting the invitation. * @return new connection record. */ suspend fun acceptOutOfBandInvitation( outOfBandRecord: OutOfBandRecord, + handshakeProtocol: HandshakeProtocol, config: ReceiveOutOfBandInvitationConfig? = null, ): ConnectionRecord { val connection = receiveInvitation( @@ -156,12 +159,20 @@ class ConnectionCommand(val agent: Agent, private val dispatcher: Dispatcher) { false, config?.alias, ) - val message = agent.connectionService.createRequest( - connection.id, - config?.label, - config?.imageUrl, - config?.autoAcceptConnection, - ) + val message = if (handshakeProtocol == HandshakeProtocol.Connections) { + agent.connectionService.createRequest( + connection.id, + config?.label, + config?.imageUrl, + config?.autoAcceptConnection, + ) + } else { + agent.didExchangeService.createRequest( + connection.id, + config?.label, + config?.autoAcceptConnection, + ) + } agent.messageSender.send(message) return message.connection } diff --git a/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/PeerDIDService.kt b/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/PeerDIDService.kt index f565acb..51ca250 100644 --- a/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/PeerDIDService.kt +++ b/ariesframework/src/main/java/org/hyperledger/ariesframework/connection/PeerDIDService.kt @@ -38,7 +38,7 @@ class PeerDIDService(val agent: Agent) { val (endpoints, routingKeys) = agent.mediationRecipient.getRoutingInfo() val didRoutingKeys = routingKeys.map { rawKey -> val key = DIDParser.convertVerkeyToDidKey(rawKey) - return "$key#${DIDParser.getMethodId(key)}" + return@map "$key#${DIDParser.getMethodId(key)}" } val authKey = VerificationMaterialAuthentication( type = VerificationMethodTypeAuthentication.ED25519_VERIFICATION_KEY_2020, diff --git a/ariesframework/src/main/java/org/hyperledger/ariesframework/ledger/LedgerService.kt b/ariesframework/src/main/java/org/hyperledger/ariesframework/ledger/LedgerService.kt index 2e80477..c5fab1b 100644 --- a/ariesframework/src/main/java/org/hyperledger/ariesframework/ledger/LedgerService.kt +++ b/ariesframework/src/main/java/org/hyperledger/ariesframework/ledger/LedgerService.kt @@ -52,7 +52,7 @@ class LedgerService(val agent: Agent) { logger.info("Initializing Pool") if (pool != null) { logger.warn("Pool already initialized.") - close() + return } setProtocolVersion(2) @@ -360,10 +360,7 @@ class LedgerService(val agent: Agent) { return response } - suspend fun close() { - if (pool != null) { - pool!!.close() - pool = null - } + fun close() { + logger.warn("Do not call close on LedgerService. It will be auto closed") } } diff --git a/ariesframework/src/main/java/org/hyperledger/ariesframework/oob/OutOfBandCommand.kt b/ariesframework/src/main/java/org/hyperledger/ariesframework/oob/OutOfBandCommand.kt index 836e04c..1184508 100644 --- a/ariesframework/src/main/java/org/hyperledger/ariesframework/oob/OutOfBandCommand.kt +++ b/ariesframework/src/main/java/org/hyperledger/ariesframework/oob/OutOfBandCommand.kt @@ -146,9 +146,8 @@ class OutOfBandCommand(val agent: Agent, private val dispatcher: Dispatcher) { return InvitationUrlParser.parseUrl(url) } - // Only Connection protocol is supported for now private fun getSupportedHandshakeProtocols(): List { - return listOf(HandshakeProtocol.Connections) + return listOf(HandshakeProtocol.Connections, HandshakeProtocol.DidExchange11) } /** @@ -259,13 +258,14 @@ class OutOfBandCommand(val agent: Agent, private val dispatcher: Dispatcher) { } } + val handshakeProtocol = selectHandshakeProtocol(handshakeProtocols) if (connectionRecord == null) { logger.debug("Creating new connection.") if (!handshakeProtocols.contains(HandshakeProtocol.Connections)) { throw Exception("Unsupported handshake protocol. Supported protocols: $handshakeProtocols") } - connectionRecord = agent.connections.acceptOutOfBandInvitation(outOfBandRecord, config) + connectionRecord = agent.connections.acceptOutOfBandInvitation(outOfBandRecord, handshakeProtocol, config) } if (agent.connectionService.fetchState(connectionRecord) != ConnectionState.Complete) { @@ -334,4 +334,21 @@ class OutOfBandCommand(val agent: Agent, private val dispatcher: Dispatcher) { } return connections.firstOrNull { it.isReady() } } + + private suspend fun selectHandshakeProtocol(handshakeProtocols: List): HandshakeProtocol { + val supportedProtocols = getSupportedHandshakeProtocols() + if (handshakeProtocols.contains(agent.agentConfig.preferredHandshakeProtocol) && + supportedProtocols.contains(agent.agentConfig.preferredHandshakeProtocol) + ) { + return agent.agentConfig.preferredHandshakeProtocol + } + + for (protocolName in handshakeProtocols) { + if (supportedProtocols.contains(protocolName)) { + return protocolName + } + } + + throw Exception("None of the provided handshake protocols $handshakeProtocols are supported. Supported protocols are $supportedProtocols") + } }