Skip to content

Commit

Permalink
Fix create from key bundle (#344)
Browse files Browse the repository at this point in the history
* tweak the client creation

* fix lint issue

* get tests passing

* remove the unneeded ready check
  • Loading branch information
nplasterer authored Jun 4, 2024
1 parent 8f0186e commit 8a9a667
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
.package(url: "https://github.com/1024jp/GzipSwift", from: "5.2.0"),
.package(url: "https://github.com/bufbuild/connect-swift", exact: "0.12.0"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"),
.package(url: "https://github.com/xmtp/libxmtp-swift", exact: "0.5.0-beta2"),
.package(url: "https://github.com/xmtp/libxmtp-swift", exact: "0.5.1-beta0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
40 changes: 21 additions & 19 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,27 @@ public final class Client {
}

static func initV3Client(
address: String,
accountAddress: String,
options: ClientOptions?,
source: LegacyIdentitySource,
privateKeyBundleV1: PrivateKeyBundleV1,
signingKey: SigningKey?
) async throws -> (FfiXmtpClient?, String) {
if options?.mlsAlpha == true, options?.api.env.supportsMLS == true {
var inboxId = try await getInboxIdForAddress(
logger: XMTPLogger(),
host: (options?.api.env ?? .local).url,
isSecure: options?.api.env.isSecure == true,
accountAddress: address
)
if inboxId?.isEmpty ?? true {
let address = accountAddress.lowercased()

var inboxId: String
do {
inboxId = try await getInboxIdForAddress(
logger: XMTPLogger(),
host: (options?.api.env ?? .local).url,
isSecure: options?.api.env.isSecure == true,
accountAddress: address
) ?? generateInboxId(accountAddress: address, nonce: 0)
} catch {
inboxId = generateInboxId(accountAddress: address, nonce: 0)
}

let alias = "xmtp-\(options?.api.env.rawValue ?? "")-\(inboxId ?? address).db3"
let alias = "xmtp-\(options?.api.env.rawValue ?? "")-\(inboxId).db3"
let dbURL = URL.documentsDirectory.appendingPathComponent(alias).path

let encryptionKey = options?.mlsEncryptionKey
Expand All @@ -156,8 +159,9 @@ public final class Client {
isSecure: options?.api.env.isSecure == true,
db: dbURL,
encryptionKey: encryptionKey,
inboxId: inboxId,
accountAddress: address,
legacyIdentitySource: source,
nonce: 0,
legacySignedPrivateKeyProto: try privateKeyBundleV1.toV2().identityKey.serializedData()
)

Expand All @@ -184,12 +188,11 @@ public final class Client {
}

static func create(account: SigningKey, apiClient: ApiClient, options: ClientOptions? = nil) async throws -> Client {
let (privateKeyBundleV1, source) = try await loadOrCreateKeys(for: account, apiClient: apiClient, options: options)
let privateKeyBundleV1 = try await loadOrCreateKeys(for: account, apiClient: apiClient, options: options)

let (v3Client, dbPath) = try await initV3Client(
address: account.address,
accountAddress: account.address,
options: options,
source: source,
privateKeyBundleV1: privateKeyBundleV1,
signingKey: account
)
Expand All @@ -206,13 +209,13 @@ public final class Client {
return client
}

static func loadOrCreateKeys(for account: SigningKey, apiClient: ApiClient, options: ClientOptions? = nil) async throws -> (PrivateKeyBundleV1, LegacyIdentitySource) {
static func loadOrCreateKeys(for account: SigningKey, apiClient: ApiClient, options: ClientOptions? = nil) async throws -> PrivateKeyBundleV1 {
if let keys = try await loadPrivateKeys(for: account, apiClient: apiClient, options: options) {
print("loading existing private keys.")
#if DEBUG
print("Loaded existing private keys.")
#endif
return (keys, .network)
return keys
} else {
#if DEBUG
print("No existing keys found, creating new bundle.")
Expand All @@ -229,7 +232,7 @@ public final class Client {
Envelope(topic: .userPrivateStoreKeyBundle(account.address), timestamp: Date(), message: encryptedKeys.serializedData()),
])

return (keys, .keyGenerator)
return keys
}
}

Expand Down Expand Up @@ -281,9 +284,8 @@ public final class Client {
let options = options ?? ClientOptions()

let (v3Client, dbPath) = try await initV3Client(
address: address,
accountAddress: address,
options: options,
source: .static,
privateKeyBundleV1: v1Bundle,
signingKey: nil
)
Expand Down
4 changes: 2 additions & 2 deletions XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.11.1"
spec.version = "0.11.2"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -44,5 +44,5 @@ Pod::Spec.new do |spec|
spec.dependency "web3.swift"
spec.dependency "GzipSwift"
spec.dependency "Connect-Swift", "= 0.12.0"
spec.dependency 'LibXMTP', '= 0.5.0-beta2'
spec.dependency 'LibXMTP', '= 0.5.1-beta0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/xmtp/libxmtp-swift",
"state" : {
"revision" : "6540f22eab6748748ede67c5a94d8188060fcbf9",
"version" : "0.5.0-beta2"
"revision" : "bc7e65b4db73430ae259bce32b391eefa82d4071",
"version" : "0.5.1-beta0"
}
},
{
Expand Down

0 comments on commit 8a9a667

Please sign in to comment.