Hello, I have faced a problem same as this Cannot read property ‘digest’ of undefined
Of cause, I tried to solve it, but I couldn’t.
Background
I used "@web3auth/react-native-sdk": "^3.5.0"
, but they stopped support.
so I tryed to update.
- SDK Version: “@web3auth/react-native-sdk”: “^8.1.0”,
- Platform: “ReactNative” + “iOS”,
- Browser Console Screenshots:
[TypeError: Cannot read property ‘digest’ of undefined]
Trace is - const encData = await metadataHelpers.encryptData(this.sessionId,
- const encParams = await eccrypto.encrypt(eccrypto.getPublic(Buffer.from(
- https://github.com/torusresearch/eccrypto/blob/master/src/index.ts#L238
async function sha512(msg: Buffer): Promise<Uint8Array> {
if (!browserCrypto.createHash) {
const hash = await subtle.digest("SHA-512", msg); <- subtle is undefined
const result = new Uint8Array(hash);
return result;
}
const hash = browserCrypto.createHash("sha512");
const result = hash.update(msg).digest();
return new Uint8Array(result);
}
Also
const browserCrypto = globalThis.crypto || globalThis.msCrypto || {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const subtle = browserCrypto.subtle || browserCrypto.webkitSubtle;
subtle is undefined
Code Snipet is
import Web3Auth, {
LANGUAGES,
LOGIN_PROVIDER,
WEB3AUTH_NETWORK,
} from '@web3auth/react-native-sdk';
import EncryptedStorage from "react-native-encrypted-storage";
import { EthereumPrivateKeyProvider } from "@web3auth/ethereum-provider";
import * as WebBrowser from '@toruslabs/react-native-web-browser';
import {
CHAIN_NAMESPACES,
IAdapter,
IProvider,
getEvmChainConfig,
} from '@web3auth/base';
import axios from 'axios';
const network =
Config.ENV === 'DEBUG' ? WEB3AUTH_NETWORK.TESTNET : WEB3AUTH_NETWORK.CYAN;
const privateKeyProvider = new EthereumPrivateKeyProvider({
config: {
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0x1",
rpcTarget: "https://rpc.ethereum.org",
displayName: "Ethereum Mainnet",
blockExplorerUrl: "https://etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
},
},
});
const whiteLabel = {
name: 'NAME'
logoLight: '',
defaultLanguage: LANGUAGES.ja,
theme: {
primary: '#000000',
},
};
const web3auth = new Web3Auth(WebBrowser, EncryptedStorage, {
clientId: Config.WEB3AUTH_CLIENTID ?? '',
network: network,
whiteLabel: whiteLabel,
privateKeyProvider,
redirectUrl: '',
loginConfig: {
jwt: {
verifier: 'apple-questry',
typeOfLogin: 'jwt',
clientId: Config.AUTH0_CLIENTID,
},
},
});
export const init = async () => {
await web3auth.init();
}
export const handleLoginProviderClick = onClose => {
const redirectUrl =
Platform.OS === 'android'
? Config.REDIRECT_URL_ANDROID
: Config.REDIRECT_URL_IOS;
web3auth
.login({
// https://web3auth.io/docs/sdk/pnp/react-native/custom-authentication#using-auth0-login
loginProvider: LOGIN_PROVIDER.JWT,
redirectUrl,
extraLoginOptions: {
domain: Config.AUTH0_DOMAIN,
verifierIdField: 'sub',
},
})
.then(async result => {
console.log("result", result)
onClose();
try {
RNRestart.Restart();
} catch (error) {
console.log('Restart Error: ', error);
}
})
.catch(error => {
console.error('Error: ', error);
});
};
same as documents in Web3Auth
Regards