import EthereumAccount from '../account' ; import { PluginInstance , PluginServices } from '../types/plugin' ; import MiniAccountsPlugin from 'ilp-plugin-mini-accounts' ; import { ServerOptions } from 'ws' ; import { IldcpResponse } from 'ilp-protocol-ildcp' ; import { BtpPacket , BtpSubProtocol } from 'ilp-plugin-btp' ; import { IlpPacket , IlpPrepare , Type , isPrepare } from 'ilp-packet' ; export interface MiniAccountsOpts { port ? : number ; wsOpts ? : ServerOptions ; debugHostIldcpInfo ? : IldcpResponse ; allowedOrigins ? : string [ ] ; } export interface EthereumServerOpts extends MiniAccountsOpts { getAccount : ( accountName : string ) => EthereumAccount ; loadAccount : ( accountName : string ) => Promise < EthereumAccount > ; } export class EthereumServerPlugin extends MiniAccountsPlugin implements PluginInstance { private getAccount : ( address : string ) => EthereumAccount ; private loadAccount : ( address : string ) => Promise < EthereumAccount > ; constructor ( { getAccount , loadAccount , ... opts } , api ) { super ( opts , api ) ; this . getAccount = ( address ) => getAccount ( this . ilpAddressToAccount ( address ) ) ; this . loadAccount = ( address ) => loadAccount ( this . ilpAddressToAccount ( address ) ) ; } _sendMessage ( accountName , message ) { return this . _call ( this . _prefix + accountName , message ) ; } async _connect ( address , message ) { await this . loadAccount ( address ) ; } _handleCustomData = async ( from , message ) : Promise < BtpSubProtocol [ ] > => { return this . getAccount ( from ) . handleData ( message ) ; } ; _handlePrepareResponse = async ( destination , responsePacket , preparePacket ) => { if ( isPrepare ( responsePacket . data ) ) { throw new Error ( 'Received PREPARE in response to PREPARE' ) ; } return this . getAccount ( destination ) . handlePrepareResponse ( preparePacket . data , responsePacket . data ) ; } ; async sendMoney ( ) { throw new Error ( 'sendMoney is not supported: use plugin balance configuration' ) ; } async _close ( from ) { return this . getAccount ( from ) . disconnect ( ) ; } }