Skip to content

Commit

Permalink
Passing SDK config params to injected provider (#1436)
Browse files Browse the repository at this point in the history
* ethereum.setAppParams

* setAppInfo

* test
  • Loading branch information
fan-zhang-sv authored Oct 25, 2024
1 parent bae2bea commit 62b243c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
33 changes: 27 additions & 6 deletions packages/wallet-sdk/src/util/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { vi } from 'vitest';

import {
CBInjectedProvider,
CBWindow,
checkErrorForInvalidRequestArgs,
fetchRPCRequest,
Expand Down Expand Up @@ -88,7 +89,12 @@ describe('Utils', () => {
})
).toBe(extensionProvider);

expect(mockSetAppInfo).toHaveBeenCalledWith('Dapp', null, []);
expect(mockSetAppInfo).toHaveBeenCalledWith(
'Dapp',
null,
[],
expect.objectContaining({ options: 'all' })
);
});

it('smartWalletOnly - should return undefined', () => {
Expand All @@ -110,11 +116,10 @@ describe('Utils', () => {
});

describe('Browser Provider', () => {
class MockCipherProviderClass {
public isCoinbaseBrowser = true;
}

const mockCipherProvider = new MockCipherProviderClass() as unknown as ProviderInterface;
const mockCipherProvider = {
isCoinbaseBrowser: true,
setAppInfo: vi.fn(),
} as unknown as CBInjectedProvider;

beforeAll(() => {
window.coinbaseWalletExtension = undefined;
Expand All @@ -138,6 +143,14 @@ describe('Utils', () => {
},
})
).toBe(mockCipherProvider);
expect(mockCipherProvider.setAppInfo).toHaveBeenCalledWith(
'Dapp',
null,
[],
expect.objectContaining({
options: 'all',
})
);
});

it('smartWalletOnly - Should still return injected browser provider', () => {
Expand All @@ -153,6 +166,14 @@ describe('Utils', () => {
},
})
).toBe(mockCipherProvider);
expect(mockCipherProvider.setAppInfo).toHaveBeenCalledWith(
'Dapp',
null,
[],
expect.objectContaining({
options: 'all',
})
);
});

it('should handle exception when accessing window.top', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/wallet-sdk/src/util/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ export function getCoinbaseInjectedProvider({
metadata,
preference,
}: Readonly<ConstructorOptions>): ProviderInterface | undefined {
const { appName, appLogoUrl, appChainIds } = metadata;

if (preference.options !== 'smartWalletOnly') {
const extension = getCoinbaseInjectedLegacyProvider();
if (extension) {
const { appName, appLogoUrl, appChainIds } = metadata;
extension.setAppInfo?.(appName, appLogoUrl, appChainIds);
extension.setAppInfo?.(appName, appLogoUrl, appChainIds, preference);
return extension;
}
}

const ethereum = getInjectedEthereum();
if (ethereum?.isCoinbaseBrowser) {
ethereum.setAppInfo?.(appName, appLogoUrl, appChainIds, preference);
return ethereum;
}

Expand Down

0 comments on commit 62b243c

Please sign in to comment.