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] Identity and Invite keys registration #706

Merged
merged 17 commits into from
Feb 13, 2023
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectJWT"
BuildableName = "WalletConnectJWT"
BlueprintName = "WalletConnectJWT"
ReferencedContainer = "container:..">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</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 = "WalletConnectJWT"
BuildableName = "WalletConnectJWT"
BlueprintName = "WalletConnectJWT"
ReferencedContainer = "container:..">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectSigner"
BuildableName = "WalletConnectSigner"
BlueprintName = "WalletConnectSigner"
ReferencedContainer = "container:..">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</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 = "WalletConnectSigner"
BuildableName = "WalletConnectSigner"
BlueprintName = "WalletConnectSigner"
ReferencedContainer = "container:..">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
3 changes: 2 additions & 1 deletion Example/IntegrationTests/Auth/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ final class AuthTests: XCTestCase {
Task(priority: .high) {
let signerFactory = DefaultSignerFactory()
let signer = MessageSignerFactory(signerFactory: signerFactory).create(projectId: InputConfig.projectId)
let signature = try! signer.sign(payload: request.payload, address: walletAccount.address, privateKey: prvKey, type: .eip191)
let payload = try! request.payload.cacaoPayload(address: walletAccount.address)
let signature = try! signer.sign(payload: payload, privateKey: prvKey, type: .eip191)
try! await walletAuthClient.respond(requestId: request.id, signature: signature, from: walletAccount)
}
}
Expand Down
8 changes: 3 additions & 5 deletions Example/IntegrationTests/Auth/Signer/CacaoSignerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ class CacaoSignerTest: XCTestCase {

func testCacaoSign() throws {
let address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
let formatted = try SIWEMessageFormatter().formatMessage(
from: payload,
address: address
)
let cacaoPayload = try payload.cacaoPayload(address: address)
let formatted = try SIWECacaoFormatter().formatMessage(from: cacaoPayload)
XCTAssertEqual(formatted, message)
XCTAssertEqual(try signer.sign(payload: payload, address: address, privateKey: privateKey, type: .eip191), signature)
XCTAssertEqual(try signer.sign(payload: cacaoPayload, privateKey: privateKey, type: .eip191), signature)
}

func testCacaoVerify() async throws {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import XCTest
@testable import Auth
@testable import WalletConnectSigner
import JSONRPC

class EIP1271VerifierTests: XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import XCTest
@testable import Auth
@testable import WalletConnectSigner

class EIP191VerifierTests: XCTestCase {

Expand Down
46 changes: 46 additions & 0 deletions Example/IntegrationTests/Chat/RegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,50 @@ import WalletConnectUtils

final class RegistryTests: XCTestCase {

let account = Account("eip155:1:0x15bca56b6e2728aec2532df9d436bd1600e86688")!
let privateKey = Data(hex: "305c6cde3846927892cd32762f6120539f3ec74c9e3a16b9b798b1e85351ae2a")

var sut: IdentityRegisterService!
var storage: IdentityStorage!
var signer: CacaoMessageSigner!

override func setUp() {
let keyserverURL = URL(string: "https://staging.keys.walletconnect.com")!
let httpService = HTTPNetworkClient(host: keyserverURL.host!)
let accountService = AccountService(currentAccount: account)
let identityNetworkService = IdentityNetworkService(accountService: accountService, httpService: httpService)
storage = IdentityStorage(keychain: KeychainStorageMock())
sut = IdentityRegisterService(
keyserverURL: keyserverURL,
identityStorage: storage,
identityNetworkService: identityNetworkService,
iatProvader: DefaultIATProvider(),
messageFormatter: SIWECacaoFormatter()
)
signer = MessageSignerFactory(signerFactory: DefaultSignerFactory()).create(projectId: InputConfig.projectId)
}

func testRegisterIdentityAndInviteKey() async throws {
var message: String!
let publicKey = try await sut.registerIdentity(account: account, isPrivate: false) { msg in
message = msg
return try! signer.sign(message: msg, privateKey: privateKey, type: .eip191)
}

let cacao = try await sut.resolveIdentity(publicKey: publicKey)
XCTAssertEqual(try SIWECacaoFormatter().formatMessage(from: cacao.p), message)

let recovered = storage.getIdentityKey(for: account)!.publicKey.hexRepresentation
XCTAssertEqual(publicKey, recovered)

let inviteKey = try await sut.registerInvite(account: account, isPrivate: false, onSign: { msg in
return try! signer.sign(message: msg, privateKey: privateKey, type: .eip191)
})

let recoveredKey = storage.getInviteKey(for: account)!
XCTAssertEqual(inviteKey, recoveredKey.publicKey.hexRepresentation)

let resolvedKey = try await sut.resolveInvite(account: account)
XCTAssertEqual(inviteKey, resolvedKey)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ final class AuthRequestInteractor {
func approve(request: AuthRequest) async throws {
let privateKey = Data(hex: "e56da0e170b5e09a8bb8f1b693392c7d56c3739a9c75740fbc558a2877868540")
let signature = try signer.sign(
payload: request.payload,
address: account.address,
payload: request.payload.cacaoPayload(address: account.address),
privateKey: privateKey,
type: .eip191)
try await Web3Wallet.instance.respond(requestId: request.id, signature: signature, from: account)
Expand Down
10 changes: 8 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let package = Package(
path: "Sources/Chat"),
.target(
name: "Auth",
dependencies: ["WalletConnectPairing"],
dependencies: ["WalletConnectPairing", "WalletConnectSigner"],
path: "Sources/Auth"),
.target(
name: "Web3Wallet",
Expand All @@ -72,7 +72,7 @@ let package = Package(
path: "Sources/WalletConnectEcho"),
.target(
name: "WalletConnectRelay",
dependencies: ["WalletConnectKMS"],
dependencies: ["WalletConnectJWT"],
path: "Sources/WalletConnectRelay",
resources: [.copy("PackageConfig.json")]),
.target(
Expand All @@ -85,6 +85,12 @@ let package = Package(
.target(
name: "Web3Inbox",
dependencies: ["WalletConnectChat"]),
.target(
name: "WalletConnectSigner",
dependencies: ["WalletConnectNetworking"]),
.target(
name: "WalletConnectJWT",
dependencies: ["WalletConnectKMS"]),
.target(
name: "WalletConnectUtils",
dependencies: ["JSONRPC"]),
Expand Down
6 changes: 3 additions & 3 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public class AuthClient: AuthClientProtocol {

/// Query pending authentication requests
/// - Returns: Pending authentication requests
public func getPendingRequests(account: Account) throws -> [AuthRequest] {
return try pendingRequestsProvider.getPendingRequests(account: account)
public func getPendingRequests() throws -> [AuthRequest] {
return try pendingRequestsProvider.getPendingRequests()
}

public func formatMessage(payload: AuthPayload, address: String) throws -> String {
return try SIWEMessageFormatter().formatMessage(from: payload, address: address)
return try SIWECacaoFormatter().formatMessage(from: payload.cacaoPayload(address: address))
}

private func setUpPublishers() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct AuthClientFactory {

let kms = KeyManagementService(keychain: keychainStorage)
let history = RPCHistoryFactory.createForNetwork(keyValueStorage: keyValueStorage)
let messageFormatter = SIWEMessageFormatter()
let messageFormatter = SIWECacaoFormatter()
let appRequestService = AppRequestService(networkingInteractor: networkingClient, kms: kms, appMetadata: metadata, logger: logger, iatProvader: iatProvider)
let messageSignerFactory = MessageSignerFactory(signerFactory: signerFactory)
let messageSigner = messageSignerFactory.create(projectId: projectId)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public protocol AuthClientProtocol {
func formatMessage(payload: AuthPayload, address: String) throws -> String
func respond(requestId: RPCID, signature: CacaoSignature, from account: Account) async throws
func reject(requestId: RPCID) async throws
func getPendingRequests(account: Account) throws -> [AuthRequest]
func getPendingRequests() throws -> [AuthRequest]
}
1 change: 1 addition & 0 deletions Sources/Auth/AuthImports.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !CocoaPods
@_exported import WalletConnectPairing
@_exported import WalletConnectSigner
#endif
7 changes: 3 additions & 4 deletions Sources/Auth/Services/App/AppRespondSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AppRespondSubscriber {
private let logger: ConsoleLogging
private let rpcHistory: RPCHistory
private let signatureVerifier: MessageSignatureVerifying
private let messageFormatter: SIWEMessageFormatting
private let messageFormatter: SIWECacaoFormatting
private let pairingRegisterer: PairingRegisterer
private var publishers = [AnyCancellable]()

Expand All @@ -17,7 +17,7 @@ class AppRespondSubscriber {
rpcHistory: RPCHistory,
signatureVerifier: MessageSignatureVerifying,
pairingRegisterer: PairingRegisterer,
messageFormatter: SIWEMessageFormatting) {
messageFormatter: SIWECacaoFormatting) {
self.networkingInteractor = networkingInteractor
self.logger = logger
self.rpcHistory = rpcHistory
Expand Down Expand Up @@ -52,8 +52,7 @@ class AppRespondSubscriber {

guard
let recovered = try? messageFormatter.formatMessage(
from: requestPayload.payloadParams,
address: address
from: requestPayload.payloadParams.cacaoPayload(address: address)
), recovered == message
else { self.onResponse?(requestId, .failure(.messageCompromised)); return }

Expand Down
11 changes: 0 additions & 11 deletions Sources/Auth/Services/Common/IATProvider.swift

This file was deleted.

Loading