import { Component } from '@angular/core' ; import { PopoverController , Platform , ToastController , AlertController } from '@ionic/angular' ; import { MnemonicSecret } from '../../models/secret' ; import { ErrorCategory , handleErrorLocal } from '../../services/error-handler/error-handler.service' ; import { NavigationService } from '../../services/navigation/navigation.service' ; import { SecretsService } from '../../services/secrets/secrets.service' ; import { SecretEditPopoverComponent } from './secret-edit-popover/secret-edit-popover.component' ; import { TranslateService } from '@ngx-translate/core' ; import { ClipboardService } from '@airgap/angular-core' ; export enum SecretEditAction { SET_RECOVERY_KEY } @ Component ( { selector : 'airgap-secret-edit' , templateUrl : './secret-edit.page.html' , styleUrls : [ './secret-edit.page.scss' ] } ) export class SecretEditPage { public isGenerating : boolean = false ; public isAndroid : boolean = false ; public secret : MnemonicSecret ; constructor ( private readonly popoverCtrl , private readonly toastCtrl , private readonly alertCtrl , private readonly translateService , private readonly clipboardService , private readonly secretsService , private readonly navigationService , private readonly platform ) { if ( this . navigationService . getState ( ) ) { this . secret = this . navigationService . getState ( ) . secret ; this . isAndroid = this . platform . is ( 'android' ) ; this . perform ( this . navigationService . getState ( ) . action ) ; } } public async confirm ( ) { try { await this . secretsService . addOrUpdateSecret ( this . secret ) ; } catch ( error ) { handleErrorLocal ( ErrorCategory . SECURE_STORAGE ) ( error ) ; return ; } await this . dismiss ( ) ; this . navigationService . route ( '/account-add' ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_NAVIGATION ) ) ; } public async dismiss ( ) { try { return this . navigationService . routeToAccountsTab ( ) ; } catch ( error ) { return false ; } } public goToSocialRecoverySetup ( ) { this . navigationService . routeWithState ( '/social-recovery-setup' , { secret : this . secret } ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_NAVIGATION ) ) ; } public goToWalletInteraction ( ) { this . navigationService . routeWithState ( '/interaction-selection-settings' , { secret : this . secret , isEdit : true } ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_NAVIGATION ) ) ; } public goToBip85ChildSeed ( ) { this . navigationService . routeWithState ( '/bip85-generate' , { secret : this . secret } ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_NAVIGATION ) ) ; } public async resetRecoveryPassword ( ) { try { const recoveryKey = await this . secretsService . resetRecoveryPassword ( this . secret ) ; this . showRecoveryKeyAlert ( recoveryKey ) ; } catch ( e ) { this . showToast ( 'secret-edit.secret-recovery-key.reset-error' ) ; } } public async showMnemonic ( ) { const alert = await this . alertCtrl . create ( { header : this . translateService . instant ( 'secret-edit.show-mnemonic.alert.title' ) , message : this . translateService . instant ( 'secret-edit.show-mnemonic.alert.message' ) , backdropDismiss : false , inputs : [ { name : 'understood' , type : 'checkbox' , label : this . translateService . instant ( 'secret-edit.show-mnemonic.alert.understood' ) , value : 'understood' , checked : false } ] , buttons : [ { text : 'Cancel' , role : 'cancel' } , { text : 'Ok' , handler : async ( result ) => { if ( result . includes ( 'understood' ) ) { const entropy = await this . secretsService . retrieveEntropyForSecret ( this . secret ) ; const secret = new MnemonicSecret ( entropy ) ; this . navigationService . routeWithState ( 'secret-show' , { secret : secret } ) . catch ( ( err ) => console . error ( err ) ) ; } } } ] } ) ; alert . present ( ) ; } public async presentEditPopover ( event ) { const popover = await this . popoverCtrl . create ( { component : SecretEditPopoverComponent , componentProps : { secret : this . secret , onDelete : ( ) : void => { this . dismiss ( ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_MODAL ) ) ; } } , event , translucent : true } ) ; popover . present ( ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_MODAL ) ) ; } private perform ( action ) { switch ( action ) { case SecretEditAction . SET_RECOVERY_KEY : this . resetRecoveryPassword ( ) ; break ; } } private async showToast ( message ) { const toast = await this . toastCtrl . create ( { message : this . translateService . instant ( message ) , duration : 1000 , position : 'top' , buttons : [ { text : 'Ok' , role : 'cancel' } ] } ) ; toast . present ( ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_ALERT ) ) ; } private async showRecoveryKeyAlert ( recoveryKey ) { const alert = await this . alertCtrl . create ( { header : this . translateService . instant ( 'secret-edit.secret-recovery-key.alert.title' ) , subHeader : this . translateService . instant ( 'secret-edit.secret-recovery-key.description' ) , message : recoveryKey , buttons : [ { text : this . translateService . instant ( 'secret-edit.secret-recovery-key.alert.copy' ) , handler : ( ) => { this . clipboardService . copy ( recoveryKey ) ; this . showToast ( 'secret-edit.secret-recovery-key.copied' ) ; } } , { text : this . translateService . instant ( 'secret-edit.secret-recovery-key.alert.done' ) , handler : ( ) => { } } ] } ) ; alert . present ( ) . catch ( handleErrorLocal ( ErrorCategory . IONIC_ALERT ) ) ; } }