From 3aa135fdb4d2f36c2759005b003591f1a9478889 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Wed, 14 Nov 2018 13:09:33 +1100 Subject: [PATCH] Grabbing last hash from server response. only requesting messages after the lasthash. Keeping track of lasthash for array of nodes (currently hardcoded for one) --- js/modules/loki_message_api.js | 37 +++++++++++++++++++++++++--------- libloki/proof-of-work.js | 2 +- package.json | 1 + preload.js | 2 +- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 0caaf8130..af835e3cd 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -36,15 +36,20 @@ function getPoWNonce(timestamp, ttl, pubKey, data) { class LokiServer { - constructor({ url }) { - if (!is.string(url)) { - throw new Error('WebAPI.initialize: Invalid server url'); - } - this.url = url; + constructor({ urls }) { + this.nodes = []; + urls.forEach(url => { + if (!is.string(url)) { + throw new Error('WebAPI.initialize: Invalid server url'); + } + this.nodes.push({ url }); + }); } async sendMessage(pubKey, data, ttl) { const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64'); + // Hardcoded to use a single node/server for now + const currentNode = this.nodes[0]; const timestamp = Math.floor(Date.now() / 1000); // Nonce is returned as a base64 string to include in header @@ -58,7 +63,7 @@ class LokiServer { } const options = { - url: `${this.url}/store`, + url: `${currentNode.url}/store`, type: 'POST', responseType: undefined, timeout: undefined, @@ -107,8 +112,11 @@ class LokiServer { } async retrieveMessages(pubKey) { + // Hardcoded to use a single node/server for now + const currentNode = this.nodes[0]; + const options = { - url: `${this.url}/retrieve`, + url: `${currentNode.url}/retrieve`, type: 'GET', responseType: 'json', timeout: undefined, @@ -116,11 +124,17 @@ class LokiServer { log.info(options.type, options.url); + const headers = { + 'X-Loki-recipient': pubKey, + }; + + if (currentNode.lastHash) { + headers['X-Loki-last-hash'] = currentNode.lastHash; + } + const fetchOptions = { method: options.type, - headers: { - 'X-Loki-recipient': pubKey, - }, + headers, timeout: options.timeout, }; @@ -146,6 +160,9 @@ class LokiServer { if (response.status >= 0 && response.status < 400) { log.info(options.type, options.url, response.status, 'Success'); + if (result.lastHash) { + currentNode.lastHash = result.lastHash; + } return result; } log.error(options.type, options.url, response.status, 'Error'); diff --git a/libloki/proof-of-work.js b/libloki/proof-of-work.js index e9bdafb1f..3adab32b2 100644 --- a/libloki/proof-of-work.js +++ b/libloki/proof-of-work.js @@ -1,6 +1,6 @@ const hash = require('js-sha512'); const bb = require('bytebuffer'); -const BigInteger = require('jsbn').BigInteger; +const { BigInteger } = require('jsbn'); const NONCE_LEN = 8; // Modify this value for difficulty scaling diff --git a/package.json b/package.json index de82011e0..67c188093 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "intl-tel-input": "^12.1.15", "jquery": "^3.3.1", "js-sha512": "^0.8.0", + "jsbn": "^1.1.0", "linkify-it": "^2.0.3", "lodash": "^4.17.4", "mkdirp": "^0.5.1", diff --git a/preload.js b/preload.js index d2383891f..abf376e3e 100644 --- a/preload.js +++ b/preload.js @@ -204,7 +204,7 @@ window.WebAPI = initializeWebAPI({ const { LokiServer } = require('./js/modules/loki_message_api'); window.LokiAPI = new LokiServer({ - url: config.serverUrl, + urls: [config.serverUrl], }); window.mnemonic = require('./libloki/mnemonic');