@metamask/connect-evm 0.4.0 → 0.5.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.
- package/CHANGELOG.md +30 -1
- package/README.md +448 -41
- package/dist/browser/es/connect-evm.mjs +222 -151
- package/dist/browser/es/connect-evm.mjs.map +1 -1
- package/dist/src/connect.d.ts +55 -31
- package/dist/src/connect.d.ts.map +1 -1
- package/dist/src/connect.js +203 -153
- package/dist/src/connect.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/logger.d.ts +1 -1
- package/dist/src/logger.d.ts.map +1 -1
- package/dist/src/logger.js +3 -5
- package/dist/src/logger.js.map +1 -1
- package/dist/src/provider.d.ts +3 -1
- package/dist/src/provider.d.ts.map +1 -1
- package/dist/src/provider.js +6 -9
- package/dist/src/provider.js.map +1 -1
- package/dist/src/types.d.ts +7 -6
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/infura.d.ts +3 -0
- package/dist/src/utils/infura.d.ts.map +1 -0
- package/dist/src/utils/infura.js +16 -0
- package/dist/src/utils/infura.js.map +1 -0
- package/dist/types/index.d.ts +68 -41
- package/package.json +5 -4
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.5.0]
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- **BREAKING** Standardize `chainId` to use `Hex` format throughout the public API ([#150](https://github.com/MetaMask/connect-monorepo/pull/150))
|
|
15
|
+
- `connect()`, `connectAndSign()`, and `connectWith()` now expect `chainIds` as hex strings instead of decimal numbers
|
|
16
|
+
- `connect()` now returns `{ accounts, chainId: Hex }` instead of `{ accounts, chainId: number }`
|
|
17
|
+
- `switchChain()` now expects `chainId: Hex` instead of `chainId: number | Hex`
|
|
18
|
+
- `createEVMClient()` param option `api.supportedNetworks` now expects hex chain IDs as keys (e.g., `'0x1'`) instead of CAIP chain IDs
|
|
19
|
+
- Event handler types for `connectAndSign` and `connectWith` now use `Hex` for `chainId`
|
|
20
|
+
- **BREAKING** `getInfuraRpcUrls` now returns a rpc url map keyed by hex chain ID rather than CAIP Chain ID ([#152](https://github.com/MetaMask/connect-monorepo/pull/152))
|
|
21
|
+
- The `debug` option param used by `createEVMClient()` now enables console debug logs of the underlying `MultichainClient` instance ([#149](https://github.com/MetaMask/connect-monorepo/pull/149))
|
|
22
|
+
- update `connect()` and `createEVMClient()` typings to be more accurate ([#153](https://github.com/MetaMask/connect-monorepo/pull/153))
|
|
23
|
+
- update `switchChain()` to return `Promise<void>` ([#153](https://github.com/MetaMask/connect-monorepo/pull/153))
|
|
24
|
+
- Make `ConnectEvm` rely on `wallet_sessionChanged` events from `ConnectMultichain` rather than explicit connect/disconnect events ([#157](https://github.com/MetaMask/connect-monorepo/pull/157))
|
|
25
|
+
- Chain add/switch deeplink now calls `openSimpleDeeplinkIfNeeded()` instead of `openDeeplinkIfNeeded()` to align with `@metamask/connect-multichain` changes ([#176](https://github.com/MetaMask/connect-monorepo/pull/176))
|
|
26
|
+
|
|
27
|
+
### Fixed
|
|
28
|
+
|
|
29
|
+
- Fix `display_uri` and `wallet_sessionChanged` events not firing after disconnect and reconnect in headless mode ([#170](https://github.com/MetaMask/connect-monorepo/pull/170))
|
|
30
|
+
|
|
31
|
+
## [0.4.1]
|
|
32
|
+
|
|
33
|
+
### Fixed
|
|
34
|
+
|
|
35
|
+
- `createEVMClient` now ensures session resumption is properly awaited before returning the client instance. Previously there was a race condition which made it possible to make requests to the EIP-1193 provider before the underlying session was fully ready ([#141](https://github.com/MetaMask/connect-monorepo/pull/141))
|
|
36
|
+
|
|
10
37
|
## [0.4.0]
|
|
11
38
|
|
|
12
39
|
### Added
|
|
@@ -92,7 +119,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
92
119
|
|
|
93
120
|
- Initial release ([#58](https://github.com/MetaMask/connect-monorepo/pull/58))
|
|
94
121
|
|
|
95
|
-
[Unreleased]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-evm@0.
|
|
122
|
+
[Unreleased]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-evm@0.5.0...HEAD
|
|
123
|
+
[0.5.0]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-evm@0.4.1...@metamask/connect-evm@0.5.0
|
|
124
|
+
[0.4.1]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-evm@0.4.0...@metamask/connect-evm@0.4.1
|
|
96
125
|
[0.4.0]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-evm@0.3.1...@metamask/connect-evm@0.4.0
|
|
97
126
|
[0.3.1]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-evm@0.3.0...@metamask/connect-evm@0.3.1
|
|
98
127
|
[0.3.0]: https://github.com/MetaMask/connect-monorepo/compare/@metamask/connect-evm@0.2.0...@metamask/connect-evm@0.3.0
|
package/README.md
CHANGED
|
@@ -22,53 +22,52 @@ or
|
|
|
22
22
|
npm install @metamask/connect-evm
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
##
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Quick Start
|
|
26
28
|
|
|
27
29
|
```typescript
|
|
28
|
-
import { createEVMClient } from '@metamask/connect-evm';
|
|
30
|
+
import { createEVMClient, getInfuraRpcUrls } from '@metamask/connect-evm';
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
const sdk = await createEVMClient({
|
|
32
|
+
const client = await createEVMClient({
|
|
32
33
|
dapp: {
|
|
33
34
|
name: 'My DApp',
|
|
34
35
|
url: 'https://mydapp.com',
|
|
35
36
|
},
|
|
37
|
+
api: {
|
|
38
|
+
supportedNetworks: {
|
|
39
|
+
// use the `getInfuraRpcUrls` helper to generate a map of Infura RPC endpoints
|
|
40
|
+
...getInfuraRpcUrls(INFURA_API_KEY),
|
|
41
|
+
// or specify your own CAIP Chain ID to rpc endpoint mapping
|
|
42
|
+
// Hex chain IDs mapped to RPC URLs
|
|
43
|
+
'0x1': 'https://mainnet.infura.io/v3/YOUR_KEY', // Ethereum Mainnet
|
|
44
|
+
'0x89': 'https://polygon-mainnet.infura.io/v3/YOUR_KEY', // Polygon
|
|
45
|
+
},
|
|
46
|
+
},
|
|
36
47
|
});
|
|
37
48
|
|
|
38
49
|
// Connect to MetaMask
|
|
39
|
-
|
|
50
|
+
let accounts, chainId;
|
|
51
|
+
try {
|
|
52
|
+
({ accounts, chainId } = await client.connect({ chainIds: ['0x1', '0x89'] })); // Connect to Ethereum Mainnet and Polygon
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error.code === 4001) {
|
|
55
|
+
console.log('User rejected the connection request');
|
|
56
|
+
} else if (error.code === -32002) {
|
|
57
|
+
console.log('Connection request already pending');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
console.log({ accounts }); // The connected accounts where the first account is the selected account
|
|
61
|
+
console.log({ chainId }); // The currently active chainId
|
|
40
62
|
|
|
41
63
|
// Get the EIP-1193 provider
|
|
42
|
-
const provider =
|
|
43
|
-
|
|
44
|
-
// Request accounts
|
|
45
|
-
const accounts = await provider.request({
|
|
46
|
-
method: 'eth_accounts',
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Usage
|
|
51
|
-
|
|
52
|
-
### Basic Connection
|
|
64
|
+
const provider = client.getProvider();
|
|
53
65
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
dapp: {
|
|
59
|
-
name: 'My DApp',
|
|
60
|
-
url: 'https://mydapp.com',
|
|
61
|
-
},
|
|
66
|
+
// Sign a message
|
|
67
|
+
const signedMessage = await provider.request({
|
|
68
|
+
method: 'personal_sign',
|
|
69
|
+
params: ['0x0', accounts[0]],
|
|
62
70
|
});
|
|
63
|
-
|
|
64
|
-
// Connect with default chain (mainnet)
|
|
65
|
-
const { accounts, chainId } = await sdk.connect();
|
|
66
|
-
|
|
67
|
-
// Connect to a specific chain
|
|
68
|
-
await sdk.connect({ chainId: 137 }); // Polygon
|
|
69
|
-
|
|
70
|
-
// Connect to a specific chain and account
|
|
71
|
-
await sdk.connect({ chainId: 1, account: '0x...' });
|
|
72
71
|
```
|
|
73
72
|
|
|
74
73
|
### React Native Support
|
|
@@ -79,14 +78,15 @@ When using `@metamask/connect-evm` in React Native, the standard browser deeplin
|
|
|
79
78
|
import { Linking } from 'react-native';
|
|
80
79
|
import { createEVMClient } from '@metamask/connect-evm';
|
|
81
80
|
|
|
82
|
-
const
|
|
81
|
+
const client = await createEVMClient({
|
|
83
82
|
dapp: {
|
|
84
83
|
name: 'My React Native DApp',
|
|
85
84
|
url: 'https://mydapp.com',
|
|
86
85
|
},
|
|
87
86
|
api: {
|
|
87
|
+
// Chain IDs are specified in hex format
|
|
88
88
|
supportedNetworks: {
|
|
89
|
-
'
|
|
89
|
+
'0x1': 'https://mainnet.infura.io/v3/YOUR_KEY',
|
|
90
90
|
},
|
|
91
91
|
},
|
|
92
92
|
// React Native: use Linking.openURL for deeplinks
|
|
@@ -97,7 +97,7 @@ const sdk = await createEVMClient({
|
|
|
97
97
|
});
|
|
98
98
|
},
|
|
99
99
|
},
|
|
100
|
-
}
|
|
100
|
+
});
|
|
101
101
|
```
|
|
102
102
|
|
|
103
103
|
The `mobile.preferredOpenLink` option is checked before falling back to browser-based deeplink methods, making it the recommended approach for React Native applications.
|
|
@@ -105,7 +105,7 @@ The `mobile.preferredOpenLink` option is checked before falling back to browser-
|
|
|
105
105
|
### Using the Provider Directly
|
|
106
106
|
|
|
107
107
|
```typescript
|
|
108
|
-
const provider =
|
|
108
|
+
const provider = client.getProvider();
|
|
109
109
|
|
|
110
110
|
// Send transaction
|
|
111
111
|
const txHash = await provider.request({
|
|
@@ -135,10 +135,6 @@ const result = await provider.request({
|
|
|
135
135
|
|
|
136
136
|
Check out the [playground examples](../../playground/browser-playground) for a complete React implementation.
|
|
137
137
|
|
|
138
|
-
## TypeScript
|
|
139
|
-
|
|
140
|
-
This package is written in TypeScript and includes full type definitions. No additional `@types` package is required.
|
|
141
|
-
|
|
142
138
|
## Development
|
|
143
139
|
|
|
144
140
|
This package is part of the MetaMask Connect monorepo. From the repo root:
|
|
@@ -157,6 +153,417 @@ yarn workspace @metamask/connect-evm run format:fix
|
|
|
157
153
|
yarn workspace @metamask/connect-evm run test
|
|
158
154
|
```
|
|
159
155
|
|
|
156
|
+
## TypeScript
|
|
157
|
+
|
|
158
|
+
This package is written in TypeScript and includes full type definitions. No additional `@types` package is required.
|
|
159
|
+
|
|
160
|
+
## API Reference
|
|
161
|
+
|
|
162
|
+
### `createEVMClient(options)`
|
|
163
|
+
|
|
164
|
+
Factory function to create a new MetaMask Connect EVM instance.
|
|
165
|
+
|
|
166
|
+
#### Parameters
|
|
167
|
+
|
|
168
|
+
| Option | Type | Required | Description |
|
|
169
|
+
| -------------------------- | --------------------------------------------- | -------- | ---------------------------------------------------- |
|
|
170
|
+
| `dapp.name` | `string` | Yes | Name of your dApp |
|
|
171
|
+
| `api.supportedNetworks` | `Record<Hex, string>` | Yes | Map of hex chain IDs to RPC URLs |
|
|
172
|
+
| `dapp.url` | `string` | No | URL of your dApp |
|
|
173
|
+
| `dapp.iconUrl` | `string` | No | Icon URL for your dApp |
|
|
174
|
+
| `ui.headless` | `boolean` | No | Run without UI (for custom QR implementations) |
|
|
175
|
+
| `ui.preferExtension` | `boolean` | No | Prefer browser extension over mobile (default: true) |
|
|
176
|
+
| `ui.showInstallModal` | `boolean` | No | Show installation modal for desktop |
|
|
177
|
+
| `mobile.preferredOpenLink` | `(deeplink: string, target?: string) => void` | No | Custom deeplink handler |
|
|
178
|
+
| `mobile.useDeeplink` | `boolean` | No | Use `metamask://` instead of universal links |
|
|
179
|
+
| `transport.extensionId` | `string` | No | Custom extension ID |
|
|
180
|
+
| `transport.onNotification` | `(notification: unknown) => void` | No | Notification handler |
|
|
181
|
+
| `eventHandlers` | `Partial<EventHandlers>` | No | Event handlers for provider events |
|
|
182
|
+
| `debug` | `boolean` | No | Enable debug logging |
|
|
183
|
+
|
|
184
|
+
#### Returns
|
|
185
|
+
|
|
186
|
+
`Promise<MetamaskConnectEVM>` - A fully initialized SDK instance.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const client = await createEVMClient({
|
|
190
|
+
dapp: { name: 'My DApp', url: 'https://mydapp.com' },
|
|
191
|
+
api: {
|
|
192
|
+
supportedNetworks: {
|
|
193
|
+
'0x1': 'https://mainnet.infura.io/v3/KEY',
|
|
194
|
+
'0x89': 'https://polygon-mainnet.infura.io/v3/KEY',
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
eventHandlers: {
|
|
198
|
+
accountsChanged: (accounts) => console.log('Accounts:', accounts),
|
|
199
|
+
chainChanged: (chainId) => console.log('Chain:', chainId),
|
|
200
|
+
},
|
|
201
|
+
debug: true,
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
### `MetamaskConnectEVM`
|
|
208
|
+
|
|
209
|
+
The main SDK class providing EVM connectivity.
|
|
210
|
+
|
|
211
|
+
#### Methods
|
|
212
|
+
|
|
213
|
+
##### `connect(options?)`
|
|
214
|
+
|
|
215
|
+
Connects to MetaMask wallet.
|
|
216
|
+
|
|
217
|
+
**Parameters**
|
|
218
|
+
|
|
219
|
+
| Name | Type | Required | Description |
|
|
220
|
+
| ---------------------- | --------- | -------- | ---------------------------------------------------------------------------------------- |
|
|
221
|
+
| `options.chainIds` | `Hex[]` | No | Array of hex chain IDs to request permission for (defaults to `['0x1']` if not provided) |
|
|
222
|
+
| `options.account` | `string` | No | Specific account address to connect |
|
|
223
|
+
| `options.forceRequest` | `boolean` | No | Force a new connection request even if already connected |
|
|
224
|
+
|
|
225
|
+
**Returns**
|
|
226
|
+
|
|
227
|
+
`Promise<{ accounts: Address[]; chainId: Hex }>` - The connected accounts and active chain ID.
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
const { accounts, chainId } = await client.connect({
|
|
231
|
+
chainIds: ['0x1', '0x89'], // Ethereum Mainnet and Polygon
|
|
232
|
+
account: '0x...',
|
|
233
|
+
forceRequest: false,
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
##### `connectAndSign(options)`
|
|
238
|
+
|
|
239
|
+
Connects and immediately signs a message using `personal_sign`.
|
|
240
|
+
|
|
241
|
+
**Parameters**
|
|
242
|
+
|
|
243
|
+
| Name | Type | Required | Description |
|
|
244
|
+
| ------------------ | -------- | -------- | --------------------------------------------------- |
|
|
245
|
+
| `options.message` | `string` | Yes | The message to sign after connecting |
|
|
246
|
+
| `options.chainIds` | `Hex[]` | No | Hex chain IDs to connect to (defaults to `['0x1']`) |
|
|
247
|
+
|
|
248
|
+
**Returns**
|
|
249
|
+
|
|
250
|
+
`Promise<string>` - The signature as a hex string.
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
const signature = await client.connectAndSign({
|
|
254
|
+
message: 'Sign this message',
|
|
255
|
+
chainIds: ['0x1'],
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
##### `connectWith(options)`
|
|
260
|
+
|
|
261
|
+
Connects and immediately invokes a method with specified parameters.
|
|
262
|
+
|
|
263
|
+
**Parameters**
|
|
264
|
+
|
|
265
|
+
| Name | Type | Required | Description |
|
|
266
|
+
| ---------------------- | ------------------------------------------------ | -------- | --------------------------------------------------------------------------------------- |
|
|
267
|
+
| `options.method` | `string` | Yes | The RPC method name to invoke |
|
|
268
|
+
| `options.params` | `unknown[] \| ((account: Address) => unknown[])` | Yes | Method parameters, or a function that receives the connected account and returns params |
|
|
269
|
+
| `options.chainIds` | `Hex[]` | No | Hex chain IDs to connect to (defaults to `['0x1']`) |
|
|
270
|
+
| `options.account` | `string` | No | Specific account to connect |
|
|
271
|
+
| `options.forceRequest` | `boolean` | No | Force a new connection request |
|
|
272
|
+
|
|
273
|
+
**Returns**
|
|
274
|
+
|
|
275
|
+
`Promise<unknown>` - The result of the method invocation.
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
const result = await client.connectWith({
|
|
279
|
+
method: 'eth_sendTransaction',
|
|
280
|
+
params: (account) => [
|
|
281
|
+
{
|
|
282
|
+
from: account,
|
|
283
|
+
to: '0x...',
|
|
284
|
+
value: '0x1',
|
|
285
|
+
},
|
|
286
|
+
],
|
|
287
|
+
chainIds: ['0x1'],
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
##### `disconnect()`
|
|
292
|
+
|
|
293
|
+
Disconnects from the wallet and cleans up resources.
|
|
294
|
+
|
|
295
|
+
**Parameters**
|
|
296
|
+
|
|
297
|
+
None.
|
|
298
|
+
|
|
299
|
+
**Returns**
|
|
300
|
+
|
|
301
|
+
`Promise<void>`
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
await client.disconnect();
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
##### `switchChain(options)`
|
|
308
|
+
|
|
309
|
+
Switches to a different chain. Will attempt to add the chain if not configured in the wallet.
|
|
310
|
+
|
|
311
|
+
**Parameters**
|
|
312
|
+
|
|
313
|
+
| Name | Type | Required | Description |
|
|
314
|
+
| ---------------------------- | --------------------------- | -------- | --------------------------------------------------------- |
|
|
315
|
+
| `options.chainId` | `Hex` | Yes | The hex chain ID to switch to |
|
|
316
|
+
| `options.chainConfiguration` | `AddEthereumChainParameter` | No | Chain configuration to use if the chain needs to be added |
|
|
317
|
+
|
|
318
|
+
**Returns**
|
|
319
|
+
|
|
320
|
+
`Promise<void>`
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
await client.switchChain({
|
|
324
|
+
chainId: '0x89',
|
|
325
|
+
chainConfiguration: {
|
|
326
|
+
chainId: '0x89',
|
|
327
|
+
chainName: 'Polygon',
|
|
328
|
+
nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 },
|
|
329
|
+
rpcUrls: ['https://polygon-rpc.com'],
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
##### `getProvider()`
|
|
335
|
+
|
|
336
|
+
Returns the EIP-1193 provider instance.
|
|
337
|
+
|
|
338
|
+
**Parameters**
|
|
339
|
+
|
|
340
|
+
None.
|
|
341
|
+
|
|
342
|
+
**Returns**
|
|
343
|
+
|
|
344
|
+
`EIP1193Provider` - The EIP-1193 compliant provider.
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
const provider = client.getProvider();
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
##### `getChainId()`
|
|
351
|
+
|
|
352
|
+
Returns the currently selected chain ID.
|
|
353
|
+
|
|
354
|
+
**Parameters**
|
|
355
|
+
|
|
356
|
+
None.
|
|
357
|
+
|
|
358
|
+
**Returns**
|
|
359
|
+
|
|
360
|
+
`Hex | undefined` - The currently selected chain ID as a hex string, or undefined if not connected.
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
const chainId = client.getChainId(); // e.g., '0x1'
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
##### `getAccount()`
|
|
367
|
+
|
|
368
|
+
Returns the currently selected account.
|
|
369
|
+
|
|
370
|
+
**Parameters**
|
|
371
|
+
|
|
372
|
+
None.
|
|
373
|
+
|
|
374
|
+
**Returns**
|
|
375
|
+
|
|
376
|
+
`Address | undefined` - The currently selected account address, or undefined if not connected.
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
const account = client.getAccount(); // e.g., '0x...'
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
#### Properties
|
|
383
|
+
|
|
384
|
+
| Property | Type | Description |
|
|
385
|
+
| ----------------- | ---------------------- | --------------------------------------------------------------------------------------------- |
|
|
386
|
+
| `accounts` | `Address[]` | Currently permitted accounts |
|
|
387
|
+
| `selectedAccount` | `Address \| undefined` | Currently selected account |
|
|
388
|
+
| `selectedChainId` | `Hex \| undefined` | Currently selected chain ID (hex) |
|
|
389
|
+
| `status` | `ConnectionStatus` | Connection status ( `'loaded'`, `'pending'`, `'connecting'`, `'connected'`, `'disconnected'`) |
|
|
390
|
+
|
|
391
|
+
---
|
|
392
|
+
|
|
393
|
+
### `EIP1193Provider`
|
|
394
|
+
|
|
395
|
+
EIP-1193 compliant provider for making Ethereum JSON-RPC requests.
|
|
396
|
+
|
|
397
|
+
#### Methods
|
|
398
|
+
|
|
399
|
+
##### `request(args)`
|
|
400
|
+
|
|
401
|
+
Makes an Ethereum JSON-RPC request.
|
|
402
|
+
|
|
403
|
+
**Parameters**
|
|
404
|
+
|
|
405
|
+
| Name | Type | Required | Description |
|
|
406
|
+
| ------------- | --------- | -------- | --------------------- |
|
|
407
|
+
| `args.method` | `string` | Yes | The RPC method name |
|
|
408
|
+
| `args.params` | `unknown` | No | The method parameters |
|
|
409
|
+
|
|
410
|
+
**Returns**
|
|
411
|
+
|
|
412
|
+
`Promise<unknown>` - The result of the RPC call.
|
|
413
|
+
|
|
414
|
+
```typescript
|
|
415
|
+
const result = await provider.request({
|
|
416
|
+
method: 'eth_getBalance',
|
|
417
|
+
params: ['0x...', 'latest'],
|
|
418
|
+
});
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
##### `sendAsync(request, callback?)` _(deprecated)_
|
|
422
|
+
|
|
423
|
+
Legacy method for JSON-RPC requests with callback support.
|
|
424
|
+
|
|
425
|
+
**Parameters**
|
|
426
|
+
|
|
427
|
+
| Name | Type | Required | Description |
|
|
428
|
+
| ----------------- | ------------------ | -------- | -------------------------- |
|
|
429
|
+
| `request.method` | `string` | Yes | The RPC method name |
|
|
430
|
+
| `request.params` | `unknown` | No | The method parameters |
|
|
431
|
+
| `request.id` | `number \| string` | No | Request ID (defaults to 1) |
|
|
432
|
+
| `request.jsonrpc` | `'2.0'` | No | JSON-RPC version |
|
|
433
|
+
| `callback` | `JsonRpcCallback` | No | Optional callback function |
|
|
434
|
+
|
|
435
|
+
**Returns**
|
|
436
|
+
|
|
437
|
+
`Promise<JsonRpcResponse> | void` - Returns a promise if no callback is provided, otherwise void.
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
provider.sendAsync(
|
|
441
|
+
{ method: 'eth_accounts', params: [] },
|
|
442
|
+
(error, response) => {
|
|
443
|
+
if (error) console.error(error);
|
|
444
|
+
else console.log(response.result);
|
|
445
|
+
},
|
|
446
|
+
);
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
##### `send(request, callback)` _(deprecated)_
|
|
450
|
+
|
|
451
|
+
Legacy synchronous-style method for JSON-RPC requests.
|
|
452
|
+
|
|
453
|
+
**Parameters**
|
|
454
|
+
|
|
455
|
+
| Name | Type | Required | Description |
|
|
456
|
+
| ---------------- | ----------------- | -------- | ----------------------------------------- |
|
|
457
|
+
| `request.method` | `string` | Yes | The RPC method name |
|
|
458
|
+
| `request.params` | `unknown` | No | The method parameters |
|
|
459
|
+
| `callback` | `JsonRpcCallback` | Yes | Callback function to receive the response |
|
|
460
|
+
|
|
461
|
+
**Returns**
|
|
462
|
+
|
|
463
|
+
`void`
|
|
464
|
+
|
|
465
|
+
#### Events
|
|
466
|
+
|
|
467
|
+
The provider extends `EventEmitter` and emits standard EIP-1193 events:
|
|
468
|
+
|
|
469
|
+
| Event | Payload | Description |
|
|
470
|
+
| ----------------- | --------------------------------- | -------------------------------------- |
|
|
471
|
+
| `connect` | `{ chainId: string }` | Emitted when connected |
|
|
472
|
+
| `disconnect` | - | Emitted when disconnected |
|
|
473
|
+
| `accountsChanged` | `Address[]` | Emitted when accounts change |
|
|
474
|
+
| `chainChanged` | `Hex` | Emitted when chain changes |
|
|
475
|
+
| `message` | `{ type: string, data: unknown }` | Emitted for provider messages |
|
|
476
|
+
| `display_uri` | `string` | Emitted with QR code URI for custom UI |
|
|
477
|
+
|
|
478
|
+
```typescript
|
|
479
|
+
provider.on('accountsChanged', (accounts) => {
|
|
480
|
+
console.log('New accounts:', accounts);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
provider.on('chainChanged', (chainId) => {
|
|
484
|
+
console.log('New chain:', chainId);
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
provider.on('display_uri', (uri) => {
|
|
488
|
+
// Display custom QR code with this URI
|
|
489
|
+
});
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
#### Properties
|
|
493
|
+
|
|
494
|
+
| Property | Type | Description |
|
|
495
|
+
| ----------------- | ---------------------- | -------------------------------------------------- |
|
|
496
|
+
| `accounts` | `Address[]` | Currently permitted accounts |
|
|
497
|
+
| `selectedAccount` | `Address \| undefined` | Currently selected account |
|
|
498
|
+
| `selectedChainId` | `Hex \| undefined` | Currently selected chain ID |
|
|
499
|
+
| `chainId` | `Hex \| undefined` | Alias for `selectedChainId` (legacy compatibility) |
|
|
500
|
+
|
|
501
|
+
---
|
|
502
|
+
|
|
503
|
+
### `getInfuraRpcUrls(infuraApiKey)`
|
|
504
|
+
|
|
505
|
+
Helper function to generate EVM Infura RPC URLs for common networks keyed by hex chain ID.
|
|
506
|
+
|
|
507
|
+
**Parameters**
|
|
508
|
+
|
|
509
|
+
| Name | Type | Required | Description |
|
|
510
|
+
| -------------- | -------- | -------- | ------------------- |
|
|
511
|
+
| `infuraApiKey` | `string` | Yes | Your Infura API key |
|
|
512
|
+
|
|
513
|
+
**Returns**
|
|
514
|
+
|
|
515
|
+
A map of hex chain IDs to Infura RPC URLs. See https://docs.metamask.io/services
|
|
516
|
+
|
|
517
|
+
```typescript
|
|
518
|
+
import { getInfuraRpcUrls } from '@metamask/connect-evm';
|
|
519
|
+
|
|
520
|
+
const rpcUrls = getInfuraRpcUrls('YOUR_INFURA_KEY');
|
|
521
|
+
// Returns: { '0x1': 'https://mainnet.infura.io/v3/KEY', ... }
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
---
|
|
525
|
+
|
|
526
|
+
### Types
|
|
527
|
+
|
|
528
|
+
#### `EventHandlers`
|
|
529
|
+
|
|
530
|
+
```typescript
|
|
531
|
+
type EventHandlers = {
|
|
532
|
+
connect: (result: { chainId: Hex; accounts: Address[] }) => void;
|
|
533
|
+
disconnect: () => void;
|
|
534
|
+
accountsChanged: (accounts: Address[]) => void;
|
|
535
|
+
chainChanged: (chainId: Hex) => void;
|
|
536
|
+
displayUri: (uri: string) => void;
|
|
537
|
+
connectAndSign: (result: {
|
|
538
|
+
accounts: Address[];
|
|
539
|
+
chainId: Hex;
|
|
540
|
+
signResponse: string;
|
|
541
|
+
}) => void;
|
|
542
|
+
connectWith: (result: {
|
|
543
|
+
accounts: Address[];
|
|
544
|
+
chainId: Hex;
|
|
545
|
+
connectWithResponse: unknown;
|
|
546
|
+
}) => void;
|
|
547
|
+
};
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
#### `AddEthereumChainParameter`
|
|
551
|
+
|
|
552
|
+
```typescript
|
|
553
|
+
type AddEthereumChainParameter = {
|
|
554
|
+
chainId?: string;
|
|
555
|
+
chainName?: string;
|
|
556
|
+
nativeCurrency?: {
|
|
557
|
+
name?: string;
|
|
558
|
+
symbol?: string;
|
|
559
|
+
decimals?: number;
|
|
560
|
+
};
|
|
561
|
+
rpcUrls?: string[];
|
|
562
|
+
blockExplorerUrls?: string[];
|
|
563
|
+
iconUrls?: string[];
|
|
564
|
+
};
|
|
565
|
+
```
|
|
566
|
+
|
|
160
567
|
## Contributing
|
|
161
568
|
|
|
162
569
|
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/connect-monorepo#readme).
|