import { constants , providers , Signature } from 'ethers' ; import { State } from './contract/state' ; import * as forceMoveTrans from './contract/transaction-creators/force-move' ; import * as nitroAdjudicatorTrans from './contract/transaction-creators/nitro-adjudicator' ; import { getStateSignerAddress , SignedState } from './signatures' ; export const MAGIC_ADDRESS_INDICATING_ETH = constants . AddressZero ; export const MAX_TX_DATA_SIZE = 128 * 1024 ; export const NITRO_MAX_GAS = 6000000 ; param signedStates returns export function createChallengeTransaction ( signedStates , challengePrivateKey ) { const { states , signatures , whoSignedWhat } = createSignatureArguments ( signedStates ) ; return forceMoveTrans . createChallengeTransaction ( states , signatures , whoSignedWhat , challengePrivateKey ) ; } param signedStates returns export function createRespondTransaction ( challengeState , response ) { if ( ! challengeState ) { throw new Error ( 'No active challenge in challenge state' ) ; } return forceMoveTrans . createRespondTransaction ( { challengeState , responseState : response . state , responseSignature : response . signature , } ) ; } param signedStates returns export function createCheckpointTransaction ( signedStates ) { const { states , signatures , whoSignedWhat } = createSignatureArguments ( signedStates ) ; return forceMoveTrans . createCheckpointTransaction ( { states , signatures , whoSignedWhat , } ) ; } param signedStates returns export function createConcludeAndTransferAllAssetsTransaction ( signedStates ) { const { states , signatures , whoSignedWhat } = createSignatureArguments ( signedStates ) ; return nitroAdjudicatorTrans . createConcludeAndTransferAllAssetsTransaction ( states , signatures , whoSignedWhat ) ; } param signedStates returns export function createTransferAllAssetsTransaction ( state ) { return nitroAdjudicatorTrans . createTransferAllAssetsTransaction ( state ) ; } param signedStates returns export function createConcludeTransaction ( conclusionProof ) { const { states , signatures , whoSignedWhat } = createSignatureArguments ( conclusionProof ) ; return forceMoveTrans . createConcludeTransaction ( states , signatures , whoSignedWhat ) ; } param signedStates returns export function createSignatureArguments ( signedStates ) { const { participants } = signedStates [ 0 ] . state . channel ; const states = [ ] ; const whoSignedWhat = new Array < number > ( participants . length ) ; const uniqueSignedStates = signedStates . filter ( ( s , i , a ) => a . indexOf ( s ) === i ) ; const uniqueStates = uniqueSignedStates . map ( s => s . state ) . filter ( ( s , i , a ) => a . indexOf ( s ) === i ) ; const signatures = new Array < Signature > ( uniqueStates . length ) ; for ( let i = 0 ; i < uniqueStates . length ; i ++ ) { states . push ( uniqueStates [ i ] ) ; const signedStatesForUniqueState = uniqueSignedStates . filter ( s => s . state === uniqueStates [ i ] ) ; for ( const ss of signedStatesForUniqueState ) { const participantIndex = participants . indexOf ( getStateSignerAddress ( ss ) ) ; signatures [ participantIndex ] = ss . signature ; whoSignedWhat [ participantIndex ] = i ; } } return { states , signatures , whoSignedWhat , } ; }