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 {
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');

@ -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

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

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

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

@ -72,6 +72,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",

@ -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');

Loading…
Cancel
Save