import { ChannelWallet as BrowserWallet } from '@statechannels/browser-wallet' ; import { isJsonRpcNotification , Message , PushMessageRequest } from '../../client-api-schema/src' ; import { MessageHandler , MessageServiceFactory , MessageServiceInterface } from '../../server-wallet/src/message-service/types' ; export class BrowserServerMessageService implements MessageServiceInterface { constructor ( private browserWallet ) { } public async send ( message ) { const convertedMessages = message . map ( generatePushMessage ) ; await Promise . all ( convertedMessages . map ( m => this . browserWallet . pushMessage ( m , 'dummyDomain' ) ) ) ; } public registerPeer ( _peerUrl ) { throw new Error ( 'Not Implemented' ) ; } public async destroy ( ) { this . browserWallet . destroy ( ) ; } public static createFactory ( browserWallet ) { return ( handler ) => { browserWallet . onSendMessage ( m => { if ( isJsonRpcNotification ( m ) ) { handler ( m . params as Message ) ; } } ) ; return new BrowserServerMessageService ( browserWallet ) ; } ; } } function generatePushMessage ( messageParams ) { return { jsonrpc : '2.0' , id : 111111111 , method : 'PushMessage' , params : messageParams } ; }