Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@web3-react/metamask

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@web3-react/metamask - npm Package Compare versions

Comparing version 8.0.5-alpha.0 to 8.0.5-beta.0

dist/cjs/index.js

24

dist/index.d.ts

@@ -1,3 +0,4 @@

import { Connector, Actions } from '@web3-react/types';
import type detectEthereumProvider from '@metamask/detect-provider';
import type { Actions, AddEthereumChainParameter } from '@web3-react/types';
import { Connector } from '@web3-react/types';
export declare class NoMetaMaskError extends Error {

@@ -8,6 +9,19 @@ constructor();

private readonly options?;
private providerPromise?;
constructor(actions: Actions, options?: NonNullable<Parameters<typeof detectEthereumProvider>[0]>, connectEagerly?: boolean);
private startListening;
activate(): Promise<void>;
private eagerConnection?;
/**
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed.
* @param options - Options to pass to `@metamask/detect-provider`
*/
constructor(actions: Actions, connectEagerly?: boolean, options?: Parameters<typeof detectEthereumProvider>[0]);
private initialize;
/**
* Initiates a connection.
*
* @param desiredChainIdOrChainParameters - If defined, indicates the desired chain to connect to. If the user is
* already connected to this chain, no additional steps will be taken. Otherwise, the user will be prompted to switch
* to the chain, if one of two conditions is met: either they already have it added in their extension, or the
* argument is of type AddEthereumChainParameter, in which case the user will be prompted to add the chain with the
* specified parameters first, before being prompted to switch.
*/
activate(desiredChainIdOrChainParameters?: number | AddEthereumChainParameter): Promise<void>;
}

@@ -22,14 +22,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

export class MetaMask extends Connector {
constructor(actions, options, connectEagerly = true) {
/**
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed.
* @param options - Options to pass to `@metamask/detect-provider`
*/
constructor(actions, connectEagerly = true, options) {
super(actions);
this.options = options;
if (connectEagerly) {
this.providerPromise = this.startListening(connectEagerly);
this.eagerConnection = this.initialize(true);
}
}
startListening(connectEagerly) {
initialize(connectEagerly) {
return __awaiter(this, void 0, void 0, function* () {
yield import('@metamask/detect-provider')
.then((m) => m.default)
.then((detectEthereumProvider) => detectEthereumProvider(this.options))
let cancelActivation;
if (connectEagerly) {
cancelActivation = this.actions.startActivation();
}
return import('@metamask/detect-provider')
.then((m) => m.default(this.options))
.then((provider) => {

@@ -49,3 +56,9 @@ var _a;

this.provider.on('accountsChanged', (accounts) => {
this.actions.update({ accounts });
if (accounts.length === 0) {
// handle this edge case by disconnecting
this.actions.reportError(undefined);
}
else {
this.actions.update({ accounts });
}
});

@@ -58,38 +71,80 @@ if (connectEagerly) {

.then(([chainId, accounts]) => {
if (accounts.length > 0) {
if (accounts.length) {
this.actions.update({ chainId: parseChainId(chainId), accounts });
}
else {
throw new Error('No accounts returned');
}
})
.catch((error) => {
console.debug('Could not connect eagerly', error);
cancelActivation();
});
}
}
else if (connectEagerly) {
cancelActivation();
}
});
});
}
activate() {
/**
* Initiates a connection.
*
* @param desiredChainIdOrChainParameters - If defined, indicates the desired chain to connect to. If the user is
* already connected to this chain, no additional steps will be taken. Otherwise, the user will be prompted to switch
* to the chain, if one of two conditions is met: either they already have it added in their extension, or the
* argument is of type AddEthereumChainParameter, in which case the user will be prompted to add the chain with the
* specified parameters first, before being prompted to switch.
*/
activate(desiredChainIdOrChainParameters) {
return __awaiter(this, void 0, void 0, function* () {
const desiredChainId = typeof desiredChainIdOrChainParameters === 'number'
? desiredChainIdOrChainParameters
: desiredChainIdOrChainParameters === null || desiredChainIdOrChainParameters === void 0 ? void 0 : desiredChainIdOrChainParameters.chainId;
this.actions.startActivation();
if (!this.providerPromise) {
this.providerPromise = this.startListening(false);
if (!this.eagerConnection) {
this.eagerConnection = this.initialize(false);
}
yield this.providerPromise;
if (this.provider) {
yield Promise.all([
this.provider.request({ method: 'eth_chainId' }),
this.provider.request({ method: 'eth_requestAccounts' }),
])
.then(([chainId, accounts]) => {
this.actions.update({ chainId: Number.parseInt(chainId, 16), accounts });
yield this.eagerConnection;
if (!this.provider) {
return this.actions.reportError(new NoMetaMaskError());
}
return Promise.all([
this.provider.request({ method: 'eth_chainId' }),
this.provider.request({ method: 'eth_requestAccounts' }),
])
.then(([chainId, accounts]) => {
const receivedChainId = parseChainId(chainId);
// if there's no desired chain, or it's equal to the received, update
if (!desiredChainId || receivedChainId === desiredChainId) {
return this.actions.update({ chainId: receivedChainId, accounts });
}
// if we're here, we can try to switch networks
const desiredChainIdHex = `0x${desiredChainId.toString(16)}`;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainIdHex }],
})
.catch((error) => {
this.actions.reportError(error);
});
}
else {
this.actions.reportError(new NoMetaMaskError());
}
if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') {
// if we're here, we can try to add a new network
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.provider.request({
method: 'wallet_addEthereumChain',
params: [Object.assign(Object.assign({}, desiredChainIdOrChainParameters), { chainId: desiredChainIdHex })],
});
}
else {
throw error;
}
})
.then(() => this.activate(desiredChainId));
})
.catch((error) => {
this.actions.reportError(error);
});
});
}
}
{
"name": "@web3-react/metamask",
"keywords": [
"web3-react",
"metamask"
],
"author": "Noah Zinsmeister <noahwz@gmail.com>",
"license": "GPL-3.0-or-later",
"repository": "github:NoahZinsmeister/web3-react",
"publishConfig": {
"access": "public"
},
"version": "8.0.5-alpha.0",
"version": "8.0.5-beta.0",
"type": "module",
"exports": "./dist/index.js",
"files": [
"dist/*"
],
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/cjs/index.js"
},
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"main": "./dist/cjs/index.js",
"scripts": {
"prebuild": "rm -rf dist",
"build": "tsc",
"build": "tsc && tsc --project tsconfig.cjs.json",
"start": "tsc --watch"

@@ -17,5 +33,8 @@ },

"@metamask/detect-provider": "^1.2.0",
"@web3-react/types": "^8.0.5-alpha.0"
"@web3-react/types": "^8.0.3-beta.0"
},
"gitHead": "b66d6733ff21e2ede6f6d5f05f9be4909d3c9ef0"
"devDependencies": {
"@web3-react/store": "^8.0.4-beta.0"
},
"gitHead": "f0d50fa4ecd438637f43676943ea4c88497115b2"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc