Skip to content

Commit

Permalink
Merge pull request #1202 from WalletConnect/native-redirect
Browse files Browse the repository at this point in the history
Native Redirect
  • Loading branch information
llbartekll authored Nov 1, 2023
2 parents 3bb4d86 + 56aa06f commit b9b59ad
Show file tree
Hide file tree
Showing 34 changed files with 174 additions and 95 deletions.
3 changes: 2 additions & 1 deletion Example/DApp/Auth/AuthCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ final class AuthCoordinator {
name: "Swift Dapp",
description: "WalletConnect DApp sample",
url: "wallet.connect",
icons: ["https://avatars.githubusercontent.com/u/37784886"])
icons: ["https://avatars.githubusercontent.com/u/37784886"],
redirect: AppMetadata.Redirect(native: "dapp://", universal: nil))

Pair.configure(metadata: metadata)
navigationController.viewControllers = [authViewController]
Expand Down
19 changes: 13 additions & 6 deletions Example/DApp/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>dapp</string>
</array>
</dict>
</array>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>metamask</string>
Expand All @@ -11,12 +24,6 @@
<string>rainbow</string>
<string>spot</string>
</array>
<key>CFBundleVersion</key>
<string>7</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>PROJECT_ID</key>
<string>$(PROJECT_ID)</string>
<key>UIApplicationSceneManifest</key>
Expand Down
3 changes: 2 additions & 1 deletion Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
name: "Swift Dapp",
description: "WalletConnect DApp sample",
url: "wallet.connect",
icons: ["https://avatars.githubusercontent.com/u/37784886"]
icons: ["https://avatars.githubusercontent.com/u/37784886"],
redirect: AppMetadata.Redirect(native: "dapp://", universal: nil)
)

WalletConnectModal.configure(
Expand Down
2 changes: 1 addition & 1 deletion Example/DApp/Sign/Connect/ConnectViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ConnectViewController: UIViewController, UITableViewDataSource, UITableVie
}

@objc func connectWithExampleWallet() {
let url = URL(string: "walletapp://wc?uri=\(uri.deeplinkUri)")!
let url = URL(string: "walletapp://wc?uri=\(uri.deeplinkUri.removingPercentEncoding!)")!
DispatchQueue.main.async {
UIApplication.shared.open(url, options: [:]) { [weak self] _ in
self?.dismiss(animated: true, completion: nil)
Expand Down
4 changes: 3 additions & 1 deletion Example/DApp/Sign/SignCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ final class SignCoordinator {
name: "Swift Dapp",
description: "WalletConnect DApp sample",
url: "wallet.connect",
icons: ["https://avatars.githubusercontent.com/u/37784886"])
icons: ["https://avatars.githubusercontent.com/u/37784886"],
redirect: AppMetadata.Redirect(native: "dapp://", universal: nil)
)

Pair.configure(metadata: metadata)
#if DEBUG
Expand Down
9 changes: 8 additions & 1 deletion Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -231,6 +231,7 @@
A74D32BA2A1E25AD00CB8536 /* QueryParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = A74D32B92A1E25AD00CB8536 /* QueryParameters.swift */; };
C5133A78294125CC00A8314C /* Web3 in Frameworks */ = {isa = PBXBuildFile; productRef = C5133A77294125CC00A8314C /* Web3 */; };
C53AA4362941251C008EA57C /* DefaultSignerFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = A59CF4F5292F83D50031A42F /* DefaultSignerFactory.swift */; };
C54C24902AEB1B5600DA4BF6 /* WalletConnectRouter in Frameworks */ = {isa = PBXBuildFile; productRef = C54C248F2AEB1B5600DA4BF6 /* WalletConnectRouter */; };
C55D347F295DD7140004314A /* AuthRequestModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C55D347A295DD7140004314A /* AuthRequestModule.swift */; };
C55D3480295DD7140004314A /* AuthRequestPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C55D347B295DD7140004314A /* AuthRequestPresenter.swift */; };
C55D3481295DD7140004314A /* AuthRequestRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C55D347C295DD7140004314A /* AuthRequestRouter.swift */; };
Expand Down Expand Up @@ -750,6 +751,7 @@
C56EE255293F569A004840D1 /* Starscream in Frameworks */,
A5B6C0F52A6EAB2800927332 /* WalletConnectNotify in Frameworks */,
C56EE27B293F56F8004840D1 /* WalletConnectAuth in Frameworks */,
C54C24902AEB1B5600DA4BF6 /* WalletConnectRouter in Frameworks */,
84943C7D2A9BA328007EBAC2 /* Mixpanel in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -2058,6 +2060,7 @@
84943C7C2A9BA328007EBAC2 /* Mixpanel */,
A59D25ED2AB3672700D7EA3A /* AsyncButton */,
A5F1526E2ACDC46B00D745A6 /* Web3ModalUI */,
C54C248F2AEB1B5600DA4BF6 /* WalletConnectRouter */,
);
productName = ChatWallet;
productReference = C56EE21B293F55ED004840D1 /* WalletApp.app */;
Expand Down Expand Up @@ -3449,6 +3452,10 @@
package = A5AE354528A1A2AC0059AE8A /* XCRemoteSwiftPackageReference "Web3" */;
productName = Web3;
};
C54C248F2AEB1B5600DA4BF6 /* WalletConnectRouter */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectRouter;
};
C55D349829630D440004314A /* Web3Wallet */ = {
isa = XCSwiftPackageProductDependency;
productName = Web3Wallet;
Expand Down
2 changes: 1 addition & 1 deletion Example/IntegrationTests/Auth/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class AuthTests: XCTestCase {
networkingClient: networkingClient)

let authClient = AuthClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
metadata: AppMetadata(name: name, description: "", url: "", icons: [""], redirect: AppMetadata.Redirect(native: "", universal: nil)),
projectId: InputConfig.projectId,
crypto: DefaultCryptoProvider(),
logger: logger,
Expand Down
4 changes: 2 additions & 2 deletions Example/IntegrationTests/Pairing/PairingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class PairingTests: XCTestCase {
appPairingClient = pairingClient

appAuthClient = AuthClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
metadata: AppMetadata(name: name, description: "", url: "", icons: [""], redirect: AppMetadata.Redirect(native: "dapp://", universal: nil)),
projectId: InputConfig.projectId,
crypto: DefaultCryptoProvider(),
logger: notifyLogger,
Expand All @@ -88,7 +88,7 @@ final class PairingTests: XCTestCase {
logger: notifyLogger
)
appAuthClient = AuthClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
metadata: AppMetadata(name: name, description: "", url: "", icons: [""], redirect: AppMetadata.Redirect(native: "", universal: nil)),
projectId: InputConfig.projectId,
crypto: DefaultCryptoProvider(),
logger: notifyLogger,
Expand Down
20 changes: 10 additions & 10 deletions Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class SignClientTests: XCTestCase {
networkingClient: networkingClient
)
let client = SignClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
metadata: AppMetadata(name: name, description: "", url: "", icons: [""], redirect: AppMetadata.Redirect(native: "", universal: nil)),
logger: logger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
Expand Down Expand Up @@ -437,7 +437,7 @@ final class SignClientTests: XCTestCase {
requiredNamespaces: requiredNamespaces,
optionalNamespaces: optionalNamespaces,
sessionProperties: nil,
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [])), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil))), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
)

let sessionNamespaces = try AutoNamespaces.build(
Expand Down Expand Up @@ -505,11 +505,11 @@ final class SignClientTests: XCTestCase {
let sessionProposal = Session.Proposal(
id: "",
pairingTopic: "",
proposer: AppMetadata(name: "", description: "", url: "", icons: []),
proposer: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil)),
requiredNamespaces: requiredNamespaces,
optionalNamespaces: optionalNamespaces,
sessionProperties: nil,
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [])), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil))), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
)

let sessionNamespaces = try AutoNamespaces.build(
Expand Down Expand Up @@ -563,11 +563,11 @@ final class SignClientTests: XCTestCase {
let sessionProposal = Session.Proposal(
id: "",
pairingTopic: "",
proposer: AppMetadata(name: "", description: "", url: "", icons: []),
proposer: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil)),
requiredNamespaces: requiredNamespaces,
optionalNamespaces: optionalNamespaces,
sessionProperties: nil,
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [])), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil))), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
)

let sessionNamespaces = try AutoNamespaces.build(
Expand Down Expand Up @@ -629,11 +629,11 @@ final class SignClientTests: XCTestCase {
let sessionProposal = Session.Proposal(
id: "",
pairingTopic: "",
proposer: AppMetadata(name: "", description: "", url: "", icons: []),
proposer: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil)),
requiredNamespaces: requiredNamespaces,
optionalNamespaces: optionalNamespaces,
sessionProperties: nil,
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [])), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil))), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
)

do {
Expand Down Expand Up @@ -692,11 +692,11 @@ final class SignClientTests: XCTestCase {
let sessionProposal = Session.Proposal(
id: "",
pairingTopic: "",
proposer: AppMetadata(name: "", description: "", url: "", icons: []),
proposer: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil)),
requiredNamespaces: requiredNamespaces,
optionalNamespaces: optionalNamespaces,
sessionProperties: nil,
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [])), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
proposal: SessionProposal(relays: [], proposer: Participant(publicKey: "", metadata: AppMetadata(name: "", description: "", url: "", icons: [], redirect: AppMetadata.Redirect(native: "", universal: nil))), requiredNamespaces: [:], optionalNamespaces: [:], sessionProperties: [:])
)

do {
Expand Down
3 changes: 2 additions & 1 deletion Example/IntegrationTests/Stubs/Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ extension AppMetadata {
name: "WalletConnectSwift",
description: "WalletConnectSwift",
url: "https://walletconnect.com",
icons: []
icons: [],
redirect: AppMetadata.Redirect(native: "dapp://", universal: nil)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class XPlatformW3WTests: XCTestCase {
networkingClient: networkingClient)

let signClient = SignClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
metadata: AppMetadata(name: name, description: "", url: "", icons: [""], redirect: AppMetadata.Redirect(native: "", universal: nil)),
logger: signLogger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
Expand All @@ -58,7 +58,7 @@ final class XPlatformW3WTests: XCTestCase {
)

let authClient = AuthClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
metadata: AppMetadata(name: name, description: "", url: "", icons: [""], redirect: AppMetadata.Redirect(native: "", universal: nil)),
projectId: InputConfig.projectId,
crypto: DefaultCryptoProvider(),
logger: authLogger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ final class ConfigurationService {
name: "Example Wallet",
description: "wallet description",
url: "example.wallet",
icons: ["https://avatars.githubusercontent.com/u/37784886"]
icons: ["https://avatars.githubusercontent.com/u/37784886"],
redirect: AppMetadata.Redirect(native: "walletapp://", universal: nil)
)

Web3Wallet.configure(metadata: metadata, crypto: DefaultCryptoProvider(), environment: BuildConfiguration.shared.apnsEnvironment)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation

import Web3Wallet
import WalletConnectRouter

final class AuthRequestInteractor {

Expand All @@ -16,10 +18,16 @@ final class AuthRequestInteractor {
privateKey: Data(hex: importAccount.privateKey),
type: .eip191)
try await Web3Wallet.instance.respond(requestId: request.id, signature: signature, from: account)

/* Redirect */
WalletConnectRouter.goBack(uri: request.requester.redirect.native)
}

func reject(request: AuthRequest) async throws {
try await Web3Wallet.instance.reject(requestId: request.id)

/* Redirect */
WalletConnectRouter.goBack(uri: request.requester.redirect.native)
}

func formatted(request: AuthRequest, account: Account) -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

import Web3Wallet
import WalletConnectRouter

final class SessionProposalInteractor {
func approve(proposal: Session.Proposal, account: Account) async throws {
Expand Down Expand Up @@ -28,9 +29,15 @@ final class SessionProposalInteractor {
accounts: supportedAccounts
)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces, sessionProperties: proposal.sessionProperties)

/* Redirect */
WalletConnectRouter.goBack(uri: proposal.proposer.redirect.native)
}

func reject(proposal: Session.Proposal) async throws {
try await Web3Wallet.instance.reject(proposalId: proposal.id, reason: .userRejected)

/* Redirect */
WalletConnectRouter.goBack(uri: proposal.proposer.redirect.native)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation

import Web3Wallet
import WalletConnectRouter

final class SessionRequestInteractor {
func approve(sessionRequest: Request, importAccount: ImportAccount) async throws {
Expand All @@ -10,6 +12,12 @@ final class SessionRequestInteractor {
requestId: sessionRequest.id,
response: .response(result)
)

/* Redirect */
let session = getSession(topic: sessionRequest.topic)
if let uri = session?.peer.redirect.native {
WalletConnectRouter.goBack(uri: uri)
}
} catch {
throw error
}
Expand All @@ -21,6 +29,12 @@ final class SessionRequestInteractor {
requestId: sessionRequest.id,
response: .error(.init(code: 0, message: ""))
)

/* Redirect */
let session = getSession(topic: sessionRequest.topic)
if let uri = session?.peer.redirect.native {
WalletConnectRouter.goBack(uri: uri)
}
}

func getSession(topic: String) -> Session? {
Expand Down
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let package = Package(
targets: ["WalletConnectPush"]),
.library(
name: "WalletConnectRouter",
targets: ["WalletConnectRouter"]),
targets: ["WalletConnectRouter", "WalletConnectRouterLegacy"]),
.library(
name: "WalletConnectNetworking",
targets: ["WalletConnectNetworking"]),
Expand Down Expand Up @@ -119,9 +119,14 @@ let package = Package(
.target(
name: "WalletConnectNetworking",
dependencies: ["HTTPClient", "WalletConnectRelay"]),
.target(
name: "WalletConnectRouterLegacy",
dependencies: [],
path: "Sources/WalletConnectRouter/RouterLegacy"),
.target(
name: "WalletConnectRouter",
dependencies: []),
dependencies: ["WalletConnectRouterLegacy"],
path: "Sources/WalletConnectRouter/Router"),
.target(
name: "WalletConnectVerify",
dependencies: ["WalletConnectUtils", "WalletConnectNetworking"]),
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/Services/Wallet/PendingRequestsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PendingRequestsProvider {
.filter {$0.request.method == "wc_authRequest"}
.compactMap {
guard let params = try? $0.request.params?.get(AuthRequestParams.self) else { return nil }
return AuthRequest(id: $0.request.id!, topic: $0.topic, payload: params.payloadParams)
return AuthRequest(id: $0.request.id!, topic: $0.topic, payload: params.payloadParams, requester: params.requester.metadata)
}

return pendingRequests.map { ($0, try? verifyContextStore.get(key: $0.id.string)) }
Expand Down
4 changes: 2 additions & 2 deletions Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class WalletRequestSubscriber {
pairingRegisterer.register(method: AuthRequestProtocolMethod())
.sink { [unowned self] (payload: RequestSubscriptionPayload<AuthRequestParams>) in
logger.debug("WalletRequestSubscriber: Received request")

pairingRegisterer.setReceived(pairingTopic: payload.topic)

let request = AuthRequest(id: payload.id, topic: payload.topic, payload: payload.request.payloadParams)
let request = AuthRequest(id: payload.id, topic: payload.topic, payload: payload.request.payloadParams, requester: payload.request.requester.metadata)

Task(priority: .high) {
let assertionId = payload.decryptedPayload.sha256().toHexString()
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/Types/Public/AuthRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public struct AuthRequest: Equatable, Codable {
public let id: RPCID
public let topic: String
public let payload: AuthPayload
public let requester: AppMetadata
}
Loading

0 comments on commit b9b59ad

Please sign in to comment.