Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ const baseConfig = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 70.68,
functions: 72.32,
lines: 71.39,
statements: 71.51,
branches: 70,
functions: 72.07,
lines: 70.82,
statements: 70.95,
},
},

Expand Down
22 changes: 1 addition & 21 deletions src/extension-provider/createExternalExtensionProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { JsonRpcRequest } from '@metamask/utils';

import {
createExternalExtensionProvider,
getBuildType,
} from './createExternalExtensionProvider';
import { createExternalExtensionProvider } from './createExternalExtensionProvider';
import config from './external-extension-config.json';
import { MockPort } from '../../test/mocks/MockPort';
import type { BaseProvider } from '../BaseProvider';
Expand Down Expand Up @@ -99,23 +96,6 @@ async function getInitializedProvider({

return { provider, port, onWrite };
}
describe('getBuildType', () => {
const testCases = [
{ payload: 'io.metamask.beta', expected: 'beta' },
{ payload: 'io.metamask', expected: 'stable' },
{ payload: 'io.metamask.flask', expected: 'flask' },
{ payload: 'io.metamask.unknown', expected: undefined },
];

it.each(testCases)(
'should return $expected for payload $payload',
({ payload, expected }) => {
const result = getBuildType(payload);
expect(result).toBe(expected);
},
);
});

describe('createExternalExtensionProvider', () => {
it('can be called and not throw', () => {
// `global.chrome.runtime` mock setup by `jest-chrome` in `jest.setup.browser.js`
Expand Down
15 changes: 0 additions & 15 deletions src/extension-provider/createExternalExtensionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,3 @@ function getExtensionId(typeOrId: ExtensionType) {

return ids[typeOrId as keyof typeof ids] ?? typeOrId;
}

/**
* Gets the build type for the given domain name identifier.
*
* @param rdns - The reverse syntax domain name identifier for the wallet.
* @returns The type or ID.
*/
export function getBuildType(rdns: string): string | undefined {
const rndsToIdDefinition: Record<string, string> = {
'io.metamask': 'stable',
'io.metamask.beta': 'beta',
'io.metamask.flask': 'flask',
};
return rndsToIdDefinition[rdns];
}
54 changes: 18 additions & 36 deletions src/initializeInpageProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { announceWallet, type CAIP294WalletData } from './CAIP294';
import { getBuildType } from './extension-provider/createExternalExtensionProvider';
import {
announceCaip294WalletData,
setGlobalProvider,
Expand Down Expand Up @@ -58,48 +57,31 @@ describe('announceCaip294WalletData', () => {
jest.clearAllMocks();
});

describe('build type is not flask', () => {
it('should not announce wallet if build type is not flask', async () => {
(getBuildType as jest.Mock).mockReturnValue('stable');
it('should announce wallet with caip-348 target for chromium browsers', async () => {
const extensionId = 'test-extension-id';
(mockProvider.request as jest.Mock).mockReturnValue({ extensionId });

await announceCaip294WalletData(mockProvider, mockProviderInfo);
await announceCaip294WalletData(mockProvider, mockProviderInfo);

expect(getBuildType).toHaveBeenCalledWith(mockProviderInfo.rdns);
expect(announceWallet).not.toHaveBeenCalled();
expect(announceWallet).toHaveBeenCalledWith({
...mockProviderInfo,
targets: [
{
type: 'caip-348',
value: extensionId,
},
],
});
});

describe('build type is flask', () => {
it('should announce wallet with caip-348 target for chromium browsers', async () => {
const extensionId = 'test-extension-id';
(getBuildType as jest.Mock).mockReturnValue('flask');
(mockProvider.request as jest.Mock).mockReturnValue({ extensionId });
it('should announce wallet without caip-348 target for firefox browser', async () => {
(mockProvider.request as jest.Mock).mockReturnValue({});

await announceCaip294WalletData(mockProvider, mockProviderInfo);
await announceCaip294WalletData(mockProvider, mockProviderInfo);

expect(getBuildType).toHaveBeenCalledWith(mockProviderInfo.rdns);
expect(announceWallet).toHaveBeenCalledWith({
...mockProviderInfo,
targets: [
{
type: 'caip-348',
value: extensionId,
},
],
});
});

it('should announce wallet without caip-348 target for firefox browser', async () => {
(getBuildType as jest.Mock).mockReturnValue('flask');
(mockProvider.request as jest.Mock).mockReturnValue({});

await announceCaip294WalletData(mockProvider, mockProviderInfo);

expect(getBuildType).toHaveBeenCalledWith(mockProviderInfo.rdns);
expect(announceWallet).toHaveBeenCalledWith({
...mockProviderInfo,
targets: [],
});
expect(announceWallet).toHaveBeenCalledWith({
...mockProviderInfo,
targets: [],
});
});
});
20 changes: 12 additions & 8 deletions src/initializeInpageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { type Duplex } from 'readable-stream';
import type { CAIP294WalletData } from './CAIP294';
import { announceWallet } from './CAIP294';
import { announceProvider as announceEip6963Provider } from './EIP6963';
import { getBuildType } from './extension-provider/createExternalExtensionProvider';
import type { MetaMaskInpageProviderOptions } from './MetaMaskInpageProvider';
import { MetaMaskInpageProvider } from './MetaMaskInpageProvider';
import { shimWeb3 } from './shimWeb3';
Expand All @@ -29,6 +28,11 @@ type InitializeProviderOptions = {
* Whether the window.web3 shim should be set.
*/
shouldShimWeb3?: boolean;

/**
* Whether the provider announce a CAIP-294 event.
*/
shouldAnnounceCaip294?: boolean;
} & MetaMaskInpageProviderOptions;

/**
Expand All @@ -42,6 +46,7 @@ type InitializeProviderOptions = {
* @param options.shouldSetOnWindow - Whether the provider should be set as window.ethereum.
* @param options.shouldShimWeb3 - Whether a window.web3 shim should be injected.
* @param options.logger - The logging API to use. Default: `console`.
* @param options.shouldAnnounceCaip294 - Whether the provider should announce itself.
* @returns The initialized provider (whether set or not).
*/
export function initializeProvider({
Expand All @@ -52,6 +57,7 @@ export function initializeProvider({
shouldSendMetadata = true,
shouldSetOnWindow = true,
shouldShimWeb3 = false,
shouldAnnounceCaip294 = true,
}: InitializeProviderOptions): MetaMaskInpageProvider {
const provider = new MetaMaskInpageProvider(connectionStream, {
logger,
Expand All @@ -74,8 +80,11 @@ export function initializeProvider({
info: providerInfo,
provider: proxiedProvider,
});
// eslint-disable-next-line no-void
void announceCaip294WalletData(provider, providerInfo);

if (shouldAnnounceCaip294) {
// eslint-disable-next-line no-void
void announceCaip294WalletData(provider, providerInfo);
}
}

if (shouldSetOnWindow) {
Expand Down Expand Up @@ -121,11 +130,6 @@ export async function announceCaip294WalletData(
provider: MetaMaskInpageProvider,
providerInfo: CAIP294WalletData,
): Promise<void> {
const buildType = getBuildType(providerInfo.rdns);
if (buildType !== 'flask') {
return;
}

const providerState = await provider.request<{ extensionId?: string }>({
method: 'metamask_getProviderState',
});
Expand Down
Loading