From 43c2e9c953ce0e6f8075f75330d61f6c7bc946b2 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Fri, 2 Aug 2019 16:11:26 +1000 Subject: [PATCH 1/4] Use different seed nodes for development vs production and strip 2 characters from pubkey on testnet --- config/default.json | 10 +--------- config/production.json | 17 ++++++++++++++++- js/modules/loki_message_api.js | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/config/default.json b/config/default.json index e3d3701b8..c6ead5686 100644 --- a/config/default.json +++ b/config/default.json @@ -7,15 +7,7 @@ "defaultPoWDifficulty": "100", "seedNodeList": [ { - "ip": "3.104.19.14", - "port": "22023" - }, - { - "ip": "13.238.53.205", - "port": "38157" - }, - { - "ip": "imaginary.stream", + "ip": "storage.testnetseed1.loki.network", "port": "38157" } ], diff --git a/config/production.json b/config/production.json index 0967ef424..7114a9bba 100644 --- a/config/production.json +++ b/config/production.json @@ -1 +1,16 @@ -{} +{ + "seedNodeList": [ + { + "ip": "storage.seed1.loki.network", + "port": "22023" + }, + { + "ip": "storage.seed2.loki.network", + "port": "38157" + }, + { + "ip": "imaginary.stream", + "port": "38157" + } + ] +} diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 5c98cc00e..c2f83fe3a 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -112,7 +112,7 @@ class LokiMessageAPI { } const params = { - pubKey, + pubKey: window.getEnvironment() === 'production' ? pubKey : pubKey.substring(0, pubKey.length - 2), ttl: ttl.toString(), nonce, timestamp: timestamp.toString(), From 6140fef1f7ebe52b4cf7cee14b3a0ad816821163 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Mon, 5 Aug 2019 14:03:12 +1000 Subject: [PATCH 2/4] Fix some bugs --- js/modules/loki_message_api.js | 28 ++++++++++++++++++++++++---- js/modules/loki_snode_api.js | 5 ++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index c2f83fe3a..a5b3c4102 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -96,7 +96,9 @@ class LokiMessageAPI { const timestamp = Date.now(); const nonce = await calcNonce( messageEventData, - pubKey, + window.getEnvironment() === 'production' + ? pubKey + : pubKey.substring(0, pubKey.length - 2), data64, timestamp, ttl @@ -112,7 +114,7 @@ class LokiMessageAPI { } const params = { - pubKey: window.getEnvironment() === 'production' ? pubKey : pubKey.substring(0, pubKey.length - 2), + pubKey, ttl: ttl.toString(), nonce, timestamp: timestamp.toString(), @@ -214,8 +216,22 @@ class LokiMessageAPI { let successiveFailures = 0; while (successiveFailures < MAX_ACCEPTABLE_FAILURES) { await sleepFor(successiveFailures * 500); + let result; try { - const result = await rpc(`https://${address}`, port, 'store', params); + 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 + ); + } // Make sure we aren't doing too much PoW const currentDifficulty = window.storage.get('PoWDifficulty', null); @@ -323,8 +339,12 @@ class LokiMessageAPI { } async retrieveNextMessages(nodeUrl, nodeData) { + let { ourKey } = this; + if (window.getEnvironment() !== 'production') { + ourKey = ourKey.substring(0, ourKey.length - 2); + } const params = { - pubKey: this.ourKey, + pubKey: ourKey, lastHash: nodeData.lastHash || '', }; const options = { diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 49e00c773..08cf90df7 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -190,7 +190,10 @@ class LokiSnodeAPI { const { ip, port } = await this.getRandomSnodeAddress(); try { const result = await rpc(`https://${ip}`, port, 'get_snodes_for_pubkey', { - pubKey, + pubKey: + window.getEnvironment() === 'production' + ? pubKey + : pubKey.substring(0, pubKey.length - 2), }); const snodes = result.snodes.filter(snode => snode.ip !== '0.0.0.0'); return snodes; From 0a231ea2ae92e5adbb7781624a4239d06ee445ab Mon Sep 17 00:00:00 2001 From: Beaudan Date: Mon, 5 Aug 2019 14:34:16 +1000 Subject: [PATCH 3/4] Allow start-prod and start-prod-multi so launch in production (mainnet) mode --- app/config.js | 4 +++- package.json | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/config.js b/app/config.js index 49423916e..3a865109d 100644 --- a/app/config.js +++ b/app/config.js @@ -20,10 +20,12 @@ if (environment === 'production') { process.env.NODE_CONFIG = ''; process.env.NODE_CONFIG_STRICT_MODE = true; process.env.HOSTNAME = ''; - process.env.NODE_APP_INSTANCE = ''; process.env.ALLOW_CONFIG_MUTATIONS = ''; process.env.SUPPRESS_NO_CONFIG_WARNING = ''; process.env.NODE_TLS_REJECT_UNAUTHORIZED = ''; + if (!process.env.LOKI_DEV) { + process.env.NODE_APP_INSTANCE = ''; + } } // We load config after we've made our modifications to NODE_ENV diff --git a/package.json b/package.json index ef7069683..332d053fe 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,8 @@ "postinstall": "electron-builder install-app-deps && rimraf node_modules/dtrace-provider", "start": "electron .", "start-multi": "NODE_APP_INSTANCE=1 electron .", + "start-prod": "LOKI_DEV=1 electron .", + "start-prod-multi": "LOKI_DEV=1 NODE_APP_INSTANCE=1 electron .", "grunt": "grunt", "icon-gen": "electron-icon-maker --input=images/icon_1024.png --output=./build", "generate": "yarn icon-gen && yarn grunt", From 492bb3723afe93e3f666eb576c31d3844bdaea86 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Mon, 5 Aug 2019 17:05:49 +1000 Subject: [PATCH 4/4] 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 {