import { Injectable } from '@angular/core' ; import { AccountService } from './account.service' ; import { Account , Address , AddressPrefix } from '@signumjs/core' ; import { generateMasterKeys , PassPhraseGenerator } from '@signumjs/crypto' ; import { NetworkService } from '../../network/network.service' ; @ Injectable ( ) export class CreateService { private phrase : string ; private salt : string ; private seed : any [ ] ; private account : Account ; private pin : string ; private step = 0 ; constructor ( private accountService , private networkService ) { } public getAccount ( ) { return this . account ; } public setPin ( pin ) { this . pin = pin ; } public getPin ( ) { return this . pin ; } public setSeed ( seed ) { this . seed = seed ; } public getSeed ( ) { return this . seed ; } public setSalt ( salt ) { this . salt = salt ; } public getSalt ( ) { return this . salt ; } public getPhrase ( ) { return this . phrase ; } public previousStep ( ) { this . step = Math . max ( 0 , this . step - 1 ) ; } public nextStep ( ) { this . step += 1 ; } public setStep ( step ) { this . step = step ; } public getStep ( ) { return this . step ; } public isSeedGenerated ( ) { return this . seed && this . seed . length > 0 ; } public isAccountGenerated ( ) { return this . account !== undefined ; } public createActiveAccount ( ) { return this . accountService . createActiveAccount ( { passphrase : this . getPhrase ( ) , pin : this . pin } ) ; } public createPassiveAccount ( address ) { return this . accountService . createOfflineAccount ( address ) ; } public reset ( ) { this . phrase = '' ; this . seed = undefined ; this . salt = undefined ; this . account = undefined ; setTimeout ( x => { this . step = 0 ; } , 0 ) ; } async generateAccount ( passphrase ? ) { this . phrase = passphrase ; if ( ! passphrase ) { const passphraseGenerator = new PassPhraseGenerator ( ) ; const wordlist = await passphraseGenerator . generatePassPhrase ( this . seed ) ; this . phrase = wordlist . concat ( this . salt ) . join ( ' ' ) ; } else { this . phrase = passphrase ; } const keys = generateMasterKeys ( this . phrase ) ; const address = Address . fromPublicKey ( keys . publicKey , this . networkService . isMainNet ( ) ? AddressPrefix . MainNet : AddressPrefix . TestNet ) ; this . account = new Account ( ) ; this . account . keys = keys ; this . account . accountRS = address . getReedSolomonAddress ( ) ; this . account . account = address . getNumericId ( ) ; return this . account ; } }