/** * Web3 specific polyfills for React Native * Addresses instanceof and constructor issues in React Native environments */ export const setupWeb3InstanceofPolyfill = () => { try { // Store the original instanceof behavior const originalInstanceof = Function.prototype[Symbol.hasInstance]; // Override Symbol.hasInstance for better Web3 detection in React Native if (typeof global !== 'undefined' && global.Symbol && global.Symbol.hasInstance) { // Create a safer instanceof check for Web3 const createSafeInstanceofCheck = (constructor: any) => { return function(instance: any) { // Try the original check first try { if (originalInstanceof) { return originalInstanceof.call(constructor, instance); } return instance instanceof constructor; } catch (error) { // Fallback to constructor and duck-typing checks if (!instance || typeof instance !== 'object') { return false; } // Check constructor name if (instance.constructor && instance.constructor.name === constructor.name) { return true; } // Check constructor reference if (instance.constructor === constructor) { return true; } // For Web3 specifically, check characteristic properties if (constructor.name === 'Web3' && 'currentProvider' in instance && 'eth' in instance && 'utils' in instance) { return true; } return false; } }; }; // Apply the polyfill to global scope for dynamic loading (global as any).__PERS_SDK_SAFE_INSTANCEOF__ = createSafeInstanceofCheck; } return true; } catch (error) { console.warn('[PERS SDK] Web3 instanceof polyfill setup failed:', error); return false; } }; export const createSafeWeb3Check = (provider: unknown): boolean => { if (!provider || typeof provider !== 'object') { return false; } try { // Method 1: Check if it's already a Web3 instance using multiple approaches const providerObj = provider as any; // Constructor name check if (providerObj.constructor?.name === 'Web3') { return true; } // Duck typing for Web3 properties if ('currentProvider' in providerObj && 'eth' in providerObj && 'utils' in providerObj && typeof providerObj.eth === 'object') { return true; } // Check for Web3 version property if ('version' in providerObj && typeof providerObj.version === 'string') { return true; } return false; } catch (error) { console.warn('Safe Web3 check failed:', error); return false; } }; // Auto-initialize setupWeb3InstanceofPolyfill();