feat(dapps) update persistance from the SDK on sessions refresh

Updates: #15189
This commit is contained in:
Stefan 2024-06-27 17:15:17 +03:00 committed by Stefan Dunca
parent 94dc7b04a5
commit dbd96133a5
7 changed files with 98 additions and 2 deletions

View File

@ -30,6 +30,9 @@ QtObject:
proc deactivateWalletConnectSession*(self: Controller, topic: string): bool {.slot.} = proc deactivateWalletConnectSession*(self: Controller, topic: string): bool {.slot.} =
return self.service.deactivateSession(topic) return self.service.deactivateSession(topic)
proc updateSessionsMarkedAsActive*(self: Controller, activeTopicsJson: string) {.slot.} =
self.service.updateSessionsMarkedAsActive(activeTopicsJson)
proc dappsListReceived*(self: Controller, dappsJson: string) {.signal.} proc dappsListReceived*(self: Controller, dappsJson: string) {.signal.}
# Emits signal dappsListReceived with the list of dApps # Emits signal dappsListReceived with the list of dApps

View File

@ -71,9 +71,38 @@ QtObject:
# TODO #14588: call it async # TODO #14588: call it async
return status_go.addSession(session_json) return status_go.addSession(session_json)
proc addSession*(self: Service, topic: string): bool = proc deactivateSession*(self: Service, topic: string): bool =
# TODO #14588: call it async # TODO #14588: call it async
return status_go.deactivateSession(topic) return status_go.disconnectSession(topic)
proc updateSessionsMarkedAsActive*(self: Service, activeTopicsJson: string) =
# TODO #14588: call it async
let activeTopicsJN = parseJson(activeTopicsJson)
if activeTopicsJN.kind != JArray:
error "invalid array of json strings"
return
var activeTopics = newSeq[string]()
for i in 0 ..< activeTopicsJN.len:
if activeTopicsJN[i].kind != JString:
error "bad topic entry at", i
return
activeTopics.add(activeTopicsJN[i].getStr())
let sessions = status_go.getActiveSessions(0)
if sessions.isNil:
error "failed to get active sessions"
return
for session in sessions:
if session.kind != JObject or not session.hasKey("topic"):
error "unexpected session object"
continue
let topic = session["topic"].getStr()
if not activeTopics.contains(topic):
if not status_go.disconnectSession(topic):
error "failed to mark session as disconnected", topic
proc getDapps*(self: Service): string = proc getDapps*(self: Service): string =
let validAtEpoch = now().toTime().toUnix() let validAtEpoch = now().toTime().toUnix()

View File

@ -15,6 +15,12 @@ import app_service/common/utils
rpc(addWalletConnectSession, "wallet"): rpc(addWalletConnectSession, "wallet"):
sessionJson: string sessionJson: string
rpc(disconnectWalletConnectSession, "wallet"):
topic: string
rpc(getWalletConnectActiveSessions, "wallet"):
validAtTimestamp: int
rpc(signTypedDataV4, "wallet"): rpc(signTypedDataV4, "wallet"):
typedJson: string typedJson: string
address: string address: string
@ -31,6 +37,33 @@ proc addSession*(sessionJson: string): bool =
warn "AddWalletConnectSession failed: ", "msg", e.msg warn "AddWalletConnectSession failed: ", "msg", e.msg
return false return false
proc disconnectSession*(topic: string): bool =
try:
let rpcRes = disconnectWalletConnectSession(topic)
return isSuccessResponse(rpcRes):
except Exception as e:
warn "wallet_disconnectWalletConnectSession failed: ", "msg", e.msg
return false
proc getActiveSessions*(validAtTimestamp: int): JsonNode =
try:
let rpcRes = getWalletConnectActiveSessions(validAtTimestamp)
if(not isSuccessResponse(rpcRes)):
return nil
let jsonResultStr = rpcRes.result.getStr()
if jsonResultStr == "null":
return nil
if rpcRes.result.kind != JArray:
error "Unexpected result kind: ", rpcRes.result.kind
return nil
return rpcRes.result
except Exception as e:
warn "GetWalletConnectActiveSessions failed: ", "msg", e.msg
return nil
proc getDapps*(validAtEpoch: int64, testChains: bool): string = proc getDapps*(validAtEpoch: int64, testChains: bool): string =
try: try:
let params = %*[validAtEpoch, testChains] let params = %*[validAtEpoch, testChains]

View File

@ -317,6 +317,20 @@ Item {
return true return true
} }
function updateWalletConnectSessions(activeTopicsJson) {
console.info("Update Persisted Sessions", activeTopicsJson)
let activeTopics = JSON.parse(activeTopicsJson)
let sessions = JSON.parse(settings.persistedSessions)
let newSessions = sessions.filter(function(session) {
return activeTopics.includes(session.topic)
})
settings.persistedSessions = JSON.stringify(newSessions)
d.updateSessionsModelAndAddNewIfNotNull(null)
return true
}
function getDapps() { function getDapps() {
let dappsJson = JSON.stringify(d.persistedDapps) let dappsJson = JSON.stringify(d.persistedDapps)
this.dappsListReceived(dappsJson) this.dappsListReceived(dappsJson)

View File

@ -110,6 +110,11 @@ Item {
function signTypedDataV4(topic, id, address, password, message) { function signTypedDataV4(topic, id, address, password, message) {
signTypedDataV4Calls.push({topic, id, address, password, message}) signTypedDataV4Calls.push({topic, id, address, password, message})
} }
property var updateWalletConnectSessionsCalls: []
function updateWalletConnectSessions(activeTopicsJson) {
updateWalletConnectSessionsCalls.push({activeTopicsJson})
}
} }
} }
@ -409,6 +414,10 @@ Item {
callback({"b536a": session, "b537b": session}) callback({"b536a": session, "b537b": session})
compare(provider.dappsModel.count, 1, "expected dappsModel have the SDK's reported dapps") compare(provider.dappsModel.count, 1, "expected dappsModel have the SDK's reported dapps")
compare(provider.dappsModel.get(0).iconUrl, "", "expected iconUrl to be missing") compare(provider.dappsModel.get(0).iconUrl, "", "expected iconUrl to be missing")
let updateCalls = provider.store.updateWalletConnectSessionsCalls
compare(updateCalls.length, 1, "expected a call to store.updateWalletConnectSessions")
verify(updateCalls[0].activeTopicsJson.includes("b536a"))
verify(updateCalls[0].activeTopicsJson.includes("b537b"))
} }
} }

View File

@ -50,6 +50,7 @@ QObject {
root.store.dappsListReceived.disconnect(dappsListReceivedFn); root.store.dappsListReceived.disconnect(dappsListReceivedFn);
let tmpMap = {} let tmpMap = {}
var topics = []
for (let key in sessions) { for (let key in sessions) {
let dapp = sessions[key].peer.metadata let dapp = sessions[key].peer.metadata
if (!!dapp.icons && dapp.icons.length > 0) { if (!!dapp.icons && dapp.icons.length > 0) {
@ -58,6 +59,7 @@ QObject {
dapp.iconUrl = "" dapp.iconUrl = ""
} }
tmpMap[dapp.url] = dapp; tmpMap[dapp.url] = dapp;
topics.push(key)
} }
// TODO #14755: on SDK dApps refresh update the model that has data source from persistence instead of using reset // TODO #14755: on SDK dApps refresh update the model that has data source from persistence instead of using reset
dapps.clear(); dapps.clear();
@ -65,6 +67,8 @@ QObject {
for (let key in tmpMap) { for (let key in tmpMap) {
dapps.append(tmpMap[key]); dapps.append(tmpMap[key]);
} }
root.store.updateWalletConnectSessions(JSON.stringify(topics))
}); });
} }

View File

@ -20,6 +20,10 @@ QObject {
return controller.deactivateWalletConnectSession(topic) return controller.deactivateWalletConnectSession(topic)
} }
function updateWalletConnectSessions(activeTopicsJson) {
return controller.updateSessionsMarkedAsActive(activeTopicsJson)
}
function authenticateUser(topic, id, address) { function authenticateUser(topic, id, address) {
let ok = controller.authenticateUser(topic, id, address) let ok = controller.authenticateUser(topic, id, address)
if(!ok) { if(!ok) {