Skip to content

Commit

Permalink
Merge pull request #1310 from WalletConnect/Sign_2_5
Browse files Browse the repository at this point in the history
Sign 2 5
  • Loading branch information
llbartekll committed Mar 7, 2024
2 parents 3c88656 + 580e246 commit cc66cdd
Show file tree
Hide file tree
Showing 136 changed files with 3,888 additions and 1,813 deletions.
53 changes: 53 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/CommonsTests.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1520"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CommonsTests"
BuildableName = "CommonsTests"
BlueprintName = "CommonsTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</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">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1510"
LastUpgradeVersion = "1520"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
98 changes: 89 additions & 9 deletions Example/DApp/Modules/Sign/SignPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,52 @@ final class SignPresenter: ObservableObject {
}

@MainActor
func connectWalletWithSign() {
func connectWalletWithSessionPropose() {
Task {
let uri = try await Pair.instance.create()
walletConnectUri = uri
do {
ActivityIndicatorManager.shared.start()
try await Sign.instance.connect(
walletConnectUri = try await Sign.instance.connect(
requiredNamespaces: Proposal.requiredNamespaces,
optionalNamespaces: Proposal.optionalNamespaces,
topic: uri.topic
optionalNamespaces: Proposal.optionalNamespaces
)
ActivityIndicatorManager.shared.stop()
router.presentNewPairing(walletConnectUri: uri)
router.presentNewPairing(walletConnectUri: walletConnectUri!)
} catch {
ActivityIndicatorManager.shared.stop()
}
}
}


@MainActor
func connectWalletWithSessionAuthenticate() {
Task {
do {
ActivityIndicatorManager.shared.start()
let uri = try await Sign.instance.authenticate(.stub())
walletConnectUri = uri
ActivityIndicatorManager.shared.stop()
router.presentNewPairing(walletConnectUri: walletConnectUri!)
} catch {
ActivityIndicatorManager.shared.stop()
}
}
}

@MainActor
func connectWalletWithSessionAuthenticateSIWEOnly() {
Task {
do {
ActivityIndicatorManager.shared.start()
let uri = try await Sign.instance.authenticate(.stub(methods: nil))
walletConnectUri = uri
ActivityIndicatorManager.shared.stop()
router.presentNewPairing(walletConnectUri: walletConnectUri!)
} catch {
ActivityIndicatorManager.shared.stop()
}
}
}

func disconnect() {
if let session {
Task { @MainActor in
Expand Down Expand Up @@ -127,21 +154,43 @@ extension SignPresenter {
}
.store(in: &subscriptions)

Sign.instance.authResponsePublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] response in
switch response.result {
case .success(let (session, _)):
if session == nil {
AlertPresenter.present(message: "Wallet Succesfully Authenticated", type: .success)
}
break
case .failure(let error):
AlertPresenter.present(message: error.localizedDescription, type: .error)
}
Task(priority: .high) { ActivityIndicatorManager.shared.stop() }
}
.store(in: &subscriptions)

Sign.instance.sessionResponsePublisher
.receive(on: DispatchQueue.main)
.sink { response in
Task(priority: .high) { ActivityIndicatorManager.shared.stop() }
}
.store(in: &subscriptions)

Sign.instance.sessionsPublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] _ in
self.router.dismiss()
self.getSession()
}
.store(in: &subscriptions)
Sign.instance.requestExpirationPublisher
.receive(on: DispatchQueue.main)
.sink { _ in
Task(priority: .high) { ActivityIndicatorManager.shared.stop() }
AlertPresenter.present(message: "Session Request has expired", type: .warning)
}
.store(in: &subscriptions)

}

private func getSession() {
Expand All @@ -164,3 +213,34 @@ extension SignPresenter {

// MARK: - SceneViewModel
extension SignPresenter: SceneViewModel {}


// MARK: - Authenticate request stub
extension AuthRequestParams {
static func stub(
domain: String = "app.web3inbox",
chains: [String] = ["eip155:1", "eip155:137"],
nonce: String = "32891756",
uri: String = "https://app.web3inbox.com/login",
nbf: String? = nil,
exp: String? = nil,
statement: String? = "I accept the ServiceOrg Terms of Service: https://app.web3inbox.com/tos",
requestId: String? = nil,
resources: [String]? = nil,
methods: [String]? = ["personal_sign", "eth_sendTransaction"]
) -> AuthRequestParams {
return try! AuthRequestParams(
domain: domain,
chains: chains,
nonce: nonce,
uri: uri,
nbf: nbf,
exp: exp,
statement: statement,
requestId: requestId,
resources: resources,
methods: methods
)
}
}

4 changes: 4 additions & 0 deletions Example/DApp/Modules/Sign/SignRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ final class SignRouter {
.push(from: viewController)
}

func dismissNewPairing() {
newPairingViewController?.dismiss()
}

func dismiss() {
viewController.dismiss(animated: true)
}
Expand Down
33 changes: 29 additions & 4 deletions Example/DApp/Modules/Sign/SignView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ struct SignView: View {
}

Spacer()



VStack(spacing: 10) {
Button {
presenter.connectWalletWithW3M()
Expand All @@ -32,17 +33,41 @@ struct SignView: View {
}

Button {
presenter.connectWalletWithSign()
presenter.connectWalletWithSessionPropose()
} label: {
Text("Connect with Sign API")
Text("Connect Session Propose")
.font(.system(size: 16, weight: .semibold))
.foregroundColor(.white)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(Color(red: 95/255, green: 159/255, blue: 248/255))
.cornerRadius(16)
}


Button {
presenter.connectWalletWithSessionAuthenticate()
} label: {
Text("Connect Session Authenticate")
.font(.system(size: 16, weight: .semibold))
.foregroundColor(.white)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(Color(red: 95/255, green: 159/255, blue: 248/255))
.cornerRadius(16)
}

Button {
presenter.connectWalletWithSessionAuthenticateSIWEOnly()
} label: {
Text("Connect Session Authenticate - SIWE only")
.font(.system(size: 16, weight: .semibold))
.foregroundColor(.white)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(Color(red: 95/255, green: 159/255, blue: 248/255))
.cornerRadius(16)
}

Button {
presenter.connectWalletWithWCM()
} label: {
Expand Down
7 changes: 4 additions & 3 deletions Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import UIKit

import Web3Modal
import WalletConnectModal
import Auth
import WalletConnectRelay
import WalletConnectNetworking
import Combine
Expand All @@ -19,8 +18,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
projectId: InputConfig.projectId,
socketFactory: DefaultSocketFactory()
)
Auth.configure(crypto: DefaultCryptoProvider())
Sign.configure(crypto: DefaultCryptoProvider())

let metadata = AppMetadata(
name: "Swift Dapp",
description: "WalletConnect DApp sample",
Expand Down Expand Up @@ -49,6 +48,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
metadata: metadata
)

Sign.instance.logger.setLogging(level: .debug)

Sign.instance.logsPublisher.sink { log in
switch log {
Expand All @@ -67,6 +67,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
}
}.store(in: &publishers)

Web3Modal.instance.disableAnalytics()
setupWindow(scene: scene)
}

Expand Down
1 change: 1 addition & 0 deletions Example/ExampleApp.xcodeproj/IntegrationTests.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"ChatTests",
"ENSResolverTests",
"HistoryTests",
"SignClientTests\/testEIP1271SessionAuthenticated()",
"SyncDerivationServiceTests",
"SyncTests"
],
Expand Down
Loading

0 comments on commit cc66cdd

Please sign in to comment.