Skip to content

Commit

Permalink
refactor: Remove business logic from WC ui components
Browse files Browse the repository at this point in the history
This commit brings a separation of concerns for the UI components involved in dApp interactions.

Issue: The UI components depend on the WalletConnectService and also on its dependencies like DAppsRequestHAndler. As a result the UI components have a hard dependency on the WalletConnect specifics and are incompatible with BC. This results in duplication of logic.
Issue: The UI components operate on WalletConnect specific JSON object. E.g. session objects, session proposal etc. As a result the UI is built around the WalletConnect message format.
Issue: The UI components operate on ListModel items received through functions and stored internally. Any change in the model would result in a crash.
Solution: Remove the WalletConnectService dependency from DAppsWorkflow. The DAppsWorkflow now operates with models, signals and functions. This is the first step in the broader refactoring. Moving the logic into the service itself will allow us to further refactor the WC and BC.

How does it work now:

Dependencies - The UI components have a dependency on models. SessionRequestsModel and DAppsModel.
Pairing - The pairing is initiated in the UI. On user input a pairingValidationRequested signal is emitted and the result is received as a function pairingValidated. If the url is valid the UI requests a pairingRequested. When the WalletConnectService is refactored we can go further and request only pairingRequested and to receive a pairingResult call as a function with the result. In the current implementation on pairingRequested we'll receive a connectDApp request.
Connecting dApps - The flow is initiated with connectDApp function. This call currently contains all the needed info as args. In the next step it could be replaced with a ConnectionRequests model. The connectDApp call triggered a connection popup if we're not currently showing one to the user. If we're currently showing one it will be queued (corner case). The connection can be accepted with connectionAccepted and rejected with connectionDeclined. Once the connection is accepted we're expecting a result connectionSuccessful or connectionFailed. The connectionSuccessful also expects a new id for the established connection.
Signing - The signing flow orbits around the SessionRequestsModel. Each item from the model will generate a popup showing the sign details to the user. Sign can be accepted or rejected using signRequestAccepted or signRequestRejected. No response is currently expected. The model is expected to remove the sign request item.
  • Loading branch information
alexjba committed Oct 3, 2024
1 parent 9acf8d8 commit 7c9ed97
Show file tree
Hide file tree
Showing 18 changed files with 717 additions and 466 deletions.
53 changes: 48 additions & 5 deletions storybook/pages/DAppsWorkflowPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,42 @@ Item {

spacing: 8

wcService: walletConnectService
readonly property var wcService: walletConnectService
loginType: Constants.LoginType.Biometrics
selectedAccountAddress: ""

model: wcService.dappsModel
accountsModel: wcService.validAccounts
networksModel: wcService.flatNetworks
sessionRequestsModel: wcService.sessionRequestsModel

//formatBigNumber: (number, symbol, noSymbolOption) => wcService.walletRootStore.currencyStore.formatBigNumber(number, symbol, noSymbolOption)

onDisconnectRequested: (connectionId) => wcService.disconnectDapp(connectionId)
onPairingRequested: (uri) => wcService.pair(uri)
onPairingValidationRequested: (uri) => wcService.validatePairingUri(uri)
onConnectionAccepted: (pairingId, chainIds, selectedAccount) => wcService.approvePairSession(pairingId, chainIds, selectedAccount)
onConnectionDeclined: (pairingId) => wcService.rejectPairSession(pairingId)
onSignRequestAccepted: (connectionId, requestId) => wcService.sign(connectionId, requestId)
onSignRequestRejected: (connectionId, requestId) => wcService.rejectSign(connectionId, requestId, false /*hasError*/)

Connections {
target: dappsWorkflow.wcService
function onPairingValidated(validationState) {
dappsWorkflow.pairingValidated(validationState)
}
function onApproveSessionResult(pairingId, err, newConnectionId) {
if (err) {
dappsWorkflow.connectionFailed(pairingId)
return
}

dappsWorkflow.connectionSuccessful(pairingId, newConnectionId)
}
function onConnectDApp(dappChains, dappUrl, dappName, dappIcon, pairingId) {
dappsWorkflow.connectDApp(dappChains, dappUrl, dappName, dappIcon, pairingId)
}
}
}
}
ColumnLayout {}
Expand Down Expand Up @@ -128,7 +161,7 @@ Item {
ListView {
Layout.fillWidth: true
Layout.preferredHeight: Math.min(50, contentHeight)
model: walletConnectService.requestHandler.requestsModel
model: walletConnectService.sessionRequestsModel
delegate: RowLayout {
StatusBaseText {
text: SQUtils.Utils.elideAndFormatWalletAddress(model.topic, 6, 4)
Expand Down Expand Up @@ -308,15 +341,25 @@ Item {
signal userAuthenticated(string topic, string id, string password, string pin)
signal userAuthenticationFailed(string topic, string id)
signal signingResult(string topic, string id, string data)
signal activeSessionsReceived(var activeSessionsJsonObj, bool success)

function addWalletConnectSession(sessionJson) {
console.info("Persist Session", sessionJson)

console.info("Add Persisted Session", sessionJson)
let session = JSON.parse(sessionJson)
d.updateSessionsModelAndAddNewIfNotNull(session)

return true
}

function getActiveSessions() {
console.info("Get Active Sessions")
let sessions = JSON.parse(settings.persistedSessions)
let response = sessions.map(function(session) {
return {
sessionJson: JSON.stringify(session),
}
})
activeSessionsReceived(response, true)
}

function deactivateWalletConnectSession(topic) {
console.info("Deactivate Persisted Session", topic)
Expand Down
12 changes: 12 additions & 0 deletions ui/StatusQ/src/StatusQ/Core/Utils/ModelUtils.qml
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,16 @@ QtObject {

return null
}

function forEach(model, callback) {
if (!model)
return

const count = model.rowCount()

for (let i = 0; i < count; i++) {
const modelItem = Internal.ModelUtils.get(model, i)
callback(modelItem)
}
}
}
Loading

0 comments on commit 7c9ed97

Please sign in to comment.