1414 * limitations under the License.
1515 */
1616
17- import { ClientEvent , createClient , IndexedDBCryptoStore , IndexedDBStore , MatrixClient , SyncState } from 'matrix-js-sdk' ;
17+ import { ClientEvent , MatrixClient , SyncState } from 'matrix-js-sdk' ;
1818import React , { useEffect , useState } from 'react' ;
1919import MatrixClientContext from '../context/MatrixClientContext' ;
20- import { cacheSecretStorageKey , cryptoCallbacks } from '../utils/secretStorageKeys' ;
20+ import { cacheSecretStorageKey } from '../utils/secretStorageKeys' ;
2121import { CryptoApi , decodeRecoveryKey } from 'matrix-js-sdk/lib/crypto-api' ;
22+ import defaultConfigClient from '../utils/client' ;
2223
2324
2425interface Props {
@@ -55,28 +56,10 @@ const MatrixClientProvider = ({
5556 // Initialize the client
5657 if ( ! accessToken || ! userId || ! deviceId ) return ;
5758
58- const indexedDBStore = new IndexedDBStore ( {
59- indexedDB : global . indexedDB ,
60- localStorage : global . localStorage ,
61- dbName : 'web-sync-store' ,
62- } ) ;
63-
64- const client = createClient ( {
65- baseUrl,
66- accessToken,
67- userId,
68- store : indexedDBStore ,
69- cryptoStore : new IndexedDBCryptoStore ( global . indexedDB , 'crypto-store' ) ,
70- deviceId,
71- timelineSupport : true ,
72- cryptoCallbacks,
73- verificationMethods : [
74- 'm.sas.v1' ,
75- ] ,
76- } ) ;
59+ const client = defaultConfigClient ( baseUrl , accessToken , userId , deviceId ) ;
7760
7861 ( async ( ) => {
79- await indexedDBStore . startup ( ) ;
62+ await client . store . startup ( ) ;
8063
8164 if ( enableCrypto ) {
8265 // Setup e2ee
@@ -94,39 +77,21 @@ const MatrixClientProvider = ({
9477
9578 // Start syncing
9679 await client . startClient ( { lazyLoadMembers : true } ) ;
97- } ) ( ) ;
98-
99- const handleStateChange = ( state : SyncState ) => {
100- // Make the client available after the first sync has completed
101- if ( state === SyncState . Prepared ) {
102- setMx ( client ) ;
103- }
104- } ;
10580
106- client . on ( ClientEvent . Sync , handleStateChange ) ;
107- return ( ) => {
108- client . removeListener ( ClientEvent . Sync , handleStateChange ) ;
109- // Clean up matrix client on unmount
110- client . stopClient ( ) ;
111- } ;
112- } , [ baseUrl , enableCrypto , rustCryptoStoreKeyFn , accessToken , userId , deviceId ] ) ;
113-
114- useEffect ( ( ) => {
115- // Add recovery key and enable
116- if ( enableKeyBackup && recoveryKeyFn && mx ) {
117- ( async ( ) => {
81+ // Activate Key Backup
82+ if ( enableCrypto && enableKeyBackup && recoveryKeyFn ) {
11883 const recoveryKey = await recoveryKeyFn ?.( ) ;
11984
12085 // Validate the recovery key
12186 let recoveryKeyValid = false ;
12287
12388 if ( recoveryKey ) {
12489 const decodedRecoveryKey = decodeRecoveryKey ( recoveryKey ) ;
125- const keyId = await mx ? .secretStorage . getDefaultKeyId ( ) ;
126- const secretStorageKeyTuple = await mx ? .secretStorage . getKey ( keyId ) ;
90+ const keyId = await client . secretStorage . getDefaultKeyId ( ) ;
91+ const secretStorageKeyTuple = await client . secretStorage . getKey ( keyId ) ;
12792 if ( keyId && secretStorageKeyTuple ) {
12893 const [ , keyInfo ] = secretStorageKeyTuple ;
129- recoveryKeyValid = await mx . secretStorage . checkKey ( decodedRecoveryKey , keyInfo ) ;
94+ recoveryKeyValid = await client . secretStorage . checkKey ( decodedRecoveryKey , keyInfo ) ;
13095
13196 if ( recoveryKeyValid ) {
13297 // cache the recovery key if it's valid
@@ -136,41 +101,63 @@ const MatrixClientProvider = ({
136101 }
137102
138103 if ( recoveryKeyValid ) {
139- const hasKeyBackup = ( await cryptoApi ?. checkKeyBackupAndEnable ( ) ) !== null ;
104+ const crypto = client . getCrypto ( ) ;
105+ const hasKeyBackup = ( await crypto ?. checkKeyBackupAndEnable ( ) ) !== null ;
140106 if ( hasKeyBackup ) {
141- await cryptoApi ?. loadSessionBackupPrivateKeyFromSecretStorage ( ) ;
107+ await crypto ?. loadSessionBackupPrivateKeyFromSecretStorage ( ) ;
142108 }
143109 }
144- } ) ( ) ;
145- }
146- } , [ enableKeyBackup , mx , cryptoApi , recoveryKeyFn ] ) ;
110+ }
147111
148- useEffect ( ( ) => {
149- if ( enableCrossSigning ) {
150- ( async ( ) => {
112+ // Activate cross-signing
113+ if ( enableCrypto && enableCrossSigning ) {
151114 // Cache missing cross-signing keys locally, and setup cross-signing
152- await cryptoApi ?. userHasCrossSigningKeys ( mx ?. getUserId ( ) || undefined , true ) ;
153- await cryptoApi ?. bootstrapCrossSigning ( { } ) ;
154- } ) ( ) ;
155- }
156- } , [ enableCrossSigning , mx , cryptoApi ] ) ;
115+ const crypto = client . getCrypto ( ) ;
116+ await crypto ?. userHasCrossSigningKeys ( client . getUserId ( ) || undefined , true ) ;
117+ await crypto ?. bootstrapCrossSigning ( { } ) ;
118+ }
157119
158- useEffect ( ( ) => {
159- if ( enableDeviceDehydration ) {
160- ( async ( ) => {
120+ // Activate device dehydration
121+ if ( enableCrypto && enableKeyBackup && enableCrossSigning && enableDeviceDehydration ) {
161122 // If supported, rehydrate from existing device (if exists) and start regular device dehydration
162- const dehydrationSupported = await cryptoApi ?. isDehydrationSupported ( ) ;
123+ const crypto = client . getCrypto ( ) ;
124+ const dehydrationSupported = await crypto ?. isDehydrationSupported ( ) ;
163125 if ( dehydrationSupported ) {
164126 try {
165- await cryptoApi ?. startDehydration ( ) ;
127+ await crypto ?. startDehydration ( ) ;
166128 } catch {
167129 // create new dehydration key if dehydration fails to start
168- await cryptoApi ?. startDehydration ( true ) ;
130+ await crypto ?. startDehydration ( true ) ;
169131 }
170132 }
171- } ) ( ) ;
172- }
173- } , [ enableDeviceDehydration , cryptoApi ] ) ;
133+ }
134+ } ) ( ) ;
135+
136+ const handleStateChange = ( state : SyncState ) => {
137+ // Make the client available after the first sync has completed
138+ if ( state === SyncState . Prepared ) {
139+ setMx ( client ) ;
140+ }
141+ } ;
142+
143+ client . on ( ClientEvent . Sync , handleStateChange ) ;
144+ return ( ) => {
145+ client . removeListener ( ClientEvent . Sync , handleStateChange ) ;
146+ // Clean up matrix client on unmount
147+ client . stopClient ( ) ;
148+ } ;
149+ } , [
150+ baseUrl ,
151+ enableCrypto ,
152+ enableKeyBackup ,
153+ enableCrossSigning ,
154+ enableDeviceDehydration ,
155+ recoveryKeyFn ,
156+ rustCryptoStoreKeyFn ,
157+ accessToken ,
158+ userId ,
159+ deviceId
160+ ] ) ;
174161
175162 return (
176163 < MatrixClientContext . Provider value = { {
0 commit comments