Skip to content
Closed
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
42 changes: 40 additions & 2 deletions packages/sdk-multichain/src/domain/multichain/api/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* c8 ignore start */
import type { RPC_URLS_MAP } from './types';
import type { RpcUrlsMap } from './types';

export const infuraRpcUrls: RPC_URLS_MAP = {
export const infuraRpcUrls: RpcUrlsMap = {
// ###### Ethereum ######
// Mainnet
'eip155:1': 'https://mainnet.infura.io/v3/',
Expand Down Expand Up @@ -64,3 +64,41 @@ export const infuraRpcUrls: RPC_URLS_MAP = {
'eip155:44787': 'https://celo-alfajores.infura.io/v3/',
};

export const RPC_HANDLED_METHODS = new Set([
'eth_blockNumber',
'eth_gasPrice',
'eth_maxPriorityFeePerGas',
'eth_blobBaseFee',
'eth_feeHistory',
'eth_getBalance',
'eth_getCode',
'eth_getStorageAt',
'eth_call',
'eth_estimateGas',
'eth_getLogs',
'eth_getProof',
'eth_getTransactionCount',
'eth_getBlockByNumber',
'eth_getBlockByHash',
'eth_getBlockTransactionCountByNumber',
'eth_getBlockTransactionCountByHash',
'eth_getUncleCountByBlockNumber',
'eth_getUncleCountByBlockHash',
'eth_getTransactionByHash',
'eth_getTransactionByBlockNumberAndIndex',
'eth_getTransactionByBlockHashAndIndex',
'eth_getTransactionReceipt',
'eth_getUncleByBlockNumberAndIndex',
'eth_getUncleByBlockHashAndIndex',
'eth_getFilterChanges',
'eth_getFilterLogs',
'eth_newBlockFilter',
'eth_newFilter',
'eth_newPendingTransactionFilter',
'eth_sendRawTransaction',
'eth_syncing',
'eth_uninstallFilter',
'web3_clientVersion',
]);

export const SDK_HANDLED_METHODS = new Set(['eth_accounts', 'eth_chainId']);
4 changes: 2 additions & 2 deletions packages/sdk-multichain/src/domain/multichain/api/infura.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { infuraRpcUrls } from './constants';
import type { RPC_URLS_MAP } from './types';
import type { RpcUrlsMap } from './types';

export function getInfuraRpcUrls(infuraAPIKey: string) {
return Object.keys(infuraRpcUrls).reduce((acc, key) => {
const typedKey = key as keyof typeof infuraRpcUrls;
acc[typedKey] = `${infuraRpcUrls[typedKey]}${infuraAPIKey}`;
return acc;
}, {} as RPC_URLS_MAP);
}, {} as RpcUrlsMap);
}
5 changes: 3 additions & 2 deletions packages/sdk-multichain/src/domain/multichain/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CaipChainId } from '@metamask/utils';
import type EIP155 from './eip155';

/**
Expand Down Expand Up @@ -71,9 +72,9 @@ export type InvokeMethodOptions = {
* This type defines the structure for providing custom RPC endpoints
* for different blockchain networks using CAIP-2 format identifiers.
*/
export type RPC_URLS_MAP = {
export type RpcUrlsMap = {
/** CAIP-2 format chain ID mapped to its RPC URL (e.g., "eip155:1" -> "https://...") */
[chainId: `${string}:${string}`]: string;
[chainId: CaipChainId]: string;
};

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk-multichain/src/domain/multichain/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { StoreClient } from '../store';
import type { MultichainCore } from '.';
import type { RPC_URLS_MAP, Scope } from './api/types';
import type { RpcUrlsMap, Scope } from './api/types';
import type { ModalFactory } from '../../ui';
import type { SessionRequest } from '@metamask/mobile-wallet-protocol-core';
import type { PlatformType } from '../platform';
import type { Transport } from '@metamask/multichain-api-client';
import type { CaipAccountId } from '@metamask/utils';
import type { CaipAccountId, CaipChainId } from '@metamask/utils';

export type { SessionData } from '@metamask/multichain-api-client';

Expand Down Expand Up @@ -44,7 +44,7 @@ export type MultichainOptions = {
/** The Infura API key to use for RPC requests */
infuraAPIKey?: string;
/** A map of RPC URLs to use for read-only requests */
readonlyRPCMap?: RPC_URLS_MAP;
readonlyRPCMap?: RpcUrlsMap;
};
/** Analytics configuration */
analytics?: { enabled: false } | { enabled: true; integrationType: string };
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk-multichain/src/domain/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export function getPlatformType() {
return PlatformType.DesktopWeb;
}


export function isSecure() {
const platformType = getPlatformType();
return isReactNative() || platformType === PlatformType.MobileWeb;
Expand Down Expand Up @@ -93,12 +92,10 @@ const detectionPromise: Promise<boolean> = (() => {
setTimeout(() => {
window.removeEventListener('eip6963:announceProvider', handler);

const hasMetaMask = providers.some(
(p) => p?.info?.rdns === 'io.metamask',
);
const hasMetaMask = providers.some((p) => p?.info?.rdns === 'io.metamask');

resolve(hasMetaMask);
}, 300); // default timeout
}, 300); // default timeout
});
})();

Expand Down
8 changes: 4 additions & 4 deletions packages/sdk-multichain/src/invoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { runTestsInNodeEnv, runTestsInRNEnv, runTestsInWebEnv, runTestsInWebMobi
import { Store } from './store';
import { mockSessionData, mockSessionRequestData } from '../tests/data';
import type { TestSuiteOptions, MockedData } from '../tests/types';
import { RPCClient } from './multichain/rpc/client';
import { RequestRouter } from './multichain/rpc/requestRouter';

vi.mock('cross-fetch', () => {
const mockFetch = vi.fn();
Expand Down Expand Up @@ -127,7 +127,7 @@ function testSuite<T extends MultichainOptions>({ platform, createSDK, options:
t.expect(sdk.storage).toBeDefined();
t.expect(sdk.transport).toBeDefined();

const providerInvokeMethodSpy = t.vi.spyOn(RPCClient.prototype, 'invokeMethod');
const providerInvokeMethodSpy = t.vi.spyOn(RequestRouter.prototype, 'invokeMethod');
const options = {
id: 1,
scope: 'eip155:1',
Expand All @@ -144,7 +144,7 @@ function testSuite<T extends MultichainOptions>({ platform, createSDK, options:
});

t.it(
`${platform} should reject invoke in case of failure in RPCClient`,
`${platform} should reject invoke in case of failure in RequestRouter`,
async () => {
const scopes = ['eip155:1'] as Scope[];
const caipAccountIds = ['eip155:1:0x1234567890abcdef1234567890abcdef12345678'] as any;
Expand Down Expand Up @@ -176,7 +176,7 @@ function testSuite<T extends MultichainOptions>({ platform, createSDK, options:
mockedData.mockWalletGetSession.mockImplementation(async () => mockSessionData);
mockedData.mockWalletCreateSession.mockImplementation(async () => mockSessionData);

// Mock the RPCClient response
// Mock the RequestRouter response
const mockJsonResponse = { result: 'success' };
const fetchModule = await import('cross-fetch');
const mockFetch = (fetchModule as any).__mockFetch;
Expand Down
12 changes: 7 additions & 5 deletions packages/sdk-multichain/src/multichain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import { DappClient } from '@metamask/mobile-wallet-protocol-dapp-client';
import { getMultichainClient, type MultichainApiClient, type SessionData } from '@metamask/multichain-api-client';
import { analytics } from '@metamask/sdk-analytics';
import type { CaipAccountId, Json } from '@metamask/utils';
import { METAMASK_CONNECT_BASE_URL, METAMASK_DEEPLINK_BASE, MWP_RELAY_URL } from 'src/config';
import { MWP_RELAY_URL } from 'src/config';
import packageJson from '../../package.json';
import { type InvokeMethodOptions, type MultichainOptions, type RPCAPI, type Scope, TransportType } from '../domain';
import { createLogger, enableDebug, isEnabled as isLoggerEnabled } from '../domain/logger';
import { type ConnectionRequest, type ExtendedTransport, MultichainCore, type SDKState } from '../domain/multichain';
import { getPlatformType, hasExtension, isSecure, PlatformType } from '../domain/platform';
import { RPCClient } from './rpc/client';
import { RequestRouter } from './rpc/requestRouter';
import { DefaultTransport } from './transports/default';

import { MWPTransport } from './transports/mwp';
import { keymanager } from './transports/mwp/KeyManager';
import { getDappId, getVersion, openDeeplink, setupDappMetadata, setupInfuraProvider } from './utils';
import { RpcClient } from './rpc/handlers/rpcClient';

//ENFORCE NAMESPACE THAT CAN BE DISABLED
const logger = createLogger('metamask-sdk:core');
Expand Down Expand Up @@ -456,11 +457,12 @@ export class MultichainSDK extends MultichainCore {
}

async invokeMethod(request: InvokeMethodOptions): Promise<Json> {
const { sdkInfo, transport } = this;
const { sdkInfo, transport, options } = this;

this.__provider ??= getMultichainClient({ transport });

const client = new RPCClient(this.transport, this.options, sdkInfo);
return client.invokeMethod(request) as Promise<Json>;
const rpcClient = new RpcClient(options, sdkInfo);
const requestRouter = new RequestRouter(transport, rpcClient, options);
return requestRouter.invokeMethod(request) as Promise<Json>;
}
}
155 changes: 0 additions & 155 deletions packages/sdk-multichain/src/multichain/rpc/client.test.ts

This file was deleted.

Loading
Loading