Skip to content

Commit

Permalink
Add new fetch methods for V3 local DB (#355)
Browse files Browse the repository at this point in the history
* bump the version

* add message history url

* add all the fucntions

* write tests for it
  • Loading branch information
nplasterer authored Jun 24, 2024
1 parent fd5efb7 commit f27dcb8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
43 changes: 41 additions & 2 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -74,6 +76,7 @@ public struct ClientOptions {
self.enableV3 = enableV3
self.dbEncryptionKey = encryptionKey
self.dbDirectory = dbDirectory
self.historySyncUrl = historySyncUrl
}
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
}
}
22 changes: 22 additions & 0 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
24 changes: 24 additions & 0 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down

0 comments on commit f27dcb8

Please sign in to comment.