paulmillr/noble-hashes

`isPlainObject` check fails in serverless env

aulneau opened this issue · 8 comments

When trying to use some of these hashing functions in the context of a serverless environment, such as vercel or cloudfare, this function

const isPlainObject = (obj: any) =>

fails to accept this object {c:2048,dkLen:64} as a plain object.

I have followed it down to this line:

obj.constructor === Object never being true. I'd suggest something like this instead:

obj.constructor.name === 'Object'

ah, applies to btc-signer, too:

Wrong object type for transaction options: [object Object]

It's most likely the case that there are different Object instances in these environments, which is why the check is failing

I think constructor.name was unreliable 10 years ago. Isn't it still? What about minification? Are you confident the solution would work, did you test it?

We have other checks, like: a instanceof Uint8Array, which, I assume, would also fail in the scenario.

I have tested it: https://github.com/aulneau/noble-edge-bug/blob/main/src/pages/api/index.ts

Here is a reproduction. I'm not familiar with the reliability of constructor.name or not unfortunately.

@paulmillr anything i can do to help move this on?

I think we can remove obj.constructor === Object check. Object.prototype.toString.call is already done. That would make it return different result for ({__proto__: []}), but if tests pass, I think it could work.

The function is used in only a few places.

@aulneau fixed!