diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index a085a81c4..b68a65d97 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -5,6 +5,8 @@ const nodeFetch = require('node-fetch'); const _ = require('lodash'); +const endpointBase = '/v1/storage_rpc'; + class HTTPError extends Error { constructor(response) { super(response.statusText); @@ -67,13 +69,19 @@ class LokiMessageAPI { const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64'); const timestamp = Math.floor(Date.now() / 1000); const p2pDetails = lokiP2pAPI.getContactP2pDetails(pubKey); + const body = { + method: 'store', + params: { + data: data64, + }, + }; if (p2pDetails && (isPing || p2pDetails.isOnline)) { try { const port = p2pDetails.port ? `:${p2pDetails.port}` : ''; - const url = `${p2pDetails.address}${port}/store`; + const url = `${p2pDetails.address}${port}${endpointBase}`; const fetchOptions = { method: 'POST', - body: data64, + body: JSON.stringify(body), }; await fetch(url, fetchOptions); @@ -109,6 +117,17 @@ class LokiMessageAPI { // Something went horribly wrong throw err; } + const storageParams = { + pubKey, + ttl: ttl.toString(), + nonce, + timestamp: timestamp.toString(), + }; + body.params = { + ...body.params, + ...storageParams, + }; + const completedNodes = []; const failedNodes = []; let successfulRequests = 0; @@ -122,17 +141,7 @@ class LokiMessageAPI { }; const doRequest = async nodeUrl => { - const url = `${nodeUrl}${this.messageServerPort}/v1/storage_rpc`; - const body = { - method: 'store', - params: { - pubKey, - ttl: ttl.toString(), - nonce, - timestamp: timestamp.toString(), - data: data64, - }, - }; + const url = `${nodeUrl}${this.messageServerPort}${endpointBase}`; const fetchOptions = { method: 'POST', body: JSON.stringify(body), @@ -224,7 +233,7 @@ class LokiMessageAPI { }; const doRequest = async (nodeUrl, nodeData) => { - const url = `${nodeUrl}${this.messageServerPort}/v1/storage_rpc`; + const url = `${nodeUrl}${this.messageServerPort}${endpointBase}`; const body = { method: 'retrieve', params: { diff --git a/libloki/local_loki_server.js b/libloki/local_loki_server.js index 262b532ba..951db7dab 100644 --- a/libloki/local_loki_server.js +++ b/libloki/local_loki_server.js @@ -32,9 +32,14 @@ class LocalLokiServer extends EventEmitter { } // Check endpoints here - if (req.url === '/store') { - // body is a base64 encoded string - this.emit('message', body); + if (req.url === '/v1/storage_rpc') { + const bodyObject = JSON.parse(body); + if (bodyObject.method !== 'store') { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Invalid endpoint!'); + return; + } + this.emit('message', bodyObject.params.data); res.statusCode = 200; res.end(); } else { diff --git a/libloki/test/node/local_loki_server_test.js b/libloki/test/node/local_loki_server_test.js index be0ea2930..11e3b6130 100644 --- a/libloki/test/node/local_loki_server_test.js +++ b/libloki/test/node/local_loki_server_test.js @@ -56,6 +56,12 @@ describe('LocalLokiServer', () => { it('should pass the POSTed data to the callback', async () => { const server = new LocalLokiServer(); await server.start(8001); + const messageData = { + method: 'store', + params: { + data: 'This is data', + }, + }; const promise = new Promise(res => { server.on('message', message => { @@ -66,7 +72,7 @@ describe('LocalLokiServer', () => { }); try { - await axios.post('http://localhost:8001/store', 'This is data'); + await axios.post('http://localhost:8001/v1/storage_rpc', messageData); } catch (error) { assert.isNotOk(error, 'Error occured'); }