@metamask/connect-evm 1.4.0 → 2.0.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 +25 -1
- package/README.md +20 -1
- package/dist/browser/es/connect-evm.mjs +358 -47
- package/dist/browser/es/connect-evm.mjs.map +1 -1
- package/dist/src/connect.d.ts +11 -2
- package/dist/src/connect.d.ts.map +1 -1
- package/dist/src/connect.js +117 -22
- package/dist/src/connect.js.map +1 -1
- package/dist/src/eip6963.d.ts +80 -0
- package/dist/src/eip6963.d.ts.map +1 -0
- package/dist/src/eip6963.js +297 -0
- package/dist/src/eip6963.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/utils/infura.js.map +1 -1
- package/dist/types/index.d.ts +21 -3
- package/package.json +9 -4
|
@@ -88,6 +88,7 @@ import {
|
|
|
88
88
|
TransportType
|
|
89
89
|
} from "@metamask/connect-multichain";
|
|
90
90
|
import { hexToNumber as hexToNumber3 } from "@metamask/utils";
|
|
91
|
+
import { satisfies } from "semver";
|
|
91
92
|
|
|
92
93
|
// src/constants.ts
|
|
93
94
|
var IGNORED_METHODS = [
|
|
@@ -119,6 +120,9 @@ var CONNECT_EVM_SESSION_PROPERTIES = {
|
|
|
119
120
|
[EIP1193_COMPATIBLE_SESSION_PROPERTY]: true
|
|
120
121
|
};
|
|
121
122
|
|
|
123
|
+
// src/assets/metamask-fox.svg
|
|
124
|
+
var metamask_fox_default = 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 142 137">%0A <path fill="%23FF5C16" d="m132.24 131.751-30.481-9.076-22.986 13.741-16.038-.007-23-13.734-30.467 9.076L0 100.465l9.268-34.723L0 36.385 9.268 0l47.607 28.443h27.757L132.24 0l9.268 36.385-9.268 29.357 9.268 34.723-9.268 31.286Z"/>%0A <path fill="%23FF5C16" d="m9.274 0 47.608 28.463-1.893 19.534L9.274 0Zm30.468 100.478 20.947 15.957-20.947 6.24v-22.197Zm19.273-26.381L54.989 48.01l-25.77 17.74-.014-.007v.013l.08 18.26 10.45-9.918h19.28ZM132.24 0 84.632 28.463l1.887 19.534L132.24 0Zm-30.467 100.478-20.948 15.957 20.948 6.24v-22.197Zm10.529-34.723h.007-.007v-.013l-.006.007-25.77-17.739L82.5 74.097h19.272l10.457 9.917.073-18.259Z"/>%0A <path fill="%23E34807" d="m39.735 122.675-30.467 9.076L0 100.478h39.735v22.197ZM59.008 74.09l5.82 37.714-8.066-20.97-27.49-6.82 10.456-9.923h19.28Zm42.764 48.585 30.468 9.076 9.268-31.273h-39.736v22.197ZM82.5 74.09l-5.82 37.714 8.065-20.97 27.491-6.82-10.463-9.923H82.5Z"/>%0A <path fill="%23FF8D5D" d="m0 100.465 9.268-34.723h19.93l.073 18.266 27.492 6.82 8.065 20.969-4.146 4.618-20.947-15.957H0v.007Zm141.508 0-9.268-34.723h-19.931l-.073 18.266-27.49 6.82-8.066 20.969 4.145 4.618 20.948-15.957h39.735v.007ZM84.632 28.443H56.875L54.99 47.977l9.839 63.8H76.68l9.845-63.8-1.893-19.534Z"/>%0A <path fill="%23661800" d="M9.268 0 0 36.385l9.268 29.357h19.93l25.784-17.745L9.268 0Zm43.98 81.665h-9.029l-4.916 4.819 17.466 4.33-3.521-9.155v.006ZM132.24 0l9.268 36.385-9.268 29.357h-19.931L86.526 47.997 132.24 0ZM88.273 81.665h9.042l4.916 4.825-17.486 4.338 3.528-9.17v.007Zm-9.507 42.305 2.06-7.542-4.146-4.618H64.82l-4.145 4.618 2.059 7.542"/>%0A <path fill="%23C0C4CD" d="M78.766 123.969v12.453H62.735v-12.453h16.03Z"/>%0A <path fill="%23E7EBF6" d="m39.742 122.662 23.006 13.754v-12.453l-2.06-7.541-20.946 6.24Zm62.031 0-23.007 13.754v-12.453l2.06-7.541 20.947 6.24Z"/>%0A</svg>%0A';
|
|
125
|
+
|
|
122
126
|
// src/logger.ts
|
|
123
127
|
import {
|
|
124
128
|
createLogger,
|
|
@@ -130,6 +134,220 @@ var enableDebug = () => {
|
|
|
130
134
|
debug(namespace);
|
|
131
135
|
};
|
|
132
136
|
|
|
137
|
+
// src/eip6963.ts
|
|
138
|
+
var EIP6963_ANNOUNCE_PROVIDER_EVENT = "eip6963:announceProvider";
|
|
139
|
+
var EIP6963_REQUEST_PROVIDER_EVENT = "eip6963:requestProvider";
|
|
140
|
+
var EIP6963_DETECTION_TIMEOUT_MS = 300;
|
|
141
|
+
var CONNECT_EVM_EIP6963_NAME = "MetaMask";
|
|
142
|
+
var CONNECT_EVM_EIP6963_RDNS = "io.metamask.mmc";
|
|
143
|
+
var METAMASK_EIP6963_RDNS = [
|
|
144
|
+
"io.metamask",
|
|
145
|
+
"io.metamask.mobile"
|
|
146
|
+
];
|
|
147
|
+
var CONNECT_EVM_EIP6963_ICON = metamask_fox_default;
|
|
148
|
+
var getBrowserWindow = () => {
|
|
149
|
+
const { window: browserWindow } = globalThis;
|
|
150
|
+
return typeof (browserWindow == null ? void 0 : browserWindow.dispatchEvent) === "function" ? browserWindow : void 0;
|
|
151
|
+
};
|
|
152
|
+
var delay = (ms) => __async(null, null, function* () {
|
|
153
|
+
return yield new Promise((resolve) => setTimeout(resolve, ms));
|
|
154
|
+
});
|
|
155
|
+
var isNativeMetaMaskRdns = (rdns) => METAMASK_EIP6963_RDNS.some((nativeRdns) => nativeRdns === rdns);
|
|
156
|
+
var getAnnouncementRdns = (event) => {
|
|
157
|
+
var _a;
|
|
158
|
+
const { detail } = event;
|
|
159
|
+
const rdns = (_a = detail == null ? void 0 : detail.info) == null ? void 0 : _a.rdns;
|
|
160
|
+
return typeof rdns === "string" ? rdns : void 0;
|
|
161
|
+
};
|
|
162
|
+
var UUID_BYTE_TO_HEX = Array.from(
|
|
163
|
+
{ length: 256 },
|
|
164
|
+
(_, byte) => byte.toString(16).padStart(2, "0")
|
|
165
|
+
);
|
|
166
|
+
var formatUuidV4 = (bytes) => {
|
|
167
|
+
bytes[6] = bytes[6] % 16 + 64;
|
|
168
|
+
bytes[8] = bytes[8] % 64 + 128;
|
|
169
|
+
return [
|
|
170
|
+
UUID_BYTE_TO_HEX[bytes[0]],
|
|
171
|
+
UUID_BYTE_TO_HEX[bytes[1]],
|
|
172
|
+
UUID_BYTE_TO_HEX[bytes[2]],
|
|
173
|
+
UUID_BYTE_TO_HEX[bytes[3]],
|
|
174
|
+
"-",
|
|
175
|
+
UUID_BYTE_TO_HEX[bytes[4]],
|
|
176
|
+
UUID_BYTE_TO_HEX[bytes[5]],
|
|
177
|
+
"-",
|
|
178
|
+
UUID_BYTE_TO_HEX[bytes[6]],
|
|
179
|
+
UUID_BYTE_TO_HEX[bytes[7]],
|
|
180
|
+
"-",
|
|
181
|
+
UUID_BYTE_TO_HEX[bytes[8]],
|
|
182
|
+
UUID_BYTE_TO_HEX[bytes[9]],
|
|
183
|
+
"-",
|
|
184
|
+
UUID_BYTE_TO_HEX[bytes[10]],
|
|
185
|
+
UUID_BYTE_TO_HEX[bytes[11]],
|
|
186
|
+
UUID_BYTE_TO_HEX[bytes[12]],
|
|
187
|
+
UUID_BYTE_TO_HEX[bytes[13]],
|
|
188
|
+
UUID_BYTE_TO_HEX[bytes[14]],
|
|
189
|
+
UUID_BYTE_TO_HEX[bytes[15]]
|
|
190
|
+
].join("");
|
|
191
|
+
};
|
|
192
|
+
var createUuid = () => {
|
|
193
|
+
const { crypto: cryptoProvider } = globalThis;
|
|
194
|
+
if (cryptoProvider == null ? void 0 : cryptoProvider.randomUUID) {
|
|
195
|
+
return cryptoProvider.randomUUID();
|
|
196
|
+
}
|
|
197
|
+
if (cryptoProvider == null ? void 0 : cryptoProvider.getRandomValues) {
|
|
198
|
+
const bytes = new Uint8Array(16);
|
|
199
|
+
cryptoProvider.getRandomValues(bytes);
|
|
200
|
+
return formatUuidV4(bytes);
|
|
201
|
+
}
|
|
202
|
+
throw new Error("Web Crypto API is required for EIP-6963 announcement");
|
|
203
|
+
};
|
|
204
|
+
var createAnnouncementEvent = (detail) => {
|
|
205
|
+
const { CustomEvent: CustomEventConstructor } = globalThis;
|
|
206
|
+
return new CustomEventConstructor(EIP6963_ANNOUNCE_PROVIDER_EVENT, {
|
|
207
|
+
detail
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
var createRequestProviderEvent = () => {
|
|
211
|
+
const { Event: EventConstructor } = globalThis;
|
|
212
|
+
return new EventConstructor(EIP6963_REQUEST_PROVIDER_EVENT);
|
|
213
|
+
};
|
|
214
|
+
var createProviderInfo = () => Object.freeze({
|
|
215
|
+
uuid: createUuid(),
|
|
216
|
+
name: CONNECT_EVM_EIP6963_NAME,
|
|
217
|
+
icon: CONNECT_EVM_EIP6963_ICON,
|
|
218
|
+
rdns: CONNECT_EVM_EIP6963_RDNS
|
|
219
|
+
});
|
|
220
|
+
var hasNativeMetaMaskProvider = () => __async(null, null, function* () {
|
|
221
|
+
const browserWindow = getBrowserWindow();
|
|
222
|
+
if (!browserWindow) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
const announcedRdns = /* @__PURE__ */ new Set();
|
|
226
|
+
const handler = (event) => {
|
|
227
|
+
const rdns = getAnnouncementRdns(event);
|
|
228
|
+
if (rdns) {
|
|
229
|
+
announcedRdns.add(rdns);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
browserWindow.addEventListener(EIP6963_ANNOUNCE_PROVIDER_EVENT, handler);
|
|
233
|
+
try {
|
|
234
|
+
browserWindow.dispatchEvent(createRequestProviderEvent());
|
|
235
|
+
yield delay(EIP6963_DETECTION_TIMEOUT_MS);
|
|
236
|
+
} finally {
|
|
237
|
+
browserWindow.removeEventListener(EIP6963_ANNOUNCE_PROVIDER_EVENT, handler);
|
|
238
|
+
}
|
|
239
|
+
return [...announcedRdns].some(isNativeMetaMaskRdns);
|
|
240
|
+
});
|
|
241
|
+
var _provider, _detail, _suppressed, _requestHandler, _announcementPromise, _EIP6963ProviderAnnouncer_instances, announceOnce_fn, installRequestProviderListener_fn, dispatchAnnouncement_fn;
|
|
242
|
+
var EIP6963ProviderAnnouncer = class {
|
|
243
|
+
/**
|
|
244
|
+
* Creates an EIP-6963 announcer for one SDK provider instance.
|
|
245
|
+
*
|
|
246
|
+
* @param provider - EIP-1193 provider to announce.
|
|
247
|
+
*/
|
|
248
|
+
constructor(provider) {
|
|
249
|
+
__privateAdd(this, _EIP6963ProviderAnnouncer_instances);
|
|
250
|
+
/** EIP-1193 provider exposed in EIP-6963 announcements. */
|
|
251
|
+
__privateAdd(this, _provider);
|
|
252
|
+
/** Stable announcement payload for this SDK provider instance. */
|
|
253
|
+
__privateAdd(this, _detail);
|
|
254
|
+
/** Whether native MetaMask detection already suppressed this provider. */
|
|
255
|
+
__privateAdd(this, _suppressed, false);
|
|
256
|
+
/** Listener used to re-announce the SDK provider on future requests. */
|
|
257
|
+
__privateAdd(this, _requestHandler);
|
|
258
|
+
/** In-flight announcement detection promise, shared by concurrent calls. */
|
|
259
|
+
__privateAdd(this, _announcementPromise);
|
|
260
|
+
__privateSet(this, _provider, provider);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Announces the SDK provider unless native MetaMask has already announced.
|
|
264
|
+
*
|
|
265
|
+
* Repeated calls are idempotent: once announced, this re-dispatches the same
|
|
266
|
+
* provider detail and does not install duplicate request listeners. The first
|
|
267
|
+
* call may take up to `EIP6963_DETECTION_TIMEOUT_MS` while native providers
|
|
268
|
+
* are requested.
|
|
269
|
+
*
|
|
270
|
+
* @returns A promise that resolves once detection and any announcement finish.
|
|
271
|
+
*/
|
|
272
|
+
announce() {
|
|
273
|
+
return __async(this, null, function* () {
|
|
274
|
+
try {
|
|
275
|
+
if (__privateGet(this, _suppressed)) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (__privateGet(this, _detail)) {
|
|
279
|
+
__privateMethod(this, _EIP6963ProviderAnnouncer_instances, dispatchAnnouncement_fn).call(this);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (!__privateGet(this, _announcementPromise)) {
|
|
283
|
+
__privateSet(this, _announcementPromise, __privateMethod(this, _EIP6963ProviderAnnouncer_instances, announceOnce_fn).call(this).catch((error) => {
|
|
284
|
+
logger("EIP-6963 provider announcement failed", error);
|
|
285
|
+
}).finally(() => {
|
|
286
|
+
__privateSet(this, _announcementPromise, void 0);
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
yield __privateGet(this, _announcementPromise);
|
|
290
|
+
} catch (error) {
|
|
291
|
+
logger("EIP-6963 provider announcement failed", error);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
_provider = new WeakMap();
|
|
297
|
+
_detail = new WeakMap();
|
|
298
|
+
_suppressed = new WeakMap();
|
|
299
|
+
_requestHandler = new WeakMap();
|
|
300
|
+
_announcementPromise = new WeakMap();
|
|
301
|
+
_EIP6963ProviderAnnouncer_instances = new WeakSet();
|
|
302
|
+
announceOnce_fn = function() {
|
|
303
|
+
return __async(this, null, function* () {
|
|
304
|
+
if (!getBrowserWindow()) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (yield hasNativeMetaMaskProvider()) {
|
|
308
|
+
logger("MetaMask EIP-6963 provider is already announced. Skipping...");
|
|
309
|
+
__privateSet(this, _suppressed, true);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
__privateSet(this, _detail, Object.freeze({
|
|
313
|
+
info: createProviderInfo(),
|
|
314
|
+
provider: __privateGet(this, _provider)
|
|
315
|
+
}));
|
|
316
|
+
__privateMethod(this, _EIP6963ProviderAnnouncer_instances, installRequestProviderListener_fn).call(this);
|
|
317
|
+
__privateMethod(this, _EIP6963ProviderAnnouncer_instances, dispatchAnnouncement_fn).call(this);
|
|
318
|
+
});
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* Installs a single persistent EIP-6963 request listener for re-announcement.
|
|
322
|
+
*/
|
|
323
|
+
installRequestProviderListener_fn = function() {
|
|
324
|
+
const browserWindow = getBrowserWindow();
|
|
325
|
+
if (!browserWindow || __privateGet(this, _requestHandler)) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
__privateSet(this, _requestHandler, () => {
|
|
329
|
+
try {
|
|
330
|
+
__privateMethod(this, _EIP6963ProviderAnnouncer_instances, dispatchAnnouncement_fn).call(this);
|
|
331
|
+
} catch (error) {
|
|
332
|
+
logger("EIP-6963 provider announcement failed", error);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
browserWindow.addEventListener(
|
|
336
|
+
EIP6963_REQUEST_PROVIDER_EVENT,
|
|
337
|
+
__privateGet(this, _requestHandler)
|
|
338
|
+
);
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* Dispatches the current provider detail through EIP-6963.
|
|
342
|
+
*/
|
|
343
|
+
dispatchAnnouncement_fn = function() {
|
|
344
|
+
const browserWindow = getBrowserWindow();
|
|
345
|
+
if (!browserWindow || !__privateGet(this, _detail)) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
browserWindow.dispatchEvent(createAnnouncementEvent(__privateGet(this, _detail)));
|
|
349
|
+
};
|
|
350
|
+
|
|
133
351
|
// src/provider.ts
|
|
134
352
|
import { EventEmitter, RPCInvokeMethodErr } from "@metamask/connect-multichain";
|
|
135
353
|
import { hexToNumber as hexToNumber2 } from "@metamask/utils";
|
|
@@ -397,7 +615,61 @@ function isAnalyticsEnabled(options) {
|
|
|
397
615
|
var _a;
|
|
398
616
|
return ((_a = options == null ? void 0 : options.analytics) == null ? void 0 : _a.enabled) !== false;
|
|
399
617
|
}
|
|
400
|
-
var
|
|
618
|
+
var createPermissionId = () => {
|
|
619
|
+
var _a;
|
|
620
|
+
if ((_a = globalThis.crypto) == null ? void 0 : _a.randomUUID) {
|
|
621
|
+
return globalThis.crypto.randomUUID();
|
|
622
|
+
}
|
|
623
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
624
|
+
};
|
|
625
|
+
var getDappInvoker = (options) => {
|
|
626
|
+
const { name, nativeScheme, url: dappUrl } = options.dapp;
|
|
627
|
+
const fallbackInvoker = nativeScheme != null ? nativeScheme : name;
|
|
628
|
+
if (!dappUrl) {
|
|
629
|
+
return fallbackInvoker;
|
|
630
|
+
}
|
|
631
|
+
try {
|
|
632
|
+
const { origin } = new URL(dappUrl);
|
|
633
|
+
return origin === "null" ? fallbackInvoker : origin;
|
|
634
|
+
} catch (e) {
|
|
635
|
+
return fallbackInvoker;
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
var getRequestedPermissions = ({
|
|
639
|
+
accounts,
|
|
640
|
+
chainIds,
|
|
641
|
+
invoker
|
|
642
|
+
}) => {
|
|
643
|
+
const id = createPermissionId();
|
|
644
|
+
const date = Date.now();
|
|
645
|
+
return [
|
|
646
|
+
{
|
|
647
|
+
id,
|
|
648
|
+
parentCapability: "eth_accounts",
|
|
649
|
+
invoker,
|
|
650
|
+
caveats: [
|
|
651
|
+
{
|
|
652
|
+
type: "restrictReturnedAccounts",
|
|
653
|
+
value: accounts
|
|
654
|
+
}
|
|
655
|
+
],
|
|
656
|
+
date
|
|
657
|
+
},
|
|
658
|
+
{
|
|
659
|
+
id,
|
|
660
|
+
parentCapability: "endowment:permitted-chains",
|
|
661
|
+
invoker,
|
|
662
|
+
caveats: [
|
|
663
|
+
{
|
|
664
|
+
type: "restrictNetworkSwitching",
|
|
665
|
+
value: chainIds
|
|
666
|
+
}
|
|
667
|
+
],
|
|
668
|
+
date
|
|
669
|
+
}
|
|
670
|
+
];
|
|
671
|
+
};
|
|
672
|
+
var _core2, _provider2, _eip6963Announcer, _sessionScopes, _eventHandlers, _sessionChangedHandler, _displayUriHandler, _removeNotificationHandler, _status, _pendingPreferredChainId, _MetamaskConnectEVM_instances, getCoreOptions_fn, createInvokeOptions_fn, trackWalletActionRequested_fn, trackWalletActionSucceeded_fn, trackWalletActionFailed_fn, getSelectedChainId_fn, requestInterceptor_fn, clearConnectionState_fn, addEthereumChain_fn, request_fn, cacheChainId_fn, onSessionChanged_fn, onChainChanged_fn, onAccountsChanged_fn, onConnect_fn, onDisconnect_fn, onDisplayUri_fn;
|
|
401
673
|
var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
402
674
|
/**
|
|
403
675
|
* Creates a new MetamaskConnectEVM instance.
|
|
@@ -412,7 +684,9 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
412
684
|
/** The core instance of the Multichain SDK */
|
|
413
685
|
__privateAdd(this, _core2);
|
|
414
686
|
/** An instance of the EIP-1193 provider interface */
|
|
415
|
-
__privateAdd(this,
|
|
687
|
+
__privateAdd(this, _provider2);
|
|
688
|
+
/** Handles EIP-6963 discovery announcements for the provider */
|
|
689
|
+
__privateAdd(this, _eip6963Announcer);
|
|
416
690
|
/** The session scopes currently permitted */
|
|
417
691
|
__privateAdd(this, _sessionScopes, {});
|
|
418
692
|
/** Optional event handlers for the EIP-1193 provider events. */
|
|
@@ -433,10 +707,11 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
433
707
|
*/
|
|
434
708
|
__privateAdd(this, _pendingPreferredChainId);
|
|
435
709
|
__privateSet(this, _core2, core);
|
|
436
|
-
__privateSet(this,
|
|
710
|
+
__privateSet(this, _provider2, new EIP1193Provider(
|
|
437
711
|
core,
|
|
438
712
|
__privateMethod(this, _MetamaskConnectEVM_instances, requestInterceptor_fn).bind(this)
|
|
439
713
|
));
|
|
714
|
+
__privateSet(this, _eip6963Announcer, new EIP6963ProviderAnnouncer(__privateGet(this, _provider2)));
|
|
440
715
|
__privateSet(this, _eventHandlers, eventHandlers);
|
|
441
716
|
__privateSet(this, _sessionChangedHandler, __privateMethod(this, _MetamaskConnectEVM_instances, onSessionChanged_fn).bind(this));
|
|
442
717
|
__privateGet(this, _core2).on("wallet_sessionChanged", __privateGet(this, _sessionChangedHandler));
|
|
@@ -498,7 +773,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
498
773
|
try {
|
|
499
774
|
const result = new Promise(
|
|
500
775
|
(resolve) => {
|
|
501
|
-
__privateGet(this,
|
|
776
|
+
__privateGet(this, _provider2).once("connect", ({ chainId, accounts }) => {
|
|
502
777
|
logger("fulfilled-request: connect", {
|
|
503
778
|
chainId,
|
|
504
779
|
accounts
|
|
@@ -544,7 +819,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
544
819
|
const { accounts, chainId } = yield this.connect({
|
|
545
820
|
chainIds: chainIds != null ? chainIds : [DEFAULT_CHAIN_ID]
|
|
546
821
|
});
|
|
547
|
-
const signature = yield __privateGet(this,
|
|
822
|
+
const signature = yield __privateGet(this, _provider2).request({
|
|
548
823
|
method: "personal_sign",
|
|
549
824
|
params: [accounts[0], message]
|
|
550
825
|
});
|
|
@@ -583,7 +858,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
583
858
|
forceRequest
|
|
584
859
|
});
|
|
585
860
|
const resolvedParams = typeof params === "function" ? params(connectedAccounts[0]) : params;
|
|
586
|
-
const result = yield __privateGet(this,
|
|
861
|
+
const result = yield __privateGet(this, _provider2).request({
|
|
587
862
|
method,
|
|
588
863
|
params: resolvedParams
|
|
589
864
|
});
|
|
@@ -645,15 +920,12 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
645
920
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionRequested_fn).call(this, method, scope, params);
|
|
646
921
|
try {
|
|
647
922
|
const result = yield __privateMethod(this, _MetamaskConnectEVM_instances, request_fn).call(this, {
|
|
923
|
+
scope,
|
|
648
924
|
method: "wallet_switchEthereumChain",
|
|
649
925
|
params
|
|
650
926
|
});
|
|
651
|
-
const resultWithError = result;
|
|
652
|
-
if (resultWithError == null ? void 0 : resultWithError.error) {
|
|
653
|
-
throw new Error(resultWithError.error.message);
|
|
654
|
-
}
|
|
655
927
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionSucceeded_fn).call(this, method, scope, params);
|
|
656
|
-
if (result
|
|
928
|
+
if (result === null) {
|
|
657
929
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, chainId);
|
|
658
930
|
__privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, chainId);
|
|
659
931
|
}
|
|
@@ -676,7 +948,19 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
676
948
|
* @returns The EIP-1193 provider instance
|
|
677
949
|
*/
|
|
678
950
|
getProvider() {
|
|
679
|
-
return __privateGet(this,
|
|
951
|
+
return __privateGet(this, _provider2);
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Announces the EIP-1193 provider through EIP-6963 wallet discovery.
|
|
955
|
+
*
|
|
956
|
+
* This is a no-op when a native MetaMask EIP-6963 provider has already
|
|
957
|
+
* announced, or when running outside a browser environment. The first call
|
|
958
|
+
* may take up to 300 ms while native providers are requested.
|
|
959
|
+
*/
|
|
960
|
+
announceProvider() {
|
|
961
|
+
return __async(this, null, function* () {
|
|
962
|
+
yield __privateGet(this, _eip6963Announcer).announce();
|
|
963
|
+
});
|
|
680
964
|
}
|
|
681
965
|
/**
|
|
682
966
|
* Gets the currently selected chain ID on the wallet
|
|
@@ -692,7 +976,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
692
976
|
* @returns The currently selected account or undefined if no account is selected
|
|
693
977
|
*/
|
|
694
978
|
getAccount() {
|
|
695
|
-
return __privateGet(this,
|
|
979
|
+
return __privateGet(this, _provider2).selectedAccount;
|
|
696
980
|
}
|
|
697
981
|
// Convenience getters for the EIP-1193 provider
|
|
698
982
|
/**
|
|
@@ -701,7 +985,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
701
985
|
* @returns The currently permitted accounts
|
|
702
986
|
*/
|
|
703
987
|
get accounts() {
|
|
704
|
-
return __privateGet(this,
|
|
988
|
+
return __privateGet(this, _provider2).accounts;
|
|
705
989
|
}
|
|
706
990
|
/**
|
|
707
991
|
* Gets the currently selected account on the wallet
|
|
@@ -709,7 +993,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
709
993
|
* @returns The currently selected account or undefined if no account is selected
|
|
710
994
|
*/
|
|
711
995
|
get selectedAccount() {
|
|
712
|
-
return __privateGet(this,
|
|
996
|
+
return __privateGet(this, _provider2).selectedAccount;
|
|
713
997
|
}
|
|
714
998
|
/**
|
|
715
999
|
* Gets the currently selected chain ID on the wallet
|
|
@@ -717,7 +1001,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
717
1001
|
* @returns The currently selected chain ID or undefined if no chain is selected
|
|
718
1002
|
*/
|
|
719
1003
|
get selectedChainId() {
|
|
720
|
-
return __privateGet(this,
|
|
1004
|
+
return __privateGet(this, _provider2).selectedChainId;
|
|
721
1005
|
}
|
|
722
1006
|
/**
|
|
723
1007
|
* Gets the current connection status
|
|
@@ -729,7 +1013,8 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
729
1013
|
}
|
|
730
1014
|
};
|
|
731
1015
|
_core2 = new WeakMap();
|
|
732
|
-
|
|
1016
|
+
_provider2 = new WeakMap();
|
|
1017
|
+
_eip6963Announcer = new WeakMap();
|
|
733
1018
|
_sessionScopes = new WeakMap();
|
|
734
1019
|
_eventHandlers = new WeakMap();
|
|
735
1020
|
_sessionChangedHandler = new WeakMap();
|
|
@@ -865,7 +1150,7 @@ requestInterceptor_fn = function(request) {
|
|
|
865
1150
|
if (permitted.length === 0) {
|
|
866
1151
|
permitted.push(DEFAULT_CHAIN_ID);
|
|
867
1152
|
}
|
|
868
|
-
const selected = __privateGet(this,
|
|
1153
|
+
const selected = __privateGet(this, _provider2).selectedChainId;
|
|
869
1154
|
const preferred = selected && permitted.includes(selected) ? selected : permitted[0];
|
|
870
1155
|
const chainIds = [
|
|
871
1156
|
preferred,
|
|
@@ -882,6 +1167,13 @@ requestInterceptor_fn = function(request) {
|
|
|
882
1167
|
if (request.method === "eth_requestAccounts") {
|
|
883
1168
|
return result.accounts;
|
|
884
1169
|
}
|
|
1170
|
+
if (request.method === "wallet_requestPermissions") {
|
|
1171
|
+
return getRequestedPermissions({
|
|
1172
|
+
accounts: result.accounts,
|
|
1173
|
+
chainIds: getPermittedEthChainIds(__privateGet(this, _sessionScopes)),
|
|
1174
|
+
invoker: getDappInvoker(__privateMethod(this, _MetamaskConnectEVM_instances, getCoreOptions_fn).call(this))
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
885
1177
|
return result;
|
|
886
1178
|
} catch (error) {
|
|
887
1179
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionFailed_fn).call(this, method, scope, params, error);
|
|
@@ -889,21 +1181,23 @@ requestInterceptor_fn = function(request) {
|
|
|
889
1181
|
}
|
|
890
1182
|
}
|
|
891
1183
|
if (isSwitchChainRequest(request)) {
|
|
892
|
-
|
|
1184
|
+
yield this.switchChain({
|
|
893
1185
|
chainId: request.params[0].chainId
|
|
894
1186
|
});
|
|
1187
|
+
return null;
|
|
895
1188
|
}
|
|
896
1189
|
if (isAddChainRequest(request)) {
|
|
897
|
-
|
|
1190
|
+
yield __privateMethod(this, _MetamaskConnectEVM_instances, addEthereumChain_fn).call(this, request.params[0]);
|
|
1191
|
+
return null;
|
|
898
1192
|
}
|
|
899
1193
|
if (isAccountsRequest(request)) {
|
|
900
1194
|
if (request.method === "eth_coinbase") {
|
|
901
|
-
return (_a = __privateGet(this,
|
|
1195
|
+
return (_a = __privateGet(this, _provider2).selectedAccount) != null ? _a : null;
|
|
902
1196
|
}
|
|
903
|
-
return __privateGet(this,
|
|
1197
|
+
return __privateGet(this, _provider2).accounts;
|
|
904
1198
|
}
|
|
905
1199
|
if (isChainIdRequest(request)) {
|
|
906
|
-
return __privateGet(this,
|
|
1200
|
+
return __privateGet(this, _provider2).selectedChainId;
|
|
907
1201
|
}
|
|
908
1202
|
logger("Request not intercepted, forwarding to default handler", request);
|
|
909
1203
|
return Promise.resolve();
|
|
@@ -913,8 +1207,8 @@ requestInterceptor_fn = function(request) {
|
|
|
913
1207
|
* Clears the internal connection state: accounts and chainId
|
|
914
1208
|
*/
|
|
915
1209
|
clearConnectionState_fn = function() {
|
|
916
|
-
__privateGet(this,
|
|
917
|
-
__privateGet(this,
|
|
1210
|
+
__privateGet(this, _provider2).accounts = [];
|
|
1211
|
+
__privateGet(this, _provider2).selectedChainId = void 0;
|
|
918
1212
|
};
|
|
919
1213
|
addEthereumChain_fn = function(chainConfiguration) {
|
|
920
1214
|
return __async(this, null, function* () {
|
|
@@ -923,17 +1217,18 @@ addEthereumChain_fn = function(chainConfiguration) {
|
|
|
923
1217
|
if (!chainConfiguration) {
|
|
924
1218
|
throw new Error("No chain configuration found.");
|
|
925
1219
|
}
|
|
926
|
-
const chainId = chainConfiguration.chainId || __privateGet(this,
|
|
1220
|
+
const chainId = chainConfiguration.chainId || __privateGet(this, _provider2).selectedChainId || "0x1";
|
|
927
1221
|
const decimalChainId = hexToNumber3(chainId);
|
|
928
1222
|
const scope = `eip155:${decimalChainId}`;
|
|
929
1223
|
const params = [chainConfiguration];
|
|
930
1224
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionRequested_fn).call(this, method, scope, params);
|
|
931
1225
|
try {
|
|
932
1226
|
const result = yield __privateMethod(this, _MetamaskConnectEVM_instances, request_fn).call(this, {
|
|
1227
|
+
scope,
|
|
933
1228
|
method: "wallet_addEthereumChain",
|
|
934
1229
|
params
|
|
935
1230
|
});
|
|
936
|
-
if (result
|
|
1231
|
+
if (result === null) {
|
|
937
1232
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, chainId);
|
|
938
1233
|
__privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, chainId);
|
|
939
1234
|
}
|
|
@@ -947,7 +1242,10 @@ addEthereumChain_fn = function(chainConfiguration) {
|
|
|
947
1242
|
request_fn = function(request) {
|
|
948
1243
|
return __async(this, null, function* () {
|
|
949
1244
|
logger("direct request to metamask-provider called", request);
|
|
950
|
-
const result = __privateGet(this, _core2).
|
|
1245
|
+
const result = __privateGet(this, _core2).invokeMethod({
|
|
1246
|
+
scope: request.scope,
|
|
1247
|
+
request: { method: request.method, params: request.params }
|
|
1248
|
+
});
|
|
951
1249
|
if (request.method === "wallet_addEthereumChain" || request.method === "wallet_switchEthereumChain") {
|
|
952
1250
|
__privateGet(this, _core2).openSimpleDeeplinkIfNeeded();
|
|
953
1251
|
}
|
|
@@ -977,11 +1275,10 @@ onSessionChanged_fn = function(session) {
|
|
|
977
1275
|
} else {
|
|
978
1276
|
let initialAccounts = [];
|
|
979
1277
|
if (__privateGet(this, _core2).status === "connected") {
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
params: []
|
|
1278
|
+
initialAccounts = yield __privateGet(this, _core2).invokeMethod({
|
|
1279
|
+
scope: `eip155:${hexToNumber3(hexPermittedChainIds[0])}`,
|
|
1280
|
+
request: { method: "eth_accounts", params: [] }
|
|
983
1281
|
});
|
|
984
|
-
initialAccounts = ethAccountsResponse.result;
|
|
985
1282
|
} else {
|
|
986
1283
|
initialAccounts = getEthAccounts(__privateGet(this, _sessionScopes));
|
|
987
1284
|
}
|
|
@@ -1000,12 +1297,12 @@ onSessionChanged_fn = function(session) {
|
|
|
1000
1297
|
*/
|
|
1001
1298
|
onChainChanged_fn = function(chainId) {
|
|
1002
1299
|
var _a, _b;
|
|
1003
|
-
if (chainId === __privateGet(this,
|
|
1300
|
+
if (chainId === __privateGet(this, _provider2).selectedChainId) {
|
|
1004
1301
|
return;
|
|
1005
1302
|
}
|
|
1006
1303
|
logger("handler: chainChanged", { chainId });
|
|
1007
|
-
__privateGet(this,
|
|
1008
|
-
__privateGet(this,
|
|
1304
|
+
__privateGet(this, _provider2).selectedChainId = chainId;
|
|
1305
|
+
__privateGet(this, _provider2).emit("chainChanged", chainId);
|
|
1009
1306
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.chainChanged) == null ? void 0 : _b.call(_a, chainId);
|
|
1010
1307
|
};
|
|
1011
1308
|
/**
|
|
@@ -1015,13 +1312,13 @@ onChainChanged_fn = function(chainId) {
|
|
|
1015
1312
|
*/
|
|
1016
1313
|
onAccountsChanged_fn = function(accounts) {
|
|
1017
1314
|
var _a, _b;
|
|
1018
|
-
const accountsUnchanged = accounts.length === __privateGet(this,
|
|
1315
|
+
const accountsUnchanged = accounts.length === __privateGet(this, _provider2).accounts.length && accounts.every((acct, idx) => acct === __privateGet(this, _provider2).accounts[idx]);
|
|
1019
1316
|
if (accountsUnchanged) {
|
|
1020
1317
|
return;
|
|
1021
1318
|
}
|
|
1022
1319
|
logger("handler: accountsChanged", accounts);
|
|
1023
|
-
__privateGet(this,
|
|
1024
|
-
__privateGet(this,
|
|
1320
|
+
__privateGet(this, _provider2).accounts = accounts;
|
|
1321
|
+
__privateGet(this, _provider2).emit("accountsChanged", accounts);
|
|
1025
1322
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.accountsChanged) == null ? void 0 : _b.call(_a, accounts);
|
|
1026
1323
|
};
|
|
1027
1324
|
/**
|
|
@@ -1046,14 +1343,14 @@ onConnect_fn = function({
|
|
|
1046
1343
|
__privateMethod(this, _MetamaskConnectEVM_instances, onAccountsChanged_fn).call(this, accounts);
|
|
1047
1344
|
return;
|
|
1048
1345
|
}
|
|
1049
|
-
__privateGet(this,
|
|
1050
|
-
__privateGet(this,
|
|
1346
|
+
__privateGet(this, _provider2).selectedChainId = chainId;
|
|
1347
|
+
__privateGet(this, _provider2).accounts = accounts;
|
|
1051
1348
|
__privateSet(this, _status, "connected");
|
|
1052
|
-
__privateGet(this,
|
|
1349
|
+
__privateGet(this, _provider2).emit("connect", data);
|
|
1053
1350
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.connect) == null ? void 0 : _b.call(_a, data);
|
|
1054
|
-
__privateGet(this,
|
|
1351
|
+
__privateGet(this, _provider2).emit("chainChanged", chainId);
|
|
1055
1352
|
(_d = (_c = __privateGet(this, _eventHandlers)) == null ? void 0 : _c.chainChanged) == null ? void 0 : _d.call(_c, chainId);
|
|
1056
|
-
__privateGet(this,
|
|
1353
|
+
__privateGet(this, _provider2).emit("accountsChanged", accounts);
|
|
1057
1354
|
(_f = (_e = __privateGet(this, _eventHandlers)) == null ? void 0 : _e.accountsChanged) == null ? void 0 : _f.call(_e, accounts);
|
|
1058
1355
|
(_g = __privateGet(this, _removeNotificationHandler)) == null ? void 0 : _g.call(this);
|
|
1059
1356
|
const onAccountsChanged = (accs) => {
|
|
@@ -1085,7 +1382,7 @@ onDisconnect_fn = function() {
|
|
|
1085
1382
|
}
|
|
1086
1383
|
__privateSet(this, _status, "disconnected");
|
|
1087
1384
|
logger("handler: disconnect");
|
|
1088
|
-
__privateGet(this,
|
|
1385
|
+
__privateGet(this, _provider2).emit("disconnect");
|
|
1089
1386
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.disconnect) == null ? void 0 : _b.call(_a);
|
|
1090
1387
|
__privateMethod(this, _MetamaskConnectEVM_instances, onAccountsChanged_fn).call(this, []);
|
|
1091
1388
|
};
|
|
@@ -1101,7 +1398,7 @@ onDisplayUri_fn = function(uri) {
|
|
|
1101
1398
|
return;
|
|
1102
1399
|
}
|
|
1103
1400
|
logger("handler: display_uri", uri);
|
|
1104
|
-
__privateGet(this,
|
|
1401
|
+
__privateGet(this, _provider2).emit("display_uri", uri);
|
|
1105
1402
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.displayUri) == null ? void 0 : _b.call(_a, uri);
|
|
1106
1403
|
};
|
|
1107
1404
|
var MetamaskConnectEVM = _MetamaskConnectEVM;
|
|
@@ -1138,14 +1435,26 @@ function createEVMClient(options) {
|
|
|
1138
1435
|
versions: {
|
|
1139
1436
|
// typeof guard needed: Metro (React Native) bundles TS source directly,
|
|
1140
1437
|
// bypassing the tsup build that substitutes __PACKAGE_VERSION__.
|
|
1141
|
-
"connect-evm": false ? "unknown" : "
|
|
1438
|
+
"connect-evm": false ? "unknown" : "2.0.0"
|
|
1142
1439
|
}
|
|
1143
1440
|
}));
|
|
1144
|
-
|
|
1441
|
+
const multichainClientPeerRange = false ? "unknown" : "^1.0.0";
|
|
1442
|
+
if (multichainClientPeerRange !== "unknown" && multichainClientPeerRange !== "" && !satisfies(core.version, multichainClientPeerRange)) {
|
|
1443
|
+
console.warn(
|
|
1444
|
+
`@metamask/connect-evm expected @metamask/connect-multichain version ${multichainClientPeerRange}, but got ${core.version}. This may lead to unexpected behavior.`
|
|
1445
|
+
);
|
|
1446
|
+
}
|
|
1447
|
+
const client = yield MetamaskConnectEVM.create({
|
|
1145
1448
|
core,
|
|
1146
1449
|
eventHandlers: options.eventHandlers,
|
|
1147
1450
|
supportedNetworks: options.api.supportedNetworks
|
|
1148
1451
|
});
|
|
1452
|
+
if (!options.skipAutoAnnounce) {
|
|
1453
|
+
client.announceProvider().catch((error) => {
|
|
1454
|
+
logger("EIP-6963 provider announcement failed", error);
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
return client;
|
|
1149
1458
|
} catch (error) {
|
|
1150
1459
|
console.error("Error creating Metamask Connect/EVM", error);
|
|
1151
1460
|
throw error;
|
|
@@ -1153,6 +1462,8 @@ function createEVMClient(options) {
|
|
|
1153
1462
|
});
|
|
1154
1463
|
}
|
|
1155
1464
|
export {
|
|
1465
|
+
EIP6963_ANNOUNCE_PROVIDER_EVENT,
|
|
1466
|
+
EIP6963_REQUEST_PROVIDER_EVENT,
|
|
1156
1467
|
createEVMClient,
|
|
1157
1468
|
getInfuraRpcUrls
|
|
1158
1469
|
};
|