From 41fa167e79ee85620898eb0d7fc8de9fe06f131e Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 5 Jun 2020 16:54:24 +1000 Subject: [PATCH] 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; }