From 41fa167e79ee85620898eb0d7fc8de9fe06f131e Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 5 Jun 2020 16:54:24 +1000 Subject: [PATCH 1/3] Improve typings for LibsignalProtocol --- .../stubs/ciphers/SessionCipherStub.ts | 39 +++++++++++++++- ts/test/tslint.json | 1 + ts/window/types/libsignal-protocol.ts | 46 ++++++++++--------- 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts index 3d08bebfe..421bc6ae1 100644 --- a/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts @@ -1,7 +1,10 @@ -import { CipherTextObject } from '../../../../window/types/libsignal-protocol'; +import { + CipherTextObject, + SessionCipher, +} from '../../../../window/types/libsignal-protocol'; import { SignalService } from '../../../../protobuf'; -export class SessionCipherStub { +export class SessionCipherStub implements SessionCipher { public storage: any; public address: any; constructor(storage: any, address: any) { @@ -17,4 +20,36 @@ export class SessionCipherStub { body: Buffer.from(buffer).toString('binary'), }; } + + public async decryptPreKeyWhisperMessage( + buffer: ArrayBuffer | Uint8Array + ): Promise { + throw new Error('Method not implemented.'); + } + + public async decryptWhisperMessage( + buffer: ArrayBuffer | Uint8Array + ): Promise { + throw new Error('Method not implemented.'); + } + + public async getRecord(encodedNumber: string): Promise { + throw new Error('Method not implemented.'); + } + + public async getRemoteRegistrationId(): Promise { + throw new Error('Method not implemented.'); + } + + public async hasOpenSession(): Promise { + throw new Error('Method not implemented.'); + } + + public async closeOpenSessionForDevice(): Promise { + throw new Error('Method not implemented.'); + } + + public async deleteAllSessionsForDevice(): Promise { + throw new Error('Method not implemented.'); + } } diff --git a/ts/test/tslint.json b/ts/test/tslint.json index 9d1d3e71b..d18bab561 100644 --- a/ts/test/tslint.json +++ b/ts/test/tslint.json @@ -9,6 +9,7 @@ "max-func-body-length": false, "no-unused-expression": false, + "no-unused-variable": false, "await-promise": [true, "PromiseLike"], "no-floating-promises": [true, "PromiseLike"] diff --git a/ts/window/types/libsignal-protocol.ts b/ts/window/types/libsignal-protocol.ts index 9b0e18146..cf7e383a0 100644 --- a/ts/window/types/libsignal-protocol.ts +++ b/ts/window/types/libsignal-protocol.ts @@ -7,15 +7,16 @@ export type CipherTextObject = { body: BinaryString; registrationId?: number; }; +export interface SignalProtocolAddressConstructor { + new (hexEncodedPublicKey: string, deviceId: number): SignalProtocolAddress; + fromString(encodedAddress: string): SignalProtocolAddress; +} -export declare class SignalProtocolAddress { - constructor(hexEncodedPublicKey: string, deviceId: number); - // tslint:disable-next-line: function-name - public static fromString(encodedAddress: string): SignalProtocolAddress; - public getName(): string; - public getDeviceId(): number; - public toString(): string; - public equals(other: SignalProtocolAddress): boolean; +export interface SignalProtocolAddress { + getName(): string; + getDeviceId(): number; + toString(): string; + equals(other: SignalProtocolAddress): boolean; } export type KeyPair = { @@ -99,29 +100,30 @@ export interface KeyHelperInterface { }>; } -export declare class SessionCipher { - constructor(storage: any, remoteAddress: SignalProtocolAddress); +export type SessionCipherConstructor = new ( + storage: any, + remoteAddress: SignalProtocolAddress +) => SessionCipher; +export interface SessionCipher { /** * @returns The envelope type, registration id and binary encoded encrypted body. */ - public encrypt(buffer: ArrayBuffer | Uint8Array): Promise; - public decryptPreKeyWhisperMessage( - buffer: ArrayBuffer | Uint8Array - ): Promise; - public decryptWhisperMessage( + encrypt(buffer: ArrayBuffer | Uint8Array): Promise; + decryptPreKeyWhisperMessage( buffer: ArrayBuffer | Uint8Array ): Promise; - public getRecord(encodedNumber: string): Promise; - public getRemoteRegistrationId(): Promise; - public hasOpenSession(): Promise; - public closeOpenSessionForDevice(): Promise; - public deleteAllSessionsForDevice(): Promise; + decryptWhisperMessage(buffer: ArrayBuffer | Uint8Array): Promise; + getRecord(encodedNumber: string): Promise; + getRemoteRegistrationId(): Promise; + hasOpenSession(): Promise; + closeOpenSessionForDevice(): Promise; + deleteAllSessionsForDevice(): Promise; } export interface LibsignalProtocol { - SignalProtocolAddress: typeof SignalProtocolAddress; + SignalProtocolAddress: SignalProtocolAddressConstructor; Curve: CurveInterface; crypto: CryptoInterface; KeyHelper: KeyHelperInterface; - SessionCipher: typeof SessionCipher; + SessionCipher: SessionCipherConstructor; } From 653efaf9cf0518409bc81dca764af99f5049af95 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 9 Jun 2020 16:18:02 +1000 Subject: [PATCH 2/3] Fixed libsignal-protocol declaration --- .../libsignal-protocol.d.ts | 0 ts/session/crypto/MessageEncrypter.ts | 2 +- ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts | 2 +- ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts | 2 +- ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts | 2 +- ts/util/user.ts | 2 +- ts/window/index.ts | 2 +- ts/window/types/SecretSessionCipher.ts | 2 +- 8 files changed, 7 insertions(+), 7 deletions(-) rename ts/window/types/libsignal-protocol.ts => libtextsecure/libsignal-protocol.d.ts (100%) diff --git a/ts/window/types/libsignal-protocol.ts b/libtextsecure/libsignal-protocol.d.ts similarity index 100% rename from ts/window/types/libsignal-protocol.ts rename to libtextsecure/libsignal-protocol.d.ts diff --git a/ts/session/crypto/MessageEncrypter.ts b/ts/session/crypto/MessageEncrypter.ts index 0a6c8d5e3..660c2448f 100644 --- a/ts/session/crypto/MessageEncrypter.ts +++ b/ts/session/crypto/MessageEncrypter.ts @@ -1,8 +1,8 @@ import { EncryptionType } from '../types/EncryptionType'; import { SignalService } from '../../protobuf'; import { libloki, libsignal, Signal, textsecure } from '../../window'; -import { CipherTextObject } from '../../window/types/libsignal-protocol'; import { UserUtil } from '../../util'; +import { CipherTextObject } from '../../../libtextsecure/libsignal-protocol'; export function padPlainTextBuffer(messageBuffer: Uint8Array): Uint8Array { const plaintext = new Uint8Array( diff --git a/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts index ea30f41ad..22aba7b01 100644 --- a/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts @@ -1,4 +1,4 @@ -import { CipherTextObject } from '../../../../window/types/libsignal-protocol'; +import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol'; import { SignalService } from '../../../../protobuf'; export class FallBackSessionCipherStub { diff --git a/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts index 826ab7308..ffabce537 100644 --- a/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts @@ -1,5 +1,5 @@ import { SignalService } from '../../../../protobuf'; -import { CipherTextObject } from '../../../../window/types/libsignal-protocol'; +import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol'; export class SecretSessionCipherStub { public async encrypt( diff --git a/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts index 421bc6ae1..8f35d8ebc 100644 --- a/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts @@ -1,7 +1,7 @@ import { CipherTextObject, SessionCipher, -} from '../../../../window/types/libsignal-protocol'; +} from '../../../../../libtextsecure/libsignal-protocol'; import { SignalService } from '../../../../protobuf'; export class SessionCipherStub implements SessionCipher { diff --git a/ts/util/user.ts b/ts/util/user.ts index cec8716ec..cea991b17 100644 --- a/ts/util/user.ts +++ b/ts/util/user.ts @@ -1,5 +1,5 @@ import { getItemById } from '../../js/modules/data'; -import { KeyPair } from '../window/types/libsignal-protocol'; +import { KeyPair } from '../../libtextsecure/libsignal-protocol'; export async function getCurrentDevicePubKey(): Promise { const item = await getItemById('number_id'); diff --git a/ts/window/index.ts b/ts/window/index.ts index d24d64142..1e7e909f7 100644 --- a/ts/window/index.ts +++ b/ts/window/index.ts @@ -1,8 +1,8 @@ -import { LibsignalProtocol } from './types/libsignal-protocol'; import { SignalInterface } from './types/signal'; import { LocalizerType } from '../types/Util'; import LokiMessageAPI from '../../js/modules/loki_message_api'; import LokiPublicChatFactoryAPI from '../../js/modules/loki_public_chat_api'; +import { LibsignalProtocol } from '../../libtextsecure/libsignal-protocol'; interface WindowInterface extends Window { seedNodeList: any; diff --git a/ts/window/types/SecretSessionCipher.ts b/ts/window/types/SecretSessionCipher.ts index 7967e2744..bdf1d3ff1 100644 --- a/ts/window/types/SecretSessionCipher.ts +++ b/ts/window/types/SecretSessionCipher.ts @@ -1,5 +1,5 @@ import { SignalService } from '../../protobuf'; -import { CipherTextObject } from './libsignal-protocol'; +import { CipherTextObject } from '../../../libtextsecure/libsignal-protocol'; export declare class SecretSessionCipher { constructor(storage: any); From f279f47b3586b42e9ef41c90d51627813da5f52c Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 10 Jun 2020 09:43:26 +1000 Subject: [PATCH 3/3] Fix other declaration files --- js/modules/metadata/SecretSessionCipher.d.ts | 42 +++++++++++++++++++ js/modules/signal.d.ts | 9 ++++ .../stubs/ciphers/SecretSessionCipherStub.ts | 3 +- ts/window/index.ts | 2 +- ts/window/types/SecretSessionCipher.ts | 20 --------- ts/window/types/signal.ts | 9 ---- 6 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 js/modules/metadata/SecretSessionCipher.d.ts create mode 100644 js/modules/signal.d.ts delete mode 100644 ts/window/types/SecretSessionCipher.ts delete mode 100644 ts/window/types/signal.ts diff --git a/js/modules/metadata/SecretSessionCipher.d.ts b/js/modules/metadata/SecretSessionCipher.d.ts new file mode 100644 index 000000000..98f85bede --- /dev/null +++ b/js/modules/metadata/SecretSessionCipher.d.ts @@ -0,0 +1,42 @@ +import { SignalService } from '../../protobuf'; +import { CipherTextObject } from '../../../libtextsecure/libsignal-protocol'; + +export interface SecretSessionCipherConstructor { + new (storage: any): SecretSessionCipherInterface; +} + +export interface SecretSessionCipherInterface { + encrypt( + destinationPubkey: string, + senderCertificate: SignalService.SenderCertificate, + innerEncryptedMessage: CipherTextObject + ): Promise; + decrypt( + cipherText: ArrayBuffer, + me: { number: string; deviceId: number } + ): Promise<{ + isMe?: boolean; + sender: string; + content: ArrayBuffer; + type: SignalService.Envelope.Type; + }>; +} + +export declare class SecretSessionCipher + implements SecretSessionCipherInterface { + constructor(storage: any); + public encrypt( + destinationPubkey: string, + senderCertificate: SignalService.SenderCertificate, + innerEncryptedMessage: CipherTextObject + ): Promise; + public decrypt( + cipherText: ArrayBuffer, + me: { number: string; deviceId: number } + ): Promise<{ + isMe?: boolean; + sender: string; + content: ArrayBuffer; + type: SignalService.Envelope.Type; + }>; +} diff --git a/js/modules/signal.d.ts b/js/modules/signal.d.ts new file mode 100644 index 000000000..4c313376a --- /dev/null +++ b/js/modules/signal.d.ts @@ -0,0 +1,9 @@ +import { SecretSessionCipherConstructor } from './metadata/SecretSessionCipher'; + +interface Metadata { + SecretSessionCipher: SecretSessionCipherConstructor; +} + +export interface SignalInterface { + Metadata: Metadata; +} diff --git a/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts index ffabce537..ec008e107 100644 --- a/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts @@ -1,7 +1,8 @@ import { SignalService } from '../../../../protobuf'; import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol'; +import { SecretSessionCipherInterface } from '../../../../../js/modules/metadata/SecretSessionCipher'; -export class SecretSessionCipherStub { +export class SecretSessionCipherStub implements SecretSessionCipherInterface { public async encrypt( _destinationPubkey: string, _senderCertificate: SignalService.SenderCertificate, diff --git a/ts/window/index.ts b/ts/window/index.ts index 1e7e909f7..4688cde7b 100644 --- a/ts/window/index.ts +++ b/ts/window/index.ts @@ -1,8 +1,8 @@ -import { SignalInterface } from './types/signal'; import { LocalizerType } from '../types/Util'; import LokiMessageAPI from '../../js/modules/loki_message_api'; import LokiPublicChatFactoryAPI from '../../js/modules/loki_public_chat_api'; import { LibsignalProtocol } from '../../libtextsecure/libsignal-protocol'; +import { SignalInterface } from '../../js/modules/signal'; interface WindowInterface extends Window { seedNodeList: any; diff --git a/ts/window/types/SecretSessionCipher.ts b/ts/window/types/SecretSessionCipher.ts deleted file mode 100644 index bdf1d3ff1..000000000 --- a/ts/window/types/SecretSessionCipher.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { SignalService } from '../../protobuf'; -import { CipherTextObject } from '../../../libtextsecure/libsignal-protocol'; - -export declare class SecretSessionCipher { - constructor(storage: any); - public encrypt( - destinationPubkey: string, - senderCertificate: SignalService.SenderCertificate, - innerEncryptedMessage: CipherTextObject - ): Promise; - public decrypt( - cipherText: ArrayBuffer, - me: { number: string; deviceId: number } - ): Promise<{ - isMe?: boolean; - sender: string; - content: ArrayBuffer; - type: SignalService.Envelope.Type; - }>; -} diff --git a/ts/window/types/signal.ts b/ts/window/types/signal.ts deleted file mode 100644 index e1ed96102..000000000 --- a/ts/window/types/signal.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { SecretSessionCipher } from './SecretSessionCipher'; - -interface Metadata { - SecretSessionCipher: typeof SecretSessionCipher; -} - -export interface SignalInterface { - Metadata: Metadata; -}