chore(walletconnect): better error handling for the rest of the implemented sdk calls

This commit is contained in:
Sale Djenic 2023-12-12 13:14:23 +01:00 committed by saledjenic
parent bf8b87f034
commit c41bb433d3
4 changed files with 141 additions and 159 deletions

View File

@ -371,7 +371,7 @@ Popup {
}
}
function onAuthSignMessage(message, address) {
function onAuthMessageFormated(formatedMessage, address) {
let details = ""
if (!!d.observedData.verifyContext.verified.isScam) {
details = "This website you`re trying to connect is flagged as malicious by multiple security providers.\nApproving may lead to loss of funds."
@ -385,8 +385,8 @@ Popup {
}
d.selectedAddress = address
d.authMessage = message
d.setDetailsText(`${details}\n\n${message}`)
d.authMessage = formatedMessage
d.setDetailsText(`${details}\n\n${formatedMessage}`)
d.state = d.waitingUserResponseToAuthRequest
}

View File

@ -35,7 +35,7 @@ Item {
signal sessionRequestUserAnswerResult(bool accept, string error)
signal authRequest(var request)
signal authSignMessage(string message, string address)
signal authMessageFormated(string formatedMessage, string address)
signal authRequestUserAnswerResult(bool accept, string error)
signal sessionDelete(var deletePayload)
@ -240,45 +240,31 @@ Item {
function acceptSessionRequest(topic, id, signature) {
console.debug(`WC WalletConnectSDK.wcCall.acceptSessionRequest; topic: "${topic}", id: ${id}, signature: "${signature}"`)
d.engine.runJavaScript(`wc.respondSessionRequest("${topic}", ${id}, "${signature}")`, function(result) {
console.debug(`WC WalletConnectSDK.wcCall.acceptSessionRequest; response: ${JSON.stringify(result, null, 2)}`)
if (result) {
if (!!result.error)
{
console.error("respondSessionRequest: ", result.error)
root.sessionRequestUserAnswerResult(true, result.error)
return
}
root.sessionRequestUserAnswerResult(true, result.error)
}
d.resetPairingsModel()
d.resetSessionsModel()
})
d.engine.runJavaScript(`
wc.respondSessionRequest("${topic}", ${id}, "${signature}")
.then((value) => {
wc.statusObject.onRespondSessionRequestResponse("")
})
.catch((e) => {
wc.statusObject.onRespondSessionRequestResponse(e.message)
})
`
)
}
function rejectSessionRequest(topic, id, error) {
console.debug(`WC WalletConnectSDK.wcCall.rejectSessionRequest; topic: "${topic}", id: ${id}, error: "${error}"`)
d.engine.runJavaScript(`wc.rejectSessionRequest("${topic}", ${id}, "${error}")`, function(result) {
console.debug(`WC WalletConnectSDK.wcCall.rejectSessionRequest; response: ${JSON.stringify(result, null, 2)}`)
d.resetPairingsModel()
d.resetSessionsModel()
if (result) {
if (!!result.error)
{
console.error("rejectSessionRequest: ", result.error)
root.sessionRequestUserAnswerResult(false, result.error)
return
}
root.sessionRequestUserAnswerResult(false, result.error)
}
})
d.engine.runJavaScript(`
wc.rejectSessionRequest("${topic}", ${id}, "${error}")
.then((value) => {
wc.statusObject.onRejectSessionRequestResponse("")
})
.catch((e) => {
wc.statusObject.onRejectSessionRequestResponse(e.message)
})
`
)
}
function disconnectTopic(topic) {
@ -314,16 +300,16 @@ Item {
function auth(authLink) {
console.debug(`WC WalletConnectSDK.wcCall.auth; authLink: ${authLink}`)
d.engine.runJavaScript(`wc.auth("${authLink}")`, function(result) {
console.debug(`WC WalletConnectSDK.wcCall.auth; response: ${JSON.stringify(result, null, 2)}`)
if (result) {
if (!!result.error) {
console.error("auth: ", result.error)
return
}
}
})
d.engine.runJavaScript(`
wc.auth("${authLink}")
.then((value) => {
wc.statusObject.onAuthResponse("")
})
.catch((e) => {
wc.statusObject.onAuthResponse(e.message)
})
`
)
}
function formatAuthMessage(cacaoPayload, address) {
@ -332,51 +318,38 @@ Item {
d.engine.runJavaScript(`wc.formatAuthMessage(${JSON.stringify(cacaoPayload)}, "${address}")`, function(result) {
console.debug(`WC WalletConnectSDK.wcCall.formatAuthMessage; response: ${JSON.stringify(result, null, 2)}`)
if (result) {
if (!!result.error) {
console.error("formatAuthMessage: ", result.error)
return
}
}
root.authSignMessage(result.result, address)
root.authMessageFormated(result, address)
})
}
function authApprove(authRequest, address, signature) {
console.debug(`WC WalletConnectSDK.wcCall.authApprove; authRequest: ${JSON.stringify(authRequest)}, address: ${address}, signature: ${signature}`)
d.engine.runJavaScript(`wc.approveAuth(${JSON.stringify(authRequest)}, "${address}", "${signature}")`, function(result) {
console.debug(`WC WalletConnectSDK.wcCall.approveAuth; response: ${JSON.stringify(result, null, 2)}`)
if (result) {
if (!!result.error)
{
console.error("approveAuth: ", result.error)
root.authRequestUserAnswerResult(true, result.error)
return
}
root.authRequestUserAnswerResult(true, result.error)
}
})
d.engine.runJavaScript(`
wc.approveAuth(${JSON.stringify(authRequest)}, "${address}", "${signature}")
.then((value) => {
wc.statusObject.onApproveAuthResponse("")
})
.catch((e) => {
wc.statusObject.onApproveAuthResponse(e.message)
})
`
)
}
function authReject(id, address) {
console.debug(`WC WalletConnectSDK.wcCall.authReject; id: ${id}, address: ${address}`)
d.engine.runJavaScript(`wc.rejectAuth(${id}, "${address}")`, function(result) {
console.debug(`WC WalletConnectSDK.wcCall.rejectAuth; response: ${JSON.stringify(result, null, 2)}`)
if (result) {
if (!!result.error)
{
console.error("rejectAuth: ", result.error)
root.authRequestUserAnswerResult(false, result.error)
return
}
root.authRequestUserAnswerResult(false, result.error)
}
})
d.engine.runJavaScript(`
wc.rejectAuth(${id}, "${address}")
.then((value) => {
wc.statusObject.onRejectAuthResponse("")
})
.catch((e) => {
wc.statusObject.onRejectAuthResponse(e.message)
})
`
)
}
}
@ -437,6 +410,20 @@ Item {
d.resetSessionsModel()
}
function onRespondSessionRequestResponse(error) {
console.debug(`WC WalletConnectSDK.onRespondSessionRequestResponse; error: ${error}`)
root.sessionRequestUserAnswerResult(true, error)
d.resetPairingsModel()
d.resetSessionsModel()
}
function onRejectSessionRequestResponse(error) {
console.debug(`WC WalletConnectSDK.onRejectSessionRequestResponse; error: ${error}`)
root.sessionRequestUserAnswerResult(false, error)
d.resetPairingsModel()
d.resetSessionsModel()
}
function onSessionProposal(details) {
console.debug(`WC WalletConnectSDK.onSessionProposal; details: ${JSON.stringify(details, null, 2)}`)
root.sessionProposal(details)
@ -487,6 +474,20 @@ Item {
console.debug(`WC WalletConnectSDK.onAuthRequest; details: ${JSON.stringify(details, null, 2)}`)
root.authRequest(details)
}
function onAuthResponse(error) {
console.debug(`WC WalletConnectSDK.onAuthResponse; error: ${error}`)
}
function onApproveAuthResponse(error) {
console.debug(`WC WalletConnectSDK.onApproveAuthResponse; error: ${error}`)
root.authRequestUserAnswerResult(true, error)
}
function onRejectAuthResponse(error) {
console.debug(`WC WalletConnectSDK.onRejectAuthResponse; error: ${error}`)
root.authRequestUserAnswerResult(false, error)
}
}
ListModel {

File diff suppressed because one or more lines are too long

View File

@ -117,10 +117,12 @@ window.wc = {
},
disconnect: async function (topic) {
await window.wc.web3wallet.disconnectSession({
topic,
reason: getSdkError('USER_DISCONNECTED')
});
await window.wc.web3wallet.disconnectSession(
{
topic,
reason: getSdkError('USER_DISCONNECTED')
}
);
},
ping: async function (topic) {
@ -132,112 +134,91 @@ window.wc = {
const { relays } = params
const approvedNamespaces = buildApprovedNamespaces({
proposal: params,
supportedNamespaces: supportedNamespaces,
});
const approvedNamespaces = buildApprovedNamespaces(
{
proposal: params,
supportedNamespaces: supportedNamespaces,
}
);
await window.wc.web3wallet.approveSession({
await window.wc.web3wallet.approveSession(
{
id,
relayProtocol: relays[0].protocol,
namespaces: approvedNamespaces,
});
}
);
},
rejectSession: async function (id) {
await window.wc.web3wallet.rejectSession({
await window.wc.web3wallet.rejectSession(
{
id,
reason: getSdkError("USER_REJECTED"), // TODO USER_REJECTED_METHODS, USER_REJECTED_CHAINS, USER_REJECTED_EVENTS
});
}
);
},
auth: function (uri) {
try {
return {
result: window.wc.authClient.core.pairing.pair({ uri }),
error: ""
};
} catch (e) {
return {
result: "",
error: e
};
}
auth: async function (uri) {
await window.wc.authClient.core.pairing.pair({ uri });
},
formatAuthMessage: function (cacaoPayload, address) {
const iss = `did:pkh:eip155:1:${address}`;
return {
result: window.wc.authClient.formatMessage(cacaoPayload, iss),
error: ""
};
return window.wc.authClient.formatMessage(cacaoPayload, iss);
},
approveAuth: function (authRequest, address, signature) {
const { id, params } = authRequest;
approveAuth: async function (authRequest, address, signature) {
const { id } = authRequest;
const iss = `did:pkh:eip155:1:${address}`;
const message = window.wc.authClient.formatMessage(params.cacaoPayload, iss);
return {
result: window.wc.authClient.respond(
{
id: id,
signature: {
s: signature,
t: "eip191",
},
await window.wc.authClient.respond(
{
id: id,
signature: {
s: signature,
t: "eip191",
},
iss),
error: ""
};
},
iss
);
},
rejectAuth: function (id, address) {
rejectAuth: async function (id, address) {
const iss = `did:pkh:eip155:1:${address}`;
return {
result: window.wc.authClient.respond(
{
id: id,
error: {
code: 4001,
message: 'Auth request has been rejected'
},
await window.wc.authClient.respond(
{
id: id,
error: {
code: 4001,
message: 'Auth request has been rejected'
},
iss),
error: ""
};
},
iss
);
},
respondSessionRequest: function (topic, id, signature) {
respondSessionRequest: async function (topic, id, signature) {
const response = formatJsonRpcResult(id, signature)
try {
let r = window.wc.web3wallet.respondSessionRequest({ topic: topic, response: response });
return {
result: r,
error: ""
};
} catch (e) {
return {
result: "",
error: e
};
}
await window.wc.web3wallet.respondSessionRequest(
{
topic: topic,
response: response
}
);
},
rejectSessionRequest: function (topic, id, error = false) {
rejectSessionRequest: async function (topic, id, error = false) {
const errorType = error ? "SESSION_SETTLEMENT_FAILED" : "USER_REJECTED";
return {
result: window.wc.web3wallet.respondSessionRequest({
await window.wc.web3wallet.respondSessionRequest(
{
topic: topic,
response: formatJsonRpcError(id, getSdkError(errorType)),
}),
error: ""
};
}
);
},
};