chore(walletconnect): store wallet connect sessions instead of pairings
Closes: #13008
This commit is contained in:
parent
3d408e4966
commit
969998dd3f
|
@ -30,7 +30,7 @@ QtObject:
|
|||
walletAccountService: wallet_account_service.Service
|
||||
sessionRequestJson: JsonNode
|
||||
txResponseDto: TxResponseDto
|
||||
hasActivePairings: Option[bool]
|
||||
hasActivePairings: bool
|
||||
|
||||
## Forward declarations
|
||||
proc invalidateData(self: Controller)
|
||||
|
@ -38,7 +38,13 @@ QtObject:
|
|||
proc finishSessionRequest(self: Controller, signature: string)
|
||||
proc finishAuthRequest(self: Controller, signature: string)
|
||||
|
||||
## signals
|
||||
proc requestOpenWalletConnectPopup*(self: Controller, uri: string) {.signal.}
|
||||
proc hasActivePairingsChanged*(self: Controller) {.signal.}
|
||||
proc respondSessionProposal*(self: Controller, sessionProposalJson: string, supportedNamespacesJson: string, error: string) {.signal.}
|
||||
proc respondSessionRequest*(self: Controller, sessionRequestJson: string, signedJson: string, error: bool) {.signal.}
|
||||
proc respondAuthRequest*(self: Controller, signature: string, error: bool) {.signal.}
|
||||
|
||||
|
||||
proc setup(self: Controller) =
|
||||
self.QObject.setup
|
||||
|
@ -88,9 +94,6 @@ QtObject:
|
|||
else:
|
||||
error "Unknown identifier"
|
||||
|
||||
# supportedNamespaces is a Namespace as defined in status-go: services/wallet/walletconnect/walletconnect.go
|
||||
proc respondSessionProposal*(self: Controller, sessionProposalJson: string, supportedNamespacesJson: string, error: string) {.signal.}
|
||||
|
||||
proc sessionProposal(self: Controller, sessionProposalJson: string) {.slot.} =
|
||||
var
|
||||
supportedNamespacesJson: string
|
||||
|
@ -107,27 +110,24 @@ QtObject:
|
|||
error "pairing", msg=error
|
||||
self.respondSessionProposal(sessionProposalJson, supportedNamespacesJson, error)
|
||||
|
||||
proc recordSuccessfulPairing(self: Controller, sessionProposalJson: string) {.slot.} =
|
||||
if backend_wallet_connect.recordSuccessfulPairing(sessionProposalJson):
|
||||
if not self.hasActivePairings.get(false):
|
||||
self.hasActivePairings = some(true)
|
||||
proc saveOrUpdateSession(self: Controller, sessionJson: string) {.slot.} =
|
||||
if not backend_wallet_connect.saveOrUpdateSession(sessionJson):
|
||||
error "Failed to save/update session"
|
||||
|
||||
proc deletePairing(self: Controller, topic: string) {.slot.} =
|
||||
if backend_wallet_connect.deletePairing(topic):
|
||||
if self.hasActivePairings.get(false):
|
||||
self.hasActivePairings = some(backend_wallet_connect.hasActivePairings())
|
||||
else:
|
||||
error "Failed to delete pairing"
|
||||
proc deleteSession(self: Controller, topic: string) {.slot.} =
|
||||
if not backend_wallet_connect.deleteSession(topic):
|
||||
error "Failed to delete session"
|
||||
|
||||
proc getHasActivePairings*(self: Controller): bool {.slot.} =
|
||||
if self.hasActivePairings.isNone:
|
||||
self.hasActivePairings = some(backend_wallet_connect.hasActivePairings())
|
||||
return self.hasActivePairings.get(false)
|
||||
return self.hasActivePairings
|
||||
|
||||
proc setHasActivePairings(self: Controller, value: bool) =
|
||||
self.hasActivePairings = value
|
||||
self.hasActivePairingsChanged()
|
||||
|
||||
QtProperty[bool] hasActivePairings:
|
||||
read = getHasActivePairings
|
||||
|
||||
proc respondSessionRequest*(self: Controller, sessionRequestJson: string, signedJson: string, error: bool) {.signal.}
|
||||
notify = hasActivePairingsChanged
|
||||
|
||||
proc sendTransactionAndRespond(self: Controller, signature: string) =
|
||||
let finalSignature = singletonInstance.utils.removeHexPrefix(signature)
|
||||
|
@ -213,8 +213,6 @@ QtObject:
|
|||
let jsonObj = % self.walletAccountService.getWalletAccounts()
|
||||
return $jsonObj
|
||||
|
||||
proc respondAuthRequest*(self: Controller, signature: string, error: bool) {.signal.}
|
||||
|
||||
proc finishAuthRequest(self: Controller, signature: string) =
|
||||
if signature.len == 0:
|
||||
self.respondAuthRequest("", true)
|
||||
|
|
|
@ -26,16 +26,13 @@ rpc(wCSendTransactionWithSignature, "wallet"):
|
|||
rpc(wCPairSessionProposal, "wallet"):
|
||||
sessionProposalJson: string
|
||||
|
||||
rpc(wCRecordSuccessfulPairing, "wallet"):
|
||||
sessionProposalJson: string
|
||||
rpc(wCSaveOrUpdateSession, "wallet"):
|
||||
sessionJson: string
|
||||
|
||||
rpc(wCChangePairingState, "wallet"):
|
||||
rpc(wCChangeSessionState, "wallet"):
|
||||
topic: string
|
||||
active: bool
|
||||
|
||||
rpc(wCHasActivePairings, "wallet"):
|
||||
discard
|
||||
|
||||
rpc(wCSessionRequest, "wallet"):
|
||||
sessionRequestJson: string
|
||||
|
||||
|
@ -63,32 +60,22 @@ proc pair*(res: var JsonNode, sessionProposalJson: string): string =
|
|||
warn e.msg
|
||||
return e.msg
|
||||
|
||||
proc recordSuccessfulPairing*(sessionProposalJson: string): bool =
|
||||
proc saveOrUpdateSession*(sessionJson: string): bool =
|
||||
try:
|
||||
let response = wCRecordSuccessfulPairing(sessionProposalJson)
|
||||
let response = wCSaveOrUpdateSession(sessionJson)
|
||||
return not isErrorResponse(response)
|
||||
except Exception as e:
|
||||
warn e.msg
|
||||
return false
|
||||
|
||||
proc deletePairing*(topic: string): bool =
|
||||
proc deleteSession*(topic: string): bool =
|
||||
try:
|
||||
let response = wCChangePairingState(topic, false)
|
||||
let response = wCChangeSessionState(topic, false)
|
||||
return not isErrorResponse(response)
|
||||
except Exception as e:
|
||||
warn e.msg
|
||||
return false
|
||||
|
||||
proc hasActivePairings*(): bool =
|
||||
try:
|
||||
let response = wCHasActivePairings()
|
||||
if isErrorResponse(response):
|
||||
return false
|
||||
return response.result.getBool()
|
||||
except Exception as e:
|
||||
warn e.msg
|
||||
return false
|
||||
|
||||
proc sessionRequest*(res: var JsonNode, sessionRequestJson: string): string =
|
||||
try:
|
||||
let response = wCSessionRequest(sessionRequestJson)
|
||||
|
|
|
@ -23,7 +23,7 @@ StatusListItem {
|
|||
title: account.name
|
||||
subTitle: {
|
||||
const elidedAddress = StatusQUtils.Utils.elideText(account.address,6,4)
|
||||
let chainShortNames = root.getNetworkShortNames(model.account.preferredSharingChainIds)
|
||||
let chainShortNames = root.getNetworkShortNames(account.preferredSharingChainIds)
|
||||
return sensor.containsMouse ? WalletUtils.colorizedChainPrefix(chainShortNames) + Utils.richColorText(elidedAddress, Theme.palette.directColor1) : chainShortNames + elidedAddress
|
||||
}
|
||||
asset.color: !!account.colorId ? Utils.getColorForId(account.colorId): ""
|
||||
|
|
|
@ -28,9 +28,6 @@ Item {
|
|||
onSessionRequestEvent: (details) => {
|
||||
modal.openWithSessionRequestEvent(details)
|
||||
}
|
||||
onSessionDelete: (deletePayload) => {
|
||||
root.controller.deletePairing(deletePayload.topic)
|
||||
}
|
||||
}
|
||||
|
||||
Connections {
|
||||
|
|
|
@ -208,7 +208,7 @@ Popup {
|
|||
model: root.sdk.sessionsModel
|
||||
|
||||
onDisconnect: function (topic) {
|
||||
root.sdk.disconnectTopic(topic)
|
||||
root.sdk.disconnectSession(topic)
|
||||
}
|
||||
|
||||
onPing: function (topic) {
|
||||
|
@ -227,7 +227,7 @@ Popup {
|
|||
model: root.sdk.pairingsModel
|
||||
|
||||
onDisconnect: function (topic) {
|
||||
root.sdk.disconnectTopic(topic)
|
||||
root.sdk.disconnectPairing(topic)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -292,6 +292,16 @@ Popup {
|
|||
root.controller.sessionProposal(JSON.stringify(sessionProposal))
|
||||
}
|
||||
|
||||
function onSessionDelete(topic, error) {
|
||||
if (!!error) {
|
||||
d.setStatusText(`Error deleting session: ${error}`, "red")
|
||||
d.setDetailsText("")
|
||||
return
|
||||
}
|
||||
|
||||
root.controller.deleteSession(topic)
|
||||
}
|
||||
|
||||
function onSessionRequestEvent(sessionRequest) {
|
||||
d.setStatusText("Approve session request")
|
||||
d.setDetailsText(JSON.stringify(sessionRequest, null, 2))
|
||||
|
@ -299,12 +309,15 @@ Popup {
|
|||
root.state = d.waitingUserResponseToSessionRequest
|
||||
}
|
||||
|
||||
function onApproveSessionResult(sessionProposal, error) {
|
||||
function onApproveSessionResult(session, error) {
|
||||
d.setDetailsText("")
|
||||
if (!error) {
|
||||
d.setStatusText("Pairing OK")
|
||||
d.state = d.pairedState
|
||||
root.controller.recordSuccessfulPairing(JSON.stringify(sessionProposal))
|
||||
|
||||
root.sdk.getActiveSessions((activeSession) => {
|
||||
root.controller.saveOrUpdateSession(JSON.stringify(session))
|
||||
})
|
||||
} else {
|
||||
d.setStatusText("Pairing error", "red")
|
||||
d.state = ""
|
||||
|
|
|
@ -29,7 +29,7 @@ Item {
|
|||
signal sdkInit(bool success, var result)
|
||||
signal sessionProposal(var sessionProposal)
|
||||
signal sessionProposalExpired()
|
||||
signal approveSessionResult(var sessionProposal, string error)
|
||||
signal approveSessionResult(var session, string error)
|
||||
signal rejectSessionResult(string error)
|
||||
signal sessionRequestEvent(var sessionRequest)
|
||||
signal sessionRequestUserAnswerResult(bool accept, string error)
|
||||
|
@ -38,14 +38,22 @@ Item {
|
|||
signal authMessageFormated(string formatedMessage, string address)
|
||||
signal authRequestUserAnswerResult(bool accept, string error)
|
||||
|
||||
signal sessionDelete(var deletePayload)
|
||||
signal sessionDelete(var topic, string error)
|
||||
|
||||
function pair(pairLink) {
|
||||
wcCalls.pair(pairLink)
|
||||
}
|
||||
|
||||
function disconnectTopic(topic) {
|
||||
wcCalls.disconnectTopic(topic)
|
||||
function getActiveSessions(callback) {
|
||||
wcCalls.getActiveSessions(callback)
|
||||
}
|
||||
|
||||
function disconnectSession(topic) {
|
||||
wcCalls.disconnectSession(topic)
|
||||
}
|
||||
|
||||
function disconnectPairing(topic) {
|
||||
wcCalls.disconnectPairing(topic)
|
||||
}
|
||||
|
||||
function ping(topic) {
|
||||
|
@ -184,7 +192,11 @@ Item {
|
|||
console.debug(`WC WalletConnectSDK.wcCall.getActiveSessions;`)
|
||||
|
||||
d.engine.runJavaScript(`wc.getActiveSessions()`, function(result) {
|
||||
console.debug(`WC WalletConnectSDK.wcCall.getActiveSessions; result: ${JSON.stringify(result, null, 2)}`)
|
||||
let allSessions = ""
|
||||
for (var key of Object.keys(result)) {
|
||||
allSessions += `\nsessionTopic: ${key} relatedPairingTopic: ${result[key].pairingTopic}`;
|
||||
}
|
||||
console.debug(`WC WalletConnectSDK.wcCall.getActiveSessions; result: ${allSessions}`)
|
||||
|
||||
if (callback && result) {
|
||||
callback(result)
|
||||
|
@ -212,11 +224,11 @@ Item {
|
|||
|
||||
d.engine.runJavaScript(`
|
||||
wc.approveSession(${JSON.stringify(sessionProposal)}, ${JSON.stringify(supportedNamespaces)})
|
||||
.then((value) => {
|
||||
wc.statusObject.onApproveSessionResponse(${JSON.stringify(sessionProposal)}, "")
|
||||
.then((session) => {
|
||||
wc.statusObject.onApproveSessionResponse(session, "")
|
||||
})
|
||||
.catch((e) => {
|
||||
wc.statusObject.onApproveSessionResponse(${JSON.stringify(sessionProposal)}, e.message)
|
||||
wc.statusObject.onApproveSessionResponse("", e.message)
|
||||
})
|
||||
`
|
||||
)
|
||||
|
@ -267,16 +279,31 @@ Item {
|
|||
)
|
||||
}
|
||||
|
||||
function disconnectTopic(topic) {
|
||||
console.debug(`WC WalletConnectSDK.wcCall.disconnectTopic; topic: "${topic}"`)
|
||||
function disconnectSession(topic) {
|
||||
console.debug(`WC WalletConnectSDK.wcCall.disconnectSession; topic: "${topic}"`)
|
||||
|
||||
d.engine.runJavaScript(`
|
||||
wc.disconnect("${topic}")
|
||||
.then((value) => {
|
||||
wc.statusObject.onDisconnectResponse("")
|
||||
.then(() => {
|
||||
wc.statusObject.onDisconnectSessionResponse("${topic}", "")
|
||||
})
|
||||
.catch((e) => {
|
||||
wc.statusObject.onDisconnectResponse(e.message)
|
||||
wc.statusObject.onDisconnectSessionResponse("", e.message)
|
||||
})
|
||||
`
|
||||
)
|
||||
}
|
||||
|
||||
function disconnectPairing(topic) {
|
||||
console.debug(`WC WalletConnectSDK.wcCall.disconnectPairing; topic: "${topic}"`)
|
||||
|
||||
d.engine.runJavaScript(`
|
||||
wc.disconnect("${topic}")
|
||||
.then(() => {
|
||||
wc.statusObject.onDisconnectPairingResponse("${topic}", "")
|
||||
})
|
||||
.catch((e) => {
|
||||
wc.statusObject.onDisconnectPairingResponse("", e.message)
|
||||
})
|
||||
`
|
||||
)
|
||||
|
@ -384,23 +411,22 @@ Item {
|
|||
console.debug(`WC WalletConnectSDK.onPingResponse; error: ${error}`)
|
||||
}
|
||||
|
||||
function onDisconnectResponse(error) {
|
||||
console.debug(`WC WalletConnectSDK.onDisconnectResponse; error: ${error}`)
|
||||
d.resetPairingsModel()
|
||||
function onDisconnectSessionResponse(topic, error) {
|
||||
console.debug(`WC WalletConnectSDK.onDisconnectSessionResponse; topic: ${topic}, error: ${error}`)
|
||||
d.resetSessionsModel()
|
||||
root.sessionDelete(topic, error)
|
||||
}
|
||||
|
||||
function onApproveSessionResponse(sessionProposal, error) {
|
||||
console.debug(`WC WalletConnectSDK.onApproveSessionResponse; sessionProposal: ${JSON.stringify(sessionProposal, null, 2)}, error: ${error}`)
|
||||
function onDisconnectPairingResponse(topic, error) {
|
||||
console.debug(`WC WalletConnectSDK.onDisconnectPairingResponse; topic: ${topic}, error: ${error}`)
|
||||
d.resetPairingsModel()
|
||||
}
|
||||
|
||||
// Update the temporary expiry with the one from the pairing
|
||||
d.resetPairingsModel((pairing) => {
|
||||
if (pairing.topic === sessionProposal.params.pairingTopic) {
|
||||
sessionProposal.params.expiry = pairing.expiry
|
||||
root.approveSessionResult(sessionProposal, error)
|
||||
}
|
||||
})
|
||||
function onApproveSessionResponse(session, error) {
|
||||
console.debug(`WC WalletConnectSDK.onApproveSessionResponse; sessionTopic: ${JSON.stringify(session, null, 2)}, error: ${error}`)
|
||||
d.resetPairingsModel()
|
||||
d.resetSessionsModel()
|
||||
root.approveSessionResult(session, error)
|
||||
}
|
||||
|
||||
function onRejectSessionResponse(error) {
|
||||
|
@ -443,7 +469,7 @@ Item {
|
|||
|
||||
function onSessionDelete(details) {
|
||||
console.debug(`WC WalletConnectSDK.onSessionDelete; details: ${JSON.stringify(details, null, 2)}`)
|
||||
root.sessionDelete(details)
|
||||
root.sessionDelete(details.topic, "")
|
||||
d.resetPairingsModel()
|
||||
d.resetSessionsModel()
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -54,47 +54,47 @@ window.wc = {
|
|||
});
|
||||
|
||||
// connect session responses https://specs.walletconnect.com/2.0/specs/clients/sign/session-events#events
|
||||
window.wc.web3wallet.on("session_proposal", async (details) => {
|
||||
window.wc.web3wallet.on("session_proposal", (details) => {
|
||||
wc.statusObject.onSessionProposal(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_update", async (details) => {
|
||||
window.wc.web3wallet.on("session_update", (details) => {
|
||||
wc.statusObject.onSessionUpdate(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_extend", async (details) => {
|
||||
window.wc.web3wallet.on("session_extend", (details) => {
|
||||
wc.statusObject.onSessionExtend(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_ping", async (details) => {
|
||||
window.wc.web3wallet.on("session_ping", (details) => {
|
||||
wc.statusObject.onSessionPing(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_delete", async (details) => {
|
||||
window.wc.web3wallet.on("session_delete", (details) => {
|
||||
wc.statusObject.onSessionDelete(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_expire", async (details) => {
|
||||
window.wc.web3wallet.on("session_expire", (details) => {
|
||||
wc.statusObject.onSessionExpire(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_request", async (details) => {
|
||||
window.wc.web3wallet.on("session_request", (details) => {
|
||||
wc.statusObject.onSessionRequest(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_request_sent", async (details) => {
|
||||
window.wc.web3wallet.on("session_request_sent", (details) => {
|
||||
wc.statusObject.onSessionRequestSent(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("session_event", async (details) => {
|
||||
window.wc.web3wallet.on("session_event", (details) => {
|
||||
wc.statusObject.onSessionEvent(details)
|
||||
});
|
||||
|
||||
window.wc.web3wallet.on("proposal_expire", async (details) => {
|
||||
window.wc.web3wallet.on("proposal_expire", (details) => {
|
||||
wc.statusObject.onProposalExpire(details)
|
||||
});
|
||||
|
||||
window.wc.authClient.on("auth_request", async (details) => {
|
||||
window.wc.authClient.on("auth_request", (details) => {
|
||||
wc.statusObject.onAuthRequest(details)
|
||||
});
|
||||
|
||||
|
@ -141,7 +141,7 @@ window.wc = {
|
|||
}
|
||||
);
|
||||
|
||||
await window.wc.web3wallet.approveSession(
|
||||
return await window.wc.web3wallet.approveSession(
|
||||
{
|
||||
id,
|
||||
relayProtocol: relays[0].protocol,
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit b52a9ce0e5f72e62086f84a6a639a6de0bfd5c7e
|
||||
Subproject commit 9dea2e8875bba654328aaf0e6a5f37c8a45a5cdd
|
Loading…
Reference in New Issue