import { DeviceShareDescription , IModule , ITKeyApi , ShareStore , StringifiedType } from "@tkey/common-types" ; import { browser } from "webextension-polyfill-ts" ; export const CHROME_EXTENSION_STORAGE_MODULE_NAME = "chromeExtensionStorage" ; export default class ChromeExtensionStorageModule implements IModule { moduleName : string ; tbSDK : ITKeyApi ; constructor ( ) { this . moduleName = CHROME_EXTENSION_STORAGE_MODULE_NAME ; } setModuleReferences ( tbSDK ) { this . tbSDK = tbSDK ; this . tbSDK . _setDeviceStorage ( this . storeDeviceShare . bind ( this ) ) ; } async initialize ( ) { } async storeDeviceShare ( deviceShareStore , customDeviceInfo ? ) { await this . storeShareOnChromeExtensionStorage ( deviceShareStore ) ; const shareDescription = { module : this . moduleName , userAgent : window . navigator . userAgent , dateAdded : Date . now ( ) , } ; if ( customDeviceInfo ) { shareDescription . customDeviceInfo = JSON . stringify ( customDeviceInfo ) ; } await this . tbSDK . addShareDescription ( deviceShareStore . share . shareIndex . toString ( "hex" ) , JSON . stringify ( shareDescription ) , true ) ; } async storeShareOnChromeExtensionStorage ( share ) { const metadata = this . tbSDK . getMetadata ( ) ; const key = metadata . pubKey . x . toString ( "hex" ) ; return browser . storage . sync . set ( { [ key ] : JSON . stringify ( share ) } ) ; } async getStoreFromChromeExtensionStorage ( ) { const metadata = this . tbSDK . getMetadata ( ) ; const key = metadata . pubKey . x . toString ( "hex" ) ; const result = await browser . storage . sync . get ( key ) ; const verifierIdObj = JSON . parse ( result [ key ] ) ; await this . tbSDK . inputShareStoreSafe ( verifierIdObj ) ; return verifierIdObj ; } async inputShareFromChromeExtensionStorage ( ) { const castedShareStore = await this . getStoreFromChromeExtensionStorage ( ) ; const latestShareDetails = await this . tbSDK . catchupToLatestShare ( { shareStore : castedShareStore , includeLocalMetadataTransitions : true } ) ; this . tbSDK . inputShareStore ( latestShareDetails . latestShare ) ; } }