Skip to content

Commit

Permalink
[V3] Remove Decrypted Messages (#418)
Browse files Browse the repository at this point in the history
* remove decrypted message

* add back enum

* remove all decrypted messages

* [V3] Remove contacts and rename to preferences (#419)

* remove the contacts and rename to conversation

* [V3] Remove the ability to create a V2 client (#420)

* remove all the client code

* a few more v2 client clean ups

* remove more v3 verbage from the code

* [V3] Fix up all the tests (#421)

* update all the tests and get rid of bob and alice

* get more tests passing

* more test tweaks

* fix up all the tests

* [V3] Update Example App (#422)

* update the readme and the example app

* get the example app working

* fix up lint
  • Loading branch information
nplasterer authored Nov 8, 2024
1 parent 7c14fc0 commit bca1537
Show file tree
Hide file tree
Showing 86 changed files with 2,475 additions and 7,523 deletions.
433 changes: 121 additions & 312 deletions README.md

Large diffs are not rendered by default.

213 changes: 84 additions & 129 deletions Sources/XMTPTestHelpers/TestHelpers.swift
Original file line number Diff line number Diff line change
@@ -1,145 +1,100 @@
//
// TestHelpers.swift
//
//
// Created by Pat Nakajima on 12/6/22.
//

#if canImport(XCTest)
import Combine
import CryptoKit
import XCTest
@testable import XMTPiOS
import LibXMTP

public struct TestConfig {
static let TEST_SERVER_ENABLED = _env("TEST_SERVER_ENABLED") == "true"
// TODO: change Client constructor to accept these explicitly (so we can config CI):
// static let TEST_SERVER_HOST = _env("TEST_SERVER_HOST") ?? "127.0.0.1"
// static let TEST_SERVER_PORT = Int(_env("TEST_SERVER_PORT")) ?? 5556
// static let TEST_SERVER_IS_SECURE = _env("TEST_SERVER_IS_SECURE") == "true"

static private func _env(_ key: String) -> String? {
ProcessInfo.processInfo.environment[key]
}

static public func skipIfNotRunningLocalNodeTests() throws {
try XCTSkipIf(!TEST_SERVER_ENABLED, "requires local node")
}

static public func skip(because: String) throws {
try XCTSkipIf(true, because)
}
}

// Helper for tests gathering transcripts in a background task.
public actor TestTranscript {
public var messages: [String] = []
public init() {}
public func add(_ message: String) {
messages.append(message)
}
}

public struct FakeWallet: SigningKey {
public static func generate() throws -> FakeWallet {
let key = try PrivateKey.generate()
return FakeWallet(key)
import Combine
import CryptoKit
import XCTest
@testable import XMTPiOS
import LibXMTP

public struct TestConfig {
static let TEST_SERVER_ENABLED = _env("TEST_SERVER_ENABLED") == "true"
// TODO: change Client constructor to accept these explicitly (so we can config CI):
// static let TEST_SERVER_HOST = _env("TEST_SERVER_HOST") ?? "127.0.0.1"
// static let TEST_SERVER_PORT = Int(_env("TEST_SERVER_PORT")) ?? 5556
// static let TEST_SERVER_IS_SECURE = _env("TEST_SERVER_IS_SECURE") == "true"

static private func _env(_ key: String) -> String? {
ProcessInfo.processInfo.environment[key]
}

static public func skipIfNotRunningLocalNodeTests() throws {
try XCTSkipIf(!TEST_SERVER_ENABLED, "requires local node")
}

static public func skip(because: String) throws {
try XCTSkipIf(true, because)
}
}

public var address: String {
key.walletAddress
// Helper for tests gathering transcripts in a background task.
public actor TestTranscript {
public var messages: [String] = []
public init() {}
public func add(_ message: String) {
messages.append(message)
}
}

public func sign(_ data: Data) async throws -> XMTPiOS.Signature {
let signature = try await key.sign(data)
return signature
}
public struct FakeWallet: SigningKey {
public static func generate() throws -> FakeWallet {
let key = try PrivateKey.generate()
return FakeWallet(key)
}

public func sign(message: String) async throws -> XMTPiOS.Signature {
let signature = try await key.sign(message: message)
return signature
}
public var address: String {
key.walletAddress
}

public var key: PrivateKey
public func sign(_ data: Data) async throws -> XMTPiOS.Signature {
let signature = try await key.sign(data)
return signature
}

public init(_ key: PrivateKey) {
self.key = key
}
}

public struct FakeSCWWallet: SigningKey {
public var walletAddress: String
private var internalSignature: String

public init() throws {
// Simulate a wallet address (could be derived from a hash of some internal data)
self.walletAddress = UUID().uuidString // Using UUID for uniqueness in this fake example
self.internalSignature = Data(repeating: 0x01, count: 64).toHex // Fake internal signature
}

public var address: String {
walletAddress
}
public func sign(message: String) async throws -> XMTPiOS.Signature {
let signature = try await key.sign(message: message)
return signature
}

public var type: WalletType {
WalletType.SCW
}

public var chainId: Int64? {
1
}

public static func generate() throws -> FakeSCWWallet {
return try FakeSCWWallet()
}

public func signSCW(message: String) async throws -> Data {
// swiftlint:disable force_unwrapping
let digest = SHA256.hash(data: message.data(using: .utf8)!)
// swiftlint:enable force_unwrapping
return Data(digest)
}
}

@available(iOS 15, *)
public struct Fixtures {
public var alice: PrivateKey!
public var aliceClient: Client!

public var bob: PrivateKey!
public var bobClient: Client!
public let clientOptions: ClientOptions? = ClientOptions(
api: ClientOptions.Api(env: XMTPEnvironment.local, isSecure: false)
)

init() async throws {
alice = try PrivateKey.generate()
bob = try PrivateKey.generate()
public var key: PrivateKey

aliceClient = try await Client.create(account: alice, options: clientOptions)
bobClient = try await Client.create(account: bob, options: clientOptions)
public init(_ key: PrivateKey) {
self.key = key
}
}

public func publishLegacyContact(client: Client) async throws {
var contactBundle = ContactBundle()
contactBundle.v1.keyBundle = try client.v1keys.toPublicKeyBundle()

var envelope = Envelope()
envelope.contentTopic = Topic.contact(client.address).description
envelope.timestampNs = UInt64(Date().millisecondsSinceEpoch * 1_000_000)
envelope.message = try contactBundle.serializedData()

try await client.publish(envelopes: [envelope])
@available(iOS 15, *)
public struct Fixtures {
public var alix: PrivateKey!
public var alixClient: Client!
public var bo: PrivateKey!
public var boClient: Client!
public var caro: PrivateKey!
public var caroClient: Client!

init() async throws {
alix = try PrivateKey.generate()
bo = try PrivateKey.generate()
caro = try PrivateKey.generate()

let key = try Crypto.secureRandomBytes(count: 32)
let clientOptions: ClientOptions = ClientOptions(
api: ClientOptions.Api(
env: XMTPEnvironment.local, isSecure: false),
dbEncryptionKey: key
)

alixClient = try await Client.create(
account: alix, options: clientOptions)
boClient = try await Client.create(
account: bo, options: clientOptions)
caroClient = try await Client.create(
account: caro, options: clientOptions)
}
}
}

public extension XCTestCase {
@available(iOS 15, *)
func fixtures() async -> Fixtures {
// swiftlint:disable force_try
return try! await Fixtures()
// swiftlint:enable force_try
extension XCTestCase {
@available(iOS 15, *)
public func fixtures() async throws -> Fixtures {
return try await Fixtures()
}
}
}
#endif
Loading

0 comments on commit bca1537

Please sign in to comment.