import { Injectable , Inject } from '@angular/core' ; import { SecurityUtilsPlugin } from 'src/app/capacitor-plugins/definitions' ; import { SECURITY_UTILS_PLUGIN } from 'src/app/capacitor-plugins/injection-tokens' ; export interface SecureStorage { init ( ) : Promise < void > ; setItem ( key , value ) : Promise < void > ; getItem ( key ) : Promise < any > ; setupRecoveryPassword ( key , value ) : Promise < any > ; removeItem ( key ) : Promise < void > ; } @ Injectable ( { providedIn : 'root' } ) export class SecureStorageService { constructor ( @ Inject ( SECURITY_UTILS_PLUGIN ) private readonly securityUtils ) { } public isDeviceSecure ( ) { return this . securityUtils . isDeviceSecure ( { alias : 'airgap-secure-storage' , isParanoia : false } ) ; } public secureDevice ( ) { return this . securityUtils . secureDevice ( { alias : 'airgap-secure-storage' , isParanoia : false } ) ; } public async get ( alias , isParanoia ) { const securityUtils = this . securityUtils ; await securityUtils . initStorage ( { alias , isParanoia } ) ; return { init ( ) { return securityUtils . initStorage ( { alias , isParanoia } ) ; } , setItem ( key , value ) { return securityUtils . setItem ( { alias , isParanoia , key , value } ) ; } , setupRecoveryPassword ( key , value ) { return securityUtils . setupRecoveryPassword ( { alias , isParanoia , key , value } ) ; } , getItem ( key ) { return securityUtils . getItem ( { alias , isParanoia , key } ) . catch ( ( error ) => { let errorMessage ; if ( typeof error === 'string' ) { errorMessage = error ; } else if ( error instanceof Object && typeof ( error as any ) . message === 'string' ) { errorMessage = ( error as any ) . message ; } if ( errorMessage && errorMessage . toLowerCase ( ) . includes ( 'item corrupted' ) ) { throw new Error ( 'Could not read from the secure storage.' ) ; } if ( errorMessage && errorMessage . toLowerCase ( ) . includes ( 'wrong paranoia password' ) ) { throw new Error ( 'Wrong passphrase.' ) ; } throw error ; } ) ; } , removeItem ( key ) { return securityUtils . removeItem ( { alias , isParanoia , key } ) ; } } ; } public async wipe ( ) { await this . securityUtils . destroy ( ) ; } }