From 2a86f6e7f67408987807d3ae6e955980bc68605d Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 25 Oct 2022 15:02:57 -0400 Subject: [PATCH 1/4] change import pattern of crypto, to allow import error to be caught and acted on. add more authz grant queries to catch up to current authz module --- packages/crypto/src/pbkdf2.ts | 21 +++++++++++-------- .../stargate/src/modules/authz/queries.ts | 18 +++++++++++++--- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/crypto/src/pbkdf2.ts b/packages/crypto/src/pbkdf2.ts index 110b16e265..82e780b336 100644 --- a/packages/crypto/src/pbkdf2.ts +++ b/packages/crypto/src/pbkdf2.ts @@ -11,9 +11,9 @@ import { sha512 as nobleSha512 } from "@noble/hashes/sha512"; */ export async function getCryptoModule(): Promise { try { - const crypto = await import("crypto"); + const crypto = await require("crypto"); // We get `Object{default: Object{}}` as a fallback when using - // `crypto: false` in Webpack 5, which we interprete as unavailable. + // `crypto: false` in Webpack 5, which we interpret as unavailable. if (typeof crypto === "object" && Object.keys(crypto).length <= 1) { return undefined; } @@ -25,14 +25,17 @@ export async function getCryptoModule(): Promise { export async function getSubtle(): Promise { 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; - } + let crypto; + if (!g.crypto) { + crypto = await getCryptoModule(); + } else { + crypto = g.crypto; + } + if (crypto && crypto.subtle) { + return crypto.subtle; + } else if (crypto.webcrypto && crypto.webcrypto.subtle) { + return crypto.webcrypto.subtle; } - return subtle; } export async function pbkdf2Sha512Subtle( diff --git a/packages/stargate/src/modules/authz/queries.ts b/packages/stargate/src/modules/authz/queries.ts index 9c548e6fe4..5c73d78e88 100644 --- a/packages/stargate/src/modules/authz/queries.ts +++ b/packages/stargate/src/modules/authz/queries.ts @@ -10,6 +10,8 @@ export interface AuthzExtension { msgTypeUrl: string, paginationKey?: Uint8Array, ) => Promise; + readonly granteeGrants: (grantee: string, paginationKey?: Uint8Array) => Promise; + readonly granterGrants: (granter: string, paginationKey?: Uint8Array) => Promise; }; } @@ -22,14 +24,24 @@ export function setupAuthzExtension(base: QueryClient): AuthzExtension { return { authz: { grants: async (granter: string, grantee: string, msgTypeUrl: string, paginationKey?: Uint8Array) => { - const response = await queryService.Grants({ + return await queryService.Grants({ granter: granter, grantee: grantee, msgTypeUrl: msgTypeUrl, pagination: createPagination(paginationKey), }); - - return response; + }, + granteeGrants: async (grantee: string, paginationKey?: Uint8Array) => { + return await queryService.GranteeGrants({ + grantee: grantee, + pagination: createPagination(paginationKey), + }); + }, + granterGrants: async (granter: string, paginationKey?: Uint8Array) => { + return await queryService.GranterGrants({ + granter: granter, + pagination: createPagination(paginationKey), + }); }, }, }; From 9832662658780ce9d14a9504edaac58b0a08aee0 Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 25 Oct 2022 18:28:17 -0400 Subject: [PATCH 2/4] remove authz queries --- packages/stargate/src/modules/authz/queries.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/packages/stargate/src/modules/authz/queries.ts b/packages/stargate/src/modules/authz/queries.ts index 5c73d78e88..9c548e6fe4 100644 --- a/packages/stargate/src/modules/authz/queries.ts +++ b/packages/stargate/src/modules/authz/queries.ts @@ -10,8 +10,6 @@ export interface AuthzExtension { msgTypeUrl: string, paginationKey?: Uint8Array, ) => Promise; - readonly granteeGrants: (grantee: string, paginationKey?: Uint8Array) => Promise; - readonly granterGrants: (granter: string, paginationKey?: Uint8Array) => Promise; }; } @@ -24,24 +22,14 @@ export function setupAuthzExtension(base: QueryClient): AuthzExtension { return { authz: { grants: async (granter: string, grantee: string, msgTypeUrl: string, paginationKey?: Uint8Array) => { - return await queryService.Grants({ + const response = await queryService.Grants({ granter: granter, grantee: grantee, msgTypeUrl: msgTypeUrl, pagination: createPagination(paginationKey), }); - }, - granteeGrants: async (grantee: string, paginationKey?: Uint8Array) => { - return await queryService.GranteeGrants({ - grantee: grantee, - pagination: createPagination(paginationKey), - }); - }, - granterGrants: async (granter: string, paginationKey?: Uint8Array) => { - return await queryService.GranterGrants({ - granter: granter, - pagination: createPagination(paginationKey), - }); + + return response; }, }, }; From ba28dd09a3efb860408c2e10e6526a2a2aa23800 Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Sun, 27 Nov 2022 17:47:51 +0100 Subject: [PATCH 3/4] extra library name into variable --- packages/crypto/src/pbkdf2.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/crypto/src/pbkdf2.ts b/packages/crypto/src/pbkdf2.ts index 82e780b336..c5b71d2563 100644 --- a/packages/crypto/src/pbkdf2.ts +++ b/packages/crypto/src/pbkdf2.ts @@ -11,7 +11,10 @@ import { sha512 as nobleSha512 } from "@noble/hashes/sha512"; */ export async function getCryptoModule(): Promise { try { - const crypto = await require("crypto"); + // HACK: Use a variable to get webpack to ignore this and cause a + // runtime error instead of build system error or fallback implementation. + const nodeCryptoPackageName = "crypto"; + const crypto = await import(nodeCryptoPackageName); // We get `Object{default: Object{}}` as a fallback when using // `crypto: false` in Webpack 5, which we interpret as unavailable. if (typeof crypto === "object" && Object.keys(crypto).length <= 1) { From e46eed678301347b605deb6c720c4a6fb270c43c Mon Sep 17 00:00:00 2001 From: Mantas Vidutis Date: Tue, 29 Nov 2022 23:30:20 +0100 Subject: [PATCH 4/4] check for global crypto in all cases --- packages/crypto/src/pbkdf2.ts | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/crypto/src/pbkdf2.ts b/packages/crypto/src/pbkdf2.ts index c5b71d2563..889e652056 100644 --- a/packages/crypto/src/pbkdf2.ts +++ b/packages/crypto/src/pbkdf2.ts @@ -10,11 +10,12 @@ import { sha512 as nobleSha512 } from "@noble/hashes/sha512"; * `undefined` in that case. */ export async function getCryptoModule(): Promise { + const g: any = globalThis; + if (g.crypto) { + return g.crypto; + } try { - // HACK: Use a variable to get webpack to ignore this and cause a - // runtime error instead of build system error or fallback implementation. - const nodeCryptoPackageName = "crypto"; - const crypto = await import(nodeCryptoPackageName); + const crypto = await require("crypto"); // We get `Object{default: Object{}}` as a fallback when using // `crypto: false` in Webpack 5, which we interpret as unavailable. if (typeof crypto === "object" && Object.keys(crypto).length <= 1) { @@ -27,13 +28,7 @@ export async function getCryptoModule(): Promise { } export async function getSubtle(): Promise { - const g: any = globalThis; - let crypto; - if (!g.crypto) { - crypto = await getCryptoModule(); - } else { - crypto = g.crypto; - } + const crypto = await getCryptoModule(); if (crypto && crypto.subtle) { return crypto.subtle; } else if (crypto.webcrypto && crypto.webcrypto.subtle) { @@ -114,12 +109,11 @@ export async function pbkdf2Sha512( const subtle = await getSubtle(); if (subtle) { return pbkdf2Sha512Subtle(subtle, secret, salt, iterations, keylen); + } + const crypto = await getCryptoModule(); + if (crypto) { + return pbkdf2Sha512Crypto(crypto, secret, salt, iterations, keylen); } else { - const crypto = await getCryptoModule(); - if (crypto) { - return pbkdf2Sha512Crypto(crypto, secret, salt, iterations, keylen); - } else { - return pbkdf2Sha512Noble(secret, salt, iterations, keylen); - } + return pbkdf2Sha512Noble(secret, salt, iterations, keylen); } }