Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve V3 Client Create #254

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public struct ClientOptions {

public var mlsAlpha = false
public var mlsEncryptionKey: Data?
public var mlsDbPath: String?

public init(
api: Api = Api(),
Expand Down Expand Up @@ -128,13 +129,28 @@ public final class Client {
signingKey: SigningKey?
) async throws -> FfiXmtpClient? {
if options?.mlsAlpha == true, options?.api.env.supportsMLS == true {
let dbURL = URL.documentsDirectory.appendingPathComponent("xmtp-\(options?.api.env.rawValue ?? "")-\(address).db3")
let dbURL = options?.mlsDbPath ?? URL.documentsDirectory.appendingPathComponent("xmtp-\(options?.api.env.rawValue ?? "")-\(address).db3").path

var encryptionKey = options?.mlsEncryptionKey
if (encryptionKey == nil) {
let preferences = UserDefaults.standard
let key = "xmtp-key"
if preferences.data(forKey: key) == nil {
let data = Data(try Crypto.secureRandomBytes(count: 32))
preferences.set(data, forKey: key)
preferences.synchronize()
encryptionKey = data
} else {
encryptionKey = preferences.data(forKey: key)
}
}

let v3Client = try await LibXMTP.createClient(
logger: XMTPLogger(),
host: (options?.api.env ?? .local).url,
isSecure: options?.api.env.isSecure == true,
db: dbURL.path,
encryptionKey: options?.mlsEncryptionKey,
db: dbURL,
encryptionKey: encryptionKey,
accountAddress: address,
legacyIdentitySource: source,
legacySignedPrivateKeyProto: try privateKeyBundleV1.toV2().identityKey.serializedData()
Expand Down
Loading