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

Fix: Refactors createFromKeyBundle swift function to guard string conv #70

Merged
23 changes: 17 additions & 6 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public class XMTPModule: Module {
var subscriptions: [String: Task<Void, Never>] = [:]

enum Error: Swift.Error {
case noClient, conversationNotFound(String), noMessage
case noClient, conversationNotFound(String), noMessage, invalidKeyBundle

}

public func definition() -> ModuleDefinition {
Expand Down Expand Up @@ -135,11 +136,21 @@ public class XMTPModule: Module {

// Create a client using its serialized key bundle.
AsyncFunction("createFromKeyBundle") { (keyBundle: String, environment: String) -> String in
let bundle = try PrivateKeyBundle(serializedData: Data(base64Encoded: Data(keyBundle.utf8))!)
let options = XMTP.ClientOptions(api: apiEnvironments[environment] ?? apiEnvironments["dev"]!)
let client = try await Client.from(bundle: bundle, options: options)
self.clients[client.address] = client
return client.address
do {
guard let keyBundleData = Data(base64Encoded: keyBundle),
let bundle = try? PrivateKeyBundle(serializedData: keyBundleData) else {
throw Error.invalidKeyBundle
}
Jonathansoufer marked this conversation as resolved.
Show resolved Hide resolved

let options = XMTP.ClientOptions(api: apiEnvironments[environment] ?? apiEnvironments["dev"]!)
let client = try await Client.from(bundle: bundle, options: options)
self.clients[client.address] = client
return client.address
} catch {
throw error
print("ERRO! Failed to create client: \(error)")
return ""
Jonathansoufer marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Export the client's serialized key bundle.
Expand Down