import { Account } from './account' ; import { DEFAULT_SCRYPT } from './consts' ; import { Identity } from './identity' ; /**
 * Class to manage Accounts and Identity
 */ export class Wallet { static parseJson ( json ) { return Wallet . parseJsonObj ( JSON . parse ( json ) ) ; } param obj static parseJsonObj ( obj ) { const wallet = new Wallet ( ) ; wallet . name = obj . name ; wallet . defaultOntid = obj . defaultOntid ; wallet . defaultAccountAddress = obj . defaultAccountAddress ; wallet . createTime = obj . createTime ; wallet . version = obj . version ; wallet . scrypt = obj . scrypt ; wallet . identities = obj . identities && ( obj . identities as any [ ] ) . map ( ( i ) => Identity . parseJsonObj ( i ) ) ; wallet . accounts = obj . accounts && ( obj . accounts as any [ ] ) . map ( ( a ) => Account . parseJsonObj ( a ) ) ; wallet . extra = obj . extra ; return wallet ; } static fromWalletFile ( obj ) { const wallet = Wallet . parseJsonObj ( obj ) ; return wallet ; } example param name static create ( name ) { const wallet = new Wallet ( ) ; wallet . name = name ; wallet . createTime = ( new Date ( ) ) . toISOString ( ) ; wallet . version = '1.0' ; wallet . scrypt = { n : DEFAULT_SCRYPT . cost , r : DEFAULT_SCRYPT . blockSize , p : DEFAULT_SCRYPT . parallel , dkLen : DEFAULT_SCRYPT . size } ; return wallet ; } name : string ; defaultOntid : string = '' ; defaultAccountAddress : string = '' ; createTime : string ; version : string ; scrypt : { n : number ; r : number ; p : number ; dkLen : number ; } ; identities : Identity [ ] = [ ] ; accounts : Account [ ] = [ ] ; extra : any ; addAccount ( account ) { for ( const ac of this . accounts ) { if ( ac . address . toBase58 ( ) === account . address . toBase58 ( ) ) { return ; } } this . accounts . push ( account ) ; } addIdentity ( identity ) { for ( const item of this . identities ) { if ( item . ontid === identity . ontid ) { return ; } } this . identities . push ( identity ) ; } setDefaultAccount ( address ) { this . defaultAccountAddress = address ; } setDefaultIdentity ( ontid ) { this . defaultOntid = ontid ; } toJson ( ) { return JSON . stringify ( this . toJsonObj ( ) ) ; } /**
     * Serializes to JSON object.
     *
     * Returned object will not be stringified.
     *
     */ toJsonObj ( ) { const obj = { name : this . name , defaultOntid : this . defaultOntid , defaultAccountAddress : this . defaultAccountAddress , createTime : this . createTime , version : this . version , scrypt : this . scrypt , identities : this . identities . map ( ( i ) => i . toJsonObj ( ) ) , accounts : this . accounts . map ( ( a ) => a . toJsonObj ( ) ) , extra : this . extra } ; return obj ; } signatureData ( ) { return '' ; } toWalletFile ( ) { const obj = this . toJsonObj ( ) ; return obj ; } }