import { ICryptoStorageProvider } from "./ICryptoStorageProvider" ; import { EncryptionEventContent } from "../models/events/EncryptionEvent" ; import * as lowdb from "lowdb" ; import * as FileSync from "lowdb/adapters/FileSync" ; import * as mkdirp from "mkdirp" ; import * as path from "path" ; import * as sha512 from "hash.js/lib/hash/sha/512" ; import * as sha256 from "hash.js/lib/hash/sha/256" ; import { IAppserviceCryptoStorageProvider } from "./IAppserviceStorageProvider" ; category export class RustSdkCryptoStorageProvider implements ICryptoStorageProvider { private db : any ; param { string } storagePath public constructor ( public readonly storagePath ) { this . storagePath = path . resolve ( this . storagePath ) ; mkdirp . sync ( storagePath ) ; const adapter = new FileSync ( path . join ( storagePath , "bot-sdk.json" ) ) ; this . db = lowdb ( adapter ) ; this . db . defaults ( { deviceId : null , rooms : { } , } ) ; } public async getDeviceId ( ) { return this . db . get ( 'deviceId' ) . value ( ) ; } public async setDeviceId ( deviceId ) { this . db . set ( 'deviceId' , deviceId ) . write ( ) ; } public async getRoom ( roomId ) { const key = sha512 ( ) . update ( roomId ) . digest ( 'hex' ) ; return this . db . get ( `rooms.${ key }` ) . value ( ) ; } public async storeRoom ( roomId , config ) { const key = sha512 ( ) . update ( roomId ) . digest ( 'hex' ) ; this . db . set ( `rooms.${ key }` , config ) . write ( ) ; } } category export class RustSdkAppserviceCryptoStorageProvider extends RustSdkCryptoStorageProvider implements IAppserviceCryptoStorageProvider { param { string } baseStoragePath public constructor ( private baseStoragePath ) { super ( path . join ( baseStoragePath , "_default" ) ) ; } public storageForUser ( userId ) { const key = sha256 ( ) . update ( userId ) . digest ( 'hex' ) ; return new RustSdkCryptoStorageProvider ( path . join ( this . baseStoragePath , key ) ) ; } }