Skip to content

Commit

Permalink
Merge pull request #1 from beatt83/feature/add-send-to-routing
Browse files Browse the repository at this point in the history
feat(routing): add new send to result parameter
  • Loading branch information
beatt83 committed Feb 12, 2024
2 parents 6e7b0cd + 5bc1e22 commit 486a953
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Sources/DIDCommSwift/Models/Results/RoutingResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public struct RoutingResult {
public struct ForwardMessageResult {
/// The identifier (DID) of the final recipient of the forwarded message.
public let finalRecipient: String
/// An list with the recipients for the first forward message (whom you need to send the forward message).
public let sendTo: [String]
/// An ordered list of identifiers (DIDs) for the mediators that routed the message.
public let routedBy: [String]
/// The final encrypted forward message intended for the final recipient.
Expand All @@ -42,6 +44,7 @@ public struct RoutingResult {
let finalRecipient = leaf.value.finalRecipient
return ForwardMessageResult(
finalRecipient: finalRecipient,
sendTo: leaf.value.encryptedResult.toKids,
routedBy: routedBy,
forwardMessage: leaf.value.encryptedResult
)
Expand Down
6 changes: 3 additions & 3 deletions Sources/DIDCommSwift/Models/Routing/ServiceTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ private func buildBranchsTree(
to: String,
parentNode: Node<ServiceTree.Branch>
) async throws {
guard let did = DID(from: to) else {
guard let didUrl = DIDUrl(from: to) else {
throw DIDCommError.invalidDID(to)
}

let document = try await didResolver.resolve(did: did)
let document = try await didResolver.resolve(did: didUrl.did)
let routingTo = try getRoutingURIAndKeys(document: document)

try await routingTo.asyncForEach {
let nextDID = DID(from: $0.uri)
let nextDID = DIDUrl(from: $0.uri)?.did
guard nextDID != nil || !$0.keys.isEmpty else {
return
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/DIDCommSwiftTests/DIDComm/RoutingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ final class RoutingTests: XCTestCase {
XCTAssertTrue(mediator1Routing.value.to.contains("did:example:bobMediator1"))
XCTAssertTrue(mediator1Routing.children.isEmpty) // Just forwarded once
XCTAssertEqual(forwardMessages[0].routedBy.count, 1) // Just routed by 1
XCTAssertEqual(forwardMessages[0].sendTo, ["did:example:bobMediator1#key-x25519-1"])
XCTAssertTrue(mediator2Routing.value.to.contains("did:example:bobMediator2"))
XCTAssertEqual(mediator2Routing.children.count, 1) // Forwarded twice
XCTAssertEqual(forwardMessages[1].routedBy.count, 2) // Just routed by 2
XCTAssertEqual(forwardMessages[1].sendTo, ["did:example:bob#key-x25519-1"])
XCTAssertTrue(mediator3Routing.value.to.contains("did:example:bobMediator3"))
XCTAssertTrue(mediator3Routing.children.isEmpty) // Just forwarded once
XCTAssertEqual(forwardMessages[2].routedBy.count, 2) // Just routed by 2
XCTAssertEqual(forwardMessages[2].sendTo, ["did:example:bob#key-x25519-1", "did:example:bobMediator3#key-x25519-1"])
}

func testPackRoutingMultipleRecipientsMessage() async throws {
Expand Down Expand Up @@ -78,12 +81,15 @@ final class RoutingTests: XCTestCase {
XCTAssertTrue(mediator2Routing.value.to.contains("did:example:bobMediator2"))
XCTAssertEqual(mediator2Routing.children.count, 1) // Forwarded twice
XCTAssertEqual(forwardMessages[1].routedBy.count, 2) // Just routed by 2
XCTAssertEqual(forwardMessages[1].sendTo, ["did:example:bob#key-x25519-1"])
XCTAssertTrue(mediator3Routing.value.to.contains("did:example:bobMediator3"))
XCTAssertTrue(mediator3Routing.value.to.contains("did:example:bob#key-x25519-1"))
XCTAssertTrue(mediator3Routing.children.isEmpty) // Just forwarded once
XCTAssertEqual(forwardMessages[2].routedBy.count, 2) // Just routed by 2
XCTAssertEqual(forwardMessages[2].sendTo, ["did:example:bob#key-x25519-1", "did:example:bobMediator3#key-x25519-1"])
XCTAssertTrue(aliceRouting.value.to.contains("did:example:alice#key-x25519-1"))
XCTAssertTrue(aliceRouting.children.isEmpty) // Just forwarded once
XCTAssertEqual(forwardMessages[3].routedBy.count, 1) // Just routed by 2
XCTAssertEqual(forwardMessages[3].sendTo, ["did:example:alice#key-x25519-1"])
}
}

0 comments on commit 486a953

Please sign in to comment.