From d17af5ed135239858e406094c46380bc68ba551d Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 4 Jun 2024 00:08:44 -0700 Subject: [PATCH] Add ability to set dbDirectory (#345) * adds the ability to set the db directory * bump the pod --- Sources/XMTPiOS/Client.swift | 25 ++++++++++++-- Tests/XMTPTests/ClientTests.swift | 54 +++++++++++++++++++++++++++++++ XMTP.podspec | 2 +- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index 3bbc7bfb..e8382f0d 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -64,7 +64,8 @@ public struct ClientOptions { preEnableIdentityCallback: PreEventCallback? = nil, preCreateIdentityCallback: PreEventCallback? = nil, mlsAlpha: Bool = false, - mlsEncryptionKey: Data? = nil + mlsEncryptionKey: Data? = nil, + mlsDbDirectory: String? = nil ) { self.api = api self.codecs = codecs @@ -72,6 +73,7 @@ public struct ClientOptions { self.preCreateIdentityCallback = preCreateIdentityCallback self.mlsAlpha = mlsAlpha self.mlsEncryptionKey = mlsEncryptionKey + self.mlsDbDirectory = mlsDbDirectory } } @@ -148,9 +150,26 @@ public final class Client { inboxId = generateInboxId(accountAddress: address, nonce: 0) } - let alias = "xmtp-\(options?.api.env.rawValue ?? "")-\(inboxId).db3" - let dbURL = URL.documentsDirectory.appendingPathComponent(alias).path + let mlsDbDirectory = options?.mlsDbDirectory + var directoryURL: URL + if let mlsDbDirectory = mlsDbDirectory { + let fileManager = FileManager.default + directoryURL = URL.documentsDirectory.appendingPathComponent(mlsDbDirectory) + // Check if the directory exists, if not, create it + if !fileManager.fileExists(atPath: directoryURL.path) { + do { + try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) + } catch { + throw ClientError.creationError("Failed db directory \(mlsDbDirectory)") + } + } + } else { + directoryURL = URL.documentsDirectory + } + let alias = "xmtp-\(options?.api.env.rawValue ?? "")-\(inboxId).db3" + let dbURL = directoryURL.appendingPathComponent(alias).path + let encryptionKey = options?.mlsEncryptionKey let v3Client = try await LibXMTP.createClient( diff --git a/Tests/XMTPTests/ClientTests.swift b/Tests/XMTPTests/ClientTests.swift index 908b250d..bba2de24 100644 --- a/Tests/XMTPTests/ClientTests.swift +++ b/Tests/XMTPTests/ClientTests.swift @@ -284,4 +284,58 @@ class ClientTests: XCTestCase { XCTFail("Error: \(error)") } } + + func testPassingMLSEncryptionKeyAndDatabaseDirectory() async throws { + let bo = try PrivateKey.generate() + let key = try Crypto.secureRandomBytes(count: 32) + + let client = try await Client.create( + account: bo, + options: .init( + api: .init(env: .local, isSecure: false), + mlsAlpha: true, + mlsEncryptionKey: key, + mlsDbDirectory: "xmtp_db" + ) + ) + + let keys = client.privateKeyBundle + let bundleClient = try await Client.from( + bundle: keys, + options: .init( + api: .init(env: .local, isSecure: false), + mlsAlpha: true, + mlsEncryptionKey: key, + mlsDbDirectory: "xmtp_db" + ) + ) + + XCTAssertEqual(client.address, bundleClient.address) + XCTAssertEqual(client.dbPath, bundleClient.dbPath) + XCTAssert(!client.installationID.isEmpty) + + await assertThrowsAsyncError( + _ = try await Client.from( + bundle: keys, + options: .init( + api: .init(env: .local, isSecure: false), + mlsAlpha: true, + mlsEncryptionKey: nil, + mlsDbDirectory: "xmtp_db" + ) + ) + ) + + await assertThrowsAsyncError( + _ = try await Client.from( + bundle: keys, + options: .init( + api: .init(env: .local, isSecure: false), + mlsAlpha: true, + mlsEncryptionKey: key, + mlsDbDirectory: nil + ) + ) + ) + } } diff --git a/XMTP.podspec b/XMTP.podspec index c8f5381b..3b5bbf85 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XMTP" - spec.version = "0.11.2" + spec.version = "0.11.3" spec.summary = "XMTP SDK Cocoapod" # This description is used to generate tags and improve search results.