Skip to content

Commit

Permalink
Merge pull request #1340 from cosmos/crypro-imports
Browse files Browse the repository at this point in the history
Don't call getCryptoModule in getSubtle
  • Loading branch information
webmaster128 authored Dec 7, 2022
2 parents 52ffcf5 + 7b32660 commit 3560cd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions packages/crypto/src/pbkdf2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3560cd4

Please sign in to comment.