|
|
|
@ -4,25 +4,6 @@ const fetch = require('node-fetch');
|
|
|
|
|
const is = require('@sindresorhus/is');
|
|
|
|
|
const { fork } = require('child_process');
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
initialize,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function initialize({ url }) {
|
|
|
|
|
if (!is.string(url)) {
|
|
|
|
|
throw new Error('WebAPI.initialize: Invalid server url');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
connect,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function connect() {
|
|
|
|
|
return {
|
|
|
|
|
sendMessage,
|
|
|
|
|
retrieveMessages,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getPoWNonce(timestamp, ttl, pubKey, data) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// Create forked node process to calculate PoW without blocking main process
|
|
|
|
@ -53,11 +34,33 @@ function initialize({ url }) {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function retrieveMessages(pubKey) {
|
|
|
|
|
class LokiServer {
|
|
|
|
|
|
|
|
|
|
constructor({ url }) {
|
|
|
|
|
if (!is.string(url)) {
|
|
|
|
|
throw new Error('WebAPI.initialize: Invalid server url');
|
|
|
|
|
}
|
|
|
|
|
this.url = url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendMessage(pubKey, data, ttl) {
|
|
|
|
|
const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64');
|
|
|
|
|
|
|
|
|
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
|
|
|
// Nonce is returned as a base64 string to include in header
|
|
|
|
|
let nonce;
|
|
|
|
|
try {
|
|
|
|
|
nonce = await getPoWNonce(timestamp, ttl, pubKey, data64);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Something went horribly wrong
|
|
|
|
|
// TODO: Handle gracefully
|
|
|
|
|
log.error('Error computing PoW');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
url: `${url}/retrieve`,
|
|
|
|
|
type: 'GET',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
url: `${this.url}/store`,
|
|
|
|
|
type: 'POST',
|
|
|
|
|
responseType: undefined,
|
|
|
|
|
timeout: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -65,7 +68,11 @@ function initialize({ url }) {
|
|
|
|
|
|
|
|
|
|
const fetchOptions = {
|
|
|
|
|
method: options.type,
|
|
|
|
|
body: data64,
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Loki-pow-nonce': nonce,
|
|
|
|
|
'X-Loki-timestamp': timestamp.toString(),
|
|
|
|
|
'X-Loki-ttl': ttl.toString(),
|
|
|
|
|
'X-Loki-recipient': pubKey,
|
|
|
|
|
},
|
|
|
|
|
timeout: options.timeout,
|
|
|
|
@ -96,27 +103,14 @@ function initialize({ url }) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
log.error(options.type, options.url, response.status, 'Error');
|
|
|
|
|
throw HTTPError('retrieveMessages: error response', response.status, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function sendMessage(pubKey, data, ttl) {
|
|
|
|
|
const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64');
|
|
|
|
|
|
|
|
|
|
const timestamp = Math.floor(Date.now() / 1000);
|
|
|
|
|
// Nonce is returned as a base64 string to include in header
|
|
|
|
|
let nonce;
|
|
|
|
|
try {
|
|
|
|
|
nonce = await getPoWNonce(timestamp, ttl, pubKey, data64);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Something went horribly wrong
|
|
|
|
|
// TODO: Handle gracefully
|
|
|
|
|
log.error('Error computing PoW');
|
|
|
|
|
throw HTTPError('sendMessage: error response', response.status, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async retrieveMessages(pubKey) {
|
|
|
|
|
const options = {
|
|
|
|
|
url: `${url}/store`,
|
|
|
|
|
type: 'POST',
|
|
|
|
|
responseType: undefined,
|
|
|
|
|
url: `${this.url}/retrieve`,
|
|
|
|
|
type: 'GET',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
timeout: undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -124,11 +118,7 @@ function initialize({ url }) {
|
|
|
|
|
|
|
|
|
|
const fetchOptions = {
|
|
|
|
|
method: options.type,
|
|
|
|
|
body: data64,
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Loki-pow-nonce': nonce,
|
|
|
|
|
'X-Loki-timestamp': timestamp.toString(),
|
|
|
|
|
'X-Loki-ttl': ttl.toString(),
|
|
|
|
|
'X-Loki-recipient': pubKey,
|
|
|
|
|
},
|
|
|
|
|
timeout: options.timeout,
|
|
|
|
@ -159,8 +149,7 @@ function initialize({ url }) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
log.error(options.type, options.url, response.status, 'Error');
|
|
|
|
|
throw HTTPError('sendMessage: error response', response.status, result);
|
|
|
|
|
}
|
|
|
|
|
throw HTTPError('retrieveMessages: error response', response.status, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -177,3 +166,7 @@ function HTTPError(message, providedCode, response, stack) {
|
|
|
|
|
}
|
|
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
LokiServer,
|
|
|
|
|
};
|
|
|
|
|