import { Address , SignedState , isSimpleAllocation , makeDestination , } from '@statechannels/wallet-core' ; import _ from 'lodash' ; import { Destination } from '../../type-aliases' ; import { SimpleAllocationOutcome } from './outcome' ; export class State { private state : SignedState ; constructor ( state ) { this . state = state ; } get turnNum ( ) : number { return this . state . turnNum ; } public signedBy ( address ) { return ! ! this . state . signatures . find ( sig => sig . signer == address ) ; } public get signerIndices ( ) : number [ ] { const res = [ ] ; if ( this . signedBy ( this . state . participants [ 0 ] . signingAddress ) ) res . push ( 0 ) ; if ( this . signedBy ( this . state . participants [ 1 ] . signingAddress ) ) res . push ( 1 ) ; return res ; } public get fullySigned ( ) : boolean { return _ . every ( this . state . participants , p => this . signedBy ( p . signingAddress ) ) ; } public get participantDestinations ( ) : Destination [ ] { return this . state . participants . map ( p => makeDestination ( p . destination ) ) ; } public get simpleAllocationOutcome ( ) : SimpleAllocationOutcome | undefined { if ( isSimpleAllocation ( this . state . outcome ) ) { return new SimpleAllocationOutcome ( this . state . outcome ) ; } else { return undefined ; } } public advanceToOutcome ( outcome ) { return new State ( { ... this . state , turnNum : this . turnNum + 1 , outcome : outcome . toLegacyAllocation , } ) ; } public get signedState ( ) : SignedState { return this . state ; } }