|
|
|
@ -760,7 +760,7 @@ 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)
|
|
|
|
|
|
|
|
|
@ -782,11 +782,17 @@ 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?
|
|
|
|
@ -798,31 +804,25 @@ class LokiSnodeAPI {
|
|
|
|
|
const confirmedNodes = [];
|
|
|
|
|
|
|
|
|
|
let cipherResolve;
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
|
const cipherPromise = () => new Promise((resolve, _reject) => {
|
|
|
|
|
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
|
|
|
|
@ -843,9 +843,7 @@ 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) {
|
|
|
|
@ -856,7 +854,7 @@ class LokiSnodeAPI {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const nodes = lnsNodes.splice(0, numRequests);
|
|
|
|
|
|
|
|
|
@ -865,16 +863,16 @@ class LokiSnodeAPI {
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
pubkey = ciphertextHex === null ? null : await decryptHex(ciphertextHex);
|
|
|
|
|
|
|
|
|
|
return { pubkey, error };
|
|
|
|
|
}
|
|
|
|
|