UNPKG

@funded-labs/plug-controller

Version:

Internet Computer Plug wallet's controller

101 lines (100 loc) 4.66 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Network = void 0; const errors_1 = require("../../../errors"); const Network_1 = require("./Network"); Object.defineProperty(exports, "Network", { enumerable: true, get: function () { return Network_1.Network; } }); const createNetwork = (fetch, network, blsVerify) => network.id === 'mainnet' ? new Network_1.Mainnet(network, fetch, blsVerify) : new Network_1.Network(network, fetch, blsVerify); const createNetworks = (fetch, networks, onChange, blsVerify) => { var _a, _b; if (!!((_a = Object.values(networks || {})) === null || _a === void 0 ? void 0 : _a.length)) { return (_b = Object.values(networks)) === null || _b === void 0 ? void 0 : _b.reduce((acum, net) => (Object.assign(Object.assign({}, acum), { [net.id]: createNetwork(fetch, Object.assign(Object.assign({}, net), { onChange }), blsVerify) })), {}); } return { mainnet: new Network_1.Mainnet({ onChange }, fetch) }; }; class NetworkModule { constructor({ networks, networkId, storage, onNetworkChange, fetch, blsVerify, }) { this.fetch = fetch; this.storage = storage; this.onNetworkChange = onNetworkChange; this.networkId = networkId || 'mainnet'; this.networks = createNetworks(this.fetch, networks, this.update.bind(this), this.blsVerify); this.blsVerify = blsVerify; } get network() { return this.networks[this.networkId]; } updateStorage() { this.storage.set({ networkModule: this.toJSON() }); } update() { var _a, _b; (_a = this.updateStorage) === null || _a === void 0 ? void 0 : _a.call(this); (_b = this.onNetworkChange) === null || _b === void 0 ? void 0 : _b.call(this, this.network); } addNetwork(networkParams) { // Validate network host is a valid https url // if (!networkParams.host.startsWith('https://')) { // throw new Error('Network must start with https://'); // } if (Object.values(this.networks).some(net => net.host === networkParams.host)) { throw new Error(`A Network with host ${networkParams.host} already exists`); } const network = createNetwork(this.fetch, Object.assign(Object.assign({}, networkParams), { onChange: this.update.bind(this) }), this.blsVerify); this.networks = Object.assign(Object.assign({}, this.networks), { [network.id]: network }); this.update(); return this.networks; } removeNetwork(networkId) { if (networkId === 'mainnet') throw new Error('Cannot remove mainnet'); if (!Object.keys(this.networks).includes(networkId)) throw new Error(errors_1.ERRORS.INVALID_NETWORK_ID); // If we remove the current network, default to mainnet. if (networkId === this.network.id) { this.networkId = 'mainnet'; } const _a = this.networks, _b = networkId, network = _a[_b], networks = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); this.networks = networks; this.update(); return this.networks; } setNetwork(networkId) { const network = this.networks[networkId]; if (!network) throw new Error(errors_1.ERRORS.INVALID_NETWORK_ID); this.networkId = networkId; this.update(); return network; } editNetwork(networkId, params) { var _a; const network = this.networks[networkId]; if (!network) throw new Error(errors_1.ERRORS.INVALID_NETWORK_ID); (_a = network === null || network === void 0 ? void 0 : network.edit) === null || _a === void 0 ? void 0 : _a.call(network, params); this.networks = Object.assign(Object.assign({}, this.networks), { [networkId]: network }); this.update(); return network; } toJSON() { return { networkId: this.networkId, networks: Object.values(this.networks).reduce((acum, net) => (Object.assign(Object.assign({}, acum), { [net.id]: net.toJSON() })), {}), }; } } exports.default = NetworkModule;