From f27dcb89d78c0935b75d412d300250df43377d59 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Mon, 24 Jun 2024 15:32:25 -0700 Subject: [PATCH] Add new fetch methods for V3 local DB (#355) * bump the version * add message history url * add all the fucntions * write tests for it --- Package.swift | 2 +- Sources/XMTPiOS/Client.swift | 43 ++++++++++++++++++- Tests/XMTPTests/ClientTests.swift | 22 ++++++++++ Tests/XMTPTests/GroupTests.swift | 24 +++++++++++ XMTP.podspec | 4 +- .../xcshareddata/swiftpm/Package.resolved | 4 +- 6 files changed, 92 insertions(+), 7 deletions(-) diff --git a/Package.swift b/Package.swift index 7fde93f3..fa22bcd0 100644 --- a/Package.swift +++ b/Package.swift @@ -25,7 +25,7 @@ let package = Package( .package(url: "https://github.com/1024jp/GzipSwift", from: "5.2.0"), .package(url: "https://github.com/bufbuild/connect-swift", exact: "0.12.0"), .package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"), - .package(url: "https://github.com/xmtp/libxmtp-swift", exact: "0.5.3-beta0"), + .package(url: "https://github.com/xmtp/libxmtp-swift", exact: "0.5.3-beta1"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index aa0aff69..393b02cc 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -57,6 +57,7 @@ public struct ClientOptions { public var enableV3 = false public var dbEncryptionKey: Data? public var dbDirectory: String? + public var historySyncUrl: String? public init( api: Api = Api(), @@ -65,7 +66,8 @@ public struct ClientOptions { preCreateIdentityCallback: PreEventCallback? = nil, enableV3: Bool = false, encryptionKey: Data? = nil, - dbDirectory: String? = nil + dbDirectory: String? = nil, + historySyncUrl: String? = nil ) { self.api = api self.codecs = codecs @@ -74,6 +76,7 @@ public struct ClientOptions { self.enableV3 = enableV3 self.dbEncryptionKey = encryptionKey self.dbDirectory = dbDirectory + self.historySyncUrl = historySyncUrl } } @@ -182,7 +185,7 @@ public final class Client { accountAddress: address, nonce: 0, legacySignedPrivateKeyProto: try privateKeyBundleV1.toV2().identityKey.serializedData(), - historySyncUrl: nil + historySyncUrl: options?.historySyncUrl ) if let signatureRequest = v3Client.signatureRequest() { @@ -518,4 +521,40 @@ public final class Client { let peerAddress = EthereumAddress(peerAddress).toChecksumAddress() return try await contacts.find(peerAddress) } + + public func inboxIdFromAddress(address: String) async throws -> String? { + guard let client = v3Client else { + throw ClientError.noV3Client("Error no V3 client initialized") + } + return try await client.findInboxId(address: address.lowercased()) + } + + public func findGroup(groupId: Data) throws -> Group? { + guard let client = v3Client else { + throw ClientError.noV3Client("Error no V3 client initialized") + } + do { + return Group(ffiGroup: try client.group(groupId: groupId), client: self) + } catch { + return nil + } + } + + public func findMessage(messageId: Data) throws -> MessageV3? { + guard let client = v3Client else { + throw ClientError.noV3Client("Error no V3 client initialized") + } + do { + return MessageV3(client: self, ffiMessage: try client.message(messageId: messageId)) + } catch { + return nil + } + } + + public func requestMessageHistorySync() async throws { + guard let client = v3Client else { + throw ClientError.noV3Client("Error no V3 client initialized") + } + try await client.requestHistorySync() + } } diff --git a/Tests/XMTPTests/ClientTests.swift b/Tests/XMTPTests/ClientTests.swift index 9fa2e78e..867fddba 100644 --- a/Tests/XMTPTests/ClientTests.swift +++ b/Tests/XMTPTests/ClientTests.swift @@ -338,4 +338,26 @@ class ClientTests: XCTestCase { ) ) } + + func testCanGetAnInboxIdFromAddress() async throws { + let bo = try PrivateKey.generate() + let alix = try PrivateKey.generate() + let boClient = try await Client.create( + account: bo, + options: .init( + api: .init(env: .local, isSecure: false), + enableV3: true + ) + ) + + let alixClient = try await Client.create( + account: alix, + options: .init( + api: .init(env: .local, isSecure: false), + enableV3: true + ) + ) + let boInboxId = try await alixClient.inboxIdFromAddress(address: boClient.address) + XCTAssertEqual(boClient.inboxID, boInboxId) + } } diff --git a/Tests/XMTPTests/GroupTests.swift b/Tests/XMTPTests/GroupTests.swift index 745b315e..b040bdea 100644 --- a/Tests/XMTPTests/GroupTests.swift +++ b/Tests/XMTPTests/GroupTests.swift @@ -777,4 +777,28 @@ class GroupTests: XCTestCase { XCTAssert(!isAllowed3) XCTAssert(isDenied3) } + + func testCanFetchGroupById() async throws { + let fixtures = try await localFixtures() + + let boGroup = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + try await fixtures.aliceClient.conversations.sync() + let alixGroup = try fixtures.aliceClient.findGroup(groupId: boGroup.id) + + XCTAssertEqual(alixGroup?.id.toHex, boGroup.id.toHex) + } + + func testCanFetchMessageById() async throws { + let fixtures = try await localFixtures() + + let boGroup = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + + let boMessageId = try await boGroup.send(content: "Hello") + try await fixtures.aliceClient.conversations.sync() + let alixGroup = try fixtures.aliceClient.findGroup(groupId: boGroup.id) + try await alixGroup?.sync() + let alixMessage = try fixtures.aliceClient.findMessage(messageId: Data(boMessageId.web3.bytesFromHex!)) + + XCTAssertEqual(alixGroup?.id.toHex, boGroup.id.toHex) + } } diff --git a/XMTP.podspec b/XMTP.podspec index c074acbb..3579d380 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XMTP" - spec.version = "0.12.2" + spec.version = "0.12.3" spec.summary = "XMTP SDK Cocoapod" # This description is used to generate tags and improve search results. @@ -44,5 +44,5 @@ Pod::Spec.new do |spec| spec.dependency "web3.swift" spec.dependency "GzipSwift" spec.dependency "Connect-Swift", "= 0.12.0" - spec.dependency 'LibXMTP', '= 0.5.3-beta0' + spec.dependency 'LibXMTP', '= 0.5.3-beta1' end diff --git a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index bbbbcb8c..efbcefd3 100644 --- a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -59,8 +59,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/xmtp/libxmtp-swift", "state" : { - "revision" : "e5c0e66acbaa1bc94fd4a09431b40c5027da8396", - "version" : "0.5.3-beta0" + "revision" : "e7df1b0d2e55ca1ffdeab0ffff3c968e9a6a5c30", + "version" : "0.5.3-beta1" } }, {