@metamask/connect-evm 1.4.0 → 2.1.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 +36 -1
- package/README.md +20 -1
- package/dist/browser/es/connect-evm.mjs +362 -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/provider.d.ts.map +1 -1
- package/dist/src/provider.js +4 -0
- package/dist/src/provider.js.map +1 -1
- package/dist/src/utils/infura.js.map +1 -1
- package/dist/types/index.d.ts +21 -3
- package/package.json +6 -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";
|
|
@@ -195,10 +413,14 @@ var EIP1193Provider = class extends EventEmitter {
|
|
|
195
413
|
});
|
|
196
414
|
} catch (error) {
|
|
197
415
|
if (error instanceof RPCInvokeMethodErr && error.rpcCode !== void 0) {
|
|
416
|
+
const invokeError = error;
|
|
198
417
|
const rpcError = new Error(
|
|
199
418
|
(_d = error.rpcMessage) != null ? _d : error.reason
|
|
200
419
|
);
|
|
201
420
|
rpcError.code = error.rpcCode;
|
|
421
|
+
if (invokeError.rpcData !== void 0) {
|
|
422
|
+
rpcError.data = invokeError.rpcData;
|
|
423
|
+
}
|
|
202
424
|
throw rpcError;
|
|
203
425
|
}
|
|
204
426
|
throw error;
|
|
@@ -397,7 +619,61 @@ function isAnalyticsEnabled(options) {
|
|
|
397
619
|
var _a;
|
|
398
620
|
return ((_a = options == null ? void 0 : options.analytics) == null ? void 0 : _a.enabled) !== false;
|
|
399
621
|
}
|
|
400
|
-
var
|
|
622
|
+
var createPermissionId = () => {
|
|
623
|
+
var _a;
|
|
624
|
+
if ((_a = globalThis.crypto) == null ? void 0 : _a.randomUUID) {
|
|
625
|
+
return globalThis.crypto.randomUUID();
|
|
626
|
+
}
|
|
627
|
+
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
628
|
+
};
|
|
629
|
+
var getDappInvoker = (options) => {
|
|
630
|
+
const { name, nativeScheme, url: dappUrl } = options.dapp;
|
|
631
|
+
const fallbackInvoker = nativeScheme != null ? nativeScheme : name;
|
|
632
|
+
if (!dappUrl) {
|
|
633
|
+
return fallbackInvoker;
|
|
634
|
+
}
|
|
635
|
+
try {
|
|
636
|
+
const { origin } = new URL(dappUrl);
|
|
637
|
+
return origin === "null" ? fallbackInvoker : origin;
|
|
638
|
+
} catch (e) {
|
|
639
|
+
return fallbackInvoker;
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
var getRequestedPermissions = ({
|
|
643
|
+
accounts,
|
|
644
|
+
chainIds,
|
|
645
|
+
invoker
|
|
646
|
+
}) => {
|
|
647
|
+
const id = createPermissionId();
|
|
648
|
+
const date = Date.now();
|
|
649
|
+
return [
|
|
650
|
+
{
|
|
651
|
+
id,
|
|
652
|
+
parentCapability: "eth_accounts",
|
|
653
|
+
invoker,
|
|
654
|
+
caveats: [
|
|
655
|
+
{
|
|
656
|
+
type: "restrictReturnedAccounts",
|
|
657
|
+
value: accounts
|
|
658
|
+
}
|
|
659
|
+
],
|
|
660
|
+
date
|
|
661
|
+
},
|
|
662
|
+
{
|
|
663
|
+
id,
|
|
664
|
+
parentCapability: "endowment:permitted-chains",
|
|
665
|
+
invoker,
|
|
666
|
+
caveats: [
|
|
667
|
+
{
|
|
668
|
+
type: "restrictNetworkSwitching",
|
|
669
|
+
value: chainIds
|
|
670
|
+
}
|
|
671
|
+
],
|
|
672
|
+
date
|
|
673
|
+
}
|
|
674
|
+
];
|
|
675
|
+
};
|
|
676
|
+
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
677
|
var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
402
678
|
/**
|
|
403
679
|
* Creates a new MetamaskConnectEVM instance.
|
|
@@ -412,7 +688,9 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
412
688
|
/** The core instance of the Multichain SDK */
|
|
413
689
|
__privateAdd(this, _core2);
|
|
414
690
|
/** An instance of the EIP-1193 provider interface */
|
|
415
|
-
__privateAdd(this,
|
|
691
|
+
__privateAdd(this, _provider2);
|
|
692
|
+
/** Handles EIP-6963 discovery announcements for the provider */
|
|
693
|
+
__privateAdd(this, _eip6963Announcer);
|
|
416
694
|
/** The session scopes currently permitted */
|
|
417
695
|
__privateAdd(this, _sessionScopes, {});
|
|
418
696
|
/** Optional event handlers for the EIP-1193 provider events. */
|
|
@@ -433,10 +711,11 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
433
711
|
*/
|
|
434
712
|
__privateAdd(this, _pendingPreferredChainId);
|
|
435
713
|
__privateSet(this, _core2, core);
|
|
436
|
-
__privateSet(this,
|
|
714
|
+
__privateSet(this, _provider2, new EIP1193Provider(
|
|
437
715
|
core,
|
|
438
716
|
__privateMethod(this, _MetamaskConnectEVM_instances, requestInterceptor_fn).bind(this)
|
|
439
717
|
));
|
|
718
|
+
__privateSet(this, _eip6963Announcer, new EIP6963ProviderAnnouncer(__privateGet(this, _provider2)));
|
|
440
719
|
__privateSet(this, _eventHandlers, eventHandlers);
|
|
441
720
|
__privateSet(this, _sessionChangedHandler, __privateMethod(this, _MetamaskConnectEVM_instances, onSessionChanged_fn).bind(this));
|
|
442
721
|
__privateGet(this, _core2).on("wallet_sessionChanged", __privateGet(this, _sessionChangedHandler));
|
|
@@ -498,7 +777,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
498
777
|
try {
|
|
499
778
|
const result = new Promise(
|
|
500
779
|
(resolve) => {
|
|
501
|
-
__privateGet(this,
|
|
780
|
+
__privateGet(this, _provider2).once("connect", ({ chainId, accounts }) => {
|
|
502
781
|
logger("fulfilled-request: connect", {
|
|
503
782
|
chainId,
|
|
504
783
|
accounts
|
|
@@ -544,7 +823,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
544
823
|
const { accounts, chainId } = yield this.connect({
|
|
545
824
|
chainIds: chainIds != null ? chainIds : [DEFAULT_CHAIN_ID]
|
|
546
825
|
});
|
|
547
|
-
const signature = yield __privateGet(this,
|
|
826
|
+
const signature = yield __privateGet(this, _provider2).request({
|
|
548
827
|
method: "personal_sign",
|
|
549
828
|
params: [accounts[0], message]
|
|
550
829
|
});
|
|
@@ -583,7 +862,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
583
862
|
forceRequest
|
|
584
863
|
});
|
|
585
864
|
const resolvedParams = typeof params === "function" ? params(connectedAccounts[0]) : params;
|
|
586
|
-
const result = yield __privateGet(this,
|
|
865
|
+
const result = yield __privateGet(this, _provider2).request({
|
|
587
866
|
method,
|
|
588
867
|
params: resolvedParams
|
|
589
868
|
});
|
|
@@ -645,15 +924,12 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
645
924
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionRequested_fn).call(this, method, scope, params);
|
|
646
925
|
try {
|
|
647
926
|
const result = yield __privateMethod(this, _MetamaskConnectEVM_instances, request_fn).call(this, {
|
|
927
|
+
scope,
|
|
648
928
|
method: "wallet_switchEthereumChain",
|
|
649
929
|
params
|
|
650
930
|
});
|
|
651
|
-
const resultWithError = result;
|
|
652
|
-
if (resultWithError == null ? void 0 : resultWithError.error) {
|
|
653
|
-
throw new Error(resultWithError.error.message);
|
|
654
|
-
}
|
|
655
931
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionSucceeded_fn).call(this, method, scope, params);
|
|
656
|
-
if (result
|
|
932
|
+
if (result === null) {
|
|
657
933
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, chainId);
|
|
658
934
|
__privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, chainId);
|
|
659
935
|
}
|
|
@@ -676,7 +952,19 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
676
952
|
* @returns The EIP-1193 provider instance
|
|
677
953
|
*/
|
|
678
954
|
getProvider() {
|
|
679
|
-
return __privateGet(this,
|
|
955
|
+
return __privateGet(this, _provider2);
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Announces the EIP-1193 provider through EIP-6963 wallet discovery.
|
|
959
|
+
*
|
|
960
|
+
* This is a no-op when a native MetaMask EIP-6963 provider has already
|
|
961
|
+
* announced, or when running outside a browser environment. The first call
|
|
962
|
+
* may take up to 300 ms while native providers are requested.
|
|
963
|
+
*/
|
|
964
|
+
announceProvider() {
|
|
965
|
+
return __async(this, null, function* () {
|
|
966
|
+
yield __privateGet(this, _eip6963Announcer).announce();
|
|
967
|
+
});
|
|
680
968
|
}
|
|
681
969
|
/**
|
|
682
970
|
* Gets the currently selected chain ID on the wallet
|
|
@@ -692,7 +980,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
692
980
|
* @returns The currently selected account or undefined if no account is selected
|
|
693
981
|
*/
|
|
694
982
|
getAccount() {
|
|
695
|
-
return __privateGet(this,
|
|
983
|
+
return __privateGet(this, _provider2).selectedAccount;
|
|
696
984
|
}
|
|
697
985
|
// Convenience getters for the EIP-1193 provider
|
|
698
986
|
/**
|
|
@@ -701,7 +989,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
701
989
|
* @returns The currently permitted accounts
|
|
702
990
|
*/
|
|
703
991
|
get accounts() {
|
|
704
|
-
return __privateGet(this,
|
|
992
|
+
return __privateGet(this, _provider2).accounts;
|
|
705
993
|
}
|
|
706
994
|
/**
|
|
707
995
|
* Gets the currently selected account on the wallet
|
|
@@ -709,7 +997,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
709
997
|
* @returns The currently selected account or undefined if no account is selected
|
|
710
998
|
*/
|
|
711
999
|
get selectedAccount() {
|
|
712
|
-
return __privateGet(this,
|
|
1000
|
+
return __privateGet(this, _provider2).selectedAccount;
|
|
713
1001
|
}
|
|
714
1002
|
/**
|
|
715
1003
|
* Gets the currently selected chain ID on the wallet
|
|
@@ -717,7 +1005,7 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
717
1005
|
* @returns The currently selected chain ID or undefined if no chain is selected
|
|
718
1006
|
*/
|
|
719
1007
|
get selectedChainId() {
|
|
720
|
-
return __privateGet(this,
|
|
1008
|
+
return __privateGet(this, _provider2).selectedChainId;
|
|
721
1009
|
}
|
|
722
1010
|
/**
|
|
723
1011
|
* Gets the current connection status
|
|
@@ -729,7 +1017,8 @@ var _MetamaskConnectEVM = class _MetamaskConnectEVM {
|
|
|
729
1017
|
}
|
|
730
1018
|
};
|
|
731
1019
|
_core2 = new WeakMap();
|
|
732
|
-
|
|
1020
|
+
_provider2 = new WeakMap();
|
|
1021
|
+
_eip6963Announcer = new WeakMap();
|
|
733
1022
|
_sessionScopes = new WeakMap();
|
|
734
1023
|
_eventHandlers = new WeakMap();
|
|
735
1024
|
_sessionChangedHandler = new WeakMap();
|
|
@@ -865,7 +1154,7 @@ requestInterceptor_fn = function(request) {
|
|
|
865
1154
|
if (permitted.length === 0) {
|
|
866
1155
|
permitted.push(DEFAULT_CHAIN_ID);
|
|
867
1156
|
}
|
|
868
|
-
const selected = __privateGet(this,
|
|
1157
|
+
const selected = __privateGet(this, _provider2).selectedChainId;
|
|
869
1158
|
const preferred = selected && permitted.includes(selected) ? selected : permitted[0];
|
|
870
1159
|
const chainIds = [
|
|
871
1160
|
preferred,
|
|
@@ -882,6 +1171,13 @@ requestInterceptor_fn = function(request) {
|
|
|
882
1171
|
if (request.method === "eth_requestAccounts") {
|
|
883
1172
|
return result.accounts;
|
|
884
1173
|
}
|
|
1174
|
+
if (request.method === "wallet_requestPermissions") {
|
|
1175
|
+
return getRequestedPermissions({
|
|
1176
|
+
accounts: result.accounts,
|
|
1177
|
+
chainIds: getPermittedEthChainIds(__privateGet(this, _sessionScopes)),
|
|
1178
|
+
invoker: getDappInvoker(__privateMethod(this, _MetamaskConnectEVM_instances, getCoreOptions_fn).call(this))
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
885
1181
|
return result;
|
|
886
1182
|
} catch (error) {
|
|
887
1183
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionFailed_fn).call(this, method, scope, params, error);
|
|
@@ -889,21 +1185,23 @@ requestInterceptor_fn = function(request) {
|
|
|
889
1185
|
}
|
|
890
1186
|
}
|
|
891
1187
|
if (isSwitchChainRequest(request)) {
|
|
892
|
-
|
|
1188
|
+
yield this.switchChain({
|
|
893
1189
|
chainId: request.params[0].chainId
|
|
894
1190
|
});
|
|
1191
|
+
return null;
|
|
895
1192
|
}
|
|
896
1193
|
if (isAddChainRequest(request)) {
|
|
897
|
-
|
|
1194
|
+
yield __privateMethod(this, _MetamaskConnectEVM_instances, addEthereumChain_fn).call(this, request.params[0]);
|
|
1195
|
+
return null;
|
|
898
1196
|
}
|
|
899
1197
|
if (isAccountsRequest(request)) {
|
|
900
1198
|
if (request.method === "eth_coinbase") {
|
|
901
|
-
return (_a = __privateGet(this,
|
|
1199
|
+
return (_a = __privateGet(this, _provider2).selectedAccount) != null ? _a : null;
|
|
902
1200
|
}
|
|
903
|
-
return __privateGet(this,
|
|
1201
|
+
return __privateGet(this, _provider2).accounts;
|
|
904
1202
|
}
|
|
905
1203
|
if (isChainIdRequest(request)) {
|
|
906
|
-
return __privateGet(this,
|
|
1204
|
+
return __privateGet(this, _provider2).selectedChainId;
|
|
907
1205
|
}
|
|
908
1206
|
logger("Request not intercepted, forwarding to default handler", request);
|
|
909
1207
|
return Promise.resolve();
|
|
@@ -913,8 +1211,8 @@ requestInterceptor_fn = function(request) {
|
|
|
913
1211
|
* Clears the internal connection state: accounts and chainId
|
|
914
1212
|
*/
|
|
915
1213
|
clearConnectionState_fn = function() {
|
|
916
|
-
__privateGet(this,
|
|
917
|
-
__privateGet(this,
|
|
1214
|
+
__privateGet(this, _provider2).accounts = [];
|
|
1215
|
+
__privateGet(this, _provider2).selectedChainId = void 0;
|
|
918
1216
|
};
|
|
919
1217
|
addEthereumChain_fn = function(chainConfiguration) {
|
|
920
1218
|
return __async(this, null, function* () {
|
|
@@ -923,17 +1221,18 @@ addEthereumChain_fn = function(chainConfiguration) {
|
|
|
923
1221
|
if (!chainConfiguration) {
|
|
924
1222
|
throw new Error("No chain configuration found.");
|
|
925
1223
|
}
|
|
926
|
-
const chainId = chainConfiguration.chainId || __privateGet(this,
|
|
1224
|
+
const chainId = chainConfiguration.chainId || __privateGet(this, _provider2).selectedChainId || "0x1";
|
|
927
1225
|
const decimalChainId = hexToNumber3(chainId);
|
|
928
1226
|
const scope = `eip155:${decimalChainId}`;
|
|
929
1227
|
const params = [chainConfiguration];
|
|
930
1228
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, trackWalletActionRequested_fn).call(this, method, scope, params);
|
|
931
1229
|
try {
|
|
932
1230
|
const result = yield __privateMethod(this, _MetamaskConnectEVM_instances, request_fn).call(this, {
|
|
1231
|
+
scope,
|
|
933
1232
|
method: "wallet_addEthereumChain",
|
|
934
1233
|
params
|
|
935
1234
|
});
|
|
936
|
-
if (result
|
|
1235
|
+
if (result === null) {
|
|
937
1236
|
yield __privateMethod(this, _MetamaskConnectEVM_instances, cacheChainId_fn).call(this, chainId);
|
|
938
1237
|
__privateMethod(this, _MetamaskConnectEVM_instances, onChainChanged_fn).call(this, chainId);
|
|
939
1238
|
}
|
|
@@ -947,7 +1246,10 @@ addEthereumChain_fn = function(chainConfiguration) {
|
|
|
947
1246
|
request_fn = function(request) {
|
|
948
1247
|
return __async(this, null, function* () {
|
|
949
1248
|
logger("direct request to metamask-provider called", request);
|
|
950
|
-
const result = __privateGet(this, _core2).
|
|
1249
|
+
const result = __privateGet(this, _core2).invokeMethod({
|
|
1250
|
+
scope: request.scope,
|
|
1251
|
+
request: { method: request.method, params: request.params }
|
|
1252
|
+
});
|
|
951
1253
|
if (request.method === "wallet_addEthereumChain" || request.method === "wallet_switchEthereumChain") {
|
|
952
1254
|
__privateGet(this, _core2).openSimpleDeeplinkIfNeeded();
|
|
953
1255
|
}
|
|
@@ -977,11 +1279,10 @@ onSessionChanged_fn = function(session) {
|
|
|
977
1279
|
} else {
|
|
978
1280
|
let initialAccounts = [];
|
|
979
1281
|
if (__privateGet(this, _core2).status === "connected") {
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
params: []
|
|
1282
|
+
initialAccounts = yield __privateGet(this, _core2).invokeMethod({
|
|
1283
|
+
scope: `eip155:${hexToNumber3(hexPermittedChainIds[0])}`,
|
|
1284
|
+
request: { method: "eth_accounts", params: [] }
|
|
983
1285
|
});
|
|
984
|
-
initialAccounts = ethAccountsResponse.result;
|
|
985
1286
|
} else {
|
|
986
1287
|
initialAccounts = getEthAccounts(__privateGet(this, _sessionScopes));
|
|
987
1288
|
}
|
|
@@ -1000,12 +1301,12 @@ onSessionChanged_fn = function(session) {
|
|
|
1000
1301
|
*/
|
|
1001
1302
|
onChainChanged_fn = function(chainId) {
|
|
1002
1303
|
var _a, _b;
|
|
1003
|
-
if (chainId === __privateGet(this,
|
|
1304
|
+
if (chainId === __privateGet(this, _provider2).selectedChainId) {
|
|
1004
1305
|
return;
|
|
1005
1306
|
}
|
|
1006
1307
|
logger("handler: chainChanged", { chainId });
|
|
1007
|
-
__privateGet(this,
|
|
1008
|
-
__privateGet(this,
|
|
1308
|
+
__privateGet(this, _provider2).selectedChainId = chainId;
|
|
1309
|
+
__privateGet(this, _provider2).emit("chainChanged", chainId);
|
|
1009
1310
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.chainChanged) == null ? void 0 : _b.call(_a, chainId);
|
|
1010
1311
|
};
|
|
1011
1312
|
/**
|
|
@@ -1015,13 +1316,13 @@ onChainChanged_fn = function(chainId) {
|
|
|
1015
1316
|
*/
|
|
1016
1317
|
onAccountsChanged_fn = function(accounts) {
|
|
1017
1318
|
var _a, _b;
|
|
1018
|
-
const accountsUnchanged = accounts.length === __privateGet(this,
|
|
1319
|
+
const accountsUnchanged = accounts.length === __privateGet(this, _provider2).accounts.length && accounts.every((acct, idx) => acct === __privateGet(this, _provider2).accounts[idx]);
|
|
1019
1320
|
if (accountsUnchanged) {
|
|
1020
1321
|
return;
|
|
1021
1322
|
}
|
|
1022
1323
|
logger("handler: accountsChanged", accounts);
|
|
1023
|
-
__privateGet(this,
|
|
1024
|
-
__privateGet(this,
|
|
1324
|
+
__privateGet(this, _provider2).accounts = accounts;
|
|
1325
|
+
__privateGet(this, _provider2).emit("accountsChanged", accounts);
|
|
1025
1326
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.accountsChanged) == null ? void 0 : _b.call(_a, accounts);
|
|
1026
1327
|
};
|
|
1027
1328
|
/**
|
|
@@ -1046,14 +1347,14 @@ onConnect_fn = function({
|
|
|
1046
1347
|
__privateMethod(this, _MetamaskConnectEVM_instances, onAccountsChanged_fn).call(this, accounts);
|
|
1047
1348
|
return;
|
|
1048
1349
|
}
|
|
1049
|
-
__privateGet(this,
|
|
1050
|
-
__privateGet(this,
|
|
1350
|
+
__privateGet(this, _provider2).selectedChainId = chainId;
|
|
1351
|
+
__privateGet(this, _provider2).accounts = accounts;
|
|
1051
1352
|
__privateSet(this, _status, "connected");
|
|
1052
|
-
__privateGet(this,
|
|
1353
|
+
__privateGet(this, _provider2).emit("connect", data);
|
|
1053
1354
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.connect) == null ? void 0 : _b.call(_a, data);
|
|
1054
|
-
__privateGet(this,
|
|
1355
|
+
__privateGet(this, _provider2).emit("chainChanged", chainId);
|
|
1055
1356
|
(_d = (_c = __privateGet(this, _eventHandlers)) == null ? void 0 : _c.chainChanged) == null ? void 0 : _d.call(_c, chainId);
|
|
1056
|
-
__privateGet(this,
|
|
1357
|
+
__privateGet(this, _provider2).emit("accountsChanged", accounts);
|
|
1057
1358
|
(_f = (_e = __privateGet(this, _eventHandlers)) == null ? void 0 : _e.accountsChanged) == null ? void 0 : _f.call(_e, accounts);
|
|
1058
1359
|
(_g = __privateGet(this, _removeNotificationHandler)) == null ? void 0 : _g.call(this);
|
|
1059
1360
|
const onAccountsChanged = (accs) => {
|
|
@@ -1085,7 +1386,7 @@ onDisconnect_fn = function() {
|
|
|
1085
1386
|
}
|
|
1086
1387
|
__privateSet(this, _status, "disconnected");
|
|
1087
1388
|
logger("handler: disconnect");
|
|
1088
|
-
__privateGet(this,
|
|
1389
|
+
__privateGet(this, _provider2).emit("disconnect");
|
|
1089
1390
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.disconnect) == null ? void 0 : _b.call(_a);
|
|
1090
1391
|
__privateMethod(this, _MetamaskConnectEVM_instances, onAccountsChanged_fn).call(this, []);
|
|
1091
1392
|
};
|
|
@@ -1101,7 +1402,7 @@ onDisplayUri_fn = function(uri) {
|
|
|
1101
1402
|
return;
|
|
1102
1403
|
}
|
|
1103
1404
|
logger("handler: display_uri", uri);
|
|
1104
|
-
__privateGet(this,
|
|
1405
|
+
__privateGet(this, _provider2).emit("display_uri", uri);
|
|
1105
1406
|
(_b = (_a = __privateGet(this, _eventHandlers)) == null ? void 0 : _a.displayUri) == null ? void 0 : _b.call(_a, uri);
|
|
1106
1407
|
};
|
|
1107
1408
|
var MetamaskConnectEVM = _MetamaskConnectEVM;
|
|
@@ -1138,14 +1439,26 @@ function createEVMClient(options) {
|
|
|
1138
1439
|
versions: {
|
|
1139
1440
|
// typeof guard needed: Metro (React Native) bundles TS source directly,
|
|
1140
1441
|
// bypassing the tsup build that substitutes __PACKAGE_VERSION__.
|
|
1141
|
-
"connect-evm": false ? "unknown" : "1.
|
|
1442
|
+
"connect-evm": false ? "unknown" : "2.1.0"
|
|
1142
1443
|
}
|
|
1143
1444
|
}));
|
|
1144
|
-
|
|
1445
|
+
const multichainClientPeerRange = false ? "unknown" : "^1.1.0";
|
|
1446
|
+
if (multichainClientPeerRange !== "unknown" && multichainClientPeerRange !== "" && !satisfies(core.version, multichainClientPeerRange)) {
|
|
1447
|
+
console.warn(
|
|
1448
|
+
`@metamask/connect-evm expected @metamask/connect-multichain version ${multichainClientPeerRange}, but got ${core.version}. This may lead to unexpected behavior.`
|
|
1449
|
+
);
|
|
1450
|
+
}
|
|
1451
|
+
const client = yield MetamaskConnectEVM.create({
|
|
1145
1452
|
core,
|
|
1146
1453
|
eventHandlers: options.eventHandlers,
|
|
1147
1454
|
supportedNetworks: options.api.supportedNetworks
|
|
1148
1455
|
});
|
|
1456
|
+
if (!options.skipAutoAnnounce) {
|
|
1457
|
+
client.announceProvider().catch((error) => {
|
|
1458
|
+
logger("EIP-6963 provider announcement failed", error);
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
return client;
|
|
1149
1462
|
} catch (error) {
|
|
1150
1463
|
console.error("Error creating Metamask Connect/EVM", error);
|
|
1151
1464
|
throw error;
|
|
@@ -1153,6 +1466,8 @@ function createEVMClient(options) {
|
|
|
1153
1466
|
});
|
|
1154
1467
|
}
|
|
1155
1468
|
export {
|
|
1469
|
+
EIP6963_ANNOUNCE_PROVIDER_EVENT,
|
|
1470
|
+
EIP6963_REQUEST_PROVIDER_EVENT,
|
|
1156
1471
|
createEVMClient,
|
|
1157
1472
|
getInfuraRpcUrls
|
|
1158
1473
|
};
|