From abe6af890b8ea9329de90e76a5cc3878133fd8fe Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Thu, 17 Oct 2024 21:35:31 -0700 Subject: [PATCH] update the signers --- Package.swift | 2 +- Sources/XMTPiOS/Client.swift | 57 ++++++++++++++----- Sources/XMTPiOS/SigningKey.swift | 3 + XMTP.podspec | 4 +- .../xcshareddata/swiftpm/Package.resolved | 4 +- 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/Package.swift b/Package.swift index 8a1e166e..8e1f5b49 100644 --- a/Package.swift +++ b/Package.swift @@ -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.git", exact: "0.5.9-beta2"), + .package(url: "https://github.com/xmtp/libxmtp-swift.git", exact: "0.5.9-beta3"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index 83febe9c..a53a06af 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -161,23 +161,26 @@ public final class Client { } } - // This is a V3 only feature - public static func createOrBuild(account: SigningKey, options: ClientOptions) async throws -> Client { - let inboxId = try await getOrCreateInboxId(options: options, address: account.address) - + private static func initializeClient( + accountAddress: String, + options: ClientOptions, + signingKey: SigningKey?, + inboxId: String + ) async throws -> Client { let (libxmtpClient, dbPath) = try await initV3Client( - accountAddress: account.address, + accountAddress: accountAddress, options: options, privateKeyBundleV1: nil, - signingKey: account, + signingKey: signingKey, inboxId: inboxId ) + guard let v3Client = libxmtpClient else { throw ClientError.noV3Client("Error no V3 client initialized") } let client = try Client( - address: account.address, + address: accountAddress, v3Client: v3Client, dbPath: dbPath, installationID: v3Client.installationId().toHex, @@ -185,15 +188,40 @@ public final class Client { environment: options.api.env ) - let conversations = client.conversations - let contacts = client.contacts - - for codec in (options.codecs) { + // Register codecs + for codec in options.codecs { client.register(codec: codec) } + return client } + public static func createV3(account: SigningKey, options: ClientOptions) async throws -> Client { + let accountAddress = if(account.isSmartContractWallet) { "eip155:\(String(describing: account.chainId)):\(account.address.lowercased())" } else { account.address } + + + let inboxId = try await getOrCreateInboxId(options: options, address: accountAddress) + + return try await initializeClient( + accountAddress: accountAddress, + options: options, + signingKey: account, + inboxId: inboxId + ) + } + + public static func buildV3(address: String, scwChainId: Int64?, options: ClientOptions) async throws -> Client { + let accountAddress = if(scwChainId != nil) { "eip155:\(String(describing: scwChainId)):\(address.lowercased())" } else { address } + let inboxId = try await getOrCreateInboxId(options: options, address: accountAddress) + + return try await initializeClient( + accountAddress: accountAddress, + options: options, + signingKey: nil, + inboxId: inboxId + ) + } + static func initV3Client( accountAddress: String, options: ClientOptions?, @@ -224,7 +252,7 @@ public final class Client { let alias = "xmtp-\(options?.api.env.rawValue ?? "")-\(inboxId).db3" let dbURL = directoryURL.appendingPathComponent(alias).path - var encryptionKey = options?.dbEncryptionKey + let encryptionKey = options?.dbEncryptionKey if (encryptionKey == nil) { throw ClientError.creationError("No encryption key passed for the database. Please store and provide a secure encryption key.") } @@ -246,14 +274,15 @@ public final class Client { if let signatureRequest = v3Client.signatureRequest() { if let signingKey = signingKey { do { - let signedData = try await signingKey.sign(message: signatureRequest.signatureText()) if signingKey.isSmartContractWallet { - try await signatureRequest.addScwSignature(signatureBytes: signedData.rawData, + let signedData = try await signingKey.signSCW(message: signatureRequest.signatureText()) + try await signatureRequest.addScwSignature(signatureBytes: signedData, address: signingKey.address, chainId: UInt64(signingKey.chainId), blockNumber: signingKey.blockNumber.flatMap { $0 >= 0 ? UInt64($0) : nil }) } else { + let signedData = try await signingKey.sign(message: signatureRequest.signatureText()) try await signatureRequest.addEcdsaSignature(signatureBytes: signedData.rawData) } try await v3Client.registerIdentity(signatureRequest: signatureRequest) diff --git a/Sources/XMTPiOS/SigningKey.swift b/Sources/XMTPiOS/SigningKey.swift index 72ebb0f4..4208cba6 100644 --- a/Sources/XMTPiOS/SigningKey.swift +++ b/Sources/XMTPiOS/SigningKey.swift @@ -35,6 +35,9 @@ public protocol SigningKey { /// Pass a personal Ethereum signed message string text to be signed, returning /// a secp256k1 compact recoverable signature. You can use ``Signature.ethPersonalMessage`` to generate this text. func sign(message: String) async throws -> Signature + + /// Pass a personal Ethereum signed message string text to be signed, return bytes to be verified + func signSCW(message: String) async throws -> Data } extension SigningKey { diff --git a/XMTP.podspec b/XMTP.podspec index 5f024aeb..509c5a04 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XMTP" - spec.version = "0.15.1-alpha1" + spec.version = "0.15.1-alpha2" spec.summary = "XMTP SDK Cocoapod" # This description is used to generate tags and improve search results. @@ -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.9-beta2' + spec.dependency 'LibXMTP', '= 0.5.9-beta3' end diff --git a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index fc48b889..4568be0a 100644 --- a/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/XMTPiOSExample/XMTPiOSExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -59,8 +59,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/xmtp/libxmtp-swift.git", "state" : { - "revision" : "d5d5134ccd177bbad3d74d8ebb6bd2cdd7d2f197", - "version" : "0.5.9-beta2" + "revision" : "a103132088fb9657b03c25006fe6a6686fa4d082", + "version" : "0.5.9-beta3" } }, {