From ed9f4e713e7f0a0155782606f2dc4d0c62dcd692 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 20 Aug 2020 10:19:47 +1000 Subject: [PATCH] add type for mnemonic.js and replace some calls with recoveryPhrase --- libloki/modules/mnemonic.d.ts | 6 +++++ libtextsecure/account_manager.js | 6 ++--- ts/components/DevicePairingDialog.tsx | 2 +- ts/components/session/SessionSeedModal.tsx | 2 +- .../session/integration/add_contacts_itest.ts | 4 ++-- ts/test/session/integration/common.ts | 24 +++++++++---------- .../session/integration/link_device_itest.ts | 2 +- .../session/integration/open_group_itest.ts | 2 +- .../session/integration/registration_itest.ts | 6 ++--- .../session/integration/sender_keys_itest.ts | 6 ++--- ts/test/session/integration/settings_itest.ts | 2 +- ts/window.d.ts | 3 ++- 12 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 libloki/modules/mnemonic.d.ts diff --git a/libloki/modules/mnemonic.d.ts b/libloki/modules/mnemonic.d.ts new file mode 100644 index 000000000..75c47462c --- /dev/null +++ b/libloki/modules/mnemonic.d.ts @@ -0,0 +1,6 @@ +export interface RecoveryPhraseUtil { + mn_encode(str: string, wordset_name: string): string; + mn_decode(str: string, wordset_name: string): string; + get_languages(): Array; + pubkey_to_secret_words(pubKey?: string): string; +} diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 790584a5f..b30778621 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -127,7 +127,7 @@ return this.queueTask(() => generateKeypair().then(async identityKeyPair => createAccount(identityKeyPair) - .then(() => this.saveMnemonic(mnemonic)) + .then(() => this.saveRecoveryPhrase(mnemonic)) .then(clearSessionsAndPreKeys) .then(generateKeys) .then(confirmKeys) @@ -507,10 +507,10 @@ const hex = StringView.arrayBufferToHex(seed); return mnemonic.mn_encode(hex, language); }, - getCurrentMnemonic() { + getCurrentRecoveryPhrase() { return textsecure.storage.get('mnemonic'); }, - saveMnemonic(mnemonic) { + saveRecoveryPhrase(mnemonic) { return textsecure.storage.put('mnemonic', mnemonic); }, async registrationDone(number, displayName) { diff --git a/ts/components/DevicePairingDialog.tsx b/ts/components/DevicePairingDialog.tsx index f8a02cd60..fed01c0b8 100644 --- a/ts/components/DevicePairingDialog.tsx +++ b/ts/components/DevicePairingDialog.tsx @@ -73,7 +73,7 @@ export class DevicePairingDialog extends React.Component { public renderFilterRequestsView() { const { currentPubKey, accepted, deviceAlias } = this.state; - let secretWords: undefined; + let secretWords: string | undefined; if (currentPubKey) { secretWords = window.mnemonic.pubkey_to_secret_words(currentPubKey); } diff --git a/ts/components/session/SessionSeedModal.tsx b/ts/components/session/SessionSeedModal.tsx index efed75bf8..094a32258 100644 --- a/ts/components/session/SessionSeedModal.tsx +++ b/ts/components/session/SessionSeedModal.tsx @@ -197,7 +197,7 @@ export class SessionSeedModal extends React.Component { } const manager = await window.getAccountManager(); - const recoveryPhrase = manager.getCurrentMnemonic(); + const recoveryPhrase = manager.getCurrentRecoveryPhrase(); this.setState({ recoveryPhrase, diff --git a/ts/test/session/integration/add_contacts_itest.ts b/ts/test/session/integration/add_contacts_itest.ts index d644284b2..9c1b8429d 100644 --- a/ts/test/session/integration/add_contacts_itest.ts +++ b/ts/test/session/integration/add_contacts_itest.ts @@ -21,12 +21,12 @@ describe('Add contact', function() { await Common.stopStubSnodeServer(); const app1Props = { - mnemonic: Common.TEST_MNEMONIC1, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_1, displayName: Common.TEST_DISPLAY_NAME1, }; const app2Props = { - mnemonic: Common.TEST_MNEMONIC2, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_2, displayName: Common.TEST_DISPLAY_NAME2, }; diff --git a/ts/test/session/integration/common.ts b/ts/test/session/integration/common.ts index e10b93900..c642b4d31 100644 --- a/ts/test/session/integration/common.ts +++ b/ts/test/session/integration/common.ts @@ -32,19 +32,19 @@ const ENABLE_LOG = false; // tslint:disable-next-line: no-unnecessary-class export class Common { /* ************** USERS ****************** */ - public static readonly TEST_MNEMONIC1 = + public static readonly TEST_RECOVERY_PHRASE_1 = 'faxed mechanic mocked agony unrest loincloth pencil eccentric boyfriend oasis speedy ribbon faxed'; public static readonly TEST_PUBKEY1 = '0552b85a43fb992f6bdb122a5a379505a0b99a16f0628ab8840249e2a60e12a413'; public static readonly TEST_DISPLAY_NAME1 = 'tester_Alice'; - public static readonly TEST_MNEMONIC2 = + public static readonly TEST_RECOVERY_PHRASE_2 = 'guide inbound jerseys bays nouns basin sulking awkward stockpile ostrich ascend pylons ascend'; public static readonly TEST_PUBKEY2 = '054e1ca8681082dbd9aad1cf6fc89a32254e15cba50c75b5a73ac10a0b96bcbd2a'; public static readonly TEST_DISPLAY_NAME2 = 'tester_Bob'; - public static readonly TEST_MNEMONIC3 = + public static readonly TEST_RECOVERY_PHRASE_3 = 'alpine lukewarm oncoming blender kiwi fuel lobster upkeep vogue simplest gasp fully simplest'; public static readonly TEST_PUBKEY3 = '05f8662b6e83da5a31007cc3ded44c601f191e07999acb6db2314a896048d9036c'; @@ -210,19 +210,19 @@ export class Common { } public static async startAndStub({ - mnemonic, + recoveryPhrase, displayName, env = 'test-integration-session', }: { - mnemonic: string; + recoveryPhrase: string; displayName: string; env?: string; }) { const app = await Common.startAndAssureCleanedApp(env); await Common.startStubSnodeServer(); - if (mnemonic && displayName) { - await Common.restoreFromMnemonic(app, mnemonic, displayName); + if (recoveryPhrase && displayName) { + await Common.restoreFromRecoveryPhrase(app, recoveryPhrase, displayName); // not sure we need Common - rtharp. await Common.timeout(2000); } @@ -240,9 +240,9 @@ export class Common { return appN; } - public static async restoreFromMnemonic( + public static async restoreFromRecoveryPhrase( app: Application, - mnemonic: string, + recoveryPhrase: string, displayName: string ) { await app.client.element(RegistrationPage.registrationTabSignIn).click(); @@ -250,7 +250,7 @@ export class Common { await Common.setValueWrapper( app, RegistrationPage.recoveryPhraseInput, - mnemonic + recoveryPhrase ); await Common.setValueWrapper( @@ -305,13 +305,13 @@ export class Common { public static async startAppsAsFriends() { const app1Props = { - mnemonic: Common.TEST_MNEMONIC1, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_1, displayName: Common.TEST_DISPLAY_NAME1, stubSnode: true, }; const app2Props = { - mnemonic: Common.TEST_MNEMONIC2, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_2, displayName: Common.TEST_DISPLAY_NAME2, stubSnode: true, }; diff --git a/ts/test/session/integration/link_device_itest.ts b/ts/test/session/integration/link_device_itest.ts index 16a484c59..f1dbb13c1 100644 --- a/ts/test/session/integration/link_device_itest.ts +++ b/ts/test/session/integration/link_device_itest.ts @@ -21,7 +21,7 @@ describe('Link Device', function() { await Common.killallElectron(); const app1Props = { - mnemonic: Common.TEST_MNEMONIC1, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_1, displayName: Common.TEST_DISPLAY_NAME1, }; diff --git a/ts/test/session/integration/open_group_itest.ts b/ts/test/session/integration/open_group_itest.ts index ecf8acf07..588bdd160 100644 --- a/ts/test/session/integration/open_group_itest.ts +++ b/ts/test/session/integration/open_group_itest.ts @@ -18,7 +18,7 @@ describe('Open groups', function() { beforeEach(async () => { await Common.killallElectron(); const login = { - mnemonic: Common.TEST_MNEMONIC1, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_1, displayName: Common.TEST_DISPLAY_NAME1, }; app = await Common.startAndStub(login); diff --git a/ts/test/session/integration/registration_itest.ts b/ts/test/session/integration/registration_itest.ts index cbbcd28bb..6f23d2ea0 100644 --- a/ts/test/session/integration/registration_itest.ts +++ b/ts/test/session/integration/registration_itest.ts @@ -47,7 +47,7 @@ describe('Window Test and Login', function() { await app.client.element(RegistrationPage.restoreFromSeedMode).click(); await app.client .element(RegistrationPage.recoveryPhraseInput) - .setValue(Common.TEST_MNEMONIC1); + .setValue(Common.TEST_RECOVERY_PHRASE_1); await app.client .element(RegistrationPage.displayNameInput) .setValue(Common.TEST_DISPLAY_NAME1); @@ -56,7 +56,7 @@ describe('Window Test and Login', function() { await app.client .element(RegistrationPage.recoveryPhraseInput) .getValue() - .should.eventually.equal(Common.TEST_MNEMONIC1); + .should.eventually.equal(Common.TEST_RECOVERY_PHRASE_1); await app.client .element(RegistrationPage.displayNameInput) .getValue() @@ -107,7 +107,7 @@ describe('Window Test and Login', function() { it('registration: can delete account when logged in', async () => { // login as user1 const login = { - mnemonic: Common.TEST_MNEMONIC1, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_1, displayName: Common.TEST_DISPLAY_NAME1, }; app = await Common.startAndStub(login); diff --git a/ts/test/session/integration/sender_keys_itest.ts b/ts/test/session/integration/sender_keys_itest.ts index ba1b17783..d20e6492a 100644 --- a/ts/test/session/integration/sender_keys_itest.ts +++ b/ts/test/session/integration/sender_keys_itest.ts @@ -83,19 +83,19 @@ async function testThreeMembers() { // 1. Make three clients A, B, C const app1Props = { - mnemonic: Common.TEST_MNEMONIC1, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_1, displayName: Common.TEST_DISPLAY_NAME1, stubSnode: true, }; const app2Props = { - mnemonic: Common.TEST_MNEMONIC2, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_2, displayName: Common.TEST_DISPLAY_NAME2, stubSnode: true, }; const app3Props = { - mnemonic: Common.TEST_MNEMONIC3, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_3, displayName: Common.TEST_DISPLAY_NAME3, stubSnode: true, }; diff --git a/ts/test/session/integration/settings_itest.ts b/ts/test/session/integration/settings_itest.ts index d97a87ce5..eabdb6a0b 100644 --- a/ts/test/session/integration/settings_itest.ts +++ b/ts/test/session/integration/settings_itest.ts @@ -27,7 +27,7 @@ describe('Settings', function() { await Common.stopStubSnodeServer(); const appProps = { - mnemonic: Common.TEST_MNEMONIC1, + recoveryPhrase: Common.TEST_RECOVERY_PHRASE_1, displayName: Common.TEST_DISPLAY_NAME1, }; diff --git a/ts/window.d.ts b/ts/window.d.ts index d6b245677..bd5d5bfed 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -10,6 +10,7 @@ import { SwarmPolling } from './session/snode_api/swarmPolling'; import { LibTextsecure } from '../libtextsecure'; import { ConversationType } from '../js/modules/data'; +import { RecoveryPhraseUtil } from '../libloki/modules/mnemonic'; /* We declare window stuff here instead of global.d.ts because we are importing other declarations. @@ -67,7 +68,7 @@ declare global { lokiPublicChatAPI: LokiPublicChatFactoryInterface; lokiSnodeAPI: LokiSnodeAPI; lokiPublicChatAPI: LokiPublicChatFactoryAPI; - mnemonic: any; + mnemonic: RecoveryPhraseUtil; onLogin: any; passwordUtil: any; pushToast: any;