import { AppIdentity } from "@counterfactual/types" ; import * as chai from "chai" ; import { solidity } from "ethereum-waffle" ; import { BigNumber , BigNumberish , defaultAbiCoder , joinSignature , keccak256 , recoverAddress , Signature , solidityPack } from "ethers/utils" ; export const expect = chai . use ( solidity ) . expect ; export const computeAppChallengeHash = ( id , appStateHash , versionNumber , timeout ) => keccak256 ( solidityPack ( [ "bytes1" , "bytes32" , "uint256" , "uint256" , "bytes32" ] , [ "0x19" , id , versionNumber , timeout , appStateHash ] ) ) ; export const computeActionHash = ( turnTaker , previousState , action , versionNumber ) => keccak256 ( solidityPack ( [ "bytes1" , "address" , "bytes" , "bytes" , "uint256" ] , [ "0x19" , turnTaker , previousState , action , versionNumber ] ) ) ; export class AppIdentityTestClass { get identityHash ( ) : string { return keccak256 ( defaultAbiCoder . encode ( [ "uint256" , "address[]" ] , [ this . channelNonce , this . participants ] ) ) ; } get appIdentity ( ) : AppIdentity { return { participants : this . participants , appDefinition : this . appDefinition , defaultTimeout : this . defaultTimeout , channelNonce : this . channelNonce } ; } constructor ( readonly participants , readonly appDefinition , readonly defaultTimeout , readonly channelNonce ) { } } param signatures export function signaturesToBytes ( ... signatures ) { return signatures . map ( joinSignature ) . map ( s => s . substr ( 2 ) ) . reduce ( ( acc , v ) => acc + v , "0x" ) ; } param signatures export function sortSignaturesBySignerAddress ( digest , signatures ) { const ret = signatures . slice ( ) ; ret . sort ( ( sigA , sigB ) => { const addrA = recoverAddress ( digest , signaturesToBytes ( sigA ) ) ; const addrB = recoverAddress ( digest , signaturesToBytes ( sigB ) ) ; return new BigNumber ( addrA ) . lt ( addrB ) ? - 1 : 1 ; } ) ; return ret ; } param signatures export function signaturesToBytesSortedBySignerAddress ( digest , ... signatures ) { return signaturesToBytes ( ... sortSignaturesBySignerAddress ( digest , signatures ) ) ; }