Merge pull request #39 from BeaudanBrown/last-hash

Last hash
pull/40/head
sachaaaaa 7 years ago committed by GitHub
commit 5bcf63e592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -36,15 +36,20 @@ function getPoWNonce(timestamp, ttl, pubKey, data) {
class LokiServer { class LokiServer {
constructor({ url }) { constructor({ urls }) {
this.nodes = [];
urls.forEach(url => {
if (!is.string(url)) { if (!is.string(url)) {
throw new Error('WebAPI.initialize: Invalid server url'); throw new Error('WebAPI.initialize: Invalid server url');
} }
this.url = url; this.nodes.push({ url });
});
} }
async sendMessage(pubKey, data, ttl) { async sendMessage(pubKey, data, ttl) {
const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64'); 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); const timestamp = Math.floor(Date.now() / 1000);
// Nonce is returned as a base64 string to include in header // Nonce is returned as a base64 string to include in header
@ -58,7 +63,7 @@ class LokiServer {
} }
const options = { const options = {
url: `${this.url}/store`, url: `${currentNode.url}/store`,
type: 'POST', type: 'POST',
responseType: undefined, responseType: undefined,
timeout: undefined, timeout: undefined,
@ -107,8 +112,11 @@ class LokiServer {
} }
async retrieveMessages(pubKey) { async retrieveMessages(pubKey) {
// Hardcoded to use a single node/server for now
const currentNode = this.nodes[0];
const options = { const options = {
url: `${this.url}/retrieve`, url: `${currentNode.url}/retrieve`,
type: 'GET', type: 'GET',
responseType: 'json', responseType: 'json',
timeout: undefined, timeout: undefined,
@ -116,11 +124,17 @@ class LokiServer {
log.info(options.type, options.url); log.info(options.type, options.url);
const headers = {
'X-Loki-recipient': pubKey,
};
if (currentNode.lastHash) {
headers['X-Loki-last-hash'] = currentNode.lastHash;
}
const fetchOptions = { const fetchOptions = {
method: options.type, method: options.type,
headers: { headers,
'X-Loki-recipient': pubKey,
},
timeout: options.timeout, timeout: options.timeout,
}; };
@ -146,6 +160,9 @@ class LokiServer {
if (response.status >= 0 && response.status < 400) { if (response.status >= 0 && response.status < 400) {
log.info(options.type, options.url, response.status, 'Success'); log.info(options.type, options.url, response.status, 'Success');
if (result.lastHash) {
currentNode.lastHash = result.lastHash;
}
return result; return result;
} }
log.error(options.type, options.url, response.status, 'Error'); log.error(options.type, options.url, response.status, 'Error');

@ -1,6 +1,6 @@
const hash = require('js-sha512'); const hash = require('js-sha512');
const bb = require('bytebuffer'); const bb = require('bytebuffer');
const BigInteger = require('jsbn').BigInteger; const { BigInteger } = require('jsbn');
const NONCE_LEN = 8; const NONCE_LEN = 8;
// Modify this value for difficulty scaling // Modify this value for difficulty scaling

@ -23,7 +23,7 @@
const ARCHIVE_AGE = 7 * 24 * 60 * 60 * 1000; const ARCHIVE_AGE = 7 * 24 * 60 * 60 * 1000;
function AccountManager(username, password) { function AccountManager(username, password) {
this.server = window.WebAPI.connect({ username, password }); // this.server = window.WebAPI.connect({ username, password });
this.pending = Promise.resolve(); this.pending = Promise.resolve();
} }
@ -44,10 +44,10 @@
AccountManager.prototype.extend({ AccountManager.prototype.extend({
constructor: AccountManager, constructor: AccountManager,
requestVoiceVerification(number) { requestVoiceVerification(number) {
return this.server.requestVerificationVoice(number); // return this.server.requestVerificationVoice(number);
}, },
requestSMSVerification(number) { requestSMSVerification(number) {
return this.server.requestVerificationSMS(number); // return this.server.requestVerificationSMS(number);
}, },
registerSingleDevice(mnemonic, mnemonicLanguage) { registerSingleDevice(mnemonic, mnemonicLanguage) {
const createAccount = this.createAccount.bind(this); const createAccount = this.createAccount.bind(this);
@ -140,18 +140,18 @@
throw new Error('account_manager: registerSecondDevice has not been implemented!'); throw new Error('account_manager: registerSecondDevice has not been implemented!');
}, },
refreshPreKeys() { refreshPreKeys() {
const generateKeys = this.generateKeys.bind(this, 0); // const generateKeys = this.generateKeys.bind(this, 0);
const registerKeys = this.server.registerKeys.bind(this.server); // const registerKeys = this.server.registerKeys.bind(this.server);
return this.queueTask(() => // return this.queueTask(() =>
this.server.getMyKeys().then(preKeyCount => { // this.server.getMyKeys().then(preKeyCount => {
window.log.info(`prekey count ${preKeyCount}`); // window.log.info(`prekey count ${preKeyCount}`);
if (preKeyCount < 10) { // if (preKeyCount < 10) {
return generateKeys().then(registerKeys); // return generateKeys().then(registerKeys);
} // }
return null; // return null;
}) // })
); // );
}, },
rotateSignedPreKey() { rotateSignedPreKey() {
return this.queueTask(() => { return this.queueTask(() => {

@ -807,6 +807,7 @@ MessageReceiver.prototype.extend({
}, },
async decryptPreKeyWhisperMessage(ciphertext, sessionCipher, address) { async decryptPreKeyWhisperMessage(ciphertext, sessionCipher, address) {
const padded = await sessionCipher.decryptPreKeyWhisperMessage(ciphertext); const padded = await sessionCipher.decryptPreKeyWhisperMessage(ciphertext);
return padded;
try { try {
return this.unpad(padded); return this.unpad(padded);
@ -1035,8 +1036,9 @@ MessageReceiver.prototype.extend({
} else if (content.receiptMessage) { } else if (content.receiptMessage) {
return this.handleReceiptMessage(envelope, content.receiptMessage); return this.handleReceiptMessage(envelope, content.receiptMessage);
} }
this.removeFromCache(envelope); if (!content.preKeyBundleMessage) {
throw new Error('Unsupported content message'); throw new Error('Unsupported content message');
}
}, },
handleCallMessage(envelope) { handleCallMessage(envelope) {
window.log.info('call message from', this.getEnvelopeId(envelope)); window.log.info('call message from', this.getEnvelopeId(envelope));

@ -134,9 +134,9 @@ OutgoingMessage.prototype = {
}) })
); );
// TODO: check if still applicable // TODO: check if still applicable
if (updateDevices === undefined) { // if (updateDevices === undefined) {
return this.server.getKeysForNumber(number, '*').then(handleResult); // return this.server.getKeysForNumber(number, '*').then(handleResult);
} // }
let promise = Promise.resolve(true); let promise = Promise.resolve(true);
updateDevices.forEach(device => { updateDevices.forEach(device => {
promise = promise.then(() => promise = promise.then(() =>

@ -72,6 +72,7 @@
"intl-tel-input": "^12.1.15", "intl-tel-input": "^12.1.15",
"jquery": "^3.3.1", "jquery": "^3.3.1",
"js-sha512": "^0.8.0", "js-sha512": "^0.8.0",
"jsbn": "^1.1.0",
"linkify-it": "^2.0.3", "linkify-it": "^2.0.3",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",

@ -204,7 +204,7 @@ window.WebAPI = initializeWebAPI({
const { LokiServer } = require('./js/modules/loki_message_api'); const { LokiServer } = require('./js/modules/loki_message_api');
window.LokiAPI = new LokiServer({ window.LokiAPI = new LokiServer({
url: config.serverUrl, urls: [config.serverUrl],
}); });
window.mnemonic = require('./libloki/mnemonic'); window.mnemonic = require('./libloki/mnemonic');

Loading…
Cancel
Save