Skip to content

Commit

Permalink
Merge pull request #1077 from WalletConnect/develop
Browse files Browse the repository at this point in the history
1.7.2
  • Loading branch information
flypaper0 authored Sep 7, 2023
2 parents bd2e5b6 + 284d345 commit 1a07a37
Show file tree
Hide file tree
Showing 26 changed files with 160 additions and 119 deletions.
12 changes: 6 additions & 6 deletions Example/IntegrationTests/Auth/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final class AuthTests: XCTestCase {
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { _ in
requestExpectation.fulfill()
}.store(in: &publishers)
Expand All @@ -88,7 +88,7 @@ final class AuthTests: XCTestCase {
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
let signerFactory = DefaultSignerFactory()
Expand Down Expand Up @@ -126,7 +126,7 @@ final class AuthTests: XCTestCase {
resources: nil
), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
let signature = CacaoSignature(t: .eip1271, s: eip1271Signature)
Expand All @@ -147,7 +147,7 @@ final class AuthTests: XCTestCase {
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
let signature = CacaoSignature(t: .eip1271, s: eip1271Signature)
Expand All @@ -168,7 +168,7 @@ final class AuthTests: XCTestCase {
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
try! await walletAuthClient.reject(requestId: request.0.id)
Expand All @@ -189,7 +189,7 @@ final class AuthTests: XCTestCase {
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
let invalidSignature = "438effc459956b57fcd9f3dac6c675f9cee88abf21acab7305e8e32aa0303a883b06dcbd956279a7a2ca21ffa882ff55cc22e8ab8ec0f3fe90ab45f306938cfa1b"
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 @@ -110,7 +110,7 @@ final class PairingTests: XCTestCase {
let expectation = expectation(description: "expects ping response")
makeWalletClients()
let uri = try! await appPairingClient.create()
try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)
try! await walletPairingClient.ping(topic: uri.topic)
walletPairingClient.pingResponsePublisher
.sink { topic in
Expand All @@ -131,7 +131,7 @@ final class PairingTests: XCTestCase {

let uri = try! await appPairingClient.create()

try! await walletPairingClient.pair(uri: uri)
try? await walletPairingClient.pair(uri: uri)

try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ final class SessionProposalInteractor {
let supportedChains = [Blockchain("eip155:1")!, Blockchain("eip155:137")!]
let supportedAccounts = [Account(blockchain: Blockchain("eip155:1")!, address: ETHSigner.address)!, Account(blockchain: Blockchain("eip155:137")!, address: ETHSigner.address)!]
*/
do {
let sessionNamespaces = try AutoNamespaces.build(
sessionProposal: proposal,
chains: Array(supportedChains),
methods: Array(supportedMethods),
events: Array(supportedEvents),
accounts: supportedAccounts
)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces, sessionProperties: proposal.sessionProperties)
} catch {
print(error)
}
let sessionNamespaces = try AutoNamespaces.build(
sessionProposal: proposal,
chains: Array(supportedChains),
methods: Array(supportedMethods),
events: Array(supportedEvents),
accounts: supportedAccounts
)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces, sessionProperties: proposal.sessionProperties)
}

func reject(proposal: Session.Proposal) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ final class SessionProposalPresenter: ObservableObject {
let sessionProposal: Session.Proposal
let verified: Bool?

@Published var showError = false
@Published var errorMessage = "Error"

private var disposeBag = Set<AnyCancellable>()

init(
Expand All @@ -30,14 +33,24 @@ final class SessionProposalPresenter: ObservableObject {

@MainActor
func onApprove() async throws {
try await interactor.approve(proposal: sessionProposal, account: importAccount.account)
router.dismiss()
do {
try await interactor.approve(proposal: sessionProposal, account: importAccount.account)
router.dismiss()
} catch {
errorMessage = error.localizedDescription
showError.toggle()
}
}

@MainActor
func onReject() async throws {
try await interactor.reject(proposal: sessionProposal)
router.dismiss()
do {
try await interactor.reject(proposal: sessionProposal)
router.dismiss()
} catch {
errorMessage = error.localizedDescription
showError.toggle()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ struct SessionProposalView: View {
Spacer()
}
}
.alert(presenter.errorMessage, isPresented: $presenter.showError) {
Button("OK", role: .cancel) {}
}
.edgesIgnoringSafeArea(.all)
}
//private func sessionProposalView(chain: String) -> some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ final class WalletPresenter: ObservableObject {
func onAppear() {
showPairingLoading = app.requestSent
removePairingIndicator()

let proposals = interactor.getPendingProposals()
if let proposal = proposals.last {
router.present(sessionProposal: proposal.proposal, importAccount: importAccount, sessionContext: proposal.context)
}


let pendingRequests = interactor.getPendingRequests()
if let request = pendingRequests.first(where: { $0.context != nil }) {
router.present(sessionRequest: request.request, importAccount: importAccount, sessionContext: request.context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class Web3InboxViewController: UIViewController {
}

@objc func refreshTapped() {
webView?.reload()
Web3Inbox.instance.reload()
}

@objc func getUrlPressed(_ sender: UIBarItem) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public struct AuthClientFactory {
let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingClient, logger: logger, rpcHistory: history, signatureVerifier: signatureVerifier, pairingRegisterer: pairingRegisterer, messageFormatter: messageFormatter)
let walletErrorResponder = WalletErrorResponder(networkingInteractor: networkingClient, logger: logger, kms: kms, rpcHistory: history)
let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingClient, logger: logger, kms: kms, walletErrorResponder: walletErrorResponder, pairingRegisterer: pairingRegisterer, verifyClient: verifyClient, verifyContextStore: verifyContextStore)
let walletRespondService = WalletRespondService(networkingInteractor: networkingClient, logger: logger, kms: kms, rpcHistory: history, verifyContextStore: verifyContextStore, walletErrorResponder: walletErrorResponder)
let walletRespondService = WalletRespondService(networkingInteractor: networkingClient, logger: logger, kms: kms, rpcHistory: history, verifyContextStore: verifyContextStore, walletErrorResponder: walletErrorResponder, pairingRegisterer: pairingRegisterer)
let pendingRequestsProvider = PendingRequestsProvider(rpcHistory: history, verifyContextStore: verifyContextStore)

return AuthClient(
Expand Down
5 changes: 1 addition & 4 deletions Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ class WalletRequestSubscriber {
.sink { [unowned self] (payload: RequestSubscriptionPayload<AuthRequestParams>) in
logger.debug("WalletRequestSubscriber: Received request")

pairingRegisterer.activate(
pairingTopic: payload.topic,
peerMetadata: payload.request.requester.metadata
)
pairingRegisterer.setReceived(pairingTopic: payload.topic)

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

Expand Down
25 changes: 18 additions & 7 deletions Sources/Auth/Services/Wallet/WalletRespondService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ actor WalletRespondService {
private let verifyContextStore: CodableStore<VerifyContext>
private let logger: ConsoleLogging
private let walletErrorResponder: WalletErrorResponder
private let pairingRegisterer: PairingRegisterer

init(networkingInteractor: NetworkInteracting,
logger: ConsoleLogging,
kms: KeyManagementService,
rpcHistory: RPCHistory,
verifyContextStore: CodableStore<VerifyContext>,
walletErrorResponder: WalletErrorResponder) {
init(
networkingInteractor: NetworkInteracting,
logger: ConsoleLogging,
kms: KeyManagementService,
rpcHistory: RPCHistory,
verifyContextStore: CodableStore<VerifyContext>,
walletErrorResponder: WalletErrorResponder,
pairingRegisterer: PairingRegisterer
) {
self.networkingInteractor = networkingInteractor
self.logger = logger
self.kms = kms
self.rpcHistory = rpcHistory
self.verifyContextStore = verifyContextStore
self.walletErrorResponder = walletErrorResponder
self.pairingRegisterer = pairingRegisterer
}

func respond(requestId: RPCID, signature: CacaoSignature, account: Account) async throws {
Expand All @@ -34,10 +39,16 @@ actor WalletRespondService {

let header = CacaoHeader(t: "eip4361")
let payload = try authRequestParams.payloadParams.cacaoPayload(address: account.address)
let responseParams = AuthResponseParams(h: header, p: payload, s: signature)
let responseParams = AuthResponseParams(h: header, p: payload, s: signature)

let response = RPCResponse(id: requestId, result: responseParams)
try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: AuthRequestProtocolMethod(), envelopeType: .type1(pubKey: keys.publicKey.rawRepresentation))

pairingRegisterer.activate(
pairingTopic: topic,
peerMetadata: authRequestParams.requester.metadata
)

verifyContextStore.delete(forKey: requestId.string)
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/WalletConnectNetworking/NetworkInteracting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public protocol NetworkInteracting {
func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws
func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws
func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws

func handleHistoryRequest(topic: String, request: RPCRequest)

func requestSubscription<Request: Codable>(
on request: ProtocolMethod
) -> AnyPublisher<RequestSubscriptionPayload<Request>, Never>
Expand Down
4 changes: 4 additions & 0 deletions Sources/WalletConnectNetworking/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public class NetworkingInteractor: NetworkInteracting {
logger.debug("Networking Interactor - Received unknown object type from networking relay")
}
}

public func handleHistoryRequest(topic: String, request: RPCRequest) {
requestPublisherSubject.send((topic, request, Data(), Date(), nil))
}

private func handleRequest(topic: String, request: RPCRequest, decryptedPayload: Data, publishedAt: Date, derivedTopic: String?) {
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ struct NotifyDeleteResponsePayload: JWTClaimsCodable {
let iat: UInt64
/// Timestamp when JWT must expire
let exp: UInt64
/// Key server URL
let ksu: String
/// Description of action intent. Must be equal to `notify_delete_response`
let act: String?

Expand Down Expand Up @@ -38,25 +36,11 @@ struct NotifyDeleteResponsePayload: JWTClaimsCodable {
}
}

let keyserver: URL
let selfPubKey: DIDKey
let subscriptionHash: String
let app: String

init(
keyserver: URL,
selfPubKey: DIDKey,
subscriptionHash: String,
app: String
) {
self.keyserver = keyserver
self.selfPubKey = selfPubKey
self.subscriptionHash = subscriptionHash
self.app = app
}

init(claims: Claims) throws {
self.keyserver = try claims.ksu.asURL()
self.selfPubKey = try DIDKey(did: claims.aud)
self.subscriptionHash = claims.sub
self.app = claims.app
Expand All @@ -66,7 +50,6 @@ struct NotifyDeleteResponsePayload: JWTClaimsCodable {
return Claims(
iat: defaultIat(),
exp: expiry(days: 1),
ksu: keyserver.absoluteString,
act: Claims.action,
iss: iss,
aud: selfPubKey.did(variant: .ED25519),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ struct NotifyMessagePayload: JWTClaimsCodable {
let iat: UInt64
/// Timestamp when JWT must expire
let exp: UInt64
/// Key server URL
let ksu: String
/// Action intent (must be `notify_message`)
let act: String?

Expand Down Expand Up @@ -41,31 +39,13 @@ struct NotifyMessagePayload: JWTClaimsCodable {
}

let castServerPubKey: DIDKey
let keyserver: URL
let account: Account
let subscriptionId: String
let app: String
let message: NotifyMessage

init(
castServerPubKey: DIDKey,
keyserver: URL,
account: Account,
subscriptionId: String,
app: String,
message: NotifyMessage
) {
self.castServerPubKey = castServerPubKey
self.keyserver = keyserver
self.account = account
self.subscriptionId = subscriptionId
self.app = app
self.message = message
}

init(claims: Claims) throws {
self.castServerPubKey = try DIDKey(did: claims.iss)
self.keyserver = try claims.ksu.asURL()
self.account = try DIDPKH(did: claims.aud).account
self.subscriptionId = claims.sub
self.app = claims.app
Expand All @@ -76,7 +56,6 @@ struct NotifyMessagePayload: JWTClaimsCodable {
return Claims(
iat: defaultIat(),
exp: expiry(days: 1),
ksu: keyserver.absoluteString,
act: Claims.action,
iss: castServerPubKey.multibase(variant: .ED25519),
aud: account.did,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ struct NotifySubscriptionResponsePayload: JWTClaimsCodable {
let iat: UInt64
/// timestamp when jwt must expire
let exp: UInt64
/// Key server URL
let ksu: String
/// Description of action intent. Must be equal to "notify_subscription_response"
let act: String?

Expand Down Expand Up @@ -38,13 +36,11 @@ struct NotifySubscriptionResponsePayload: JWTClaimsCodable {
}
}

let keyserver: URL
let selfPubKey: DIDKey
let publicKey: DIDKey
let app: String

init(claims: Claims) throws {
self.keyserver = try claims.ksu.asURL()
self.selfPubKey = try DIDKey(did: claims.aud)
self.publicKey = try DIDKey(did: claims.sub)
self.app = claims.app
Expand All @@ -54,7 +50,6 @@ struct NotifySubscriptionResponsePayload: JWTClaimsCodable {
return Claims(
iat: defaultIat(),
exp: expiry(days: 1),
ksu: keyserver.absoluteString,
act: Claims.action,
iss: iss,
aud: selfPubKey.did(variant: .ED25519),
Expand Down
Loading

0 comments on commit 1a07a37

Please sign in to comment.