Don't call getCryptoModule in getSubtle

This commit is contained in:
Simon Warta 2022-12-07 18:48:55 +01:00
parent b5290390bb
commit 7b32660d08
2 changed files with 21 additions and 8 deletions

View File

@ -14,6 +14,15 @@ and this project adheres to
[#1326]: https://github.com/cosmos/cosmjs/pull/1326
### Changed
- @cosmjs/crypto: `getSubtle()` does not use `getCryptoModule()` anymore to find
a subtle implementation. Turns out all environments we support have subtle in
`globalThis` or do not have it at all ([#1307], [#1340]).
[#1307]: https://github.com/cosmos/cosmjs/pull/1307
[#1340]: https://github.com/cosmos/cosmjs/pull/1340
### Deprecated
- @cosmjs/stargate: Deprecate `QueryClient.queryUnverified` in favour of newly

View File

@ -24,14 +24,18 @@ export async function getCryptoModule(): Promise<any | undefined> {
}
export async function getSubtle(): Promise<any | undefined> {
const g: any = globalThis;
let subtle = g.crypto && g.crypto.subtle;
if (!subtle) {
const crypto = await getCryptoModule();
if (crypto && crypto.webcrypto && crypto.webcrypto.subtle) {
subtle = crypto.webcrypto.subtle;
}
}
// From Node.js 15 onwards, webcrypto is available in globalThis.
// In version 15 and 16 this was stored under the webcrypto key.
// With Node.js 17 it was moved to the same locations where browsers
// make it available.
// Loading `require("crypto")` here seems unnecessary since it only
// causes issues with bundlers and does not increase compatibility.
// Browsers and Node.js 17+
let subtle: any | undefined = (globalThis as any)?.crypto?.subtle;
// Node.js 15+
if (!subtle) subtle = (globalThis as any)?.crypto?.webcrypto?.subtle;
return subtle;
}