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

[Chat] History API #892

Merged
merged 20 commits into from
Jun 14, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectHistory"
BuildableName = "WalletConnectHistory"
BlueprintName = "WalletConnectHistory"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectHistory"
BuildableName = "WalletConnectHistory"
BlueprintName = "WalletConnectHistory"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
19 changes: 19 additions & 0 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
84FE684628ACDB4700C893FF /* RequestParams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84FE684528ACDB4700C893FF /* RequestParams.swift */; };
A507BE1A29E8032E0038EF70 /* EIP55Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A507BE1929E8032E0038EF70 /* EIP55Tests.swift */; };
A50C036528AAD32200FE72D3 /* ClientDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50C036428AAD32200FE72D3 /* ClientDelegate.swift */; };
A50DF19D2A25084A0036EA6C /* WalletConnectHistory in Frameworks */ = {isa = PBXBuildFile; productRef = A50DF19C2A25084A0036EA6C /* WalletConnectHistory */; };
A50F3946288005B200064555 /* Types.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50F3945288005B200064555 /* Types.swift */; };
A51606F82A2F47BD00CACB92 /* DefaultBIP44Provider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51606F72A2F47BD00CACB92 /* DefaultBIP44Provider.swift */; };
A51606F92A2F47BD00CACB92 /* DefaultBIP44Provider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51606F72A2F47BD00CACB92 /* DefaultBIP44Provider.swift */; };
Expand All @@ -86,6 +87,7 @@
A518B31428E33A6500A2CE93 /* InputConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A518B31328E33A6500A2CE93 /* InputConfig.swift */; };
A51AC0D928E436A3001BACF9 /* InputConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51AC0D828E436A3001BACF9 /* InputConfig.swift */; };
A51AC0DF28E4379F001BACF9 /* InputConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51AC0DE28E4379F001BACF9 /* InputConfig.swift */; };
A5321C2B2A250367006CADC3 /* HistoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5321C2A2A250367006CADC3 /* HistoryTests.swift */; };
A5417BBE299BFC3E00B469F3 /* ImportAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5417BBD299BFC3E00B469F3 /* ImportAccount.swift */; };
A541959E2934BFEF0035AD19 /* CacaoSignerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A541959A2934BFEF0035AD19 /* CacaoSignerTests.swift */; };
A541959F2934BFEF0035AD19 /* SignerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A541959B2934BFEF0035AD19 /* SignerTests.swift */; };
Expand Down Expand Up @@ -426,6 +428,7 @@
A518B31328E33A6500A2CE93 /* InputConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InputConfig.swift; sourceTree = "<group>"; };
A51AC0D828E436A3001BACF9 /* InputConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputConfig.swift; sourceTree = "<group>"; };
A51AC0DE28E4379F001BACF9 /* InputConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputConfig.swift; sourceTree = "<group>"; };
A5321C2A2A250367006CADC3 /* HistoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryTests.swift; sourceTree = "<group>"; };
A5417BBD299BFC3E00B469F3 /* ImportAccount.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImportAccount.swift; sourceTree = "<group>"; };
A541959A2934BFEF0035AD19 /* CacaoSignerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacaoSignerTests.swift; sourceTree = "<group>"; };
A541959B2934BFEF0035AD19 /* SignerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignerTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -665,6 +668,7 @@
A5E03DFF2864662500888481 /* WalletConnect in Frameworks */,
A561C80529DFCD4500DF540D /* WalletConnectSync in Frameworks */,
A5E03DF52864651200888481 /* Starscream in Frameworks */,
A50DF19D2A25084A0036EA6C /* WalletConnectHistory in Frameworks */,
847CF3AF28E3141700F1D760 /* WalletConnectPush in Frameworks */,
A5C8BE85292FE20B006CC85C /* Web3 in Frameworks */,
84DDB4ED28ABB663003D66ED /* WalletConnectAuth in Frameworks */,
Expand Down Expand Up @@ -955,6 +959,14 @@
path = Types;
sourceTree = "<group>";
};
A5321C292A25035A006CADC3 /* History */ = {
isa = PBXGroup;
children = (
A5321C2A2A250367006CADC3 /* HistoryTests.swift */,
);
path = History;
sourceTree = "<group>";
};
A54195992934BFDD0035AD19 /* Signer */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1376,6 +1388,7 @@
A5E03DEE286464DB00888481 /* IntegrationTests */ = {
isa = PBXGroup;
children = (
A5321C292A25035A006CADC3 /* History */,
A561C80129DFCCD300DF540D /* Sync */,
849D7A91292E2115006A2BD4 /* Push */,
84CEC64728D8A98900D081A8 /* Pairing */,
Expand Down Expand Up @@ -1860,6 +1873,7 @@
C5DD5BE0294E09E3008FD3A4 /* Web3Wallet */,
A561C80429DFCD4500DF540D /* WalletConnectSync */,
A573C53A29EC365800E3CBFD /* HDWalletKit */,
A50DF19C2A25084A0036EA6C /* WalletConnectHistory */,
);
productName = IntegrationTests;
productReference = A5E03DED286464DB00888481 /* IntegrationTests.xctest */;
Expand Down Expand Up @@ -2212,6 +2226,7 @@
7694A5262874296A0001257E /* RegistryTests.swift in Sources */,
A541959F2934BFEF0035AD19 /* SignerTests.swift in Sources */,
A50C036528AAD32200FE72D3 /* ClientDelegate.swift in Sources */,
A5321C2B2A250367006CADC3 /* HistoryTests.swift in Sources */,
A58A1ECC29BF458600A82A20 /* ENSResolverTests.swift in Sources */,
A5E03DFA286465C700888481 /* SignClientTests.swift in Sources */,
A54195A02934BFEF0035AD19 /* EIP1271VerifierTests.swift in Sources */,
Expand Down Expand Up @@ -3110,6 +3125,10 @@
isa = XCSwiftPackageProductDependency;
productName = WalletConnectPush;
};
A50DF19C2A25084A0036EA6C /* WalletConnectHistory */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectHistory;
};
A54195A42934E83F0035AD19 /* Web3 */ = {
isa = XCSwiftPackageProductDependency;
package = A5AE354528A1A2AC0059AE8A /* XCRemoteSwiftPackageReference "Web3" */;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"state": {
"branch": null,
"revision": "eee9ad754926c40a0f7e73f152357d37b119b7fa",
"version": "1.7.1"
"revision": "32f641cf24fc7abc1c591a2025e9f2f572648b0f",
"version": "1.7.2"
}
},
{
Expand Down
79 changes: 79 additions & 0 deletions Example/IntegrationTests/History/HistoryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Foundation
import Combine
import XCTest
@testable import WalletConnectHistory

final class HistoryTests: XCTestCase {
flypaper0 marked this conversation as resolved.
Show resolved Hide resolved

var publishers = Set<AnyCancellable>()

let relayUrl = "wss://relay.walletconnect.com"
let historyUrl = "https://history.walletconnect.com"

var relayClient1: RelayClient!
var relayClient2: RelayClient!

var historyClient: HistoryNetworkService!

override func setUp() {
let keychain1 = KeychainStorageMock()
let keychain2 = KeychainStorageMock()
relayClient1 = makeRelayClient(prefix: "🐄", keychain: keychain1)
relayClient2 = makeRelayClient(prefix: "🐫", keychain: keychain2)
historyClient = makeHistoryClient(keychain: keychain1)
}

private func makeRelayClient(prefix: String, keychain: KeychainStorageProtocol) -> RelayClient {
return RelayClient(
relayHost: InputConfig.relayHost,
projectId: InputConfig.projectId,
keyValueStorage: RuntimeKeyValueStorage(),
keychainStorage: keychain,
socketFactory: DefaultSocketFactory(),
logger: ConsoleLogger(suffix: prefix + " [Relay]", loggingLevel: .debug))
}

private func makeHistoryClient(keychain: KeychainStorageProtocol) -> HistoryNetworkService {
let clientIdStorage = ClientIdStorage(keychain: keychain)
return HistoryNetworkService(clientIdStorage: clientIdStorage)
}

func testRegister() async throws {
let payload = RegisterPayload(tags: ["7000"], relayUrl: relayUrl)

try await historyClient.registerTags(payload: payload, historyUrl: historyUrl)
}

func testGetMessages() async throws {
let exp = expectation(description: "Test Get Messages")
let tag = 7000
let payload = "{}"
let agreement = AgreementPrivateKey()
let topic = agreement.publicKey.rawRepresentation.sha256().hex

relayClient2.messagePublisher.sink { (topic, message, publishedAt) in
exp.fulfill()
}.store(in: &publishers)

try await historyClient.registerTags(
payload: RegisterPayload(tags: [String(tag)], relayUrl: relayUrl),
historyUrl: historyUrl)

try await relayClient2.subscribe(topic: topic)
try await relayClient1.publish(topic: topic, payload: payload, tag: tag, prompt: false, ttl: 3000)

wait(for: [exp], timeout: InputConfig.defaultTimeout)

sleep(5) // History server has a queue
flypaper0 marked this conversation as resolved.
Show resolved Hide resolved

let messages = try await historyClient.getMessages(
payload: GetMessagesPayload(
topic: topic,
originId: nil,
messageCount: 200,
direction: .forward),
historyUrl: historyUrl)

XCTAssertEqual(messages.messages, [payload])
}
}
7 changes: 3 additions & 4 deletions Example/IntegrationTests/Sync/SyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import Web3

final class SyncTests: XCTestCase {

struct TestObject: SyncObject {
struct TestObject: DatabaseObject {
let id: String
let value: String

var syncId: String {
var databaseId: String {
return id
}
}
Expand Down Expand Up @@ -75,8 +75,7 @@ final class SyncTests: XCTestCase {
}

func makeSyncStore(client: SyncClient, indexStore: SyncIndexStore) -> SyncStore<TestObject> {
let store = NewKeyedDatabase<[String: TestObject]>(storage: RuntimeKeyValueStorage(), identifier: "objectStore")
let objectStore = SyncObjectStore(store: store)
let objectStore = KeyedDatabase<TestObject>(storage: RuntimeKeyValueStorage(), identifier: "objectStore")
return SyncStore(name: storeName, syncClient: client, indexStore: indexStore, objectStore: objectStore)
}

Expand Down
4 changes: 2 additions & 2 deletions Example/RelayIntegrationTests/RelayClientEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ final class RelayClientEndToEndTests: XCTestCase {

func makeRelayClient(prefix: String) -> RelayClient {
let clientIdStorage = ClientIdStorage(keychain: KeychainStorageMock())
let socketAuthenticator = SocketAuthenticator(
let socketAuthenticator = ClientIdAuthenticator(
clientIdStorage: clientIdStorage,
relayHost: InputConfig.relayHost
url: InputConfig.relayUrl
)
let urlFactory = RelayUrlFactory(
relayHost: InputConfig.relayHost,
Expand Down
4 changes: 4 additions & 0 deletions Example/Shared/Tests/InputConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ struct InputConfig {
return config(for: "RELAY_HOST")!
}

static var relayUrl: String {
return "wss://\(relayHost)"
}

static var projectId: String {
return config(for: "PROJECT_ID")!
}
Expand Down
12 changes: 6 additions & 6 deletions Example/Showcase/Classes/DomainLayer/Chat/ImportAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ enum ImportAccount: Codable {
var account: Account {
switch self {
case .swift:
return Account("eip155:1:0x1AAe9864337E821f2F86b5D27468C59AA333C877")!
return Account("eip155:1:0x5F847B18b4a2Dd0F428796E89CaEe71480a2a98e")!
case .kotlin:
return Account("eip155:1:0x4c0fb06CD854ab7D5909E830a5f49D184EB41BF5")!
return Account("eip155:1:0xC313B6F74FcB89147e751220184F0C56D37a210e")!
case .js:
return Account("eip155:1:0x7ABa5B1F436e42f6d4A579FB3Ad6D204F6A91863")!
return Account("eip155:1:0x265F4Eb49ab95ED142C4995EF8B5FC9e57538836")!
case .custom(let privateKey):
let address = try! EthereumPrivateKey(hexPrivateKey: "0x" + privateKey, ctx: nil).address.hex(eip55: true)
return Account("eip155:1:\(address)")!
Expand All @@ -79,11 +79,11 @@ enum ImportAccount: Codable {
var privateKey: String {
switch self {
case .swift:
return "4dc0055d1831f7df8d855fc8cd9118f4a85ddc05395104c4cb0831a6752621a8"
return "85f52ec43821c1e2e24a248ee464e8d3f883e460acb0506e1eb6b520eb67ae15"
case .kotlin:
return "ebe738a76b9a3b7457c3d5eca8d3d9ea6909bc563e05b6e0c5c35448f93100a0"
return "646a0ebac6bd34ba5f498b809148b2aca3793374cafe9dc417cf63bea80450bf"
case .js:
return "de15cb11963e9bde0a5cce06a5ee2bda1cf3a67be6fbcd7a4fc8c0e4c4db0298"
return "8df6b8206eebcd3da89b750f1cf9bba887630c3c5eade83f44c06fa4f7cc5f65"
case .custom(let privateKey):
return privateKey
case .web3Modal:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ extension ImportPresenter: SceneViewModel {
private extension ImportPresenter {

func setupInitialState() {
Sign.instance.sessionSettlePublisher.sink { session in
Task(priority: .userInitiated) {
try await self.importAccount(.web3Modal(account: session.accounts.first!, topic: session.topic))
}

}.store(in: &disposeBag)
}

@MainActor
Expand Down
1 change: 1 addition & 0 deletions Example/SmokeTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"EIP191VerifierTests",
"EIP55Tests",
"ENSResolverTests",
"HistoryTests",
"PairingTests",
"PushTests",
"PushTests\/testDappDeletePushSubscription()",
Expand Down
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ let package = Package(
.library(
name: "WalletConnectVerify",
targets: ["WalletConnectVerify"]),
.library(
name: "WalletConnectHistory",
targets: ["WalletConnectHistory"]),
.library(
name: "Web3Inbox",
targets: ["Web3Inbox"]),
Expand All @@ -61,7 +64,7 @@ let package = Package(
path: "Sources/WalletConnectSign"),
.target(
name: "WalletConnectChat",
dependencies: ["WalletConnectIdentity", "WalletConnectSync"],
dependencies: ["WalletConnectIdentity", "WalletConnectSync", "WalletConnectHistory"],
path: "Sources/Chat"),
.target(
name: "Auth",
Expand Down Expand Up @@ -91,6 +94,9 @@ let package = Package(
.target(
name: "WalletConnectPairing",
dependencies: ["WalletConnectNetworking"]),
.target(
name: "WalletConnectHistory",
dependencies: ["HTTPClient", "WalletConnectRelay"]),
.target(
name: "Web3Inbox",
dependencies: ["WalletConnectChat", "WalletConnectPush"]),
Expand Down
5 changes: 4 additions & 1 deletion Sources/Chat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public class ChatClient {
) async throws -> String {
let publicKey = try await identityClient.register(account: account, onSign: onSign)

try await syncRegisterService.register(account: account, onSign: onSign)
if !syncRegisterService.isRegistered(account: account) {
try await chatStorage.initializeHistory(account: account)
try await syncRegisterService.register(account: account, onSign: onSign)
}

guard !isPrivate else {
return publicKey
Expand Down
Loading