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 DID Exchange protocol #30

Merged
merged 1 commit into from
Jun 17, 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
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HandshakeProtocol> {
return listOf(HandshakeProtocol.Connections)
return listOf(HandshakeProtocol.Connections, HandshakeProtocol.DidExchange11)
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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>): 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")
}
}
Loading