paulmillr / noble-hashes

Audited & minimal JS implementation of hash functions, MACs and KDFs.
https://paulmillr.com/noble
MIT License
555 stars 46 forks source link

Build error "Cannot find name 'Crypto'" #9

Closed webmaster128 closed 2 years ago

webmaster128 commented 2 years ago

Heyho! Thank you for this initiative! It's amazing to see hashing implementations without Buffer dependencies.

I'd love to get this into CosmJS to replace first ripemd160 and maybe sha.js and js-sha3. When I tried doing this, I got the following compile error:

./../.yarn/cache/@noble-hashes-npm-0.4.1-4637b511e4-7d06789f41.zip/node_modules/@noble/hashes/lib/utils.d.ts:49:11 - error TS2304: Cannot find name 'Crypto'.

49     web?: Crypto;
             ~~~~~~

Found 1 error.

Seems like my tsconfig does not have the DOM lib. Is this enabled by default in your tsconfig?

Would it be possible to avoid the dependency on those types or pull them in explicitly?

paulmillr commented 2 years ago

Not sure i'm following. window.crypto does not require dom. It has been the part of node.js for a couple releases. Seems like you're using old node types or sth?

webmaster128 commented 2 years ago

Looking at the latest @types/node, there is

    namespace webcrypto {
        class CryptoKey {} // placeholder
    }

but no symbol called Crypto. When I open this library in an IDE, it shows lib.dom.d.ts is the source of the symbol:

Bildschirmfoto 2021-11-18 um 09 03 35 Bildschirmfoto 2021-11-18 um 09 04 05
paulmillr commented 2 years ago

Not sure how we can solve it. Also the same code resides in other noble packages.

paulmillr commented 2 years ago

Perhaps, declaring web as any would help

webmaster128 commented 2 years ago

Yeah, making it any would be the easiest fix.

There is some problem description of this in https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#supporting-lib-from-node_modules

It is hard to customize these files to match your needs with the needs of your project’s dependencies (e.g. if your dependencies declare that they use the DOM APIs, you might also be forced into using the DOM APIs).

And a dependency on https://www.npmjs.com/package/@types/web would potentially help. But I guess this adds a lot of overhead.

paulmillr commented 2 years ago

84f3d35df797a6e3bd23ad90897a45b90a47382b