Skip to content

Commit

Permalink
Dev prototype of how deduplication should work. TODO finish me and sq…
Browse files Browse the repository at this point in the history
…uash
  • Loading branch information
stefandunca committed Aug 1, 2024
1 parent e25f212 commit 743c98e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
35 changes: 18 additions & 17 deletions storybook/qmlTests/tests/tst_DAppsWorkflow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -694,26 +694,27 @@ Item {
compare(eip155.events.length, 2)
}

function test_activeSessionsWithKnownAccountAddresses() {
function test_getAccountsInSession() {
const account1 = accountsModel.get(0)
const account2 = accountsModel.get(1)
const chainIds = [chainsModel.get(0).chainId, chainsModel.get(1).chainId]
const knownSession = JSON.parse(Testing.formatApproveSessionResponse(chainIds, [account2.address]))
// Allow the unlikely unknown accounts to cover for the deleted accounts case
const unknownSessionWithKnownAccount = JSON.parse(Testing.formatApproveSessionResponse(chainIds, ['0x03acc', account1.address]))
const unknownSession1 = JSON.parse(Testing.formatApproveSessionResponse(chainIds, ['0x83acc']))
const unknownSession2 = JSON.parse(Testing.formatApproveSessionResponse(chainIds, ['0x12acc']))
let testSessions = {
"b536a": knownSession,
"b537b": unknownSession1,
"b538c": unknownSession2,
"b539d": unknownSessionWithKnownAccount
}
const res = DAppsHelpers.activeSessionsWithKnownAccountAddresses(testSessions)
compare(Object.keys(res).length, 4, "expected four sessions to be returned")
// Also test that order is stable
compare(res["b536a"], knownSession, "expected the known session to be returned")
compare(res["b539d"], unknownSessionWithKnownAccount, "expected the known session to be returned")

const oneAccountSession = JSON.parse(Testing.formatApproveSessionResponse(chainIds, [account2.address]))
const twoAccountsSession = JSON.parse(Testing.formatApproveSessionResponse(chainIds, ['0x03acc', account1.address]))
const duplicateAccountsSession = JSON.parse(Testing.formatApproveSessionResponse(chainIds, ['0x83acb', '0x83acb']))

const res = DAppsHelpers.getAccountsInSession(oneAccountSession)
compare(res.length, 1, "expected the only account to be returned")
compare(res[0], account2.address, "expected the only account to be the one in the session")

const res2 = DAppsHelpers.getAccountsInSession(twoAccountsSession)
compare(res2.length, 2, "expected the two accounts to be returned")
compare(res2[0], '0x03acb', "expected the first account to be the one in the session")
compare(res2[1], account1.address, "expected the second account to be the one in the session")

const res3 = DAppsHelpers.getAccountsInSession(duplicateAccountsSession)
compare(res3.length, 1, "expected the only account to be returned")
compare(res3[0], '0x83acc', "expected the duplicated account")
}
}

Expand Down
29 changes: 16 additions & 13 deletions ui/app/AppLayouts/Wallet/services/dapps/DAppsListProvider.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ QObject {
readonly property SortFilterProxyModel dappsModel: SortFilterProxyModel {
objectName: "DAppsModelFiltered"
sourceModel: d.dappsModel.count ? d.dappsModel : null
filters: ValueFilter {
filters: FastExpressionFilter {
enabled: !!root.selectedAddress
expression: accountAddresses.contains(root.selectedAddress)

value: root.selectedAddress
roleName: "address"
expectedRoles: "accountAddresses"
}
}

Expand Down Expand Up @@ -68,9 +68,8 @@ QObject {
sdk.getActiveSessions((allSessions) => {
root.store.dappsListReceived.disconnect(dappsListReceivedFn);

const dAppsPerAddressList = []
const dAppsMap = []
const topics = []
const sessions = DAppsHelpers.activeSessionsWithKnownAccountAddresses(allSessions)
for (const sessionID in sessions) {
const session = sessions[sessionID]
const dapp = session.peer.metadata
Expand All @@ -79,20 +78,24 @@ QObject {
} else {
dapp.iconUrl = ""
}
for(const addressEntry of dapp.accountAddresses) {
let dAppWithSingleAddress = JSON.parse(JSON.stringify(dapp))
dAppWithSingleAddress.address = addressEntry.address
delete dAppWithSingleAddress.accountAddresses
dAppsPerAddressList.push(dAppWithSingleAddress)
const accounts = DAppsHelpers.getAccountsInSession(session)
const existingDApp = dAppsMap[dapp.url]
if (existingDApp) {
existingDApp.accountAddresses = [...new Set([...existingDApp.accountAddresses, ...dapp.accountAddresses])];
} else {
dapp.accountAddresses = accounts
dAppsMap[dapp.url] = dapp
}

topics.push(sessionID)
}

// TODO #15075: on SDK dApps refresh update the model that has data source from persistence instead of using reset
dapps.clear();
// Iterate tmpMap and fill dapps
for (const item of dAppsPerAddressList) {
dapps.append(item);

// Iterate dAppsMap and fill dapps
for (const topic in dAppsMap) {
dapps.append(dAppsMap[topic]);
}

root.store.updateWalletConnectSessions(JSON.stringify(topics))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ QObject {

function disconnectDapp(url) {
wcSDK.getActiveSessions((allSessions) => {
// TODO: DEV
const sessions = DAppsHelpers.activeSessionsWithKnownAccountAddresses(allSessions)
for (const sessionID in sessions) {
const session = sessions[sessionID]
const dapp = session.peer.metadata
const topic = session.topic
if (dapp.url === url) {
if (!dappsProvider.selectedAddress ||
(dapp.accountAddresses.map(entry => entry.address).includes(dappsProvider.selectedAddress))) {
(dapp.accountAddresses.includes(dappsProvider.selectedAddress)))
{
wcSDK.disconnectSession(topic)
}
}
Expand Down
21 changes: 7 additions & 14 deletions ui/app/AppLayouts/Wallet/services/dapps/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,11 @@ function extractInfoFromPairUri(uri) {
return { topic, expiry }
}

function activeSessionsWithKnownAccountAddresses(sessions) {
let knownSessions = ({})
Object.keys(sessions).forEach((topic) => {
const session = sessions[topic]
const eip155Addresses = session.namespaces.eip155.accounts
const accountSet = new Set(
eip155Addresses.map(eip155Address => eip155Address.split(':').pop().trim())
);
const uniqueAddresses = Array.from(accountSet);

session.peer.metadata.accountAddresses = uniqueAddresses.map(value => ({ address: value }));
knownSessions[topic] = session
})
return knownSessions
function getAccountsInSession(session) {
const eip155Addresses = session.namespaces.eip155.accounts
const accountSet = new Set(
eip155Addresses.map(eip155Address => eip155Address.split(':').pop().trim())
);
const uniqueAddresses = Array.from(accountSet);
return uniqueAddresses
}

0 comments on commit 743c98e

Please sign in to comment.