bitlogik / pyWalletConnect

WalletConnect implementation for Python wallets
GNU General Public License v3.0
47 stars 15 forks source link

Sample to Reject a Transaction? #16

Closed wayne-yu-anchorage closed 1 year ago

wayne-yu-anchorage commented 1 year ago

Hi @bitlogik Do you have any code sample, or any pointers on how a Wallet could reject a transaction from DApp? I've tried returning

{
 id: 123
 jsonrpc: "2.0"
 error: {
   code: 5002 // also tried -32000
   message: "User rejected methods."
 }
}

but it doesn't work (tried it with Snapshot). The error code/message above is based on what WalletConnect's own web-examples

Thanks 🙏

algolog commented 1 year ago

I think replies with JSON-RPC Error object are not supported yet. My solution would be something like this with usage like

WC_ERROR_REJECTED = 5002

def wcerr_obj(code: int):
    """Constuct object for JSON-RPC error reply"""
    msg = {
        WC_ERROR_REJECTED: 'User Rejected Request',
    }
    return {'code': code, 'message': msg[code]}

wclient.reply(request_id, wcerr_obj(WC_ERROR_REJECTED), res_type="error")
wayne-yu-anchorage commented 1 year ago

I think replies with JSON-RPC Error object are not supported yet. My solution would be something like this with usage like

Thank you @algolog . (I think _restype is a typo - should be _resptype? Also, the reply API current doesn't take a _resptype but I think I got what you're saying.)

I actually have tried exactly this: send a response with this payload:

{
  id: 123 // matching what
  jsonrpc: "2.0"
  error: {
    code: 5002 // or 5000
    message: "Rejected by user"
  }
}

I also tried with code=-32000 and message="User rejected methods." (according to my reading of how react-web3wallet implements Rejection)

algolog commented 1 year ago

In the corresponding pull request I propose this syntax for responding with error: wclient.reply(request_id, {'code': 5002, 'message': 'Rejected by user'}, as_error=True)

bitlogik commented 1 year ago

The rational for pyWalletConnect was the best way to reject a query is to ignore it and be silent. So it only sends a message back when it's granted and processed successfully. Declining a message was perceived as optional. There's currently no way to send an error or telling the dapp the query was rejected (not approved). Even the protocol treats a rejection as an error. Still, regarding the protocol, it's better indeed to reply with the right message in any case, to inform the dapp specifically that the user declined the request. Else (in the current case), the dapp just knows that the user has not yet accepted the request.

To be clear, this limitation was mostly present because of lack of development time; This rejection feature was not perceived as important, relatively to the others core features needed. But now this library becomes more mature, it's time to add this feature to adhere the more possible to the WC protocol. Luckily, there's a PR opened by @algolog for this. Thanks @algolog !

bitlogik commented 1 year ago

@wayne-yu-anchorage Maybe the issue is related to IRN metadata tags in messages lower in the communication stack ? One way to investigate is to sneak what happens using the Dev Console (console and network) when you use the official wallet https://react-wallet.walletconnect.com/ and the official demo dapp https://react-app.walletconnect.com/. The development of this library mostly relied on this kind of reverse-engineering technique, rather that studying the standard (it's not what we call a standard, at least in term of engineering. We even imagine at some point that the WalletConnect private company does its best to obscucify the protocol to avoid having multiple implementations).

bitlogik commented 1 year ago

Fixed by d85b690 Version about to be released v1.5.0 is going have this. The gist demo referred by the ReadMe was updated https://gist.github.com/bitlogik/89b41bb60443c041704f82bcd9b43901