Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safeSend Uses A Callback Innappropriately #194

Open
dysbulic opened this issue Jul 25, 2024 · 0 comments
Open

safeSend Uses A Callback Innappropriately #194

dysbulic opened this issue Jul 25, 2024 · 0 comments

Comments

@dysbulic
Copy link

dysbulic commented Jul 25, 2024

Description

In @didtools/pkh-ethereum, there is a safeSend function defined in the utils file which handles performing a call to an RPC endpoint for the Ethereum blockchain. This method uses a callback to retrieve the value from the send function, which doesn't take a callback, but instead returns a promise.

Technical Information

Part of the definition of safeSend is as follows:

 if (provider.sendAsync || provider.send) {
  const sendFunc = (provider.sendAsync ? provider.sendAsync : provider.send).bind(provider);
  const request = encodeRpcMessage(method, params);
  return new Promise((resolve, reject)=>{
      sendFunc(request, (error, response)=>{
          if (error) reject(error);
          if (response.error) {
              const error = new Error(response.error.message);
              error.code = response.error.code;
              error.data = response.error.data;
              reject(error);
          }
          resolve(response.result);
      });
  });
}

The problem is that the Legacy Provider API only defines a callback for sendAsync. Regular send returns a promise.

As expected ethers v6 send follows this format and a call to getAccountId or (more importantly) EthereumWebAuth.getAuthMethod will fail silently when the call is performed and the result is returned as a promise rather than in a callback.

Additionally

The part of safeSend that uses the request method looks like:

return provider.request({ method, params }).then(
      (response: any) => response,
      (error: any) => { throw error },
    )

According to the documentation, I'm pretty sure that should be:

return provider.request({ method, params })
  .then((response: any) => response)
  .catch((error: any) => { throw error })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant