import { calculateChannelId , SignedStateVarsWithHash } from '@statechannels/wallet-core' ; import _ from 'lodash' ; import { flow } from 'fp-ts/lib/function' ; import { Channel , RequiredColumns } from '../../../models/channel' ; import { Fixture , fixture , DeepPartial } from '../../../engine/__test__/fixtures/utils' ; import { addHash , dropNonVariables , addChannelId , addHashes } from '../../../state-utils' ; import { alice , bob } from '../../../engine/__test__/fixtures/signing-wallets' ; import { createState , stateSignedBy } from '../../../engine/__test__/fixtures/states' ; import { SigningWallet } from '../../signing-wallet' ; export const channel = ( props ? ) => { const { appDefinition , chainId , challengeDuration , channelNonce , participants } = createState ( ) ; const defaults = { appDefinition , channelNonce , chainId , challengeDuration , participants , signingAddress : alice ( ) . address , vars : [ ] , fundingStrategy : 'Direct' , isLedgerChannel : false , } ; const columns = _ . merge ( defaults , props ) ; columns . vars . map ( s => ( s = dropNonVariables ( addHash ( { ... columns , ... s } ) ) ) ) ; ( columns as any ) . channelId = calculateChannelId ( columns ) ; const channel = Channel . fromJson ( { ... columns } ) ; return channel ; } ; export const channelWithVars = fixture < Channel > ( channel ( { vars : [ addHash ( stateSignedBy ( ) ( ) ) ] } ) ) ; const signVars = ( signingWallets ) => ( channel ) : Channel => { const { channelConstants , vars } = channel ; vars . map ( state => ( state . signatures = signingWallets . map ( sw => sw . signState ( { ... channelConstants , ... state } ) ) ) ) ; return channel ; } ; function overwriteVars ( result , props ? ) { if ( props ?. vars ) result . vars = props . vars ; return result ; } export const withSupportedState = ( signingWallets = [ alice ( ) , bob ( ) ] ) : Fixture < Channel > => fixture ( channel ( { signingWallet : signingWallets . length > 0 ? signingWallets [ 0 ] : undefined } ) , flow ( overwriteVars , addHashes , signVars ( signingWallets ) , addChannelId ) ) ;