Skip to content

Commit

Permalink
update integration with rejection on failed autonamespace build
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Feb 14, 2024
1 parent 4036cc2 commit 00578da
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ final class SignClientTests: XCTestCase {
wallet.sessionProposalPublisher.sink { [unowned self] (proposal, _) in
Task(priority: .high) {
do {
try await wallet.reject(proposalId: proposal.id, reason: .userRejectedChains) // TODO: Review reason
try await wallet.reject(proposalId: proposal.id, reason: .unsupportedChains)
store.rejectedProposal = proposal
semaphore.signal()
} catch { XCTFail("\(error)") }
Expand All @@ -119,7 +119,7 @@ final class SignClientTests: XCTestCase {
dapp.sessionRejectionPublisher.sink { proposal, _ in
semaphore.wait()
XCTAssertEqual(store.rejectedProposal, proposal)
sessionRejectExpectation.fulfill() // TODO: Assert reason code
sessionRejectExpectation.fulfill()
}.store(in: &publishers)
await fulfillment(of: [sessionRejectExpectation], timeout: InputConfig.defaultTimeout)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,31 @@ final class SessionProposalInteractor {

let supportedAccounts = Array(supportedChains).map { Account(blockchain: $0, address: account.address)! }

let solanaChain: Set = [Blockchain("solana:4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ")!]

supportedChains = supportedChains.union(solanaChain)

/* Use only supported values for production. I.e:
let supportedMethods = ["eth_signTransaction", "personal_sign", "eth_signTypedData", "eth_sendTransaction", "eth_sign"]
let supportedEvents = ["accountsChanged", "chainChanged"]
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)!]
*/
let sessionNamespaces = try AutoNamespaces.build(
sessionProposal: proposal,
chains: Array(supportedChains),
methods: Array(supportedMethods),
events: Array(supportedEvents),
accounts: supportedAccounts
)
var sessionNamespaces: [String: SessionNamespace]!

do {
sessionNamespaces = try AutoNamespaces.build(
sessionProposal: proposal,
chains: Array(supportedChains),
methods: Array(supportedMethods),
events: Array(supportedEvents),
accounts: supportedAccounts
)
} catch let error as AutoNamespacesError {
try await reject(proposal: proposal, reason: RejectionReason(from: error))
AlertPresenter.present(message: error.localizedDescription, type: .error)
return false
} catch {
try await reject(proposal: proposal, reason: .userRejected)
AlertPresenter.present(message: error.localizedDescription, type: .error)
return false
}
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces, sessionProperties: proposal.sessionProperties)

if let uri = proposal.proposer.redirect?.native {
Expand All @@ -42,7 +50,7 @@ final class SessionProposalInteractor {
}
}

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

/* Redirect */
Expand Down
6 changes: 3 additions & 3 deletions Sources/WalletConnectSign/Namespace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public enum AutoNamespacesError: Error, LocalizedError {
case requiredAccountsNotSatisfied
case requiredMethodsNotSatisfied
case requiredEventsNotSatisfied
case emtySessionNamespacesForbidden
case emptySessionNamespacesForbidden

public var errorDescription: String? {
switch self {
Expand All @@ -17,7 +17,7 @@ public enum AutoNamespacesError: Error, LocalizedError {
return "The required methods are not satisfied."
case .requiredEventsNotSatisfied:
return "The required events are not satisfied."
case .emtySessionNamespacesForbidden:
case .emptySessionNamespacesForbidden:
return "Empty session namespaces are not allowed."
}
}
Expand Down Expand Up @@ -341,7 +341,7 @@ public enum AutoNamespaces {
}
}
}
guard !sessionNamespaces.isEmpty else { throw AutoNamespacesError.emtySessionNamespacesForbidden }
guard !sessionNamespaces.isEmpty else { throw AutoNamespacesError.emptySessionNamespacesForbidden }

return sessionNamespaces
}
Expand Down
34 changes: 27 additions & 7 deletions Sources/WalletConnectSign/RejectionReason.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,42 @@ import Foundation
/// https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-25.md
public enum RejectionReason {
case userRejected
case userRejectedChains
case userRejectedMethods
case userRejectedEvents
case unsupportedChains
case unsupportedMethods
case unsupportedAccounts
case upsupportedEvents
}

internal extension RejectionReason {
func internalRepresentation() -> SignReasonCode {
switch self {
case .userRejected:
return SignReasonCode.userRejected
case .userRejectedChains:
return SignReasonCode.userRejectedChains
case .userRejectedMethods:
case .unsupportedChains:
return SignReasonCode.unsupportedChains
case .unsupportedMethods:
return SignReasonCode.userRejectedMethods
case .userRejectedEvents:
case .upsupportedEvents:
return SignReasonCode.userRejectedEvents
case .unsupportedAccounts:
return SignReasonCode.unsupportedAccounts
}
}
}

public extension RejectionReason {
init(from error: AutoNamespacesError) {
switch error {
case .requiredChainsNotSatisfied:
self = .unsupportedChains
case .requiredAccountsNotSatisfied:
self = .unsupportedAccounts
case .requiredMethodsNotSatisfied:
self = .unsupportedMethods
case .requiredEventsNotSatisfied:
self = .upsupportedEvents
case .emptySessionNamespacesForbidden:
self = .unsupportedAccounts
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ final class AutoNamespacesValidationTests: XCTestCase {
events: [],
accounts: []
), "Expected to throw AutoNamespacesError.emtySessionNamespacesForbidden, but it did not") { error in
guard case AutoNamespacesError.emtySessionNamespacesForbidden = error else {
guard case AutoNamespacesError.emptySessionNamespacesForbidden = error else {
return XCTFail("Unexpected error type: \(error)")
}
}
Expand Down

0 comments on commit 00578da

Please sign in to comment.