import { Component , OnInit , ChangeDetectorRef } from '@angular/core' ; import { Wallet } from '../wallet' ; import { ZilliqaService } from '../../zilliqa.service' ; @ Component ( { selector : 'app-walletbase' , templateUrl : './walletbase.component.html' , styleUrls : [ './walletbase.component.css' ] } ) export class WalletbaseComponent implements OnInit { privateKeyDisplay : string ; revealBtn : string ; walletEncryptPassphrase : string ; wallet : Wallet ; userPubkey : string ; loading : boolean ; constructor ( public zilliqaService , private ref ) { this . wallet = new Wallet ( ) ; this . privateKeyDisplay = '****************************************************************' ; this . revealBtn = 'Show' ; this . walletEncryptPassphrase = '' ; this . loading = false ; } ngOnInit ( ) { this . wallet = this . zilliqaService . getWallet ( ) ; this . userPubkey = this . zilliqaService . getPublicKeyfromPrivateKey ( ) ; this . zilliqaService . triggerPopup ( ) ; } revealPrivateKey ( ) { if ( this . privateKeyDisplay == undefined || this . privateKeyDisplay . length == 0 || this . privateKeyDisplay [ 0 ] != '*' ) { this . privateKeyDisplay = '****************************************************************' ; this . revealBtn = 'Show' ; } else { this . privateKeyDisplay = this . wallet . privateKey ; this . revealBtn = 'Hide' ; } } validPassphrase ( ) { return ( this . walletEncryptPassphrase && this . walletEncryptPassphrase . length >= 8 ) ; } getWalletFilename ( ) { return 'UTC--' + ( new Date ( ) ) . toJSON ( ) + '.0--' + this . wallet . address + '.json' ; } downloadWallet ( ) { this . loading = true ; this . ref . detectChanges ( ) ; setTimeout ( ( ) => { this . downloadWalletAsync ( ) ; } , 100 ) ; } downloadWalletAsync ( ) { let that = this ; this . zilliqaService . generateWalletJson ( this . walletEncryptPassphrase ) . then ( ( data ) => { let text = data . result ; let filename = that . getWalletFilename ( ) ; let element = document . createElement ( 'a' ) ; element . setAttribute ( 'href' , 'data:text/plain;charset=utf-8,' + encodeURIComponent ( text ) ) ; element . setAttribute ( 'download' , filename ) ; element . style . display = 'none' ; document . body . appendChild ( element ) ; element . click ( ) ; document . body . removeChild ( element ) ; that . loading = false ; that . walletEncryptPassphrase = '' ; } ) ; } }