import * as ILDCP from 'ilp-protocol-ildcp' ; import * as IlpPacket from 'ilp-packet' ; import createLogger from 'ilp-logger' ; import './util/formatters' ; import { Connection , ConnectionOpts } from './connection' ; export { Connection } from './connection' ; export { DataAndMoneyStream } from './stream' ; export { Server , ServerOpts , createServer , GenerateAddressSecretOpts } from './server' ; export { createReceipt , decodeReceipt , verifyReceipt , Receipt , ReceiptOpts , ReceiptWithHMAC , RECEIPT_VERSION } from './util/receipt' ; export interface CreateConnectionOpts extends ConnectionOpts { /** ILP Address of the server */ destinationAccount : string ; /** Shared secret generated by the server */ sharedSecret : Buffer ; } /**
 * Create a [`Connection`] Connection to a [`Server`] Server using the `destinationAccount` and `sharedSecret` provided.
  export async function createConnection ( opts ) { const plugin = opts . plugin ; await plugin . connect ( ) ; const log = createLogger ( 'ilp-protocol-stream:Client' ) ; const { clientAddress , assetCode , assetScale } = await ILDCP . fetch ( plugin . sendData . bind ( plugin ) , { expiresAt : opts . getExpiry && opts . getExpiry ( 'peer.config' ) } ) ; const connection = await Connection . build ( { ... opts , sourceAccount : clientAddress , assetCode , assetScale , isServer : false , plugin } ) ; plugin . registerDataHandler ( async ( data ) : Promise < Buffer > => { let prepare ; try { prepare = IlpPacket . deserializeIlpPrepare ( data ) ; } catch ( err ) { log . error ( 'got data that is not an ILP Prepare packet: %h' , data ) ; return IlpPacket . serializeIlpReject ( { code : 'F00' , message : `Expected an ILP Prepare packet (type 12), but got packet with type: ${ data [ 0 ] }` , data : Buffer . alloc ( 0 ) , triggeredBy : clientAddress } ) ; } try { const fulfill = await connection . handlePrepare ( prepare ) ; return IlpPacket . serializeIlpFulfill ( fulfill ) ; } catch ( err ) { if ( ! err . ilpErrorCode ) { log . error ( 'error handling prepare:' , err ) ; } return IlpPacket . serializeIlpReject ( { code : err . ilpErrorCode || 'F00' , message : err . ilpErrorMessage || '' , data : err . ilpErrorData || Buffer . alloc ( 0 ) , triggeredBy : clientAddress } ) ; } } ) ; connection . once ( 'close' , ( ) => { plugin . deregisterDataHandler ( ) ; plugin . disconnect ( ) . then ( ( ) => log . info ( 'plugin disconnected' ) ) . catch ( ( err ) => log . error ( 'error disconnecting plugin:' , err ) ) ; } ) ; await connection . connect ( ) ; return connection ; }