@web3-vue-org2/openblock
Version:
Web3 Vue Connector for OpenBlock
180 lines (179 loc) • 9.43 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenBlock = exports.NoOpenBlockError = void 0;
const types_1 = require("@web3-vue-org2/types");
require("regenerator-runtime");
class NoOpenBlockError extends Error {
constructor() {
super('OpenBlock not installed');
this.name = NoOpenBlockError.name;
Object.setPrototypeOf(this, NoOpenBlockError.prototype);
}
}
exports.NoOpenBlockError = NoOpenBlockError;
function parseChainId(chainId) {
return Number.parseInt(chainId, 16);
}
const SUPPORTED_CHAIN_IDS = [1, 56, 128, 250, 137, 43114, 25, 8217, 10, 100, 42161, 42170, 10000, 1666600000, 5, 280];
function detectOpenBlock() {
return new Promise((resolve, reject) => {
Promise.resolve().then(() => __importStar(require('@openblockhq/dappsdk'))).then(() => {
resolve(window.openblock);
});
});
}
class OpenBlock extends types_1.Connector {
constructor({ actions, onError }) {
super(actions, false, onError);
}
isomorphicInitialize() {
return __awaiter(this, void 0, void 0, function* () {
if (this.eagerConnection)
return;
return (this.eagerConnection = detectOpenBlock().then((provider) => {
if (provider) {
this.provider = provider;
this.provider.on('connect', ({ chainId }) => {
this.actions.update({ chainId: parseChainId(chainId) });
});
this.provider.on('disconnect', (error) => {
var _a;
// 1013 indicates that OpenBlock is attempting to reestablish the connection
// https://github.com/MetaMask/providers/releases/tag/v8.0.0
if (error.code === 1013) {
console.debug('OpenBlock logged connection error 1013: "Try again later"');
return;
}
this.actions.resetState();
(_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, error);
});
this.provider.on('chainChanged', (chainId) => {
this.actions.update({ chainId: parseChainId(chainId) });
});
this.provider.on('accountsChanged', (accounts) => {
if (accounts.length === 0) {
// handle this edge case by disconnecting
this.actions.resetState();
}
else {
this.actions.update({ accounts });
}
});
}
}));
});
}
/** {@inheritdoc Connector.connectEagerly} */
connectEagerly() {
return __awaiter(this, void 0, void 0, function* () {
const cancelActivation = this.actions.startActivation();
try {
yield this.isomorphicInitialize();
if (!this.provider)
return cancelActivation();
// Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing
// chains; they should be requested serially, with accounts first, so that the chainId can settle.
const accounts = (yield this.provider.request({ method: 'eth_accounts' }));
if (!accounts.length)
throw new Error('No accounts returned');
const chainId = (yield this.provider.request({ method: 'eth_chainId' }));
this.actions.update({ chainId: parseChainId(chainId), accounts, changing: false });
}
catch (error) {
console.debug('Could not connect eagerly', error);
// we should be able to use `cancelActivation` here, but on mobile, metamask emits a 'connect'
// event, meaning that chainId is updated, and cancelActivation doesn't work because an intermediary
// update has occurred, so we reset state instead
this.actions.resetState();
}
});
}
/**
* 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* () {
return this.isomorphicInitialize().then(() => __awaiter(this, void 0, void 0, function* () {
if (!this.provider)
throw new NoOpenBlockError();
if (desiredChainIdOrChainParameters && !SUPPORTED_CHAIN_IDS.includes(desiredChainIdOrChainParameters)) {
throw new types_1.UnSupportedChainError(desiredChainIdOrChainParameters);
}
// Wallets may resolve eth_chainId and hang on eth_accounts pending user interaction, which may include changing
// chains; they should be requested serially, with accounts first, so that the chainId can settle.
const accounts = (yield this.provider.request({ method: 'eth_requestAccounts' }));
const chainId = (yield this.provider.request({ method: 'eth_chainId' }));
const receivedChainId = parseChainId(chainId);
const desiredChainId = desiredChainIdOrChainParameters;
// 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, changing: false });
const desiredChainIdHex = `0x${desiredChainId.toString(16)}`;
// if we're here, we can try to switch networks
return this.provider
.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: desiredChainIdHex }],
})
.catch((error) => {
throw error;
})
.then(() => this._activate(desiredChainId));
}));
});
}
/**
* 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) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
return this.startActive(!!((_a = this.provider) === null || _a === void 0 ? void 0 : _a.isLogin), desiredChainIdOrChainParameters);
});
}
}
exports.OpenBlock = OpenBlock;