import { BN , SignedState , SignedStateWithHash , State , hashState , simpleEthAllocation , SignatureEntry , makeAddress , } from '@statechannels/wallet-core' ; import _ from 'lodash' ; import { flow } from 'fp-ts/lib/function' ; import { utils } from 'ethers' ; import { SigningWallet } from '../../../models/signing-wallet' ; import { addHash } from '../../../state-utils' ; import { defaultTestWalletConfig } from '../../../config' ; import { Fixture , fixture , overwriteOutcome } from './utils' ; import { alice , bob } from './participants' ; import { alice as aliceWallet } from './signing-wallets' ; const defaultState = { appData : '0x' , appDefinition : makeAddress ( '0x000000000000000000000000000000000000adef' ) , isFinal : false , turnNum : 0 , outcome : simpleEthAllocation ( [ { destination : alice ( ) . destination , amount : BN . from ( 1 ) } , { destination : bob ( ) . destination , amount : BN . from ( 3 ) } , ] ) , participants : [ alice ( ) , bob ( ) ] , channelNonce : 1 , chainId : utils . hexlify ( defaultTestWalletConfig ( ) . networkConfiguration . chainNetworkID ) , challengeDuration : 9001 , } ; const signatureCache = { } ; const _signState = ( s , sw ) : SignatureEntry => { const key = `${ sw . privateKey }-${ hashState ( s ) }` ; return ( signatureCache [ key ] = signatureCache [ key ] || sw . signState ( s ) ) ; } ; export const createState = fixture ( defaultState , overwriteOutcome ) ; const addSignatures = ( wallets ) => ( s ) : SignedState => ( { ... s , signatures : wallets . map ( sw => _signState ( s , sw ) ) , } ) ; export const stateSignedBy = ( signingWallets = [ aliceWallet ( ) ] ) : Fixture < SignedState > => fixture < SignedState > ( _ . merge ( { signatures : [ ] } , defaultState ) , flow ( overwriteOutcome , addSignatures ( signingWallets ) ) ) ; export const stateWithHashSignedBy = ( signingWallets = [ aliceWallet ( ) ] ) : Fixture < SignedStateWithHash > => fixture ( createState ( ) as SignedStateWithHash , flow ( overwriteOutcome , addSignatures ( signingWallets ) , addHash ) ) ;