Added string utils

pull/1182/head
Mikunj 5 years ago
parent 57b5effaf2
commit 3f93b25ac2

@ -118,6 +118,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/backbone": "^1.4.2", "@types/backbone": "^1.4.2",
"@types/bytebuffer": "^5.0.41",
"@types/chai": "4.1.2", "@types/chai": "4.1.2",
"@types/chai-as-promised": "^7.1.2", "@types/chai-as-promised": "^7.1.2",
"@types/classnames": "2.2.3", "@types/classnames": "2.2.3",

@ -11,6 +11,7 @@ import { trigger } from '../../shims/events';
import { SessionHtmlRenderer } from './SessionHTMLRenderer'; import { SessionHtmlRenderer } from './SessionHTMLRenderer';
import { SessionIdEditable } from './SessionIdEditable'; import { SessionIdEditable } from './SessionIdEditable';
import { SessionSpinner } from './SessionSpinner'; import { SessionSpinner } from './SessionSpinner';
import { StringUtils } from '../../session/utils';
enum SignInMode { enum SignInMode {
Default, Default,
@ -178,7 +179,7 @@ export class RegistrationTabs extends React.Component<{}, State> {
'hex' 'hex'
).toArrayBuffer(); ).toArrayBuffer();
const keyPair = await window.libsignal.Curve.async.createKeyPair(seed); 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({ this.setState({
generatedMnemonicSeed: mnemonic, generatedMnemonicSeed: mnemonic,

@ -7,7 +7,7 @@ import {
} from '../../../js/modules/data'; } from '../../../js/modules/data';
import { PrimaryPubKey, PubKey, SecondaryPubKey } from '../types'; import { PrimaryPubKey, PubKey, SecondaryPubKey } from '../types';
import { UserUtil } from '../../util'; 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. 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, primaryDevicePubKey,
secondaryDevicePubKey, secondaryDevicePubKey,
requestSignature: BufferUtils.base64toUint8Array(requestSignature) requestSignature: StringUtils.encode(requestSignature, 'base64'),
.buffer, grantSignature: StringUtils.encode(grantSignature, 'base64'),
grantSignature: BufferUtils.base64toUint8Array(grantSignature).buffer,
}) })
); );
} }

@ -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);
} }

@ -5,6 +5,7 @@ import { PairingAuthorisation } from '../../../../js/modules/data';
import { MultiDeviceProtocol } from '../../../session/protocols'; import { MultiDeviceProtocol } from '../../../session/protocols';
import { PubKey } from '../../../session/types'; import { PubKey } from '../../../session/types';
import { UserUtil } from '../../../util'; import { UserUtil } from '../../../util';
import { StringUtils } from '../../../session/utils';
function generateFakeAuthorisations( function generateFakeAuthorisations(
primary: PubKey, primary: PubKey,
@ -112,7 +113,7 @@ describe('MultiDeviceProtocol', () => {
} = authorisations[0]; } = authorisations[0];
expect(primaryDevicePubKey).to.equal(networkAuth.primaryDevicePubKey); expect(primaryDevicePubKey).to.equal(networkAuth.primaryDevicePubKey);
expect(secondaryDevicePubKey).to.equal(networkAuth.secondaryDevicePubKey); expect(secondaryDevicePubKey).to.equal(networkAuth.secondaryDevicePubKey);
expect(Buffer.from(requestSignature).toString('base64')).to.equal( expect(StringUtils.decode(requestSignature, 'base64')).to.equal(
networkAuth.requestSignature networkAuth.requestSignature
); );
expect(grantSignature).to.not.equal( expect(grantSignature).to.not.equal(
@ -120,7 +121,7 @@ describe('MultiDeviceProtocol', () => {
'Grant signature should not be undefined.' 'Grant signature should not be undefined.'
); );
// tslint:disable-next-line: no-non-null-assertion // 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 networkAuth.grantSignature
); );
}); });

@ -1,11 +1,12 @@
import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol'; import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol';
import { SignalService } from '../../../../protobuf'; import { SignalService } from '../../../../protobuf';
import { StringUtils } from '../../../../session/utils';
export class FallBackSessionCipherStub { export class FallBackSessionCipherStub {
public async encrypt(buffer: ArrayBuffer): Promise<CipherTextObject> { public async encrypt(buffer: ArrayBuffer): Promise<CipherTextObject> {
return { return {
type: SignalService.Envelope.Type.SESSION_REQUEST, type: SignalService.Envelope.Type.SESSION_REQUEST,
body: Buffer.from(buffer).toString('binary'), body: StringUtils.decode(buffer, 'binary'),
}; };
} }
} }

@ -1,6 +1,7 @@
import { SignalService } from '../../../../protobuf'; import { SignalService } from '../../../../protobuf';
import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol'; import { CipherTextObject } from '../../../../../libtextsecure/libsignal-protocol';
import { SecretSessionCipherInterface } from '../../../../../js/modules/metadata/SecretSessionCipher'; import { SecretSessionCipherInterface } from '../../../../../js/modules/metadata/SecretSessionCipher';
import { StringUtils } from '../../../../session/utils';
export class SecretSessionCipherStub implements SecretSessionCipherInterface { export class SecretSessionCipherStub implements SecretSessionCipherInterface {
public async encrypt( public async encrypt(
@ -10,7 +11,7 @@ export class SecretSessionCipherStub implements SecretSessionCipherInterface {
): Promise<ArrayBuffer> { ): Promise<ArrayBuffer> {
const { body } = innerEncryptedMessage; const { body } = innerEncryptedMessage;
return Buffer.from(body, 'binary').buffer; return StringUtils.encode(body, 'binary');
} }
public async decrypt( public async decrypt(

@ -3,6 +3,7 @@ import {
SessionCipher, SessionCipher,
} from '../../../../../libtextsecure/libsignal-protocol'; } from '../../../../../libtextsecure/libsignal-protocol';
import { SignalService } from '../../../../protobuf'; import { SignalService } from '../../../../protobuf';
import { StringUtils } from '../../../../session/utils';
export class SessionCipherStub implements SessionCipher { export class SessionCipherStub implements SessionCipher {
public storage: any; public storage: any;
@ -17,7 +18,7 @@ export class SessionCipherStub implements SessionCipher {
): Promise<CipherTextObject> { ): Promise<CipherTextObject> {
return { return {
type: SignalService.Envelope.Type.CIPHERTEXT, type: SignalService.Envelope.Type.CIPHERTEXT,
body: Buffer.from(buffer).toString('binary'), body: StringUtils.decode(buffer, 'binary'),
}; };
} }

@ -171,6 +171,14 @@
"@types/jquery" "*" "@types/jquery" "*"
"@types/underscore" "*" "@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": "@types/chai-as-promised@^7.1.2":
version "7.1.2" version "7.1.2"
resolved "https://registry.yarnpkg.com/@types/chai-as-promised/-/chai-as-promised-7.1.2.tgz#2f564420e81eaf8650169e5a3a6b93e096e5068b" 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" resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.106.tgz#6093e9a02aa567ddecfe9afadca89e53e5dce4dd"
integrity sha512-tOSvCVrvSqFZ4A/qrqqm6p37GZoawsZtoR0SJhlF7EonNZUgrn8FfT+RNQ11h+NUpMt6QVe36033f3qEKBwfWA== 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": "@types/long@^3.0.32":
version "3.0.32" version "3.0.32"
resolved "https://registry.yarnpkg.com/@types/long/-/long-3.0.32.tgz#f4e5af31e9e9b196d8e5fca8a5e2e20aa3d60b69" resolved "https://registry.yarnpkg.com/@types/long/-/long-3.0.32.tgz#f4e5af31e9e9b196d8e5fca8a5e2e20aa3d60b69"

Loading…
Cancel
Save