@0xsequence/web3-react-v6-connector
Version:
A simple, maximally extensible, dependency minimized framework for building modern Ethereum dApps
242 lines (238 loc) • 8.84 kB
JavaScript
import { AbstractConnector } from '@web3-react/abstract-connector';
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
_setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
var supportedNetworks = [1, 137, 4, 5];
function parseChainId(chainId) {
if (typeof chainId === 'number') {
return chainId;
}
return Number.parseInt(chainId, 16);
}
var SequenceConnector = /*#__PURE__*/function (_AbstractConnector) {
_inheritsLoose(SequenceConnector, _AbstractConnector);
function SequenceConnector(_ref) {
var _this;
var chainId = _ref.chainId,
appName = _ref.appName;
_this = _AbstractConnector.call(this, {
supportedChainIds: supportedNetworks.concat([chainId])
}) || this;
_this.chainId = chainId;
_this.appName = appName || 'app';
_this.handleNetworkChanged = _this.handleNetworkChanged.bind(_assertThisInitialized(_this));
_this.handleChainChanged = _this.handleChainChanged.bind(_assertThisInitialized(_this));
_this.handleAccountsChanged = _this.handleAccountsChanged.bind(_assertThisInitialized(_this));
_this.handleClose = _this.handleClose.bind(_assertThisInitialized(_this));
return _this;
}
var _proto = SequenceConnector.prototype;
_proto.handleChainChanged = function handleChainChanged(chainId) {
this.emitUpdate({
chainId: parseChainId(chainId),
provider: this.sequenceProvider
});
};
_proto.handleAccountsChanged = function handleAccountsChanged(accounts) {
if (accounts.length === 0) {
this.deactivate();
} else {
this.emitUpdate({
account: accounts[0]
});
}
};
_proto.handleClose = function handleClose() {
this.deactivate();
};
_proto.handleNetworkChanged = function handleNetworkChanged(networkId) {
this.emitUpdate({
chainId: parseChainId(networkId),
provider: this.sequenceProvider
});
};
_proto.listenToEvents = function listenToEvents() {
this.sequenceProvider.on('chainChanged', this.handleChainChanged);
this.sequenceProvider.on('accountsChanged', this.handleAccountsChanged);
this.sequenceProvider.on('close', this.handleClose);
this.sequenceProvider.on('networkChanged', this.handleNetworkChanged);
};
_proto.activate = function activate() {
try {
var _temp9 = function _temp9(_result2) {
if (_exit4) return _result2;
function _temp6() {
var _exit2 = false;
function _temp4(_result3) {
var _exit3 = false;
if (_exit2) return _result3;
function _temp2(_result4) {
if (_exit3) return _result4;
console.error('Failed to connect');
throw new Error("Failed to connect");
}
var _temp = function () {
if (_this3.sequenceWallet.isConnected()) {
// @ts-ignore
_this3.sequenceProvider = _this3.sequenceWallet.getProvider();
return Promise.resolve(_this3.sequenceWallet.getAddress()).then(function (walletAddress) {
return Promise.resolve(_this3.sequenceWallet.getChainId()).then(function (chainId) {
_this3.chainId = parseChainId(chainId);
_this3.listenToEvents();
var _provider$account2 = {
provider: _this3.sequenceProvider,
account: walletAddress
};
_exit3 = true;
return _provider$account2;
});
});
}
}();
return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
}
var _temp3 = function () {
if (!_this3.sequenceWallet.isConnected()) {
return Promise.resolve(_this3.sequenceWallet.connect({
app: _this3.appName,
authorize: true
})).then(function (connectDetails) {
if (!connectDetails.connected) {
console.error('Failed to connect');
throw new Error("Failed to connect");
}
});
}
}();
return _temp3 && _temp3.then ? _temp3.then(_temp4) : _temp4(_temp3);
}
var _temp5 = function () {
if (!_this3.sequenceWallet) {
return Promise.resolve(import('0xsequence').then(function (m) {
return m == null ? void 0 : m.sequence;
})).then(function (sequence) {
var defaultNetwork = _this3.chainId || 137;
return Promise.resolve(sequence.initWallet(defaultNetwork)).then(function (_sequence$initWallet) {
_this3.sequenceWallet = _sequence$initWallet;
});
});
}
}();
return _temp5 && _temp5.then ? _temp5.then(_temp6) : _temp6(_temp5);
};
var _exit4 = false;
var _this3 = this;
var _temp10 = function () {
var _window;
if ((_window = window) != null && _window.ethereum && window.ethereum.isSequence) {
_this3.sequenceProvider = window.ethereum;
return function () {
if (_this3.sequenceProvider) {
return Promise.resolve(_this3.sequenceProvider.request({
method: 'eth_requestAccounts'
})).then(function () {
return Promise.resolve(Promise.all([_this3.sequenceProvider.request({
method: 'eth_chainId'
}), _this3.sequenceProvider.request({
method: 'eth_accounts'
})])).then(function (_ref2) {
var chainId = _ref2[0],
accounts = _ref2[1];
_this3.chainId = parseChainId(chainId);
_this3.listenToEvents();
var _provider$account = {
provider: _this3.sequenceProvider,
account: accounts[0]
};
_exit4 = true;
return _provider$account;
});
});
}
}();
}
}();
return Promise.resolve(_temp10 && _temp10.then ? _temp10.then(_temp9) : _temp9(_temp10));
} catch (e) {
return Promise.reject(e);
}
};
_proto.getProvider = function getProvider() {
try {
var _this5 = this;
return Promise.resolve(_this5.sequenceProvider);
} catch (e) {
return Promise.reject(e);
}
};
_proto.getChainId = function getChainId() {
try {
var _this7 = this;
return Promise.resolve(_this7.chainId);
} catch (e) {
return Promise.reject(e);
}
};
_proto.getAccount = function getAccount() {
try {
var _this9 = this;
return Promise.resolve(_this9.sequenceProvider.request({
method: 'eth_accounts'
}).then(function (accounts) {
return accounts[0];
}));
} catch (e) {
return Promise.reject(e);
}
};
_proto.deactivate = function deactivate() {
try {
var _temp13 = function _temp13() {
if (_this11.sequenceProvider) {
_this11.sequenceProvider.removeListener('chainChanged', _this11.handleChainChanged);
_this11.sequenceProvider.removeListener('accountsChanged', _this11.handleAccountsChanged);
_this11.sequenceProvider.removeListener('close', _this11.handleClose);
_this11.sequenceProvider.removeListener('networkChanged', _this11.handleNetworkChanged);
}
_this11.sequenceWallet = undefined;
_this11.sequenceProvider = undefined;
_this11.emitDeactivate();
};
var _this11 = this;
var _temp14 = function () {
if (_this11.sequenceWallet) {
return Promise.resolve(_this11.sequenceWallet.disconnect()).then(function () {});
}
}();
return Promise.resolve(_temp14 && _temp14.then ? _temp14.then(_temp13) : _temp13(_temp14));
} catch (e) {
return Promise.reject(e);
}
};
_proto.close = function close() {
try {
var _this13 = this;
return Promise.resolve(_this13.deactivate()).then(function () {});
} catch (e) {
return Promise.reject(e);
}
};
return SequenceConnector;
}(AbstractConnector);
export { SequenceConnector };
//# sourceMappingURL=web3-react-v6-connector.esm.js.map