import { Component , Input , OnInit , OnDestroy , ChangeDetectorRef } from '@angular/core' ; import { Wallet } from '../wallet/wallet' ; import { ZilliqaService } from '../zilliqa.service' ; @ Component ( { selector : 'app-create' , templateUrl : './create.component.html' , styleUrls : [ './create.component.css' ] } ) export class CreateComponent implements OnInit , OnDestroy { state : number ; generatedPrivateKey : string ; uploadedWallet : File = null ; walletDecryptKey : string ; walletFilename : string ; wallet : Wallet ; userPubkey : string ; loading : boolean ; @ Input ( ) importPrivateKey = '' ; constructor ( private zilliqaService , private ref ) { this . zilliqaService . resetWallet ( ) ; this . state = 0 ; this . wallet = new Wallet ( ) ; this . loading = false ; } ngOnInit ( ) { this . state = 0 ; this . wallet = new Wallet ( ) ; this . loading = false ; this . walletFilename = '' ; this . userPubkey = '' ; } ngOnDestroy ( ) { this . wallet = new Wallet ( ) ; this . generatedPrivateKey = '' ; this . loading = false ; this . walletFilename = '' ; } setState ( newState ) { if ( newState == 0 ) { this . wallet = new Wallet ( ) ; this . generatedPrivateKey = '' ; this . loading = false ; this . walletFilename = '' ; } if ( [ 7 , 8 , 9 ] . indexOf ( newState ) > - 1 ) { this . uploadedWallet = null ; this . walletDecryptKey = '' ; } this . state = newState ; if ( newState == 2 ) { this . zilliqaService . triggerPopup ( ) ; } } generateWallet ( ) { this . generatedPrivateKey = this . zilliqaService . createWallet ( ) ; this . wallet = this . zilliqaService . getWallet ( ) ; this . userPubkey = this . zilliqaService . getPublicKeyfromPrivateKey ( ) ; this . setState ( 2 ) ; } importWallet ( ) { let that = this ; this . zilliqaService . importWallet ( this . importPrivateKey ) . then ( function ( data ) { if ( data . result ) that . setState ( 6 ) ; else that . setState ( 7 ) ; } , function ( data ) { that . setState ( 10 ) ; } ) ; } invalidPrivateKey ( ) { return ( this . importPrivateKey . length > 0 && ! ( this . importPrivateKey . match ( /^[0-9a-fA-F]{64}$/ ) ) ) ; } invalidImportBtn ( ) { return ! ( this . importPrivateKey . match ( /^[0-9a-fA-F]{64}$/ ) ) ; } selectWallet ( files ) { this . uploadedWallet = files . item ( 0 ) ; this . walletFilename = files . item ( 0 ) . name ; let reader = new FileReader ( ) ; let that = this ; reader . onload = function ( event ) { let contents = event . target [ 'result' ] ; that . zilliqaService . uploadWalletFile ( contents ) ; } ; reader . readAsText ( this . uploadedWallet ) ; } decryptWallet ( ) { if ( ! ( this . zilliqaService . checkEncryptedWallet ( ) ) ) { this . setState ( 9 ) ; } this . loading = true ; this . ref . detectChanges ( ) ; setTimeout ( ( ) => { this . decryptWalletAsync ( ) ; } , 100 ) ; } decryptWalletAsync ( ) { let that = this ; this . zilliqaService . decryptWalletFile ( this . walletDecryptKey ) . then ( ( data ) => { that . loading = false ; that . zilliqaService . importWallet ( data . result ) . then ( ( data1 ) => { if ( data1 . result ) { that . setState ( 6 ) ; } else { that . setState ( 8 ) ; } } , function ( data ) { that . setState ( 10 ) ; } ) ; } ) ; } }