@metamask/connect-evm 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Type guard for connect-like requests:
3
+ * - wallet_requestPermissions
4
+ * - eth_requestAccounts
5
+ *
6
+ * @param req - The request object to check
7
+ * @returns True if the request is a connect-like request, false otherwise
8
+ */
9
+ export function isConnectRequest(req) {
10
+ return (req.method === 'wallet_requestPermissions' ||
11
+ req.method === 'eth_requestAccounts');
12
+ }
13
+ /**
14
+ * Type guard for wallet_switchEthereumChain request.
15
+ *
16
+ * @param req - The request object to check
17
+ * @returns True if the request is a wallet_switchEthereumChain request, false otherwise
18
+ */
19
+ export function isSwitchChainRequest(req) {
20
+ return req.method === 'wallet_switchEthereumChain';
21
+ }
22
+ /**
23
+ * Type guard for wallet_addEthereumChain request.
24
+ *
25
+ * @param req - The request object to check
26
+ * @returns True if the request is a wallet_addEthereumChain request, false otherwise
27
+ */
28
+ export function isAddChainRequest(req) {
29
+ return req.method === 'wallet_addEthereumChain';
30
+ }
31
+ /**
32
+ * Type guard for generic accounts request:
33
+ * - eth_accounts
34
+ * - eth_coinbase
35
+ *
36
+ * @param req - The request object to check
37
+ * @returns True if the request is a generic accounts request, false otherwise
38
+ */
39
+ export function isAccountsRequest(req) {
40
+ return req.method === 'eth_accounts' || req.method === 'eth_coinbase';
41
+ }
42
+ /**
43
+ * Validates that all values in a Record are valid URLs.
44
+ *
45
+ * @param record - The record to validate (e.g., supportedNetworks)
46
+ * @param recordName - The name of the record for error messages
47
+ * @throws Error if any values are invalid URLs
48
+ */
49
+ export function validSupportedChainsUrls(record, recordName) {
50
+ const invalidUrls = [];
51
+ for (const [key, url] of Object.entries(record)) {
52
+ try {
53
+ // eslint-disable-next-line no-new
54
+ new URL(url);
55
+ }
56
+ catch (_a) {
57
+ invalidUrls.push(`${key}: ${url}`);
58
+ }
59
+ }
60
+ if (invalidUrls.length > 0) {
61
+ throw new Error(`${recordName} contains invalid URLs:\n${invalidUrls.join('\n')}`);
62
+ }
63
+ }
64
+ //# sourceMappingURL=type-guards.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-guards.js","sourceRoot":"","sources":["../../../src/utils/type-guards.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAoB;IAMnD,OAAO,CACL,GAAG,CAAC,MAAM,KAAK,2BAA2B;QAC1C,GAAG,CAAC,MAAM,KAAK,qBAAqB,CACrC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,GAAoB;IAEpB,OAAO,GAAG,CAAC,MAAM,KAAK,4BAA4B,CAAC;AACrD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAC/B,GAAoB;IAEpB,OAAO,GAAG,CAAC,MAAM,KAAK,yBAAyB,CAAC;AAClD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAC/B,GAAoB;IAKpB,OAAO,GAAG,CAAC,MAAM,KAAK,cAAc,IAAI,GAAG,CAAC,MAAM,KAAK,cAAc,CAAC;AACxE,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,wBAAwB,CACtC,MAA8B,EAC9B,UAAkB;IAElB,MAAM,WAAW,GAAa,EAAE,CAAC;IACjC,KAAK,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAChD,IAAI,CAAC;YACH,kCAAkC;YAClC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACf,CAAC;QAAC,WAAM,CAAC;YACP,WAAW,CAAC,IAAI,CAAC,GAAG,GAAG,KAAK,GAAG,EAAE,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IAED,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,KAAK,CACb,GAAG,UAAU,4BAA4B,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAClE,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -16,6 +16,11 @@ type EIP1193ProviderEvents = {
16
16
  type: string;
17
17
  data: unknown;
18
18
  }];
19
+ /**
20
+ * Emitted when a QR code URI is available for display.
21
+ * This allows consumers to show their own custom QR code UI.
22
+ */
23
+ display_uri: [string];
19
24
  connectAndSign: [
20
25
  {
21
26
  accounts: readonly Address[];
@@ -39,6 +44,7 @@ type EventHandlers = {
39
44
  disconnect: () => void;
40
45
  accountsChanged: (accounts: Address[]) => void;
41
46
  chainChanged: (chainId: Hex) => void;
47
+ displayUri: (uri: string) => void;
42
48
  connectAndSign: (result: {
43
49
  accounts: readonly Address[];
44
50
  chainId: number;
@@ -96,6 +102,23 @@ type GenericProviderRequest = {
96
102
  };
97
103
  type ProviderRequest = ConnectRequest | RevokePermissionsRequest | SwitchEthereumChainRequest | AddEthereumChainRequest | AccountsRequest | GenericProviderRequest;
98
104
  type ProviderRequestInterceptor = (req: ProviderRequest) => Promise<unknown>;
105
+ type JsonRpcRequest<T = unknown> = {
106
+ id?: number | string;
107
+ jsonrpc?: '2.0';
108
+ method: string;
109
+ params?: T;
110
+ };
111
+ type JsonRpcResponse<T = unknown> = {
112
+ id: number | string;
113
+ jsonrpc: '2.0';
114
+ result?: T;
115
+ error?: {
116
+ code: number;
117
+ message: string;
118
+ data?: unknown;
119
+ };
120
+ };
121
+ type JsonRpcCallback<T = unknown> = (error: Error | null, response: JsonRpcResponse<T> | null) => void;
99
122
 
100
123
  /**
101
124
  * EIP-1193 Provider wrapper around the Multichain SDK.
@@ -115,8 +138,37 @@ declare class EIP1193Provider extends EventEmitter<EIP1193ProviderEvents> {
115
138
  get accounts(): Address[];
116
139
  get selectedChainId(): Hex | undefined;
117
140
  set selectedChainId(chainId: Hex | number | undefined);
141
+ /**
142
+ * Alias for selectedChainId for legacy compatibility.
143
+ * Many dApps expect a `chainId` property on the provider.
144
+ */
145
+ get chainId(): Hex | undefined;
146
+ /**
147
+ * Legacy method for sending JSON-RPC requests.
148
+ * @deprecated Use `request` instead. This method is provided for backwards compatibility.
149
+ * @param request - The JSON-RPC request object
150
+ * @param callback - Optional callback function. If provided, the method returns void.
151
+ * @returns A promise resolving to the JSON-RPC response, or void if a callback is provided.
152
+ */
153
+ sendAsync<TParams = unknown, TResult = unknown>(request: JsonRpcRequest<TParams>, callback?: JsonRpcCallback<TResult>): Promise<JsonRpcResponse<TResult> | void>;
154
+ /**
155
+ * Legacy method for sending JSON-RPC requests synchronously (callback-based).
156
+ * @deprecated Use `request` instead. This method is provided for backwards compatibility.
157
+ * @param request - The JSON-RPC request object
158
+ * @param callback - The callback function to receive the response
159
+ */
160
+ send<TParams = unknown, TResult = unknown>(request: JsonRpcRequest<TParams>, callback: JsonRpcCallback<TResult>): void;
118
161
  }
119
162
 
163
+ /** The options for the connect method */
164
+ type ConnectOptions = {
165
+ /** The account to connect to */
166
+ account?: string | undefined;
167
+ /** Whether to force a request regardless of an existing session */
168
+ forceRequest?: boolean;
169
+ /** All available chain IDs in the dapp */
170
+ chainIds: number[];
171
+ };
120
172
  /**
121
173
  * The MetamaskConnectEVM class provides an EIP-1193 compatible interface for connecting
122
174
  * to MetaMask and interacting with Ethereum Virtual Machine (EVM) networks.
@@ -135,7 +187,7 @@ declare class EIP1193Provider extends EventEmitter<EIP1193ProviderEvents> {
135
187
  *
136
188
  * @example
137
189
  * ```typescript
138
- * const sdk = await createMetamaskConnectEVM({
190
+ * const sdk = await createEVMClient({
139
191
  * dapp: { name: 'My DApp', url: 'https://mydapp.com' }
140
192
  * });
141
193
  *
@@ -158,43 +210,44 @@ declare class MetamaskConnectEVM {
158
210
  * Connects to the wallet with the specified chain ID and optional account.
159
211
  *
160
212
  * @param options - The connection options
161
- * @param options.chainId - The chain ID to connect to (defaults to 1 for mainnet)
162
213
  * @param options.account - Optional specific account to connect to
163
214
  * @param options.forceRequest - Wwhether to force a request regardless of an existing session
215
+ * @param options.chainIds - Array of chain IDs to connect to
164
216
  * @returns A promise that resolves with the connected accounts and chain ID
165
217
  */
166
- connect({ chainId, account, forceRequest, }?: {
167
- chainId: number;
168
- account?: string | undefined;
169
- forceRequest?: boolean;
170
- }): Promise<{
218
+ connect({ account, forceRequest, chainIds }?: ConnectOptions): Promise<{
171
219
  accounts: Address[];
172
220
  chainId: number;
173
221
  }>;
174
222
  /**
175
223
  * Connects to the wallet and signs a message using personal_sign.
176
224
  *
177
- * @param message - The message to sign
225
+ * @param options - The connection options
226
+ * @param options.message - The message to sign after connecting
227
+ * @param options.chainIds - Optional chain IDs to connect to (defaults to ethereum mainnet if not provided)
178
228
  * @returns A promise that resolves with the signature
179
229
  * @throws Error if the selected account is not available after timeout
180
230
  */
181
- connectAndSign(message: string): Promise<string>;
231
+ connectAndSign({ message, chainIds, }: {
232
+ message: string;
233
+ chainIds?: number[];
234
+ }): Promise<string>;
182
235
  /**
183
236
  * Connects to the wallet and invokes a method with specified parameters.
184
237
  *
185
238
  * @param options - The options for connecting and invoking the method
186
239
  * @param options.method - The method name to invoke
187
240
  * @param options.params - The parameters to pass to the method, or a function that receives the account and returns params
188
- * @param options.chainId - Optional chain ID to connect to (defaults to mainnet)
241
+ * @param options.chainIds - Optional chain IDs to connect to (defaults to ethereum mainnet if not provided)
189
242
  * @param options.account - Optional specific account to connect to
190
243
  * @param options.forceRequest - Whether to force a request regardless of an existing session
191
244
  * @returns A promise that resolves with the result of the method invocation
192
245
  * @throws Error if the selected account is not available after timeout (for methods that require an account)
193
246
  */
194
- connectWith({ method, params, chainId, account, forceRequest, }: {
247
+ connectWith({ method, params, chainIds, account, forceRequest, }: {
195
248
  method: string;
196
249
  params: unknown[] | ((account: Address) => unknown[]);
197
- chainId?: number;
250
+ chainIds?: number[];
198
251
  account?: string | undefined;
199
252
  forceRequest?: boolean;
200
253
  }): Promise<unknown>;
@@ -264,9 +317,9 @@ declare class MetamaskConnectEVM {
264
317
  * @param options.eventHandlers - The event handlers to use for the Metamask Connect/EVM layer
265
318
  * @returns The Metamask Connect/EVM layer instance
266
319
  */
267
- declare function createMetamaskConnectEVM(options: Pick<MultichainOptions, 'dapp' | 'api'> & {
320
+ declare function createEVMClient(options: Pick<MultichainOptions, 'dapp' | 'api'> & {
268
321
  eventHandlers?: Partial<EventHandlers>;
269
322
  debug?: boolean;
270
323
  }): Promise<MetamaskConnectEVM>;
271
324
 
272
- export { type AddEthereumChainParameter, type Address, type CaipAccountId, type CaipChainId, EIP1193Provider, type EIP1193ProviderEvents, type EventHandlers, type Hex, MetamaskConnectEVM, type MetamaskConnectEVMOptions, type ProviderRequest, type ProviderRequestInterceptor, createMetamaskConnectEVM };
325
+ export { type AddEthereumChainParameter, type Address, type CaipAccountId, type CaipChainId, EIP1193Provider, type EIP1193ProviderEvents, type EventHandlers, type Hex, type JsonRpcCallback, type JsonRpcRequest, type JsonRpcResponse, MetamaskConnectEVM, type MetamaskConnectEVMOptions, type ProviderRequest, type ProviderRequestInterceptor, createEVMClient };
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@metamask/connect-evm",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "EVM Layer for MetaMask Connect",
5
5
  "keywords": [
6
6
  "MetaMask",
7
7
  "Ethereum"
8
8
  ],
9
- "homepage": "https://github.com/MetaMask/metamask-connect-monorepo/tree/main/packages/connect-evm#readme",
9
+ "homepage": "https://github.com/MetaMask/connect-monorepo/tree/main/packages/connect-evm#readme",
10
10
  "bugs": {
11
11
  "url": ""
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "https://github.com/MetaMask/metamask-connect-monorepo.git"
15
+ "url": "https://github.com/MetaMask/connect-monorepo.git"
16
16
  },
17
17
  "license": "MIT",
18
18
  "sideEffects": false,
@@ -56,7 +56,7 @@
56
56
  "dependencies": {
57
57
  "@metamask/analytics": "^0.2.0",
58
58
  "@metamask/chain-agnostic-permission": "^1.2.2",
59
- "@metamask/connect-multichain": "^0.3.2",
59
+ "@metamask/connect-multichain": "^0.5.0",
60
60
  "@metamask/utils": "^11.8.1"
61
61
  },
62
62
  "devDependencies": {
@@ -68,11 +68,11 @@
68
68
  "tsup": "^8.4.0",
69
69
  "typedoc": "^0.28.14",
70
70
  "typedoc-plugin-missing-exports": "^4.1.2",
71
- "typescript": "~5.9.2",
71
+ "typescript": "^5.9.3",
72
72
  "vitest": "^3.1.2"
73
73
  },
74
74
  "engines": {
75
- "node": "^18.18 || >=20"
75
+ "node": ">=20.19.0"
76
76
  },
77
77
  "publishConfig": {
78
78
  "access": "public",