Skip to content

Commit

Permalink
Add more decrypted message functions (#196)
Browse files Browse the repository at this point in the history
* Add batch list for decrypted messages

* Add streamAllDecryptedMessages
  • Loading branch information
nakajima authored Nov 27, 2023
1 parent 7e1edef commit 6e81fe1
Show file tree
Hide file tree
Showing 3 changed files with 399 additions and 315 deletions.
9 changes: 9 additions & 0 deletions Sources/XMTP/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ public enum Conversation: Sendable {
}
}

public func decrypt(_ envelope: Envelope) throws -> DecryptedMessage {
switch self {
case let .v1(conversationV1):
return try conversationV1.decrypt(envelope: envelope)
case let .v2(conversationV2):
return try conversationV2.decrypt(envelope: envelope)
}
}

public func encode<Codec: ContentCodec, T>(codec: Codec, content: T) async throws -> Data where Codec.T == T {
switch self {
case let .v1:
Expand Down
8 changes: 6 additions & 2 deletions Sources/XMTP/ConversationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ public struct ConversationV2 {
let envelopes = try await client.apiClient.envelopes(topic: topic.description, pagination: pagination)

return try envelopes.map { envelope in
let message = try Message(serializedData: envelope.message)
return try MessageV2.decrypt(generateID(from: envelope), topic, message.v2, keyMaterial: keyMaterial, client: client)
try decrypt(envelope: envelope)
}
}

func decrypt(envelope: Envelope) throws -> DecryptedMessage {
let message = try Message(serializedData: envelope.message)
return try MessageV2.decrypt(generateID(from: envelope), topic, message.v2, keyMaterial: keyMaterial, client: client)
}

var ephemeralTopic: String {
topic.replacingOccurrences(of: "/xmtp/0/m", with: "/xmtp/0/mE")
}
Expand Down
Loading

0 comments on commit 6e81fe1

Please sign in to comment.