Further improve imports

pull/1204/head
Maxim Shishmarev 5 years ago
parent b31b6bb912
commit 7dd9469074

@ -1,6 +1,6 @@
import { EnvelopePlus } from './types'; import { EnvelopePlus } from './types';
import { StringUtils } from '../session/utils'; import { StringUtils } from '../session/utils';
import { toNumber } from 'lodash'; import _ from 'lodash';
export async function removeFromCache(envelope: EnvelopePlus) { export async function removeFromCache(envelope: EnvelopePlus) {
const { id } = envelope; const { id } = envelope;
@ -33,7 +33,7 @@ export async function addToCache(
export async function getAllFromCache() { export async function getAllFromCache() {
window.log.info('getAllFromCache'); window.log.info('getAllFromCache');
const { textsecure, Lodash: _ } = window; const { textsecure } = window;
const count = await textsecure.storage.unprocessed.getCount(); const count = await textsecure.storage.unprocessed.getCount();
@ -50,7 +50,7 @@ export async function getAllFromCache() {
return Promise.all( return Promise.all(
_.map(items, async (item: any) => { _.map(items, async (item: any) => {
const attempts = toNumber(item.attempts || 0) + 1; const attempts = _.toNumber(item.attempts || 0) + 1;
try { try {
if (attempts >= 3) { if (attempts >= 3) {

@ -17,6 +17,7 @@ import { PubKey } from '../session/types';
import { handleSyncMessage } from './syncMessages'; import { handleSyncMessage } from './syncMessages';
import { onError } from './errors'; import { onError } from './errors';
import ByteBuffer from 'bytebuffer';
export async function handleContentMessage(envelope: EnvelopePlus) { export async function handleContentMessage(envelope: EnvelopePlus) {
const plaintext = await decrypt(envelope, envelope.content); const plaintext = await decrypt(envelope, envelope.content);
@ -113,7 +114,7 @@ async function decryptPreKeyWhisperMessage(
if (e.message === 'Unknown identity key') { if (e.message === 'Unknown identity key') {
// create an error that the UI will pick up and ask the // create an error that the UI will pick up and ask the
// user if they want to re-negotiate // user if they want to re-negotiate
const buffer = window.dcodeIO.ByteBuffer.wrap(ciphertext); const buffer = ByteBuffer.wrap(ciphertext);
throw new window.textsecure.IncomingIdentityKeyError( throw new window.textsecure.IncomingIdentityKeyError(
address.toString(), address.toString(),
buffer.toArrayBuffer(), buffer.toArrayBuffer(),
@ -312,7 +313,7 @@ async function decrypt(
if (error && error.message === 'Unknown identity key') { if (error && error.message === 'Unknown identity key') {
// create an error that the UI will pick up and ask the // create an error that the UI will pick up and ask the
// user if they want to re-negotiate // user if they want to re-negotiate
const buffer = window.dcodeIO.ByteBuffer.wrap(ciphertext); const buffer = ByteBuffer.wrap(ciphertext);
errorToThrow = new textsecure.IncomingIdentityKeyError( errorToThrow = new textsecure.IncomingIdentityKeyError(
address.toString(), address.toString(),
buffer.toArrayBuffer(), buffer.toArrayBuffer(),

@ -4,6 +4,7 @@ import { EnvelopePlus } from './types';
import { MediumGroupResponseKeysMessage } from '../session/messages/outgoing'; import { MediumGroupResponseKeysMessage } from '../session/messages/outgoing';
import { getMessageQueue } from '../session'; import { getMessageQueue } from '../session';
import { PubKey } from '../session/types'; import { PubKey } from '../session/types';
import _ from 'lodash';
async function handleSenderKeyRequest( async function handleSenderKeyRequest(
envelope: EnvelopePlus, envelope: EnvelopePlus,
@ -59,14 +60,7 @@ async function handleSenderKey(envelope: EnvelopePlus, groupUpdate: any) {
} }
async function handleNewGroup(envelope: EnvelopePlus, groupUpdate: any) { async function handleNewGroup(envelope: EnvelopePlus, groupUpdate: any) {
const { const { SenderKeyAPI, StringView, Whisper, log, textsecure } = window;
SenderKeyAPI,
StringView,
Whisper,
log,
textsecure,
Lodash: _,
} = window;
const senderIdentity = envelope.source; const senderIdentity = envelope.source;

@ -11,6 +11,8 @@ import { StringUtils } from '../session/utils';
import { MultiDeviceProtocol, SessionProtocol } from '../session/protocols'; import { MultiDeviceProtocol, SessionProtocol } from '../session/protocols';
import { PubKey } from '../session/types'; import { PubKey } from '../session/types';
import ByteBuffer from 'bytebuffer';
async function unpairingRequestIsLegit(source: string, ourPubKey: string) { async function unpairingRequestIsLegit(source: string, ourPubKey: string) {
const { textsecure, storage, lokiFileServerAPI } = window; const { textsecure, storage, lokiFileServerAPI } = window;
@ -204,7 +206,7 @@ async function handleAuthorisationForSelf(
} }
function parseContacts(arrbuf: ArrayBuffer): Array<any> { function parseContacts(arrbuf: ArrayBuffer): Array<any> {
const buffer = new window.dcodeIO.ByteBuffer(); const buffer = new ByteBuffer();
buffer.append(arrbuf); buffer.append(arrbuf);
buffer.offset = 0; buffer.offset = 0;
buffer.limit = arrbuf.byteLength; buffer.limit = arrbuf.byteLength;
@ -270,9 +272,14 @@ export async function handleContacts(
window.log.info('contact sync'); window.log.info('contact sync');
// const { blob } = contacts; // const { blob } = contacts;
if (!contacts.data) {
window.log.error('Contacts without data');
return;
}
const attachmentPointer = { const attachmentPointer = {
contacts, contacts,
data: window.dcodeIO.ByteBuffer.wrap(contacts.data).toArrayBuffer(), // ByteBuffer to ArrayBuffer data: ByteBuffer.wrap(contacts.data).toArrayBuffer(), // ByteBuffer to ArrayBuffer
}; };
const contactDetails = parseContacts(attachmentPointer.data); const contactDetails = parseContacts(attachmentPointer.data);

@ -3,6 +3,7 @@ import { SignalService } from '../protobuf';
import { removeFromCache } from './cache'; import { removeFromCache } from './cache';
import { getEnvelopeId } from './common'; import { getEnvelopeId } from './common';
import _ from 'lodash'; import _ from 'lodash';
import ByteBuffer from 'bytebuffer';
import { handleEndSession } from './sessionHandling'; import { handleEndSession } from './sessionHandling';
import { handleMediumGroupUpdate } from './mediumGroups'; import { handleMediumGroupUpdate } from './mediumGroups';
@ -131,7 +132,7 @@ async function handleSentMessage(
function handleAttachment(attachment: any) { function handleAttachment(attachment: any) {
return { return {
...attachment, ...attachment,
data: window.dcodeIO.ByteBuffer.wrap(attachment.data).toArrayBuffer(), // ByteBuffer to ArrayBuffer data: ByteBuffer.wrap(attachment.data).toArrayBuffer(), // ByteBuffer to ArrayBuffer
}; };
} }

@ -2,6 +2,8 @@ import fetch from 'node-fetch';
import https from 'https'; import https from 'https';
import { Snode } from './snodePool'; import { Snode } from './snodePool';
import ByteBuffer from 'bytebuffer';
import { StringUtils } from '../utils';
const BAD_PATH = 'bad_path'; const BAD_PATH = 'bad_path';
@ -43,7 +45,7 @@ async function encryptForRelay(
const reqObj = { const reqObj = {
...destination, ...destination,
ciphertext: dcodeIO.ByteBuffer.wrap(payload).toString('base64'), ciphertext: ByteBuffer.wrap(payload).toString('base64'),
ephemeral_key: StringView.arrayBufferToHex(ctx.ephemeralKey), ephemeral_key: StringView.arrayBufferToHex(ctx.ephemeralKey),
}; };
@ -51,13 +53,11 @@ async function encryptForRelay(
} }
async function makeGuardPayload(guardCtx: any) { async function makeGuardPayload(guardCtx: any) {
const ciphertextBase64 = window.dcodeIO.ByteBuffer.wrap( const ciphertextBase64 = StringUtils.decode(guardCtx.ciphertext, 'base64');
guardCtx.ciphertext
).toString('base64');
const guardPayloadObj = { const guardPayloadObj = {
ciphertext: ciphertextBase64, ciphertext: ciphertextBase64,
ephemeral_key: window.StringView.arrayBufferToHex(guardCtx.ephemeralKey), ephemeral_key: StringUtils.decode(guardCtx.ephemeralKey, 'hex'),
}; };
return guardPayloadObj; return guardPayloadObj;
} }

@ -10,6 +10,7 @@ import {
} from './serviceNodeAPI'; } from './serviceNodeAPI';
import semver from 'semver'; import semver from 'semver';
import _ from 'lodash';
export type SnodeEdKey = string; export type SnodeEdKey = string;
@ -34,7 +35,7 @@ const nodesForPubkey: { [key: string]: Array<SnodeEdKey> } = {};
async function tryGetSnodeListFromLokidSeednode( async function tryGetSnodeListFromLokidSeednode(
seedNodes = window.seedNodeList seedNodes = window.seedNodeList
): Promise<Array<Snode>> { ): Promise<Array<Snode>> {
const { log, Lodash: _ } = window; const { log } = window;
if (!seedNodes.length) { if (!seedNodes.length) {
log.info( log.info(
@ -130,7 +131,7 @@ export async function markUnreachableForPubkey(
} }
export function markNodeUnreachable(snode: Snode): void { export function markNodeUnreachable(snode: Snode): void {
const { Lodash: _, log } = window; const { log } = window;
randomSnodePool = _.without(randomSnodePool, snode); randomSnodePool = _.without(randomSnodePool, snode);
log.warn( log.warn(
@ -139,8 +140,6 @@ export function markNodeUnreachable(snode: Snode): void {
} }
export async function getRandomSnodeAddress(): Promise<Snode> { export async function getRandomSnodeAddress(): Promise<Snode> {
const { Lodash: _ } = window;
// resolve random snode // resolve random snode
if (randomSnodePool.length === 0) { if (randomSnodePool.length === 0) {
// allow exceptions to pass through upwards without the unhandled promise rejection // allow exceptions to pass through upwards without the unhandled promise rejection
@ -154,7 +153,8 @@ export async function getRandomSnodeAddress(): Promise<Snode> {
} }
} }
return _.sample(randomSnodePool); // We know the pool can't be empty at this point
return _.sample(randomSnodePool) as Snode;
} }
function compareSnodes(lhs: any, rhs: any): boolean { function compareSnodes(lhs: any, rhs: any): boolean {
@ -284,7 +284,7 @@ async function getSnodeListFromLokidSeednode(
} }
async function refreshRandomPoolDetail(seedNodes: Array<any>): Promise<void> { async function refreshRandomPoolDetail(seedNodes: Array<any>): Promise<void> {
const { log, Lodash: _ } = window; const { log } = window;
// are we running any _getAllVerionsForRandomSnodePool // are we running any _getAllVerionsForRandomSnodePool
if (stopGetAllVersionPromiseControl !== false) { if (stopGetAllVersionPromiseControl !== false) {
@ -303,6 +303,7 @@ async function refreshRandomPoolDetail(seedNodes: Array<any>): Promise<void> {
port: snode.storage_port, port: snode.storage_port,
pubkey_x25519: snode.pubkey_x25519, pubkey_x25519: snode.pubkey_x25519,
pubkey_ed25519: snode.pubkey_ed25519, pubkey_ed25519: snode.pubkey_ed25519,
version: '',
})); }));
log.info( log.info(
'LokiSnodeAPI::refreshRandomPool - Refreshed random snode pool with', 'LokiSnodeAPI::refreshRandomPool - Refreshed random snode pool with',

@ -62,9 +62,9 @@ export class SwarmPolling {
public removePubkey(pubkey: PubKey) { public removePubkey(pubkey: PubKey) {
if (this.pubkeys.indexOf(pubkey) !== -1) { if (this.pubkeys.indexOf(pubkey) !== -1) {
window.Lodash.remove(this.pubkeys, pubkey); _.remove(this.pubkeys, pubkey);
} else if (this.groupPubkeys.indexOf(pubkey) !== -1) { } else if (this.groupPubkeys.indexOf(pubkey) !== -1) {
window.Lodash.remove(this.groupPubkeys, pubkey); _.remove(this.groupPubkeys, pubkey);
} }
} }

Loading…
Cancel
Save