Add more type annotations

pull/1204/head
Maxim Shishmarev 5 years ago
parent e2b5b6654b
commit 468ad9f71d

@ -8,7 +8,10 @@ import { toNumber } from 'lodash';
import * as libsession from '../session'; import * as libsession from '../session';
import { handleSessionRequestMessage } from './sessionHandling'; import { handleSessionRequestMessage } from './sessionHandling';
import { handlePairingAuthorisationMessage } from './multidevice'; import { handlePairingAuthorisationMessage } from './multidevice';
import { MediumGroupRequestKeysMessage } from '../session/messages/outgoing'; import {
MediumGroupRequestKeysMessage,
ReceiptMessage,
} from '../session/messages/outgoing';
import { MultiDeviceProtocol, SessionProtocol } from '../session/protocols'; import { MultiDeviceProtocol, SessionProtocol } from '../session/protocols';
import { PubKey } from '../session/types'; import { PubKey } from '../session/types';
@ -29,7 +32,7 @@ export async function handleContentMessage(envelope: EnvelopePlus) {
async function decryptForMediumGroup( async function decryptForMediumGroup(
envelope: EnvelopePlus, envelope: EnvelopePlus,
ciphertextObj: any ciphertextObj: ArrayBuffer
) { ) {
const { textsecure, dcodeIO, libloki } = window; const { textsecure, dcodeIO, libloki } = window;
@ -47,9 +50,9 @@ async function decryptForMediumGroup(
const { const {
ciphertext: outerCiphertext, ciphertext: outerCiphertext,
ephemeralKey, ephemeralKey,
} = textsecure.protobuf.MediumGroupContent.decode(ciphertextObj); } = SignalService.MediumGroupContent.decode(new Uint8Array(ciphertextObj));
const ephemKey = ephemeralKey.toArrayBuffer(); const ephemKey = ephemeralKey.buffer;
const secretKey = dcodeIO.ByteBuffer.wrap( const secretKey = dcodeIO.ByteBuffer.wrap(
secretKeyHex, secretKeyHex,
'hex' 'hex'
@ -58,16 +61,15 @@ async function decryptForMediumGroup(
const mediumGroupCiphertext = await libloki.crypto.decryptForPubkey( const mediumGroupCiphertext = await libloki.crypto.decryptForPubkey(
secretKey, secretKey,
ephemKey, ephemKey,
outerCiphertext.toArrayBuffer() outerCiphertext.buffer
); );
const { const { ciphertext, keyIdx } = SignalService.MediumGroupCiphertext.decode(
ciphertext, mediumGroupCiphertext
keyIdx, );
} = textsecure.protobuf.MediumGroupCiphertext.decode(mediumGroupCiphertext);
const plaintext = await window.SenderKeyAPI.decryptWithSenderKey( const plaintext = await window.SenderKeyAPI.decryptWithSenderKey(
ciphertext.toArrayBuffer(), ciphertext.buffer,
keyIdx, keyIdx,
groupId, groupId,
senderIdentity senderIdentity
@ -206,7 +208,7 @@ async function decryptUnidentifiedSender(
async function doDecrypt( async function doDecrypt(
envelope: EnvelopePlus, envelope: EnvelopePlus,
ciphertext: any, ciphertext: ArrayBuffer,
address: any address: any
): Promise<ArrayBuffer | null> { ): Promise<ArrayBuffer | null> {
const { textsecure, libloki } = window; const { textsecure, libloki } = window;
@ -229,9 +231,7 @@ async function doDecrypt(
address address
); );
return fallBackSessionCipher return fallBackSessionCipher.decrypt(ciphertext).then(unpad);
.decrypt(ciphertext.toArrayBuffer())
.then(unpad);
} }
case SignalService.Envelope.Type.PREKEY_BUNDLE: case SignalService.Envelope.Type.PREKEY_BUNDLE:
window.log.info('prekey message from', getEnvelopeId(envelope)); window.log.info('prekey message from', getEnvelopeId(envelope));
@ -241,14 +241,17 @@ async function doDecrypt(
address address
); );
case SignalService.Envelope.Type.UNIDENTIFIED_SENDER: { case SignalService.Envelope.Type.UNIDENTIFIED_SENDER: {
return decryptUnidentifiedSender(envelope, ciphertext.toArrayBuffer()); return decryptUnidentifiedSender(envelope, ciphertext);
} }
default: default:
throw new Error('Unknown message type'); throw new Error('Unknown message type');
} }
} }
async function decrypt(envelope: EnvelopePlus, ciphertext: any): Promise<any> { async function decrypt(
envelope: EnvelopePlus,
ciphertext: ArrayBuffer
): Promise<any> {
const { textsecure, libsignal, log } = window; const { textsecure, libsignal, log } = window;
// Envelope.source will be null on UNIDENTIFIED_SENDER // Envelope.source will be null on UNIDENTIFIED_SENDER
@ -332,11 +335,11 @@ async function decrypt(envelope: EnvelopePlus, ciphertext: any): Promise<any> {
export async function innerHandleContentMessage( export async function innerHandleContentMessage(
envelope: EnvelopePlus, envelope: EnvelopePlus,
plaintext: any plaintext: ArrayBuffer
): Promise<void> { ): Promise<void> {
const { ConversationController, textsecure } = window; const { ConversationController } = window;
const content = textsecure.protobuf.Content.decode(plaintext); const content = SignalService.Content.decode(new Uint8Array(plaintext));
const { SESSION_REQUEST } = SignalService.Envelope.Type; const { SESSION_REQUEST } = SignalService.Envelope.Type;
@ -352,6 +355,11 @@ export async function innerHandleContentMessage(
} }
if (content.pairingAuthorisation) { if (content.pairingAuthorisation) {
if (!content.dataMessage || !content.syncMessage) {
window.log.error('Missing fields in pairingAuthorisation');
return;
}
await handlePairingAuthorisationMessage( await handlePairingAuthorisationMessage(
envelope, envelope,
content.pairingAuthorisation, content.pairingAuthorisation,
@ -426,10 +434,11 @@ export function onDeliveryReceipt(source: any, timestamp: any) {
async function handleReceiptMessage( async function handleReceiptMessage(
envelope: EnvelopePlus, envelope: EnvelopePlus,
receiptMessage: SignalService.ReceiptMessage receiptMessage: SignalService.IReceiptMessage
) { ) {
const { textsecure } = window; const receipt = receiptMessage as SignalService.ReceiptMessage;
const { type, timestamp } = receiptMessage;
const { type, timestamp } = receipt;
const results = []; const results = [];
if (type === SignalService.ReceiptMessage.Type.DELIVERY) { if (type === SignalService.ReceiptMessage.Type.DELIVERY) {
@ -464,10 +473,12 @@ async function handleCallMessage(envelope: EnvelopePlus) {
async function handleTypingMessage( async function handleTypingMessage(
envelope: EnvelopePlus, envelope: EnvelopePlus,
typingMessage: SignalService.TypingMessage iTypingMessage: SignalService.ITypingMessage
): Promise<void> { ): Promise<void> {
const ev = new Event('typing'); const ev = new Event('typing');
const typingMessage = iTypingMessage as SignalService.TypingMessage;
const { ConversationController } = window; const { ConversationController } = window;
const { timestamp, groupId, action } = typingMessage; const { timestamp, groupId, action } = typingMessage;
const { source } = envelope; const { source } = envelope;

@ -254,7 +254,7 @@ function isMessageEmpty(message: SignalService.DataMessage) {
export async function handleDataMessage( export async function handleDataMessage(
envelope: EnvelopePlus, envelope: EnvelopePlus,
dataMessage: SignalService.DataMessage dataMessage: SignalService.IDataMessage
): Promise<void> { ): Promise<void> {
window.log.info('data message from', getEnvelopeId(envelope)); window.log.info('data message from', getEnvelopeId(envelope));
@ -264,7 +264,10 @@ export async function handleDataMessage(
} }
// tslint:disable-next-line no-bitwise // tslint:disable-next-line no-bitwise
if (dataMessage.flags & SignalService.DataMessage.Flags.END_SESSION) { if (
dataMessage.flags &&
dataMessage.flags & SignalService.DataMessage.Flags.END_SESSION
) {
await handleEndSession(envelope.source); await handleEndSession(envelope.source);
return; return;
} }

@ -357,9 +357,7 @@ async function onContactReceived(details: any) {
// when we do not have a session with it already // when we do not have a session with it already
deviceConversations.forEach(device => { deviceConversations.forEach(device => {
// tslint:disable-next-line: no-floating-promises // tslint:disable-next-line: no-floating-promises
SessionProtocol.sendSessionRequestIfNeeded( SessionProtocol.sendSessionRequestIfNeeded(new PubKey(device.id));
new PubKey(device.id)
);
}); });
if (details.profileKey) { if (details.profileKey) {

@ -128,7 +128,7 @@ async function handleRequestDetail(
): Promise<void> { ): Promise<void> {
const { textsecure } = window; const { textsecure } = window;
const envelope : any = SignalService.Envelope.decode(plaintext); const envelope: any = SignalService.Envelope.decode(plaintext);
// After this point, decoding errors are not the server's // After this point, decoding errors are not the server's
// fault, and we should handle them gracefully and tell the // fault, and we should handle them gracefully and tell the
@ -147,7 +147,7 @@ async function handleRequestDetail(
// plaintext (and protobuf.Envelope) does not have that field... // plaintext (and protobuf.Envelope) does not have that field...
envelope.source = options.conversationId; envelope.source = options.conversationId;
// tslint:disable-next-line no-parameter-reassignment // tslint:disable-next-line no-parameter-reassignment
plaintext = textsecure.protobuf.Envelope.encode(envelope).toArrayBuffer(); plaintext = SignalService.Envelope.encode(envelope).finish();
envelope.senderIdentity = senderIdentity; envelope.senderIdentity = senderIdentity;
} }
@ -241,12 +241,6 @@ async function queueCached(item: any) {
if (decrypted) { if (decrypted) {
const payloadPlaintext = StringUtils.encode(decrypted, 'base64'); const payloadPlaintext = StringUtils.encode(decrypted, 'base64');
if (typeof payloadPlaintext === 'string') {
// payloadPlaintext = await MessageReceiver.stringToArrayBuffer(
// payloadPlaintext
// );
}
// Convert preKeys to array buffer // Convert preKeys to array buffer
if (typeof envelope.preKeyBundleMessage === 'string') { if (typeof envelope.preKeyBundleMessage === 'string') {
// envelope.preKeyBundleMessage = await MessageReceiver.stringToArrayBuffer( // envelope.preKeyBundleMessage = await MessageReceiver.stringToArrayBuffer(
@ -279,7 +273,7 @@ async function queueCached(item: any) {
} }
} }
async function queueDecryptedEnvelope(envelope: any, plaintext: any) { async function queueDecryptedEnvelope(envelope: any, plaintext: ArrayBuffer) {
const id = getEnvelopeId(envelope); const id = getEnvelopeId(envelope);
window.log.info('queueing decrypted envelope', id); window.log.info('queueing decrypted envelope', id);
@ -298,7 +292,10 @@ async function queueDecryptedEnvelope(envelope: any, plaintext: any) {
}); });
} }
async function handleDecryptedEnvelope(envelope: EnvelopePlus, plaintext: any) { async function handleDecryptedEnvelope(
envelope: EnvelopePlus,
plaintext: ArrayBuffer
) {
// if (this.stoppingProcessing) { // if (this.stoppingProcessing) {
// return Promise.resolve(); // return Promise.resolve();
// } // }

@ -2,9 +2,9 @@ import { EnvelopePlus } from './types';
import { SignalService } from '../protobuf'; import { SignalService } from '../protobuf';
import * as libsession from './../session'; import * as libsession from './../session';
import { toNumber } from 'lodash'; import { toNumber } from 'lodash';
import { PubKey } from '../session/types'; import { PubKey } from '../session/types';
import { SessionEstablishedMessage } from '../session/messages/outgoing'; import { SessionEstablishedMessage } from '../session/messages/outgoing';
import { SessionProtocol } from '../session/protocols' import { SessionProtocol } from '../session/protocols';
export async function handleEndSession(number: string): Promise<void> { export async function handleEndSession(number: string): Promise<void> {
window.log.info('got end session'); window.log.info('got end session');
@ -110,9 +110,9 @@ export async function handleSessionRequestMessage(
const user = new PubKey(envelope.source); const user = new PubKey(envelope.source);
const sessionEstablished = new SessionEstablishedMessage( const sessionEstablished = new SessionEstablishedMessage({
{ timestamp: Date.now() } timestamp: Date.now(),
); });
await libsession.getMessageQueue().send(user, sessionEstablished); await libsession.getMessageQueue().send(user, sessionEstablished);
libloki.api.sendSessionEstablishedMessage(envelope.source); libloki.api.sendSessionEstablishedMessage(envelope.source);

@ -14,7 +14,7 @@ import { MultiDeviceProtocol } from '../session/protocols';
export async function handleSyncMessage( export async function handleSyncMessage(
envelope: EnvelopePlus, envelope: EnvelopePlus,
syncMessage: SignalService.SyncMessage syncMessage: SignalService.ISyncMessage
): Promise<void> { ): Promise<void> {
const { textsecure } = window; const { textsecure } = window;

@ -4,6 +4,7 @@ import { retrieveNextMessages } from './serviceNodeAPI';
import { SignalService } from '../../protobuf'; import { SignalService } from '../../protobuf';
import * as Receiver from '../../receiver/receiver'; import * as Receiver from '../../receiver/receiver';
import _ from 'lodash'; import _ from 'lodash';
import * as Data from '../../../js/modules/data';
import { StringUtils } from '../../session/utils'; import { StringUtils } from '../../session/utils';
@ -16,7 +17,7 @@ interface Message {
} }
// Some websocket nonsence // Some websocket nonsence
export function processMessage(message: any, options: any = {}) { export function processMessage(message: string, options: any = {}) {
try { try {
const dataPlaintext = new Uint8Array(StringUtils.encode(message, 'base64')); const dataPlaintext = new Uint8Array(StringUtils.encode(message, 'base64'));
const messageBuf = SignalService.WebSocketMessage.decode(dataPlaintext); const messageBuf = SignalService.WebSocketMessage.decode(dataPlaintext);
@ -87,18 +88,18 @@ export class SwarmPolling {
} }
const incomingHashes = messages.map((m: Message) => m.hash); const incomingHashes = messages.map((m: Message) => m.hash);
const dupHashes = await window.Signal.Data.getSeenMessagesByHashList(
incomingHashes const dupHashes = await Data.getSeenMessagesByHashList(incomingHashes);
);
const newMessages = messages.filter( const newMessages = messages.filter(
(m: Message) => !dupHashes.includes(m.hash) (m: Message) => !dupHashes.includes(m.hash)
); );
if (newMessages.length) { if (newMessages.length) {
const newHashes = newMessages.map((m: Message) => ({ const newHashes = newMessages.map((m: Message) => ({
expiresAt: m.expiration, expiresAt: m.expiration,
hash: m.hash, hash: m.hash,
})); }));
await window.Signal.Data.saveSeenMessageHashes(newHashes); await Data.saveSeenMessageHashes(newHashes);
} }
return newMessages; return newMessages;
} }
@ -125,7 +126,7 @@ export class SwarmPolling {
): Promise<void> { ): Promise<void> {
const pkStr = (pubkey.key ? pubkey.key : pubkey) as string; const pkStr = (pubkey.key ? pubkey.key : pubkey) as string;
await window.Signal.Data.updateLastHash({ await Data.updateLastHash({
convoId: pkStr, convoId: pkStr,
snode: edkey, snode: edkey,
hash, hash,
@ -148,10 +149,7 @@ export class SwarmPolling {
const nodeRecords = this.lastHashes[nodeEdKey]; const nodeRecords = this.lastHashes[nodeEdKey];
if (!nodeRecords || !nodeRecords[pubkey]) { if (!nodeRecords || !nodeRecords[pubkey]) {
const lastHash = await window.Signal.Data.getLastHashBySnode( const lastHash = await Data.getLastHashBySnode(pubkey, nodeEdKey);
pubkey,
nodeEdKey
);
return lastHash || ''; return lastHash || '';
} else { } else {
@ -166,7 +164,6 @@ export class SwarmPolling {
node: Snode, node: Snode,
pubkey: PubKey pubkey: PubKey
): Promise<Array<any>> { ): Promise<Array<any>> {
// console.warn('Polling node: ', node.pubkey_ed25519);
const edkey = node.pubkey_ed25519; const edkey = node.pubkey_ed25519;

Loading…
Cancel
Save