Skip to content

Commit

Permalink
add ApproveEngineLoggingHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Dec 19, 2024
1 parent 1ec6b8d commit 6a4abe0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class ConfigurationService {
)

Notify.instance.setLogging(level: .off)
Sign.instance.setLogging(level: .off)
Sign.instance.setLogging(level: .debug)
Events.instance.setLogging(level: .off)

if let clientId = try? Networking.interactor.getClientId() {
Expand Down
13 changes: 12 additions & 1 deletion Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ final class ApproveEngine {
private let rpcHistory: RPCHistory
private let authRequestSubscribersTracking: AuthRequestSubscribersTracking
private let eventsClient: EventsClientProtocol
private let approveEngineLoggingHelper: ApproveEngineLoggingHelper

private var publishers = Set<AnyCancellable>()

Expand Down Expand Up @@ -64,6 +65,7 @@ final class ApproveEngine {
self.rpcHistory = rpcHistory
self.authRequestSubscribersTracking = authRequestSubscribersTracking
self.eventsClient = eventsClient
self.approveEngineLoggingHelper = ApproveEngineLoggingHelper(logger: logger)

setupRequestSubscriptions()
setupResponseSubscriptions()
Expand All @@ -73,7 +75,10 @@ final class ApproveEngine {

func approveProposal(proposerPubKey: String, validating sessionNamespaces: [String: SessionNamespace], sessionProperties: [String: String]? = nil) async throws -> Session {
eventsClient.startTrace(topic: "")
logger.debug("Approving session proposal")
logger.debug("Approving session proposal...")

approveEngineLoggingHelper.logSessionNamespaces(sessionNamespaces)

eventsClient.saveTraceEvent(SessionApproveExecutionTraceEvents.approvingSessionProposal)

guard !sessionNamespaces.isEmpty else {
Expand Down Expand Up @@ -394,6 +399,12 @@ private extension ApproveEngine {
func handleSessionProposeRequest(payload: RequestSubscriptionPayload<SessionType.ProposeParams>) {
logger.debug("Received Session Proposal")
let proposal = payload.request

approveEngineLoggingHelper.logProposalNamespaces(title: "Required Namespaces", proposal.requiredNamespaces)
if let optionalNamespaces = proposal.optionalNamespaces {
approveEngineLoggingHelper.logProposalNamespaces(title: "Optional Namespaces", optionalNamespaces)
}

do { try Namespace.validate(proposal.requiredNamespaces) } catch {
return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: SessionProposeProtocolMethod.responseAutoReject())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

import Foundation


final class ApproveEngineLoggingHelper {

private let logger: ConsoleLogging

init(logger: ConsoleLogging) {
self.logger = logger
}

func logProposalNamespaces(title: String, _ namespaces: [String: ProposalNamespace]) {
logger.debug("\(title):")
for (key, namespace) in namespaces {
logger.debug(" Namespace Key: \(key)")

if let chains = namespace.chains, !chains.isEmpty {
let chainList = chains.map { $0.absoluteString }.joined(separator: ", ")
logger.debug(" Chains: [\(chainList)]")
} else {
logger.debug(" Chains: None")
}

if !namespace.methods.isEmpty {
let methodsList = namespace.methods.sorted().joined(separator: ", ")
logger.debug(" Methods: [\(methodsList)]")
} else {
logger.debug(" Methods: None")
}

if !namespace.events.isEmpty {
let eventsList = namespace.events.sorted().joined(separator: ", ")
logger.debug(" Events: [\(eventsList)]")
} else {
logger.debug(" Events: None")
}
}
}

func logSessionNamespaces(_ sessionNamespaces: [String: SessionNamespace]) {
logger.debug("Session Namespaces:")
for (namespaceKey, ns) in sessionNamespaces {
logger.debug("Namespace: \(namespaceKey)")

if let chains = ns.chains, !chains.isEmpty {
let chainStrings = chains.map { $0.absoluteString }.joined(separator: ", ")
logger.debug(" Chains: [\(chainStrings)]")
} else {
logger.debug(" Chains: None")
}

if !ns.accounts.isEmpty {
// Assuming `Account` has a property `address` and `blockchain`
let accountStrings = ns.accounts.map { "\($0.blockchain.absoluteString):\($0.address)" }.joined(separator: ", ")
logger.debug(" Accounts: [\(accountStrings)]")
} else {
logger.debug(" Accounts: None")
}

if !ns.methods.isEmpty {
let methodList = ns.methods.sorted().joined(separator: ", ")
logger.debug(" Methods: [\(methodList)]")
} else {
logger.debug(" Methods: None")
}

if !ns.events.isEmpty {
let eventList = ns.events.sorted().joined(separator: ", ")
logger.debug(" Events: [\(eventList)]")
} else {
logger.debug(" Events: None")
}
}
}
}

0 comments on commit 6a4abe0

Please sign in to comment.