UNPKG

@walletpack/core

Version:

> TODO: description

464 lines (403 loc) 14.1 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"] = exports.LocationInformation = exports.PersonalInformation = exports.IdentityRequiredFields = exports.LocationFields = exports.PersonalFields = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _aesOop = _interopRequireDefault(require("aes-oop")); var _IdGenerator = _interopRequireDefault(require("../util/IdGenerator")); var _Network = _interopRequireDefault(require("./Network")); var _StoreService = _interopRequireDefault(require("../services/utility/StoreService")); var Actions = _interopRequireWildcard(require("../store/constants")); var _PluginRepository = _interopRequireDefault(require("../plugins/PluginRepository")); var _Blockchains = require("./Blockchains"); /********************************************/ /* REQUIREABLE FIELDS */ /********************************************/ var PersonalFields = { firstname: 'firstname', lastname: 'lastname', email: 'email', birthdate: 'birthdate' }; exports.PersonalFields = PersonalFields; var LocationFields = { phone: 'phone', address: 'address', city: 'city', state: 'state', country: 'country', zipcode: 'zipcode' }; exports.LocationFields = LocationFields; var IdentityRequiredFields = /*#__PURE__*/ function () { function IdentityRequiredFields() { (0, _classCallCheck2["default"])(this, IdentityRequiredFields); this.accounts = []; this.personal = []; this.location = []; } (0, _createClass2["default"])(IdentityRequiredFields, [{ key: "clone", value: function clone() { return IdentityRequiredFields.fromJson(JSON.parse(JSON.stringify(this))); } }, { key: "isEmpty", value: function isEmpty() { return !this.personal.length && !this.location.length; } }, { key: "isValid", value: function isValid() { if (JSON.stringify(Object.keys(new IdentityRequiredFields())) !== JSON.stringify(Object.keys(this))) return false; if (!this.personal.every(function (field) { return Object.keys(PersonalFields).includes(field); })) return false; if (!this.location.every(function (field) { return Object.keys(LocationFields).includes(field); })) return false; return true; } }, { key: "forPermission", value: function forPermission() { var fields = []; this.accounts.map(function (x) { return fields.push("account:".concat(_Network["default"].fromJson(x).unique())); }); this.location.map(function (x) { return fields.push("location:".concat(x)); }); this.personal.map(function (x) { return fields.push("personal:".concat(x)); }); return fields.sort(); } }], [{ key: "placeholder", value: function placeholder() { return new IdentityRequiredFields(); } }, { key: "fromJson", value: function fromJson(json) { return Object.assign(new IdentityRequiredFields(), json); } }, { key: "fromPermission", value: function fromPermission(requirements) { var p = IdentityRequiredFields.placeholder(); p.accounts = requirements.filter(function (x) { return x.indexOf('account:') > -1; }).map(function (x) { return _Network["default"].fromUnique(x.split('account:')[1]); }); p.personal = requirements.filter(function (x) { return x.indexOf('personal:') > -1; }).map(function (x) { return x.split('personal:')[1]; }); p.location = requirements.filter(function (x) { return x.indexOf('location:') > -1; }).map(function (x) { return x.split('location:')[1]; }); return p; } }]); return IdentityRequiredFields; }(); /********************************************/ /* Personal Information */ /********************************************/ exports.IdentityRequiredFields = IdentityRequiredFields; var PersonalInformation = /*#__PURE__*/ function () { function PersonalInformation() { var _this = this; (0, _classCallCheck2["default"])(this, PersonalInformation); Object.keys(PersonalFields).forEach(function (fieldName) { return _this[fieldName] = ''; }); } (0, _createClass2["default"])(PersonalInformation, [{ key: "findFields", value: function findFields(fields) { var _this2 = this; return fields.filter(function (field) { return _this2.hasOwnProperty(field) && _this2[field].length; }); } }], [{ key: "placeholder", value: function placeholder() { return new PersonalInformation(); } }, { key: "fromJson", value: function fromJson(json) { return Object.assign(this.placeholder(), json); } }]); return PersonalInformation; }(); /********************************************/ /* Location Information */ /********************************************/ exports.PersonalInformation = PersonalInformation; var LocationInformation = /*#__PURE__*/ function () { function LocationInformation() { var _this3 = this; (0, _classCallCheck2["default"])(this, LocationInformation); this.id = _IdGenerator["default"].numeric(10); this.name = 'Unnamed Location'; this.isDefault = false; Object.keys(LocationFields).forEach(function (fieldName) { return _this3[fieldName] = ''; }); } (0, _createClass2["default"])(LocationInformation, [{ key: "clone", value: function clone() { return LocationInformation.fromJson(JSON.parse(JSON.stringify(this))); } }, { key: "findFields", value: function findFields(fields) { var _this4 = this; var foundFields = fields.filter(function (field) { return field !== LocationFields.country; }).filter(function (field) { return _this4.hasOwnProperty(field) && _this4[field].length; }); if (fields.includes(LocationFields.country) && this.hasOwnProperty('country') && typeof this.country !== 'string') foundFields.push(LocationFields.country); return foundFields; } }, { key: "hasFields", value: function hasFields(fields) { return this.findFields(fields).length === fields.length; } }], [{ key: "placeholder", value: function placeholder() { return new LocationInformation(); } }, { key: "fromJson", value: function fromJson(json) { return Object.assign(this.placeholder(), json); } }]); return LocationInformation; }(); /********************************************/ /* Identity */ /********************************************/ exports.LocationInformation = LocationInformation; var _require = require('eosjs-ecc'), PrivateKey = _require.PrivateKey; var Identity = /*#__PURE__*/ function () { function Identity() { (0, _classCallCheck2["default"])(this, Identity); // Basic fields this.id = _IdGenerator["default"].text(24); this.hash = ''; this.privateKey = ''; this.publicKey = ''; this.name = ''; // Requireable fields this.personal = PersonalInformation.placeholder(); // this.locations = [LocationInformation.placeholder()]; this.location = null; // KYC this.kyc = false; this.ridl = -1; this.fioDomain = null; } (0, _createClass2["default"])(Identity, [{ key: "initialize", value: function initialize(hash) { var _this5 = this; return new Promise(function (resolve, reject) { PrivateKey.randomKey().then(function (privateKey) { _this5.privateKey = PrivateKey(privateKey.toWif()).toBuffer(); _this5.publicKey = privateKey.toPublic().toString(); _this5.hash = hash; resolve(true); }); }); } }, { key: "clone", value: function clone() { return Identity.fromJson(JSON.parse(JSON.stringify(this))); } }, { key: "isEncrypted", value: function isEncrypted() { return this.privateKey.length > 70; } }, { key: "encrypt", value: function encrypt(seed) { if (!this.isEncrypted()) this.privateKey = _aesOop["default"].encrypt(this.privateKey, seed); } }, { key: "decrypt", value: function decrypt(seed) { if (this.isEncrypted()) this.privateKey = _aesOop["default"].decrypt(this.privateKey, seed); } }, { key: "defaultLocation", value: function defaultLocation() { return this.getLocation() || _StoreService["default"].get().state.scatter.keychain.locations[0]; } /*** * Checks if an Identity has specified fields. * This is used when an interacting application requires specific information. * @param fields - The fields to check for * @param selectedLocation * @returns {boolean} */ }, { key: "hasRequiredFields", value: function hasRequiredFields(fields) { var _this6 = this; var selectedLocation = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var requiredFields = IdentityRequiredFields.fromJson(fields); if (!requiredFields.isValid()) return false; if (requiredFields.personal.length) if (!requiredFields.personal.every(function (field) { return _this6.personal[field].length; })) return false; if (selectedLocation) { if (!selectedLocation.hasFields(fields.location)) return false; } else { if (requiredFields.location.length) { if (this.getLocation()) { return !!this.getLocation().hasFields(requiredFields.location); } else { return false; } } } return true; } /*** * Returns an object with only the required fields from this Identity * @param fields * @param location */ }, { key: "asOnlyRequiredFields", value: function asOnlyRequiredFields(fields) { var _this7 = this; var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var requiredFields = IdentityRequiredFields.fromJson(fields); if (!requiredFields.isValid()) return null; var identity = { hash: this.hash, publicKey: this.publicKey, name: this.name }; if (requiredFields.personal.length) { identity.personal = {}; requiredFields.personal.map(function (field) { return identity.personal[field] = _this7.personal[field]; }); } if (requiredFields.location.length) { identity.location = {}; if (!location) location = this.defaultLocation(); if (location) { requiredFields.location.map(function (field) { return identity.location[field] = location[field]; }); } } return identity; } /*** * Sets up the fields returned to the application * @param requiredFields * @param identity * @param selectedLocation */ }, { key: "getPropertyValueByName", /*** * Returns the value of a property based on the requirable name. * @param requirable * @param location */ value: function getPropertyValueByName(requirable) { var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; if (Object.keys(this).includes(requirable)) return this[requirable];else if (Object.keys(this.personal).includes(requirable)) return this.personal[requirable];else { var field = (location ? location : this.defaultLocation())[requirable]; return (0, _typeof2["default"])(field) === 'object' ? field.name : field; } } }, { key: "fullname", value: function fullname() { return "".concat(this.personal.firstname || '[NO FIRST NAME]', " ").concat(this.personal.lastname || '[NO LAST NAME]'); } }, { key: "getLocation", value: function getLocation() { var _this8 = this; if (!this.location) return; return _StoreService["default"].get().state.scatter.keychain.locations.find(function (x) { return x.id === _this8.location; }); } }, { key: "setAsLastUsed", value: function setAsLastUsed() { var scatter = _StoreService["default"].get().state.scatter.clone(); scatter.keychain.lastUsedIdentity = this.id; return _StoreService["default"].get().dispatch(Actions.SET_SCATTER, scatter); } }], [{ key: "placeholder", value: function placeholder() { return new Identity(); } }, { key: "fromJson", value: function fromJson(json) { var p = Object.assign(this.placeholder(), json); p.personal = PersonalInformation.fromJson(json.personal); // if(json.hasOwnProperty('locations')) p.locations = json.locations.map(location => LocationInformation.fromJson(location)); // else p.locations = [LocationInformation.placeholder()]; return p; } }, { key: "asReturnedFields", value: function asReturnedFields(requiredFields, identity) { var selectedLocation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; var returnedFields = identity.asOnlyRequiredFields(requiredFields, selectedLocation); delete returnedFields.hash; delete returnedFields.name; delete returnedFields.publicKey; delete returnedFields.kyc; delete returnedFields.ridl; return returnedFields; } }, { key: "nameIsValid", value: function nameIsValid(name) { return /^[a-zA-Z0-9_-]{3,20}$/.test(name); } }]); return Identity; }(); exports["default"] = Identity;