Skip to content

Commit 990a1e1

Browse files
jiexiadonesky1
andauthored
feat: Remove api.infuraAPIKey SDK param. Export getInfuraRpcUrls. Add env to playgrounds (#19)
* rename RpcUrlsMap * migrate changes over * make multichain package a module * Fix import * Fix multichain package typedef? * revert export typedef change * Fix connect package build issue / multichain package exported types * Apply suggestion from @adonesky1 Co-authored-by: Alex Donesky <[email protected]> * fix typo * remove biome comments * revert esm module change * comment constants * Update packages/connect-multichain/src/multichain/rpc/requestRouter.test.ts Co-authored-by: Alex Donesky <[email protected]> * Update packages/connect-multichain/src/multichain/rpc/requestRouter.test.ts Co-authored-by: Alex Donesky <[email protected]> * Update packages/connect-multichain/src/multichain/rpc/requestRouter.test.ts Co-authored-by: Alex Donesky <[email protected]> * WIP * use getInfuraRpcUrls in playgrounds * fix rpcClient test mocks * remove old test * add dotenv for react-playground * add dotenv for node-playground * add env for react-native-playground --------- Co-authored-by: Alex Donesky <[email protected]>
1 parent 15a3008 commit 990a1e1

File tree

20 files changed

+64
-111
lines changed

20 files changed

+64
-111
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ scripts/coverage
3737

3838
# typescript
3939
packages/*/*.tsbuildinfo
40+
41+
# env files
42+
**/.env

packages/connect-multichain/src/domain/multichain/types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ export type MultichainOptions = {
4141
/** Dapp identification and branding settings */
4242
dapp: DappSettings;
4343
/** Optional API configuration for external services */
44-
api?: {
45-
/** The Infura API key to use for RPC requests */
46-
infuraAPIKey?: string;
44+
api: {
4745
/** A map of RPC URLs to use for read-only requests */
48-
readonlyRPCMap?: RpcUrlsMap;
46+
readonlyRPCMap: RpcUrlsMap;
4947
};
5048
/** Analytics configuration */
5149
analytics?: { enabled: false } | { enabled: true; integrationType: string };

packages/connect-multichain/src/multichain/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ import {
6767
getDappId,
6868
openDeeplink,
6969
setupDappMetadata,
70-
setupInfuraProvider,
7170
} from './utils';
7271
import { RpcClient } from './rpc/handlers/rpcClient';
7372

73+
export { getInfuraRpcUrls } from '../domain/multichain/api/infura';
74+
7475
// ENFORCE NAMESPACE THAT CAN BE DISABLED
7576
const logger = createLogger('metamask-sdk:core');
7677

@@ -129,8 +130,7 @@ export class MultichainSDK extends MultichainCore {
129130
}
130131

131132
private constructor(options: MultichainOptions) {
132-
const withInfuraRPCMethods = setupInfuraProvider(options);
133-
const withDappMetadata = setupDappMetadata(withInfuraRPCMethods);
133+
const withDappMetadata = setupDappMetadata(options);
134134
const allOptions = {
135135
...withDappMetadata,
136136
ui: {

packages/connect-multichain/src/multichain/rpc/handlers/rpcClient.test.ts

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ t.describe('RpcClient', () => {
3434

3535
mockConfig = {
3636
api: {
37-
infuraAPIKey: 'test-infura-key',
3837
readonlyRPCMap: {
38+
'eip155:1': 'https://mainnet.infura.io/v3/01234567890',
3939
'eip155:11155111': 'https://custom-sepolia.com',
4040
},
4141
},
@@ -79,7 +79,7 @@ t.describe('RpcClient', () => {
7979
});
8080

8181
t.describe('request', () => {
82-
t.it('should use readonlyRPCMap rpc endpoint when infuraAPIKey is provided and readonlyRPCMap contains a chainId that also exists in the infura RPC constants', async () => {
82+
t.it('should use readonlyRPCMap rpc endpoint', async () => {
8383
const mockJsonResponse = {
8484
jsonrpc: '2.0',
8585
result: '0x1234567890abcdef',
@@ -103,47 +103,6 @@ t.describe('RpcClient', () => {
103103
});
104104
});
105105

106-
t.it(
107-
'should use readonlyRPCMap rpc endpoint when infuraAPIKey is provided and readonlyRPCMap does not contain a chainId that also exists in the infura RPC constants',
108-
async () => {
109-
const mockJsonResponse = {
110-
jsonrpc: '2.0',
111-
result: '0x1234567890abcdef',
112-
id: 1,
113-
};
114-
115-
const mockResponse = {
116-
ok: true,
117-
json: t.vi.fn().mockResolvedValue(mockJsonResponse),
118-
};
119-
120-
mockFetch.mockResolvedValue(mockResponse);
121-
122-
const clientModule = await import('./rpcClient');
123-
const rpcClient = new clientModule.RpcClient(
124-
{
125-
...mockConfig,
126-
api: {
127-
...mockConfig.api,
128-
readonlyRPCMap: {
129-
'eip155:10000': 'https://custom-rpc.com',
130-
},
131-
},
132-
},
133-
sdkInfo,
134-
);
135-
136-
const result = await rpcClient.request({ ...baseOptions, scope: 'eip155:10000' });
137-
138-
t.expect(result).toBe('0x1234567890abcdef');
139-
t.expect(mockFetch).toHaveBeenCalledWith('https://custom-rpc.com', {
140-
method: 'POST',
141-
headers: defaultHeaders,
142-
body: t.expect.stringContaining('"method":"eth_getBalance"'),
143-
});
144-
},
145-
);
146-
147106
t.it('should throw RPCReadonlyResponseErr when response cannot be parsed as JSON', async () => {
148107
const mockResponse = {
149108
ok: true,

packages/connect-multichain/src/multichain/rpc/handlers/rpcClient.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,9 @@ export class RpcClient {
4545
}
4646

4747
private getRpcEndpoint(scope: Scope) {
48-
let infuraAPIKey = this.config?.api?.infuraAPIKey;
4948

5049
let readonlyRPCMap: RpcUrlsMap = this.config?.api?.readonlyRPCMap ?? {};
51-
if (infuraAPIKey) {
52-
const urlsWithToken = getInfuraRpcUrls(infuraAPIKey);
53-
if (readonlyRPCMap) {
54-
readonlyRPCMap = {
55-
...urlsWithToken,
56-
...readonlyRPCMap,
57-
};
58-
} else {
59-
readonlyRPCMap = urlsWithToken;
60-
}
61-
}
50+
6251
const rpcEndpoint = readonlyRPCMap[scope];
6352
if (!rpcEndpoint) {
6453
throw new MissingRpcEndpointErr(`No RPC endpoint found for scope ${scope}`);

packages/connect-multichain/src/multichain/utils/index.test.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ t.describe('Utils', () => {
2626
url: 'test',
2727
},
2828
api: {
29-
infuraAPIKey: 'testKey',
3029
},
3130
} as MultichainOptions;
3231
});
@@ -127,30 +126,6 @@ t.describe('Utils', () => {
127126
});
128127
});
129128

130-
t.describe('setupInfuraProvider', () => {
131-
t.it('should not set up infura provider if infuraAPIKey is not provided', async () => {
132-
options.api!.infuraAPIKey = undefined;
133-
await utils.setupInfuraProvider(options);
134-
t.expect(options.api?.readonlyRPCMap).toBeUndefined();
135-
});
136-
137-
t.it('should set up infura provider with infuraAPIKey', async () => {
138-
await utils.setupInfuraProvider(options);
139-
t.expect(options.api?.readonlyRPCMap?.['eip155:1']).toBe(`https://mainnet.infura.io/v3/testKey`);
140-
});
141-
142-
t.it('Should allow customizing the readonlyRPCMap + merge with defaults', async () => {
143-
const customChainId = 'eip155:12345';
144-
const customEndpoint = 'https://mainnet.infura.io/12345';
145-
options.api!.readonlyRPCMap = {
146-
[customChainId]: customEndpoint,
147-
};
148-
await utils.setupInfuraProvider(options);
149-
t.expect(options.api?.readonlyRPCMap?.['eip155:1']).toBe(`https://mainnet.infura.io/v3/testKey`);
150-
t.expect(options.api?.readonlyRPCMap?.[customChainId]).toBe(customEndpoint);
151-
});
152-
});
153-
154129
t.describe('setupDappMetadata', () => {
155130
t.beforeEach(() => {
156131
// Mock the document object to avoid undefined errors

packages/connect-multichain/src/multichain/utils/index.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '@metamask/utils';
88
import {
99
type DappSettings,
10-
getInfuraRpcUrls,
1110
getPlatformType,
1211
type MultichainOptions,
1312
PlatformType,
@@ -118,25 +117,6 @@ export const extractFavicon = () => {
118117
return favicon;
119118
};
120119

121-
export function setupInfuraProvider(
122-
options: MultichainOptions,
123-
): MultichainOptions {
124-
const infuraAPIKey = options.api?.infuraAPIKey;
125-
if (!infuraAPIKey) {
126-
return options;
127-
}
128-
const urlsWithToken = getInfuraRpcUrls(infuraAPIKey);
129-
if (options.api?.readonlyRPCMap) {
130-
options.api.readonlyRPCMap = {
131-
...options.api.readonlyRPCMap,
132-
...urlsWithToken,
133-
};
134-
} else if (options.api) {
135-
options.api.readonlyRPCMap = urlsWithToken;
136-
}
137-
return options;
138-
}
139-
140120
export function setupDappMetadata(
141121
options: MultichainOptions,
142122
): MultichainOptions {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INFURA_API_KEY=

playground/multichain-node-playground/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Node.js SDK example
22

3-
Get started by running:
3+
## Configuration
4+
5+
`cp .env.example .env`
6+
7+
Then fill out the resulting `.env` file.
8+
9+
10+
## Usage
411

512
```bash
613
# This repo expects you to use `nvm` (https://github.com/nvm-sh/nvm) for node version management.

playground/multichain-node-playground/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@types/jest": "^29.5.14",
3939
"@types/node": "^22.9.0",
4040
"deepmerge": "^4.2.2",
41+
"dotenv": "^17.2.3",
4142
"ts-jest": "^27.1.4",
4243
"ts-node": "^10.9.1",
4344
"tsconfig-paths": "^4.2.0",

0 commit comments

Comments
 (0)