Skip to content

Commit 4ccd3f2

Browse files
fix(passport): prevent eth_requestAccounts from being called on Magic RPC (#2576)
Co-authored-by: Hayden Fowler <hayden.fowler@gmail.com>
1 parent 16a71aa commit 4ccd3f2

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

packages/passport/sdk/src/magic/magicProviderProxyFactory.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,17 @@ describe('MagicProviderProxyFactory', () => {
107107

108108
await expect(proxy.request!(params)).rejects.toThrow('ProviderProxy: Test error');
109109
});
110+
111+
it('should convert eth_requestAccounts to eth_accounts to avoid Magic overlay', async () => {
112+
const proxy = factory.createProxy(mockMagicClient);
113+
const params = { method: 'eth_requestAccounts' };
114+
(mockMagicClient.user.isLoggedIn as jest.Mock).mockResolvedValue(true);
115+
116+
await proxy.request!(params);
117+
118+
expect(mockMagicClient.user.isLoggedIn).toHaveBeenCalled();
119+
expect(mockRpcProvider.request).toHaveBeenCalledWith({ method: 'eth_accounts' });
120+
expect(mockRpcProvider.request).not.toHaveBeenCalledWith(params);
121+
});
110122
});
111123
});

packages/passport/sdk/src/magic/magicProviderProxyFactory.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ const shouldCheckMagicSession = (args: any[]): boolean => (
88
&& typeof args[0] === 'object'
99
&& 'method' in args[0]
1010
&& typeof args[0].method === 'string'
11-
&& ['personal_sign', 'eth_accounts'].includes(args[0].method)
11+
&& ['personal_sign', 'eth_accounts', 'eth_requestAccounts'].includes(args[0].method)
12+
);
13+
14+
const isEthRequestAccountsCall = (args: any[]): boolean => (
15+
args?.length > 0
16+
&& typeof args[0] === 'object'
17+
&& 'method' in args[0]
18+
&& typeof args[0].method === 'string'
19+
&& args[0].method === 'eth_requestAccounts'
1220
);
1321

1422
/**
@@ -47,6 +55,12 @@ export class MagicProviderProxyFactory {
4755
providerId: this.config.magicProviderId,
4856
});
4957
}
58+
59+
if (isEthRequestAccountsCall(args)) {
60+
// @ts-ignore - Calling eth_requestAccounts on the Magic RPC provider displays an overlay, so this
61+
// should be avoided - call eth_accounts instead.
62+
return target.request!({ method: 'eth_accounts' });
63+
}
5064
}
5165

5266
// @ts-ignore - Invoke the request method with the provided arguments

0 commit comments

Comments
 (0)