Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 5, 2024
1 parent 70a61ee commit 327b000
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 23 deletions.
47 changes: 28 additions & 19 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public enum ClientError: Error {
case creationError(String)
}

public enum MLSAlphaOption {
case disabled, enabled(SigningKey?)
}

/// Specify configuration options for creating a ``Client``.
public struct ClientOptions {
// Specify network options
Expand Down Expand Up @@ -44,15 +48,15 @@ public struct ClientOptions {
/// `preCreateIdentityCallback` will be called immediately before a Create Identity wallet signature is requested from the user.
public var preCreateIdentityCallback: PreEventCallback?

public var enableAlphaMLS: Bool = false
public var enableAlphaMLS: MLSAlphaOption = .disabled
public var mlsEncryptionKey: Data?

public init(
api: Api = Api(),
codecs: [any ContentCodec] = [],
preEnableIdentityCallback: PreEventCallback? = nil,
preCreateIdentityCallback: PreEventCallback? = nil,
enableAlphaMLS: Bool = false,
enableAlphaMLS: MLSAlphaOption = .disabled,
mlsEncryptionKey: Data? = nil
) {
self.api = api
Expand Down Expand Up @@ -112,12 +116,15 @@ public final class Client {
}
}

static func initV3Client(address: String, options: ClientOptions?, source: LegacyIdentitySource, privateKeyBundleV1: PrivateKeyBundleV1) async throws -> FfiXmtpClient? {
let v3Client: FfiXmtpClient?

if options?.enableAlphaMLS == true && options?.api.env == .local {
static func initV3Client(
address: String,
options: ClientOptions?,
source: LegacyIdentitySource,
privateKeyBundleV1: PrivateKeyBundleV1
) async throws -> FfiXmtpClient? {
if case let .enabled(signingKey) = options?.enableAlphaMLS, options?.api.env == .local {
let dbURL = URL.documentsDirectory.appendingPathComponent("xmtp-\(options?.api.env.rawValue ?? "")-\(address).db3")
v3Client = try await LibXMTP.createClient(
let v3Client = try await LibXMTP.createClient(
logger: XMTPLogger(),
host: GRPCApiClient.envToUrl(env: options?.api.env ?? .local),
isSecure: (options?.api.env ?? .local) != .local,
Expand All @@ -127,11 +134,22 @@ public final class Client {
legacyIdentitySource: source,
legacySignedPrivateKeyProto: try privateKeyBundleV1.toV2().identityKey.serializedData()
)

if let textToSign = v3Client.textToSign() {
guard let signingKey else {
throw ClientError.creationError("No v3 keys found, you must pass a SigningKey in order to enable alpha MLS features")
}

let signature = try await signingKey.sign(message: textToSign)
try await v3Client.registerIdentity(recoverableWalletSignature: signature.rawData)
} else {
try await v3Client.registerIdentity(recoverableWalletSignature: nil)
}

return v3Client
} else {
v3Client = nil
return nil
}

return v3Client
}

static func create(account: SigningKey, apiClient: ApiClient, options: ClientOptions? = nil) async throws -> Client {
Expand Down Expand Up @@ -240,15 +258,6 @@ public final class Client {
privateKeyBundleV1: v1Bundle
)

if let v3Client, let textToSign = v3Client.textToSign() {
guard let signingKey else {
throw ClientError.creationError("No v3 keys found, you must pass a SigningKey in order to enable alpha MLS features")
}

let signature = try await signingKey.sign(message: textToSign)
try await v3Client.registerIdentity(recoverableWalletSignature: signature.rawData)
}

let result = try Client(address: address, privateKeyBundleV1: v1Bundle, apiClient: apiClient, v3Client: v3Client)

for codec in options.codecs {
Expand Down
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Extensions/URL.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// File.swift
// URL.swift
//
//
// Created by Pat Nakajima on 2/1/24.
Expand Down
48 changes: 45 additions & 3 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,49 @@ class ClientTests: XCTestCase {
_ = try await Client.create(account: fakeWallet)
}

func testPassingSavedKeysWithNoSignerWithMLSErrors() async throws {
try TestConfig.skipIfNotRunningLocalNodeTests()

let bo = try PrivateKey.generate()

do {
let client = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
enableAlphaMLS: .enabled(bo)
)
)
} catch {
XCTAssert(error.localizedDescription.contains("no keys"))
}
}

func testPassingSavedKeysWithMLS() async throws {
try TestConfig.skipIfNotRunningLocalNodeTests()

let bo = try PrivateKey.generate()
let client = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
enableAlphaMLS: .enabled(bo)
)
)

let keys = client.privateKeyBundle
let otherClient = try await Client.from(
bundle: keys,
options: .init(
api: .init(env: .local, isSecure: false),
// Should not need to pass the signer again
enableAlphaMLS: .enabled(nil)
)
)

XCTAssertEqual(client.address, otherClient.address)
}

func testPassingMLSEncryptionKey() async throws {
try TestConfig.skipIfNotRunningLocalNodeTests()

Expand All @@ -30,7 +73,7 @@ class ClientTests: XCTestCase {
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
enableAlphaMLS: true,
enableAlphaMLS: .enabled(bo),
mlsEncryptionKey: key
)
)
Expand All @@ -40,7 +83,7 @@ class ClientTests: XCTestCase {
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
enableAlphaMLS: true,
enableAlphaMLS: .enabled(bo),
mlsEncryptionKey: nil // No key should error
)
)
Expand All @@ -51,7 +94,6 @@ class ClientTests: XCTestCase {
}
}


func testCanMessage() async throws {
let fixtures = await fixtures()
let notOnNetwork = try PrivateKey.generate()
Expand Down

0 comments on commit 327b000

Please sign in to comment.