From de0f2ec5eda2919392198d75be07f702659af4d5 Mon Sep 17 00:00:00 2001 From: Xi Zhang Date: Mon, 23 Sep 2024 23:51:47 -0500 Subject: [PATCH] clients: Support Brave wallet default --- clients/js/src/provider.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/clients/js/src/provider.ts b/clients/js/src/provider.ts index 2b7dbe0c..4f51c252 100644 --- a/clients/js/src/provider.ts +++ b/clients/js/src/provider.ts @@ -63,6 +63,8 @@ export function fillOptions( const SAPPHIRE_WRAPPED_ETHEREUMPROVIDER = '#SAPPHIRE_WRAPPED_ETHEREUMPROVIDER' as const; +const BRAVE_READ_ONLY_PROPERTIES = [ 'on', 'emit', 'removeListener', 'removeAllListeners']; + export function isWrappedEthereumProvider

( p: P, ): p is P & { [SAPPHIRE_WRAPPED_ETHEREUMPROVIDER]: SapphireWrapOptions } { @@ -208,7 +210,13 @@ export function makeTaggedProxyObject( if (prop === propname) return options; if (hooks && prop in hooks) return Reflect.get(hooks, prop); const value = Reflect.get(upstream, prop); - return typeof value === 'function' ? value.bind(upstream) : value; + // Brave wallet web3provider properties are read only and throw typeerror + // https://github.com/brave/brave-core/blob/74bf470a0291ea3719f1a75af066ee10b7057dbd/components/brave_wallet/resources/ethereum_provider.js#L13-L27 + // https://github.com/wevm/wagmi/blob/86c42248c2f34260a52ee85183c607315ae63ce8/packages/core/src/connectors/injected.ts#L327-L335 + if (typeof value === 'function' && !BRAVE_READ_ONLY_PROPERTIES.includes(prop as string)) { + return value.bind(upstream); + } + return value; }, }); }