import { BigNumber , deserializeObject , objectToBuffer , serializeObject , } from '@pigi/core-utils' ; import { Message } from '../../types/serialization' ; import { AddressBalance , StateChannelMessage } from './examples' ; param message returns export const stateChannelMessageToString = ( message ) : string => { return serializeObject ( message ) ; } ; param message param messageSerializer returns export const messageToBuffer = ( message , messageSerializer = ( _ ) => '{}' ) : Buffer => { return objectToBuffer ( { channelID : message . channelID . toString ( ) , nonce : message . nonce , data : messageSerializer ( message . data ) , } ) ; } ; param message param messageSerializer returns export const messageToString = ( message , messageSerializer = ( _ ) => '{}' ) : string => { return serializeObject ( { channelID : message . channelID . toString ( ) , nonce : message . nonce , data : messageSerializer ( message . data ) , } ) ; } ; param buffer param messageDeserializer param functionParams returns export const deserializeBuffer = ( buffer , messageDeserializer = ( s ) => JSON . parse ( s ) , functionParams ? ) : any => { return messageDeserializer ( buffer . toString ( ) , functionParams ) ; } ; param message param messageDeserializer param functionParams returns export const deserializeMessageString = ( message , messageDeserializer = ( s ) => JSON . parse ( s ) , functionParams ? ) : any => { return messageDeserializer ( message , functionParams ) ; } ; param message param dataDeserializer returns export const deserializeMessage = ( message , dataDeserializer = ( d ) => d ) : Message => { const parsedObject = deserializeObject ( message ) ; return { channelID : parsedObject [ 'channelID' ] , nonce : 'nonce' in parsedObject ? new BigNumber ( parsedObject [ 'nonce' ] ) : undefined , data : dataDeserializer ( parsedObject [ 'data' ] ) , } ; } ; param message returns export const stateChannelMessageDeserializer = ( message ) : StateChannelMessage => { const deserialized = deserializeObject ( message ) ; const addressBalance = { } ; Object . entries ( deserialized [ 'addressBalance' ] ) . forEach ( ( [ address , balance ] ) => { addressBalance [ address ] = new BigNumber ( balance ) ; } ) ; return { addressBalance , } ; } ;