Skip to content

Commit

Permalink
Improve V3 Client Create (#254)
Browse files Browse the repository at this point in the history
* add the ability to pass in a DB url

* set a default encryption key if one is not passed
  • Loading branch information
nplasterer authored Feb 20, 2024
1 parent 56310bc commit c0eacb2
Showing 1 changed file with 19 additions and 3 deletions.
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

0 comments on commit c0eacb2

Please sign in to comment.