Constants

pull/1091/head
Vincent 5 years ago
parent 8992234dd7
commit d78a49d689

@ -760,13 +760,13 @@ class LokiSnodeAPI {
// Returns { pubkey, error } // Returns { pubkey, error }
// pubkey is: // pubkey is:
// null when there is confirmed to be no LNS mapping // null when there is confirmed to be no LNS mapping
// undefined when unconfirmee // undefined when unconfirmed
// string when found // string when found
// timeout parameter optional (ms) // timeout parameter optional (ms)
// How many nodes to fetch data from? // How many nodes to fetch data from?
const numRequests = 5; const numRequests = 5;
// How many nodes must have the same response value? // How many nodes must have the same response value?
const numRequiredConfirms = 3; const numRequiredConfirms = 3;
@ -782,47 +782,47 @@ class LokiSnodeAPI {
// Return value of null represents a timeout // Return value of null represents a timeout
const timeoutResponse = { timedOut: true }; const timeoutResponse = { timedOut: true };
const timeoutPromise = (cb, interval) => () => new Promise(resolve => setTimeout(() => cb(resolve), interval)); const timeoutPromise = (cb, interval) => () =>
const onTimeout = timeoutPromise(resolve => resolve(timeoutResponse), timeout || Number.MAX_SAFE_INTEGER); new Promise(resolve => setTimeout(() => cb(resolve), interval));
const onTimeout = timeoutPromise(
resolve => resolve(timeoutResponse),
timeout || Number.MAX_SAFE_INTEGER
);
// Get nodes capable of doing LNS // 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); lnsNodes = _.shuffle(lnsNodes);
// Enough nodes? // Enough nodes?
if (lnsNodes.length < numRequiredConfirms) { if (lnsNodes.length < numRequiredConfirms) {
error = window.i18n('lnsTooFewNodes'); error = window.i18n('lnsTooFewNodes');
return {pubkey, error}; return { pubkey, error };
} }
const confirmedNodes = []; const confirmedNodes = [];
let cipherResolve; let cipherResolve;
// eslint-disable-next-line no-unused-vars const cipherPromise = () =>
const cipherPromise = () => new Promise((resolve, _reject) => { new Promise(resolve => {
cipherResolve = resolve; cipherResolve = resolve;
}); });
const decryptHex = async cipherHex => { const decryptHex = async cipherHex => {
const ciphertext = new Uint8Array( const ciphertext = new Uint8Array(StringView.hexToArrayBuffer(cipherHex));
StringView.hexToArrayBuffer(cipherHex)
);
const res = await window.decryptLnsEntry(lnsName, ciphertext); const res = await window.decryptLnsEntry(lnsName, ciphertext);
const pubicKey = StringView.arrayBufferToHex(res); const pubicKey = StringView.arrayBufferToHex(res);
return pubicKey; return pubicKey;
} };
const fetchFromNode = async node => { const fetchFromNode = async node => {
const res = await this._requestLnsMapping(node, nameHash); const res = await this._requestLnsMapping(node, nameHash);
// Do validation // Do validation
if ( if (res && res.result && res.result.status === 'OK') {
res &&
res.result &&
res.result.status === 'OK'
) {
const hasMapping = res.result.entries && res.result.entries.length > 0; const hasMapping = res.result.entries && res.result.entries.length > 0;
const resValue = hasMapping const resValue = hasMapping
@ -832,7 +832,7 @@ class LokiSnodeAPI {
confirmedNodes.push(resValue); confirmedNodes.push(resValue);
if (confirmedNodes.length >= numRequiredConfirms) { if (confirmedNodes.length >= numRequiredConfirms) {
if (ciphertextHex){ if (ciphertextHex) {
// result already found, dont worry // result already found, dont worry
return; return;
} }
@ -843,40 +843,38 @@ class LokiSnodeAPI {
); );
if (count >= numRequiredConfirms) { if (count >= numRequiredConfirms) {
ciphertextHex = winner === String(null) ciphertextHex = winner === String(null) ? null : winner;
? null
: winner;
// null represents no LNS mapping // null represents no LNS mapping
if (ciphertextHex === null){ if (ciphertextHex === null) {
error = window.i18n('lnsMappingNotFound'); error = window.i18n('lnsMappingNotFound');
} }
cipherResolve({ciphertextHex}); cipherResolve({ ciphertextHex });
} }
} }
} }
} };
const nodes = lnsNodes.splice(0, numRequests); const nodes = lnsNodes.splice(0, numRequests);
// Start fetching from nodes // Start fetching from nodes
Promise.resolve(nodes.map(async node => fetchFromNode(node))); Promise.resolve(nodes.map(async node => fetchFromNode(node)));
// Timeouts (optional parameter) // Timeouts (optional parameter)
// Wait for cipher to be found; race against timeout // 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) { if (timedOut) {
error = window.i18n('lnsLookupTimeout'); error = window.i18n('lnsLookupTimeout');
return { pubkey, error }; return { pubkey, error };
} }
pubkey = ciphertextHex === null pubkey = ciphertextHex === null ? null : await decryptHex(ciphertextHex);
? null
: await decryptHex(ciphertextHex); return { pubkey, error };
return {pubkey, error};
} }
// get snodes for pubkey from random snode // get snodes for pubkey from random snode

@ -70,17 +70,29 @@ window.isBeforeVersion = (toCheck, baseVersion) => {
} }
}; };
window.CONSTANTS = { // eslint-disable-next-line func-names
MAX_LOGIN_TRIES: 3, window.CONSTANTS = new function() {
MAX_PASSWORD_LENGTH: 64, this.MAX_LOGIN_TRIES = 3;
MAX_USERNAME_LENGTH: 20, this.MAX_PASSWORD_LENGTH = 64;
MAX_GROUP_NAME_LENGTH: 64, this.MAX_USERNAME_LENGTH = 20;
DEFAULT_PUBLIC_CHAT_URL: appConfig.get('defaultPublicChatServer'), this.MAX_GROUP_NAME_LENGTH = 64;
MAX_CONNECTION_DURATION: 5000, this.DEFAULT_PUBLIC_CHAT_URL = appConfig.get('defaultPublicChatServer');
MAX_MESSAGE_BODY_LENGTH: 64 * 1024, this.MAX_CONNECTION_DURATION = 5000;
this.MAX_MESSAGE_BODY_LENGTH = 64 * 1024;
// Limited due to the proof-of-work requirement // Limited due to the proof-of-work requirement
SMALL_GROUP_SIZE_LIMIT: 10, this.SMALL_GROUP_SIZE_LIMIT = 10;
NOTIFICATION_ENABLE_TIMEOUT_SECONDS: 10, // number of seconds to turn on notifications after reconnect/start of app // 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 = { window.versionInfo = {

Loading…
Cancel
Save