From d78a49d6896eec33ac58bf750f9ddc6f7eb2e677 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 22 Apr 2020 17:10:41 +1000 Subject: [PATCH] Constants --- js/modules/loki_snode_api.js | 66 +++++++++++++++++------------------- preload.js | 32 +++++++++++------ 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 98f078473..e4b739854 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -760,13 +760,13 @@ class LokiSnodeAPI { // Returns { pubkey, error } // pubkey is: // null when there is confirmed to be no LNS mapping - // undefined when unconfirmee + // undefined when unconfirmed // string when found // timeout parameter optional (ms) // How many nodes to fetch data from? const numRequests = 5; - + // How many nodes must have the same response value? const numRequiredConfirms = 3; @@ -782,47 +782,47 @@ class LokiSnodeAPI { // Return value of null represents a timeout const timeoutResponse = { timedOut: true }; - const timeoutPromise = (cb, interval) => () => new Promise(resolve => setTimeout(() => cb(resolve), interval)); - const onTimeout = timeoutPromise(resolve => resolve(timeoutResponse), timeout || Number.MAX_SAFE_INTEGER); + const timeoutPromise = (cb, interval) => () => + new Promise(resolve => setTimeout(() => cb(resolve), interval)); + const onTimeout = timeoutPromise( + resolve => resolve(timeoutResponse), + timeout || Number.MAX_SAFE_INTEGER + ); // Get nodes capable of doing LNS - let lnsNodes = await this.getNodesMinVersion(window.CONSTANTS.LNS_CAPABLE_NODES_VERSION); + let lnsNodes = await this.getNodesMinVersion( + window.CONSTANTS.LNS_CAPABLE_NODES_VERSION + ); lnsNodes = _.shuffle(lnsNodes); // Enough nodes? if (lnsNodes.length < numRequiredConfirms) { error = window.i18n('lnsTooFewNodes'); - return {pubkey, error}; + return { pubkey, error }; } const confirmedNodes = []; let cipherResolve; - // eslint-disable-next-line no-unused-vars - const cipherPromise = () => new Promise((resolve, _reject) => { - cipherResolve = resolve; - }); + const cipherPromise = () => + new Promise(resolve => { + cipherResolve = resolve; + }); const decryptHex = async cipherHex => { - const ciphertext = new Uint8Array( - StringView.hexToArrayBuffer(cipherHex) - ); + const ciphertext = new Uint8Array(StringView.hexToArrayBuffer(cipherHex)); const res = await window.decryptLnsEntry(lnsName, ciphertext); const pubicKey = StringView.arrayBufferToHex(res); return pubicKey; - } - + }; + const fetchFromNode = async node => { const res = await this._requestLnsMapping(node, nameHash); // Do validation - if ( - res && - res.result && - res.result.status === 'OK' - ) { + if (res && res.result && res.result.status === 'OK') { const hasMapping = res.result.entries && res.result.entries.length > 0; const resValue = hasMapping @@ -832,7 +832,7 @@ class LokiSnodeAPI { confirmedNodes.push(resValue); if (confirmedNodes.length >= numRequiredConfirms) { - if (ciphertextHex){ + if (ciphertextHex) { // result already found, dont worry return; } @@ -843,40 +843,38 @@ class LokiSnodeAPI { ); if (count >= numRequiredConfirms) { - ciphertextHex = winner === String(null) - ? null - : winner; + ciphertextHex = winner === String(null) ? null : winner; // null represents no LNS mapping - if (ciphertextHex === null){ + if (ciphertextHex === null) { error = window.i18n('lnsMappingNotFound'); } - cipherResolve({ciphertextHex}); + cipherResolve({ ciphertextHex }); } } } - } + }; const nodes = lnsNodes.splice(0, numRequests); - + // Start fetching from nodes Promise.resolve(nodes.map(async node => fetchFromNode(node))); // Timeouts (optional parameter) // Wait for cipher to be found; race against timeout - const { timedOut } = await Promise.race([cipherPromise, onTimeout].map(f => f())); + const { timedOut } = await Promise.race( + [cipherPromise, onTimeout].map(f => f()) + ); if (timedOut) { error = window.i18n('lnsLookupTimeout'); return { pubkey, error }; } - pubkey = ciphertextHex === null - ? null - : await decryptHex(ciphertextHex); - - return {pubkey, error}; + pubkey = ciphertextHex === null ? null : await decryptHex(ciphertextHex); + + return { pubkey, error }; } // get snodes for pubkey from random snode diff --git a/preload.js b/preload.js index 7178e1f69..8a84536ef 100644 --- a/preload.js +++ b/preload.js @@ -70,17 +70,29 @@ window.isBeforeVersion = (toCheck, baseVersion) => { } }; -window.CONSTANTS = { - MAX_LOGIN_TRIES: 3, - MAX_PASSWORD_LENGTH: 64, - MAX_USERNAME_LENGTH: 20, - MAX_GROUP_NAME_LENGTH: 64, - DEFAULT_PUBLIC_CHAT_URL: appConfig.get('defaultPublicChatServer'), - MAX_CONNECTION_DURATION: 5000, - MAX_MESSAGE_BODY_LENGTH: 64 * 1024, +// eslint-disable-next-line func-names +window.CONSTANTS = new function() { + this.MAX_LOGIN_TRIES = 3; + this.MAX_PASSWORD_LENGTH = 64; + this.MAX_USERNAME_LENGTH = 20; + this.MAX_GROUP_NAME_LENGTH = 64; + this.DEFAULT_PUBLIC_CHAT_URL = appConfig.get('defaultPublicChatServer'); + this.MAX_CONNECTION_DURATION = 5000; + this.MAX_MESSAGE_BODY_LENGTH = 64 * 1024; // Limited due to the proof-of-work requirement - SMALL_GROUP_SIZE_LIMIT: 10, - NOTIFICATION_ENABLE_TIMEOUT_SECONDS: 10, // number of seconds to turn on notifications after reconnect/start of app + this.SMALL_GROUP_SIZE_LIMIT = 10; + // Number of seconds to turn on notifications after reconnect/start of app + this.NOTIFICATION_ENABLE_TIMEOUT_SECONDS = 10; + this.SESSION_ID_LENGTH = 66; + + // Loki Name System (LNS) + this.LNS_DEFAULT_LOOKUP_TIMEOUT = 6000; + // Minimum nodes version for LNS lookup + this.LNS_CAPABLE_NODES_VERSION = '2.0.3'; + this.LNS_MAX_LENGTH = 64; + // Conforms to naming rules here + // https://loki.network/2020/03/25/loki-name-system-the-facts/ + this.LNS_REGEX = `^[a-zA-Z0-9_]([a-zA-Z0-9_-]{0,${this.LNS_MAX_LENGTH - 2}}[a-zA-Z0-9_]){0,1}$`; }; window.versionInfo = {