Handle webView Java bridge undefined values

This commit is contained in:
Pranav 2025-08-19 14:49:22 +05:30
parent 17cb1156d7
commit 52d6dfe200
4 changed files with 17 additions and 30 deletions

View File

@ -1,6 +1,6 @@
{
"name": "web-wallet",
"version": "0.1.7-nym-0.1.2",
"version": "0.1.7-nym-0.1.3",
"private": true,
"dependencies": {
"@cerc-io/registry-sdk": "^0.2.5",

3
src/global.d.ts vendored
View File

@ -3,9 +3,6 @@ declare global {
interface Window {
// Android bridge callbacks for signature and accounts related events
Android?: {
// Check if running in Android WebView environment
isAndroidWebView(): boolean;
// Store a key-value pair securely in encrypted storage
setItem(key: string, value: string): boolean;

View File

@ -8,7 +8,6 @@ import { useNetworks } from '../context/NetworksContext';
import { StackParamsList } from '../types';
import useGetOrCreateAccounts from './useGetOrCreateAccounts';
import { retrieveAccountsForNetwork, createWallet, retrieveNetworksData, isWalletCreated } from '../utils/accounts';
import { getInternetCredentials } from '../utils/key-store';
export const useWebViewHandler = () => {
// Navigation and context hooks
@ -41,15 +40,18 @@ export const useWebViewHandler = () => {
try {
// Get available networks data
const networksData = await retrieveNetworksData();
if (!networksData || networksData.length === 0) {
window.Android?.onAccountError?.('No networks configured');
return;
}
// Check if wallet exists and if account data exists for the requested network
// Create wallet using the provided mnemonic if it doesn't exist
const walletExists = await isWalletCreated();
if (!walletExists) {
await createWallet(networksData, mnemonic);
}
// Find the requested network
const requestedNetwork = networksData.find(network => network.chainId === chainId);
if (!requestedNetwork) {
@ -57,14 +59,6 @@ export const useWebViewHandler = () => {
return;
}
// Check if account data exists for this network
const accountKey = `accounts/${requestedNetwork.namespace}:${requestedNetwork.chainId}/0`;
const accountData = await getInternetCredentials(accountKey);
if (!walletExists || !accountData) {
await createWallet(networksData, mnemonic);
}
// Update networks context
setNetworksData(networksData);
setSelectedNetwork(requestedNetwork);

View File

@ -1,29 +1,25 @@
// Check if running in Android WebView with secure storage available
const isAndroidSecureStorageAvailable = (): boolean => {
return typeof window !== 'undefined' &&
window.Android !== undefined &&
window.Android.isAndroidWebView();
};
const setInternetCredentials = (name:string, username:string, password:string) => {
if (isAndroidSecureStorageAvailable()) {
window.Android!.setItem(name, password);
if (window.Android?.setItem) {
window.Android.setItem(name, password);
} else {
localStorage.setItem(name, password);
}
};
const getInternetCredentials = (name:string) : string | null => {
if (isAndroidSecureStorageAvailable()) {
return window.Android!.getItem(name);
if (window.Android?.getItem) {
const result = window.Android.getItem(name);
// Normalize undefined to null to match localStorage behavior
return result === undefined ? null : result;
} else {
return localStorage.getItem(name);
}
};
const resetInternetCredentials = (name:string) => {
if (isAndroidSecureStorageAvailable()) {
window.Android!.removeItem(name);
if (window.Android?.removeItem) {
window.Android.removeItem(name);
} else {
localStorage.removeItem(name);
}