@@ -2,6 +2,8 @@ import * as lowdb from "lowdb";
2
2
import * as FileSync from "lowdb/adapters/FileSync" ;
3
3
import * as mkdirp from "mkdirp" ;
4
4
import * as path from "path" ;
5
+ import { stat , rename , mkdir } from "fs/promises" ;
6
+ import { PathLike } from "fs" ;
5
7
import * as sha512 from "hash.js/lib/hash/sha/512" ;
6
8
import * as sha256 from "hash.js/lib/hash/sha/256" ;
7
9
import { StoreType as RustSdkCryptoStoreType } from "@matrix-org/matrix-sdk-crypto-nodejs" ;
@@ -12,6 +14,10 @@ import { ICryptoRoomInformation } from "../e2ee/ICryptoRoomInformation";
12
14
13
15
export { RustSdkCryptoStoreType } ;
14
16
17
+ async function doesFileExist ( path : PathLike ) {
18
+ return stat ( path ) . then ( ( ) => true ) . catch ( ( ) => false ) ;
19
+ }
20
+
15
21
/**
16
22
* A crypto storage provider for the file-based rust-sdk store.
17
23
* @category Storage providers
@@ -40,6 +46,26 @@ export class RustSdkCryptoStorageProvider implements ICryptoStorageProvider {
40
46
} ) ;
41
47
}
42
48
49
+ public async getMachineStoragePath ( deviceId : string ) : Promise < string > {
50
+ const newPath = path . join ( this . storagePath , sha256 ( ) . update ( deviceId ) . digest ( 'hex' ) ) ;
51
+ if ( await doesFileExist ( newPath ) ) {
52
+ // Already exists, short circuit.
53
+ return newPath ;
54
+ } // else: If the path does NOT exist we might need to perform a migration.
55
+
56
+ const legacyFilePath = path . join ( this . storagePath , 'matrix-sdk-crypto.sqlite3' ) ;
57
+ // XXX: Slightly gross cross-dependency file name expectations.
58
+ if ( await doesFileExist ( legacyFilePath ) === false ) {
59
+ // No machine files at all, we can skip.
60
+ return newPath ;
61
+ }
62
+
63
+ // We need to move the file.
64
+ await mkdir ( newPath ) ;
65
+ await rename ( legacyFilePath , path . join ( newPath , 'matrix-sdk-crypto.sqlite3' ) ) ;
66
+ return newPath ;
67
+ }
68
+
43
69
public async getDeviceId ( ) : Promise < string > {
44
70
return this . db . get ( 'deviceId' ) . value ( ) ;
45
71
}
@@ -75,7 +101,7 @@ export class RustSdkAppserviceCryptoStorageProvider extends RustSdkCryptoStorage
75
101
76
102
public storageForUser ( userId : string ) : ICryptoStorageProvider {
77
103
// sha256 because sha512 is a bit big for some operating systems
78
- const key = sha256 ( ) . update ( userId ) . digest ( 'hex' ) ;
79
- return new RustSdkCryptoStorageProvider ( path . join ( this . baseStoragePath , key ) , this . storageType ) ;
104
+ const storagePath = path . join ( this . baseStoragePath , sha256 ( ) . update ( userId ) . digest ( 'hex' ) ) ;
105
+ return new RustSdkCryptoStorageProvider ( storagePath , this . storageType ) ;
80
106
}
81
107
}
0 commit comments