@@ -11,7 +11,7 @@ import React, { createRef } from "react";
1111import FileSaver from "file-saver" ;
1212import { logger } from "matrix-js-sdk/src/logger" ;
1313import { AuthDict , CrossSigningKeys , MatrixError , UIAFlow , UIAResponse } from "matrix-js-sdk/src/matrix" ;
14- import { GeneratedSecretStorageKey , KeyBackupInfo } from "matrix-js-sdk/src/crypto-api" ;
14+ import { GeneratedSecretStorageKey } from "matrix-js-sdk/src/crypto-api" ;
1515import classNames from "classnames" ;
1616import CheckmarkIcon from "@vector-im/compound-design-tokens/assets/web/icons/check" ;
1717
@@ -70,16 +70,6 @@ interface IState {
7070 downloaded : boolean ;
7171 setPassphrase : boolean ;
7272
73- /** Information on the current key backup version, as returned by the server.
74- *
75- * `null` could mean any of:
76- * * we haven't yet requested the data from the server.
77- * * we were unable to reach the server.
78- * * the server returned key backup version data we didn't understand or was malformed.
79- * * there is actually no backup on the server.
80- */
81- backupInfo : KeyBackupInfo | null ;
82-
8373 // does the server offer a UI auth flow with just m.login.password
8474 // for /keys/device_signing/upload?
8575 canUploadKeysWithPasswordOnly : boolean | null ;
@@ -131,15 +121,17 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
131121 this . queryKeyUploadAuth ( ) ;
132122 }
133123
124+ const keyFromCustomisations = ModuleRunner . instance . extensions . cryptoSetup . createSecretStorageKey ( ) ;
125+ const phase = keyFromCustomisations ? Phase . Loading : Phase . ChooseKeyPassphrase ;
126+
134127 this . state = {
135- phase : Phase . Loading ,
128+ phase,
136129 passPhrase : "" ,
137130 passPhraseValid : false ,
138131 passPhraseConfirm : "" ,
139132 copied : false ,
140133 downloaded : false ,
141134 setPassphrase : false ,
142- backupInfo : null ,
143135 // does the server offer a UI auth flow with just m.login.password
144136 // for /keys/device_signing/upload?
145137 accountPasswordCorrect : null ,
@@ -149,40 +141,15 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
149141 accountPassword,
150142 } ;
151143
152- this . getInitialPhase ( ) ;
153- }
154-
155- private getInitialPhase ( ) : void {
156- const keyFromCustomisations = ModuleRunner . instance . extensions . cryptoSetup . createSecretStorageKey ( ) ;
157- if ( keyFromCustomisations ) {
158- logger . log ( "CryptoSetupExtension: Created key via extension, jumping to bootstrap step" ) ;
159- this . recoveryKey = {
160- privateKey : keyFromCustomisations ,
161- } ;
162- this . bootstrapSecretStorage ( ) ;
163- return ;
164- }
165-
166- this . fetchBackupInfo ( ) ;
144+ if ( keyFromCustomisations ) this . initExtension ( keyFromCustomisations ) ;
167145 }
168146
169- /**
170- * Attempt to get information on the current backup from the server, and update the state.
171- *
172- * Updates {@link IState.backupInfo} and set the phase to {@link Phase.ChooseKeyPassphrase} if successful.
173- */
174- private async fetchBackupInfo ( ) : Promise < void > {
175- try {
176- const cli = MatrixClientPeg . safeGet ( ) ;
177- const backupInfo = await cli . getKeyBackupVersion ( ) ;
178- this . setState ( {
179- phase : Phase . ChooseKeyPassphrase ,
180- backupInfo,
181- } ) ;
182- } catch ( e ) {
183- console . error ( "Error fetching backup data from server" , e ) ;
184- this . setState ( { phase : Phase . LoadError } ) ;
185- }
147+ private initExtension ( keyFromCustomisations : Uint8Array ) : void {
148+ logger . log ( "CryptoSetupExtension: Created key via extension, jumping to bootstrap step" ) ;
149+ this . recoveryKey = {
150+ privateKey : keyFromCustomisations ,
151+ } ;
152+ this . bootstrapSecretStorage ( ) ;
186153 }
187154
188155 private async queryKeyUploadAuth ( ) : Promise < void > {
@@ -296,16 +263,28 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
296263 } ;
297264
298265 private bootstrapSecretStorage = async ( ) : Promise < void > => {
266+ const cli = MatrixClientPeg . safeGet ( ) ;
267+ const crypto = cli . getCrypto ( ) ! ;
268+ const { forceReset } = this . props ;
269+
270+ let backupInfo ;
271+ // First, we try to get the keybackup info
272+ if ( ! forceReset ) {
273+ try {
274+ this . setState ( { phase : Phase . Loading } ) ;
275+ backupInfo = await cli . getKeyBackupVersion ( ) ;
276+ } catch ( e ) {
277+ logger . error ( "Error fetching backup data from server" , e ) ;
278+ this . setState ( { phase : Phase . LoadError } ) ;
279+ return ;
280+ }
281+ }
282+
299283 this . setState ( {
300284 phase : Phase . Storing ,
301285 error : undefined ,
302286 } ) ;
303287
304- const cli = MatrixClientPeg . safeGet ( ) ;
305- const crypto = cli . getCrypto ( ) ! ;
306-
307- const { forceReset } = this . props ;
308-
309288 try {
310289 if ( forceReset ) {
311290 logger . log ( "Forcing secret storage reset" ) ;
@@ -327,7 +306,7 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
327306 } ) ;
328307 await crypto . bootstrapSecretStorage ( {
329308 createSecretStorageKey : async ( ) => this . recoveryKey ! ,
330- setupNewKeyBackup : ! this . state . backupInfo ,
309+ setupNewKeyBackup : ! backupInfo ,
331310 } ) ;
332311 }
333312 await initialiseDehydration ( true ) ;
@@ -346,8 +325,7 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
346325 } ;
347326
348327 private onLoadRetryClick = ( ) : void => {
349- this . setState ( { phase : Phase . Loading } ) ;
350- this . fetchBackupInfo ( ) ;
328+ this . bootstrapSecretStorage ( ) ;
351329 } ;
352330
353331 private onShowKeyContinueClick = ( ) : void => {
0 commit comments