ceramicnetwork / ceramicnetwork/js-did

`safeSend` Uses A Callback Innappropriately

Open
#194 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
103
Forks
29
PR merge metrics
No merged PRs in 30d

Description

# Description

In `@didtools/pkh-ethereum`, there is a [`safeSend` function defined in the `utils` file](https://github.com/ceramicnetwork/js-did/blob/main/packages/pkh-ethereum/src/utils.ts#L5-L39) 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:

```js
… 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`](https://eips.ethereum.org/EIPS/eip-1193#sendasync-deprecated). [Regular `send` returns a promise.](https://eips.ethereum.org/EIPS/eip-1193#send-deprecated)

As expected [`ethers` v6 `send`](https://docs.ethers.org/v6/api/providers/#BrowserProvider-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:

```ts
return provider.request({ method, params }).then(
(response: any) => response,
(error: any) => { throw error },
)
```
According to the documentation, I'm pretty sure that should be:

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.