import EthereumAccount , { generateBtpRequestId } from '../account' ; import BtpPlugin , { BtpPacket , BtpSubProtocol , IlpPluginBtpConstructorOptions } from 'ilp-plugin-btp' ; import { TYPE_MESSAGE , MIME_APPLICATION_OCTET_STREAM } from 'btp-packet' ; import { PluginInstance , PluginServices } from '../types/plugin' ; import { isPrepare , deserializeIlpPrepare , deserializeIlpReply } from 'ilp-packet' ; export interface EthereumClientOpts extends IlpPluginBtpConstructorOptions { getAccount : ( accountName : string ) => EthereumAccount ; loadAccount : ( accountName : string ) => Promise < EthereumAccount > ; } export class EthereumClientPlugin extends BtpPlugin implements PluginInstance { private getAccount : ( ) => EthereumAccount ; private loadAccount : ( ) => Promise < EthereumAccount > ; constructor ( { getAccount , loadAccount , ... opts } , { log } ) { super ( opts , { log } ) ; this . getAccount = ( ) => getAccount ( 'peer' ) ; this . loadAccount = ( ) => loadAccount ( 'peer' ) ; } _sendMessage ( accountName , message ) { return this . _call ( '' , message ) ; } async _connect ( ) { await this . loadAccount ( ) ; } _handleData ( from , message ) { return this . getAccount ( ) . handleData ( message ) ; } async sendData ( buffer ) { const prepare = deserializeIlpPrepare ( buffer ) ; if ( ! isPrepare ( prepare ) ) { throw new Error ( 'Packet must be a PREPARE' ) ; } const response = await this . _call ( '' , { type : TYPE_MESSAGE , requestId : await generateBtpRequestId ( ) , data : { protocolData : [ { protocolName : 'ilp' , contentType : MIME_APPLICATION_OCTET_STREAM , data : buffer } ] } } ) ; const ilpResponse = response . protocolData . find ( p => p . protocolName === 'ilp' ) ; if ( ilpResponse ) { const reply = deserializeIlpReply ( ilpResponse . data ) ; this . getAccount ( ) . handlePrepareResponse ( prepare , reply ) ; return ilpResponse . data ; } return Buffer . alloc ( 0 ) ; } sendMoney ( amount ) { return this . getAccount ( ) . sendMoney ( amount ) ; } _disconnect ( ) { return this . getAccount ( ) . disconnect ( ) ; } }