From 3f93b25ac237ff6d1c816645fee380d2571c7146 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 18 Jun 2020 11:29:29 +1000 Subject: [PATCH] Added string utils --- package.json | 1 + ts/components/session/RegistrationTabs.tsx | 3 ++- ts/session/protocols/MultiDeviceProtocol.ts | 7 +++---- ts/session/utils/String.ts | 21 ++++++++++++++++++- .../protocols/MultiDeviceProtocol_test.ts | 5 +++-- .../ciphers/FallBackSessionCipherStub.ts | 3 ++- .../stubs/ciphers/SecretSessionCipherStub.ts | 3 ++- .../stubs/ciphers/SessionCipherStub.ts | 3 ++- yarn.lock | 13 ++++++++++++ 9 files changed, 48 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 34a7ef7cf..28c10e0bb 100644 --- a/package.json +++ b/package.json @@ -118,6 +118,7 @@ }, "devDependencies": { "@types/backbone": "^1.4.2", + "@types/bytebuffer": "^5.0.41", "@types/chai": "4.1.2", "@types/chai-as-promised": "^7.1.2", "@types/classnames": "2.2.3", diff --git a/ts/components/session/RegistrationTabs.tsx b/ts/components/session/RegistrationTabs.tsx index a21c38714..db7fdd8da 100644 --- a/ts/components/session/RegistrationTabs.tsx +++ b/ts/components/session/RegistrationTabs.tsx @@ -11,6 +11,7 @@ import { trigger } from '../../shims/events'; import { SessionHtmlRenderer } from './SessionHTMLRenderer'; import { SessionIdEditable } from './SessionIdEditable'; import { SessionSpinner } from './SessionSpinner'; +import { StringUtils } from '../../session/utils'; enum SignInMode { Default, @@ -178,7 +179,7 @@ export class RegistrationTabs extends React.Component<{}, State> { 'hex' ).toArrayBuffer(); const keyPair = await window.libsignal.Curve.async.createKeyPair(seed); - const hexGeneratedPubKey = Buffer.from(keyPair.pubKey).toString('hex'); + const hexGeneratedPubKey = StringUtils.decode(keyPair.pubKey, 'hex'); this.setState({ generatedMnemonicSeed: mnemonic, diff --git a/ts/session/protocols/MultiDeviceProtocol.ts b/ts/session/protocols/MultiDeviceProtocol.ts index ab28ad5b2..c4c40605b 100644 --- a/ts/session/protocols/MultiDeviceProtocol.ts +++ b/ts/session/protocols/MultiDeviceProtocol.ts @@ -7,7 +7,7 @@ import { } from '../../../js/modules/data'; import { PrimaryPubKey, PubKey, SecondaryPubKey } from '../types'; import { UserUtil } from '../../util'; -import { BufferUtils } from '../utils'; +import { StringUtils } from '../utils'; /* The reason we're exporing a class here instead of just exporting the functions directly is for the sake of testing. @@ -93,9 +93,8 @@ export class MultiDeviceProtocol { }) => ({ primaryDevicePubKey, secondaryDevicePubKey, - requestSignature: BufferUtils.base64toUint8Array(requestSignature) - .buffer, - grantSignature: BufferUtils.base64toUint8Array(grantSignature).buffer, + requestSignature: StringUtils.encode(requestSignature, 'base64'), + grantSignature: StringUtils.encode(grantSignature, 'base64'), }) ); } diff --git a/ts/session/utils/String.ts b/ts/session/utils/String.ts index 2dbee1307..0c2b16c37 100644 --- a/ts/session/utils/String.ts +++ b/ts/session/utils/String.ts @@ -1,3 +1,22 @@ -export function test() { +import ByteBuffer from 'bytebuffer'; +type Encoding = 'base64' | 'hex' | 'binary' | 'utf8'; +type BufferType = ByteBuffer | Buffer | ArrayBuffer | Uint8Array; + +/** + * Take a string value with the given encoding and converts it to an `ArrayBuffer`. + * @param value The string value. + * @param encoding The encoding of the string value. + */ +export function encode(value: string, encoding: Encoding): ArrayBuffer { + return ByteBuffer.wrap(value, encoding).toArrayBuffer(); +} + +/** + * Take a buffer and convert it to a string with the given encoding. + * @param buffer The buffer. + * @param stringEncoding The encoding of the converted string value. + */ +export function decode(buffer: BufferType, stringEncoding: Encoding): string { + return ByteBuffer.wrap(buffer).toString(stringEncoding); } diff --git a/ts/test/session/protocols/MultiDeviceProtocol_test.ts b/ts/test/session/protocols/MultiDeviceProtocol_test.ts index b92d0305a..3da1c298e 100644 --- a/ts/test/session/protocols/MultiDeviceProtocol_test.ts +++ b/ts/test/session/protocols/MultiDeviceProtocol_test.ts @@ -5,6 +5,7 @@ import { PairingAuthorisation } from '../../../../js/modules/data'; import { MultiDeviceProtocol } from '../../../session/protocols'; import { PubKey } from '../../../session/types'; import { UserUtil } from '../../../util'; +import { StringUtils } from '../../../session/utils'; function generateFakeAuthorisations( primary: PubKey, @@ -112,7 +113,7 @@ describe('MultiDeviceProtocol', () => { } = authorisations[0]; expect(primaryDevicePubKey).to.equal(networkAuth.primaryDevicePubKey); expect(secondaryDevicePubKey).to.equal(networkAuth.secondaryDevicePubKey); - expect(Buffer.from(requestSignature).toString('base64')).to.equal( + expect(StringUtils.decode(requestSignature, 'base64')).to.equal( networkAuth.requestSignature ); expect(grantSignature).to.not.equal( @@ -120,7 +121,7 @@ describe('MultiDeviceProtocol', () => { 'Grant signature should not be undefined.' ); // tslint:disable-next-line: no-non-null-assertion - expect(Buffer.from(grantSignature!).toString('base64')).to.equal( + expect(StringUtils.decode(grantSignature!, 'base64')).to.equal( networkAuth.grantSignature ); }); diff --git a/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts index cbc6f1785..d53b258d6 100644 --- a/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/FallBackSessionCipherStub.ts @@ -1,11 +1,12 @@ import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol'; import { SignalService } from '../../../../protobuf'; +import { StringUtils } from '../../../../session/utils'; export class FallBackSessionCipherStub { public async encrypt(buffer: ArrayBuffer): Promise { return { type: SignalService.Envelope.Type.SESSION_REQUEST, - body: Buffer.from(buffer).toString('binary'), + body: StringUtils.decode(buffer, 'binary'), }; } } diff --git a/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts index ec008e107..8f9474026 100644 --- a/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/SecretSessionCipherStub.ts @@ -1,6 +1,7 @@ import { SignalService } from '../../../../protobuf'; import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol'; import { SecretSessionCipherInterface } from '../../../../../js/modules/metadata/SecretSessionCipher'; +import { StringUtils } from '../../../../session/utils'; export class SecretSessionCipherStub implements SecretSessionCipherInterface { public async encrypt( @@ -10,7 +11,7 @@ export class SecretSessionCipherStub implements SecretSessionCipherInterface { ): Promise { const { body } = innerEncryptedMessage; - return Buffer.from(body, 'binary').buffer; + return StringUtils.encode(body, 'binary'); } public async decrypt( diff --git a/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts b/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts index 4e246d1f4..922f7e02c 100644 --- a/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts +++ b/ts/test/test-utils/stubs/ciphers/SessionCipherStub.ts @@ -3,6 +3,7 @@ import { SessionCipher, } from '../../../../../libtextsecure/libsignal-protocol'; import { SignalService } from '../../../../protobuf'; +import { StringUtils } from '../../../../session/utils'; export class SessionCipherStub implements SessionCipher { public storage: any; @@ -17,7 +18,7 @@ export class SessionCipherStub implements SessionCipher { ): Promise { return { type: SignalService.Envelope.Type.CIPHERTEXT, - body: Buffer.from(buffer).toString('binary'), + body: StringUtils.decode(buffer, 'binary'), }; } diff --git a/yarn.lock b/yarn.lock index 131f524d6..1680b3349 100644 --- a/yarn.lock +++ b/yarn.lock @@ -171,6 +171,14 @@ "@types/jquery" "*" "@types/underscore" "*" +"@types/bytebuffer@^5.0.41": + version "5.0.41" + resolved "https://registry.yarnpkg.com/@types/bytebuffer/-/bytebuffer-5.0.41.tgz#6850dba4d4cd2846596b4842874d5bfc01cd3db1" + integrity sha512-Mdrv4YcaHvpkx25ksqqFaezktx3yZRcd51GZY0rY/9avyaqZdiT/GiWRhfrJhMpgzXqTOSHgGvsumGxJFNiZZA== + dependencies: + "@types/long" "*" + "@types/node" "*" + "@types/chai-as-promised@^7.1.2": version "7.1.2" resolved "https://registry.yarnpkg.com/@types/chai-as-promised/-/chai-as-promised-7.1.2.tgz#2f564420e81eaf8650169e5a3a6b93e096e5068b" @@ -293,6 +301,11 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.106.tgz#6093e9a02aa567ddecfe9afadca89e53e5dce4dd" integrity sha512-tOSvCVrvSqFZ4A/qrqqm6p37GZoawsZtoR0SJhlF7EonNZUgrn8FfT+RNQ11h+NUpMt6QVe36033f3qEKBwfWA== +"@types/long@*": + version "4.0.1" + resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.1.tgz#459c65fa1867dafe6a8f322c4c51695663cc55e9" + integrity sha512-5tXH6Bx/kNGd3MgffdmP4dy2Z+G4eaXw0SE81Tq3BNadtnMR5/ySMzX4SLEzHJzSmPNn4HIdpQsBvXMUykr58w== + "@types/long@^3.0.32": version "3.0.32" resolved "https://registry.yarnpkg.com/@types/long/-/long-3.0.32.tgz#f4e5af31e9e9b196d8e5fca8a5e2e20aa3d60b69"