UNPKG

@broxus/tvm-connect

Version:

TypeScript SDK for connecting to Nekoton-compatible wallets using a unified interface.

190 lines (189 loc) 7.52 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TvmConnectDialogStore = void 0; const js_core_1 = require("@broxus/js-core"); const js_utils_1 = require("@broxus/js-utils"); const events_1 = __importDefault(require("events")); const mobx_1 = require("mobx"); const utils_1 = require("../utils"); class TvmConnectDialogStore extends js_core_1.AbstractStore { _service; params; _events; constructor(_service, params) { super(); this._service = _service; this.params = params; this._events = new events_1.default(); this.setState(() => ({ popupType: params?.popupType, qrEnabled: params?.qrEnabled ?? true, })); (0, mobx_1.makeObservable)(this); } connect(options) { this.setData('network', options?.network); this.setState({ isActive: true, popupType: options?.popupType || this._state.popupType, qrEnabled: options?.qrEnabled || this._state.qrEnabled, }); const events = { onClose: () => { this._events.off('connected', events.onConnect); this._events.off('close', events.onClose); options?.onClose?.(); }, onConnect: async (connector) => { this._events.off('connected', events.onConnect); this._events.off('close', events.onClose); await options?.onConnect?.(connector); this.close(); }, }; this._events.once('connected', events.onConnect); this._events.once('close', events.onClose); } async connectTo(providerConfig, options) { try { this.setData('connectingProvider', providerConfig); const recentMeta = (0, utils_1.getRecentConnectionMeta)(this.params?.recentMetaStorageKey); const existedChainId = this._service.chainId; const prevConnector = this._service.connector; // eslint-disable-next-line no-nested-ternary const networkParams = options?.network ?? this._data.network ?? (recentMeta?.chainId ? (this._service.networks.find(network => network.chainId === recentMeta?.chainId) ?? existedChainId) : existedChainId); (0, js_utils_1.debug)(`Try to activate ${providerConfig.info?.name} connection with chain`, typeof networkParams === 'number' ? networkParams : networkParams?.chainId); await providerConfig.connector?.connect(typeof networkParams === 'number' ? networkParams : networkParams && (0, utils_1.toChainParams)(networkParams), options); if (this._service.providerId && providerConfig.id !== this._service.providerId) { await prevConnector?.disconnect({ force: true }); } await options?.onConnect?.(providerConfig.connector); this._events.emit('connected', providerConfig.connector); (0, utils_1.storeRecentConnectionMeta)({ chainId: providerConfig.connector.chainId?.toString(), disconnected: false, providerId: providerConfig.id, type: providerConfig.connector.type, }, this.params?.recentMetaStorageKey); this._service.setState('providerId', providerConfig.id); this.setData('connectingProvider', undefined); this.setState('error', undefined); } catch (e) { this.setState('error', e); } } open() { this.setState('isActive', true); this._events.emit('open'); } close() { this.setState('isActive', false); this._events.emit('close'); } reset() { this.setData({ connectingProvider: undefined, network: undefined, }); this.setState('error', undefined); } get connectingProvider() { return this._data.connectingProvider; } get error() { return this._state.error; } get isActive() { return this._state.isActive; } get popupType() { return this._state.popupType; } get qrEnabled() { return this._state.qrEnabled; } get requestState() { return this._state.requestState; } } exports.TvmConnectDialogStore = TvmConnectDialogStore; __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], TvmConnectDialogStore.prototype, "connect", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], TvmConnectDialogStore.prototype, "connectTo", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TvmConnectDialogStore.prototype, "open", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TvmConnectDialogStore.prototype, "close", null); __decorate([ mobx_1.action.bound, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TvmConnectDialogStore.prototype, "reset", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TvmConnectDialogStore.prototype, "connectingProvider", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TvmConnectDialogStore.prototype, "error", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TvmConnectDialogStore.prototype, "isActive", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TvmConnectDialogStore.prototype, "popupType", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TvmConnectDialogStore.prototype, "qrEnabled", null); __decorate([ mobx_1.computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], TvmConnectDialogStore.prototype, "requestState", null);