From 492bb3723afe93e3f666eb576c31d3844bdaea86 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Mon, 5 Aug 2019 17:05:49 +1000 Subject: [PATCH] Move pubkey truncation to loki_rpc and add window function --- js/modules/loki_message_api.js | 26 +++----------------------- js/modules/loki_rpc.js | 10 +++++++++- js/modules/loki_snode_api.js | 5 +---- preload.js | 3 +++ 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index a5b3c4102..3ea0ed6b7 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -96,9 +96,7 @@ class LokiMessageAPI { const timestamp = Date.now(); const nonce = await calcNonce( messageEventData, - window.getEnvironment() === 'production' - ? pubKey - : pubKey.substring(0, pubKey.length - 2), + window.getStoragePubKey(pubKey), data64, timestamp, ttl @@ -216,22 +214,8 @@ class LokiMessageAPI { let successiveFailures = 0; while (successiveFailures < MAX_ACCEPTABLE_FAILURES) { await sleepFor(successiveFailures * 500); - let result; try { - if (window.getEnvironment() === 'production') { - result = await rpc(`https://${address}`, port, 'store', params); - } else { - const testnetParams = { - ...params, - pubKey: params.pubKey.substring(0, params.pubKey.length - 2), - }; - result = await rpc( - `https://${address}`, - port, - 'store', - testnetParams - ); - } + const result = await rpc(`https://${address}`, port, 'store', params); // Make sure we aren't doing too much PoW const currentDifficulty = window.storage.get('PoWDifficulty', null); @@ -339,12 +323,8 @@ class LokiMessageAPI { } async retrieveNextMessages(nodeUrl, nodeData) { - let { ourKey } = this; - if (window.getEnvironment() !== 'production') { - ourKey = ourKey.substring(0, ourKey.length - 2); - } const params = { - pubKey: ourKey, + pubKey: this.ourKey, lastHash: nodeData.lastHash || '', }; const options = { diff --git a/js/modules/loki_rpc.js b/js/modules/loki_rpc.js index 43c9eb060..74acbc4f3 100644 --- a/js/modules/loki_rpc.js +++ b/js/modules/loki_rpc.js @@ -1,4 +1,4 @@ -/* global log, libloki, textsecure */ +/* global log, libloki, textsecure, getStoragePubKey */ const nodeFetch = require('node-fetch'); const { parse } = require('url'); @@ -113,6 +113,14 @@ const rpc = ( const portString = port ? `:${port}` : ''; const url = `${address}${portString}${endpoint}`; // TODO: The jsonrpc and body field will be ignored on storage server + if (params.pubKey) { + // Ensure we always take a copy + // eslint-disable-next-line no-param-reassign + params = { + ...params, + pubKey: getStoragePubKey(params.pubKey), + }; + } const body = { jsonrpc: '2.0', id: '0', diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 08cf90df7..49e00c773 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -190,10 +190,7 @@ class LokiSnodeAPI { const { ip, port } = await this.getRandomSnodeAddress(); try { const result = await rpc(`https://${ip}`, port, 'get_snodes_for_pubkey', { - pubKey: - window.getEnvironment() === 'production' - ? pubKey - : pubKey.substring(0, pubKey.length - 2), + pubKey, }); const snodes = result.snodes.filter(snode => snode.ip !== '0.0.0.0'); return snodes; diff --git a/preload.js b/preload.js index 57a25ee21..75ae8edcd 100644 --- a/preload.js +++ b/preload.js @@ -26,6 +26,7 @@ window.platform = process.platform; window.getDefaultPoWDifficulty = () => config.defaultPoWDifficulty; window.getTitle = () => title; window.getEnvironment = () => config.environment; +window.isDev = () => config.environment === 'development'; window.getAppInstance = () => config.appInstance; window.getVersion = () => config.version; window.isImportMode = () => config.importMode; @@ -36,6 +37,8 @@ window.getHostName = () => config.hostname; window.getServerTrustRoot = () => config.serverTrustRoot; window.isBehindProxy = () => Boolean(config.proxyUrl); window.JobQueue = JobQueue; +window.getStoragePubKey = key => + window.isDev() ? key.substring(0, key.length - 2) : key; window.isBeforeVersion = (toCheck, baseVersion) => { try {