From e704cea8a722b55876259a3b75d80e9cec074855 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 23 Oct 2024 12:46:36 -0700 Subject: [PATCH] update the SCW functionality and message listing --- Sources/XMTPTestHelpers/TestHelpers.swift | 4 +-- Sources/XMTPiOS/Client.swift | 2 +- Sources/XMTPiOS/Group.swift | 37 +++++++++++++++++------ Sources/XMTPiOS/SigningKey.swift | 12 +++++--- Tests/XMTPTests/ClientTests.swift | 19 ++++++++++++ 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/Sources/XMTPTestHelpers/TestHelpers.swift b/Sources/XMTPTestHelpers/TestHelpers.swift index 41a8fa52..c6808a34 100644 --- a/Sources/XMTPTestHelpers/TestHelpers.swift +++ b/Sources/XMTPTestHelpers/TestHelpers.swift @@ -82,8 +82,8 @@ public struct FakeSCWWallet: SigningKey { walletAddress } - public var isSmartContractWallet: Bool { - true + public var type: WalletType { + WalletType.SCW } public var chainId: Int64 { diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index bafcb6e4..68ecf5be 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -271,7 +271,7 @@ public final class Client { if let signatureRequest = v3Client.signatureRequest() { if let signingKey = signingKey { do { - if signingKey.isSmartContractWallet { + if signingKey.type == WalletType.SCW { guard let chainId = signingKey.chainId else { throw ClientError.creationError("Chain id must be present to sign Smart Contract Wallet") } diff --git a/Sources/XMTPiOS/Group.swift b/Sources/XMTPiOS/Group.swift index dfc4a2e1..6f43cbc1 100644 --- a/Sources/XMTPiOS/Group.swift +++ b/Sources/XMTPiOS/Group.swift @@ -373,7 +373,8 @@ public struct Group: Identifiable, Equatable, Hashable { sentBeforeNs: nil, sentAfterNs: nil, limit: nil, - deliveryStatus: nil + deliveryStatus: nil, + direction: nil ) if let before { @@ -402,16 +403,20 @@ public struct Group: Identifiable, Equatable, Hashable { }() options.deliveryStatus = status + + let direction: FfiDirection? = { + switch direction { + case .ascending: + return FfiDirection.ascending + default: + return FfiDirection.descending + } + }() - let messages = try ffiGroup.findMessages(opts: options).compactMap { ffiMessage in - return MessageV3(client: self.client, ffiMessage: ffiMessage).decodeOrNull() - } + options.direction = direction - switch direction { - case .ascending: - return messages - default: - return messages.reversed() + return try ffiGroup.findMessages(opts: options).compactMap { ffiMessage in + return MessageV3(client: self.client, ffiMessage: ffiMessage).decodeOrNull() } } @@ -426,7 +431,8 @@ public struct Group: Identifiable, Equatable, Hashable { sentBeforeNs: nil, sentAfterNs: nil, limit: nil, - deliveryStatus: nil + deliveryStatus: nil, + direction: nil ) if let before { @@ -455,6 +461,17 @@ public struct Group: Identifiable, Equatable, Hashable { }() options.deliveryStatus = status + + let direction: FfiDirection? = { + switch direction { + case .ascending: + return FfiDirection.ascending + default: + return FfiDirection.descending + } + }() + + options.direction = direction let messages = try ffiGroup.findMessages(opts: options).compactMap { ffiMessage in return MessageV3(client: self.client, ffiMessage: ffiMessage).decryptOrNull() diff --git a/Sources/XMTPiOS/SigningKey.swift b/Sources/XMTPiOS/SigningKey.swift index 5c720edf..22a3fda9 100644 --- a/Sources/XMTPiOS/SigningKey.swift +++ b/Sources/XMTPiOS/SigningKey.swift @@ -9,6 +9,10 @@ import Foundation import web3 import LibXMTP +public enum WalletType { + case EOA, SCW +} + /// Defines a type that is used by a ``Client`` to sign keys and messages. /// /// You can use ``Account`` for an easier WalletConnect flow, or ``PrivateKey`` @@ -20,8 +24,8 @@ public protocol SigningKey { /// A wallet address for this key var address: String { get } - /// If this signing key is a smart contract wallet - var isSmartContractWallet: Bool { get } + /// The wallet type if Smart Contract Wallet this should be type SCW. Default EOA + var type: WalletType { get } /// The name of the chainId for example "1" var chainId: Int64? { get } @@ -41,8 +45,8 @@ public protocol SigningKey { } extension SigningKey { - public var isSmartContractWallet: Bool { - return false + public var type: WalletType { + return WalletType.EOA } public var chainId: Int64? { diff --git a/Tests/XMTPTests/ClientTests.swift b/Tests/XMTPTests/ClientTests.swift index 00843749..f5a6a9d4 100644 --- a/Tests/XMTPTests/ClientTests.swift +++ b/Tests/XMTPTests/ClientTests.swift @@ -521,4 +521,23 @@ class ClientTests: XCTestCase { let newState = try await alixClient3.inboxState(refreshFromNetwork: true) XCTAssertEqual(newState.installations.count, 1) } + + func testCreatesASCWClient() async throws { + let key = try Crypto.secureRandomBytes(count: 32) + let alix = try FakeSCWWallet.generate() + let options = ClientOptions.init( + api: .init(env: .local, isSecure: false), + enableV3: true, + encryptionKey: key + ) + + + let inboxId = try await Client.getOrCreateInboxId(options: options, address: alix.address) + let alixClient = try await Client.createV3( + account: alix, + options: options + ) + + XCTAssertEqual(inboxId, alixClient.inboxID) + } }