UNPKG

@walletpack/core

Version:

> TODO: description

255 lines (237 loc) 8.17 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _Identity = _interopRequireWildcard(require("./Identity")); var _Permission = _interopRequireDefault(require("./Permission")); var _Keypair = _interopRequireDefault(require("./Keypair")); var _Account = _interopRequireDefault(require("./Account")); var _AuthorizedApp = _interopRequireDefault(require("./AuthorizedApp")); var _CreditCard = _interopRequireDefault(require("./CreditCard")); var _StoreService = _interopRequireDefault(require("../services/utility/StoreService")); var Actions = _interopRequireWildcard(require("../store/constants")); var Keychain = /*#__PURE__*/ function () { function Keychain() { (0, _classCallCheck2["default"])(this, Keychain); this.keypairs = []; this.accounts = []; this.identities = []; this.locations = []; this.permissions = []; this.cards = []; this.apps = []; this.avatars = {}; this.lastUsedIdentity = null; } (0, _createClass2["default"])(Keychain, [{ key: "clone", value: function clone() { return Keychain.fromJson(JSON.parse(JSON.stringify(this))); } }, { key: "findIdentity", value: function findIdentity(id) { return this.identities.find(function (identity) { return identity.id === id; }); } }, { key: "updateOrPushApp", value: function updateOrPushApp(app) { this.apps.find(function (x) { return x.origin === app.origin; }) ? this.apps = this.apps.map(function (x) { return x.origin === app.origin ? app : x; }) : this.apps.unshift(app); } }, { key: "removeApp", value: function removeApp(app) { this.apps = this.apps.filter(function (x) { return x.origin !== app.origin; }); } }, { key: "findApp", value: function findApp(origin) { return this.apps.find(function (x) { return x.origin === origin; }); } }, { key: "updateOrPushIdentity", value: function updateOrPushIdentity(identity) { this.identities.find(function (id) { return id.id === identity.id; }) ? this.identities = this.identities.map(function (id) { return id.id === identity.id ? identity : id; }) : this.identities.unshift(identity); } }, { key: "removeIdentity", value: function removeIdentity(identity) { this.identities = this.identities.filter(function (id) { return id.id !== identity.id; }); this.permissions = this.permissions.filter(function (perm) { return perm.identity !== identity.id; }); delete this.avatars[identity.id]; } }, { key: "updateOrPushLocation", value: function updateOrPushLocation(location) { this.locations.find(function (id) { return id.id === location.id; }) ? this.locations = this.locations.map(function (id) { return id.id === location.id ? location : id; }) : this.locations.unshift(location); } }, { key: "removeLocation", value: function removeLocation(location) { this.locations = this.locations.filter(function (x) { return x.id !== location.id; }); this.identities.map(function (identity) { if (identity.location === location.id) { identity.location = null; } }); } }, { key: "getKeyPairByName", value: function getKeyPairByName(name) { return this.keypairs.find(function (key) { return key.name.toLowerCase() === name.toLowerCase(); }); } }, { key: "getKeyPairByPublicKey", value: function getKeyPairByPublicKey(publicKey) { if (!publicKey) return; return this.keypairs.find(function (key) { return key.publicKeys.find(function (x) { return x.key.toLowerCase() === publicKey.toLowerCase(); }); }); } }, { key: "removeKeyPair", value: function removeKeyPair(keypair) { var accountsToRemove = this.accounts.filter(function (x) { return x.keypairUnique === keypair.unique(); }).map(function (x) { return x.unique(); }); this.permissions = this.permissions.filter(function (x) { return !x.accounts.some(function (a) { return accountsToRemove.includes(a); }); }); this.accounts = this.accounts.filter(function (x) { return x.keypairUnique !== keypair.unique(); }); this.keypairs = this.keypairs.filter(function (key) { return key.unique() !== keypair.unique(); }); this.correctHistories(); this.correctAppLinks(); } }, { key: "addAccount", value: function addAccount(account) { if (!this.accounts.find(function (a) { return a.unique() === account.unique(); })) this.accounts.push(account); } }, { key: "removeAccount", value: function removeAccount(account) { var accountsToRemove = this.accounts.filter(function (x) { return x.unique() === account.unique(); }).map(function (x) { return x.unique(); }); this.permissions = this.permissions.filter(function (x) { return !x.accounts.some(function (a) { return accountsToRemove.includes(a); }); }); this.accounts = this.accounts.filter(function (a) { return a.unique() !== account.unique(); }); this.correctHistories(); this.correctAppLinks(); } }, { key: "correctHistories", value: function correctHistories() { var keypairUniques = this.keypairs.map(function (x) { return x.unique(); }); var accountUniques = this.accounts.map(function (x) { return x.unique(); }); _StoreService["default"].get().state.history.map(function (x) { var acc = _Account["default"].fromJson(x.type === 'action' ? x.account : x.from); if (!keypairUniques.includes(acc.keypairUnique) || !accountUniques.includes(acc.unique())) { _StoreService["default"].get().dispatch(Actions.DELTA_HISTORY, x); } }); } }, { key: "correctAppLinks", value: function correctAppLinks() { var origins = this.permissions.map(function (x) { return x.origin; }); this.apps = this.apps.filter(function (x) { return origins.includes(function (x) { return x.origin; }); }); } }], [{ key: "placeholder", value: function placeholder() { return new Keychain(); } }, { key: "fromJson", value: function fromJson(json) { var p = Object.assign(this.placeholder(), json); if (json.hasOwnProperty('keypairs')) p.keypairs = json.keypairs.map(function (x) { return _Keypair["default"].fromJson(x); }); if (json.hasOwnProperty('accounts')) p.accounts = json.accounts.map(function (x) { return _Account["default"].fromJson(x); }); if (json.hasOwnProperty('identities')) p.identities = json.identities.map(function (x) { return _Identity["default"].fromJson(x); }); if (json.hasOwnProperty('locations')) p.locations = json.locations.map(function (x) { return _Identity.LocationInformation.fromJson(x); }); if (json.hasOwnProperty('permissions')) p.permissions = json.permissions.map(function (x) { return _Permission["default"].fromJson(x); }); if (json.hasOwnProperty('cards')) p.cards = json.cards.map(function (x) { return _CreditCard["default"].fromJson(x); }); if (json.hasOwnProperty('apps')) p.apps = json.apps.map(function (x) { return _AuthorizedApp["default"].fromJson(x); }); return p; } }]); return Keychain; }(); exports["default"] = Keychain;