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
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class XMTPModule : Module() {
}

AsyncFunction("createFromKeyBundle") { keyBundle: String, environment: String ->
try {
logV("createFromKeyBundle")
val options =
ClientOptions(api = apiEnvironments[environment] ?: apiEnvironments["dev"]!!)
Expand All @@ -141,6 +142,9 @@ class XMTPModule : Module() {
val client = Client().buildFromBundle(bundle = bundle, options = options)
clients[client.address] = client
client.address
} catch (e: Exception) {
throw XMTPException("Failed to create client: $e")
}
}

AsyncFunction("exportKeyBundle") { clientAddress: String ->
Expand Down
11 changes: 11 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as XMTP from "../../src/index";
import { DecodedMessage } from "../../src/index";
import { CodecError } from "../../src/lib/CodecError";
import { CodecRegistry } from "../../src/lib/CodecRegistry";
import { randomBytes } from "crypto";

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -178,3 +179,13 @@ test("can send and receive number codec", async () => {
return false;
}
});

test("createFromKeyBundle throws error for non string value", async () => {
try {
const bytes = randomBytes(32);
await XMTP.Client.createFromKeyBundle(JSON.stringify(bytes), "local");
} catch (e) {
return true;
}
return false;
});
22 changes: 16 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,20 @@ 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 {
print("ERRO! Failed to create client: \(error)")
throw error
}
}

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