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

Static Can Message #444

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
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
46 changes: 42 additions & 4 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,20 @@ public final class Client {
)
}

public static func build(address: String, options: ClientOptions, inboxId: String? = nil)
public static func build(
address: String, options: ClientOptions, inboxId: String? = nil
)
async throws -> Client
{
let accountAddress = address.lowercased()
let resolvedInboxId: String
if let existingInboxId = inboxId {
resolvedInboxId = existingInboxId
} else {
resolvedInboxId = try await getOrCreateInboxId(api: options.api, address: accountAddress)
resolvedInboxId = try await getOrCreateInboxId(
api: options.api, address: accountAddress)
}

return try await initializeClient(
accountAddress: accountAddress,
options: options,
Expand Down Expand Up @@ -284,6 +287,40 @@ public final class Client {
return inboxId
}

public static func canMessage(
accountAddresses: [String],
api: ClientOptions.Api
) async throws -> [String: Bool] {
let address = "0x0000000000000000000000000000000000000000"
let inboxId = try await getOrCreateInboxId(api: api, address: address)

var directoryURL: URL = URL.documentsDirectory
let alias = "xmtp-\(api.env.rawValue)-\(inboxId).db3"
let dbURL = directoryURL.appendingPathComponent(alias).path

let ffiClient = try await LibXMTP.createClient(
logger: XMTPLogger(),
host: api.env.url,
isSecure: api.env.isSecure == true,
db: dbURL,
encryptionKey: nil,
inboxId: inboxId,
accountAddress: address,
nonce: 0,
legacySignedPrivateKeyProto: nil,
historySyncUrl: nil
)

let result = try await ffiClient.canMessage(
accountAddresses: accountAddresses)

try ffiClient.releaseDbConnection()
let fm = FileManager.default
try fm.removeItem(atPath: dbURL)

return result
}

init(
address: String, ffiClient: LibXMTP.FfiXmtpClient, dbPath: String,
installationID: String, inboxID: String, environment: XMTPEnvironment
Expand All @@ -299,7 +336,8 @@ public final class Client {
public func addAccount(newAccount: SigningKey)
async throws
{
let signatureRequest = try await ffiClient.addWallet(newWalletAddress: newAccount.address.lowercased())
let signatureRequest = try await ffiClient.addWallet(
newWalletAddress: newAccount.address.lowercased())
do {
try await Client.handleSignature(
for: signatureRequest, signingKey: newAccount)
Expand Down
54 changes: 47 additions & 7 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ class ClientTests: XCTestCase {
)
}

func testStaticCanMessage() async throws {
let fixtures = try await fixtures()
let notOnNetwork = try PrivateKey.generate()

let canMessageList = try await Client.canMessage(
accountAddresses: [
fixtures.alix.walletAddress,
notOnNetwork.address,
fixtures.bo.walletAddress,
],
api: ClientOptions.Api(env: .local, isSecure: false)
)

let expectedResults: [String: Bool] = [
fixtures.alix.walletAddress.lowercased(): true,
notOnNetwork.address.lowercased(): false,
fixtures.bo.walletAddress.lowercased(): true,
]

for (address, expected) in expectedResults {
XCTAssertEqual(
canMessageList[address.lowercased()], expected,
"Failed for address: \(address)")
}
}

func testCanDeleteDatabase() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let bo = try PrivateKey.generate()
Expand Down Expand Up @@ -460,7 +486,7 @@ class ClientTests: XCTestCase {
installationId: alixInstallationId
))
}

func testCreatesADevClientPerformance() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let fakeWallet = try PrivateKey.generate()
Expand Down Expand Up @@ -506,13 +532,27 @@ class ClientTests: XCTestCase {
print("PERF: Built a client with inboxId in \(time3)s")

// Assert performance comparisons
XCTAssertTrue(time2 < time1, "Building a client should be faster than creating one.")
XCTAssertTrue(time3 < time1, "Building a client with inboxId should be faster than creating one.")
XCTAssertTrue(time3 < time2, "Building a client with inboxId should be faster than building one without.")

XCTAssertTrue(
time2 < time1,
"Building a client should be faster than creating one.")
XCTAssertTrue(
time3 < time1,
"Building a client with inboxId should be faster than creating one."
)
XCTAssertTrue(
time3 < time2,
"Building a client with inboxId should be faster than building one without."
)

// Assert that inbox IDs match
XCTAssertEqual(client.inboxID, buildClient1.inboxID, "Inbox ID of the created client and first built client should match.")
XCTAssertEqual(client.inboxID, buildClient2.inboxID, "Inbox ID of the created client and second built client should match.")
XCTAssertEqual(
client.inboxID, buildClient1.inboxID,
"Inbox ID of the created client and first built client should match."
)
XCTAssertEqual(
client.inboxID, buildClient2.inboxID,
"Inbox ID of the created client and second built client should match."
)
}

}
Loading