@@ -2,6 +2,8 @@ import * as lowdb from "lowdb";
22import * as FileSync from "lowdb/adapters/FileSync" ;
33import * as mkdirp from "mkdirp" ;
44import * as path from "path" ;
5+ import { stat , rename , mkdir } from "fs/promises" ;
6+ import { PathLike } from "fs" ;
57import * as sha512 from "hash.js/lib/hash/sha/512" ;
68import * as sha256 from "hash.js/lib/hash/sha/256" ;
79import { StoreType as RustSdkCryptoStoreType } from "@matrix-org/matrix-sdk-crypto-nodejs" ;
@@ -12,6 +14,10 @@ import { ICryptoRoomInformation } from "../e2ee/ICryptoRoomInformation";
1214
1315export { RustSdkCryptoStoreType } ;
1416
17+ async function doesFileExist ( path : PathLike ) {
18+ return stat ( path ) . then ( ( ) => true ) . catch ( ( ) => false ) ;
19+ }
20+
1521/**
1622 * A crypto storage provider for the file-based rust-sdk store.
1723 * @category Storage providers
@@ -40,6 +46,26 @@ export class RustSdkCryptoStorageProvider implements ICryptoStorageProvider {
4046 } ) ;
4147 }
4248
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+
4369 public async getDeviceId ( ) : Promise < string > {
4470 return this . db . get ( 'deviceId' ) . value ( ) ;
4571 }
@@ -75,7 +101,7 @@ export class RustSdkAppserviceCryptoStorageProvider extends RustSdkCryptoStorage
75101
76102 public storageForUser ( userId : string ) : ICryptoStorageProvider {
77103 // 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 ) ;
80106 }
81107}
0 commit comments