UNPKG

identifi-lib

Version:

Basic tools for reading and writing Identifi messages and identities.

195 lines (152 loc) 5.81 kB
'use strict'; exports.__esModule = true; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _identicon = require('identicon.js'); var _identicon2 = _interopRequireDefault(_identicon); var _util = require('./util'); var _util2 = _interopRequireDefault(_util); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /*eslint no-useless-escape: "off", camelcase: "off" */ var UNIQUE_ID_VALIDATORS = { email: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i, bitcoin: /^[13][a-km-zA-HJ-NP-Z0-9]{26,33}$/, bitcoin_address: /^[13][a-km-zA-HJ-NP-Z0-9]{26,33}$/, ip: /^(([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|2[0-5][0-5]|2[0-4]\d)$/, ipv6: /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/, gpg_fingerprint: null, gpg_keyid: null, google_oauth2: null, tel: /^\d{7,}$/, phone: /^\d{7,}$/, keyID: null, url: /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi, account: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i, uuid: /[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}/ }; /** * A simple key-value pair. */ var Attribute = function () { /** * Usage: new Attribute(value) or new Attribute(type, value) * @param {string} a * @param {string} b */ function Attribute(a, b) { _classCallCheck(this, Attribute); if (typeof a === 'object' && typeof a.type === 'string' && typeof a.value === 'string') { b = a.value; a = a.type; } if (typeof a !== 'string') { throw new Error('First param must be a string, got ' + (typeof a === 'undefined' ? 'undefined' : _typeof(a)) + ': ' + JSON.stringify(a)); } if (!a.length) { throw new Error('First param string is empty'); } if (b) { if (typeof b !== 'string') { throw new Error('Second parameter must be a string, got ' + (typeof b === 'undefined' ? 'undefined' : _typeof(b)) + ': ' + JSON.stringify(b)); } if (!b.length) { throw new Error('Second param string is empty'); } this.type = a; this.value = b; } else { this.value = a; var t = Attribute.guessTypeOf(this.value); if (t) { this.type = t; } else { throw new Error('Type of attribute was omitted and could not be guessed'); } } } Attribute.getUuid = function getUuid() { var b = function b(a) { return a ? (a ^ Math.random() * 16 >> a / 4).toString(16) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, b); }; return new Attribute('uuid', b()); }; /** * @returns {Object} an object with attribute types as keys and regex patterns as values */ Attribute.getUniqueIdValidators = function getUniqueIdValidators() { return UNIQUE_ID_VALIDATORS; }; /** * @param {string} type attribute type * @returns {boolean} true if the attribute type is unique */ Attribute.isUniqueType = function isUniqueType(type) { return Object.keys(UNIQUE_ID_VALIDATORS).indexOf(type) > -1; }; /** * @returns {boolean} true if the attribute type is unique */ Attribute.prototype.isUniqueType = function isUniqueType() { return Attribute.isUniqueType(this.type); }; /** * @param {string} value guess type of this attribute value * @returns {string} type of attribute value or undefined */ Attribute.guessTypeOf = function guessTypeOf(value) { for (var key in UNIQUE_ID_VALIDATORS) { if (value.match(UNIQUE_ID_VALIDATORS[key])) { return key; } } }; /** * @param {Attribute} a * @param {Attribute} b * @returns {boolean} true if params are equal */ Attribute.equals = function equals(a, b) { try { return a.equals(b); } catch (e) { return false; } }; /** * @param {Attribute} a attribute to compare to * @returns {boolean} true if attribute matches param */ Attribute.prototype.equals = function equals(a) { return a && this.type === a.type && this.value === a.value; }; Attribute.prototype.uri = function uri() { return encodeURIComponent(this.value) + ':' + encodeURIComponent(this.type); }; /** * @param {integer} width width of the identicon * @returns {HTMLElement} img element containing the identicon */ Attribute.prototype.identicon = function identicon(width) { _util2.default.injectCss(); // some other way that is not called on each identicon generation? var div = document.createElement('div'); div.className = 'identifi-identicon'; div.style.width = width + 'px'; div.style.height = width + 'px'; var img = document.createElement('img'); img.alt = ''; img.width = width; img.height = width; var hash = _util2.default.getHash(encodeURIComponent(this.type) + ':' + encodeURIComponent(this.value), 'hex'); var identicon = new _identicon2.default(hash, { width: width, format: 'svg' }); img.src = 'data:image/svg+xml;base64,' + identicon.toString(); var name = document.createElement('span'); name.className = 'identifi-distance'; name.style.fontSize = width > 50 ? width / 4 + 'px' : '10px'; name.textContent = this.type.slice(0, 5); div.appendChild(name); div.appendChild(img); return div; }; return Attribute; }(); exports.default = Attribute; module.exports = exports['default'];