import BigNumber from 'bignumber.js' ; import * as crypto from 'crypto' ; import * as ILDCP from 'ilp-protocol-ildcp' ; import * as SPSP from './lib/spsp' ; import * as STREAM from 'ilp-protocol-stream' ; import * as express from './extensions/express' ; import { JsonInvoice , InvoiceReceiver , Invoice } from './lib/invoice' ; import * as PluginApi from './lib/plugin' ; import { Receipt } from './lib/receipt' ; const createLogger = require ( 'ilp-logger' ) ; const log = createLogger ( 'ilp' ) ; export const DEFAULT_PLUGIN_MODULE = 'ilp-plugin-btp' ; param { * } pluginOptions param { * } pluginModuleName function createPlugin ( pluginOptions ? , pluginModuleName = DEFAULT_PLUGIN_MODULE ) { const envModuleName = process . env . ILP_PLUGIN || DEFAULT_PLUGIN_MODULE ; const envOptions = process . env . ILP_PLUGIN_OPTIONS || process . env . ILP_CREDENTIALS ; const moduleName = pluginModuleName || envModuleName ; let options = envOptions ? JSON . parse ( envOptions ) : { } ; if ( process . env . ILP_CREDENTIALS && ! process . env . ILP_PLUGIN_OPTIONS ) { log . warn ( `Loading options from environment var ILP_CREDENTIALS is deprecated, use ILP_PLUGIN_OPTIONS instead.` ) ; } if ( moduleName === 'ilp-plugin-btp' ) { const name = ( pluginOptions && pluginOptions . name ) || '' ; if ( name ) { log . warn ( `'pluginOptions.name' is deprecated. ` + `Please provide the correct options for the plugin. ` + `Example: '{ "server" : "btp+ws://<name>:<secret>@localhost:7768" }'` ) ; } else { if ( pluginOptions ) { options = pluginOptions ; } } if ( Object . keys ( options ) . length === 0 ) { options . server = `btp+ws://${ name }:${ crypto . randomBytes ( 16 ) . toString ( 'hex' ) }@localhost:7768` ; } } else { options = pluginOptions ; } const Plugin = require ( moduleName ) ; return new Plugin ( options ) as PluginApi . PluginV2 ; } param plugin async function fetchConfig ( plugin ) { if ( ! plugin . isConnected ) { throw Error ( 'Plugin must be connected to get config.' ) ; } return ILDCP . fetch ( plugin . sendData . bind ( plugin ) ) ; } param { * } amount param { * } reference param { * } pluginOrServer async function receive ( amount , reference , pluginOrServer = createPlugin ( ) ) { const server = ( pluginOrServer instanceof STREAM . Server ) ? pluginOrServer : await STREAM . createServer ( { plugin : pluginOrServer } ) ; return new InvoiceReceiver ( amount , reference , server ) ; } type PaymentPointerAndAmount = { amount : BigNumber . Value ; paymentPointer : string ; } ; /**
 */ async function pay ( payee , plugin = createPlugin ( ) ) { const paymentPointerReceiver = payee as PaymentPointerAndAmount ; if ( paymentPointerReceiver . paymentPointer ) { return SPSP . pay ( plugin , { receiver : paymentPointerReceiver . paymentPointer , sourceAmount : paymentPointerReceiver . amount } ) ; } else { const { destinationAccount , sharedSecret , amount } = payee as Invoice ; const connection = await STREAM . createConnection ( { destinationAccount , plugin , sharedSecret } ) ; const stream = connection . createStream ( ) ; await stream . sendTotal ( amount ) ; await connection . end ( ) ; return { sourceAccount : connection . sourceAccount , destinationAccount : destinationAccount , sent : { amount : new BigNumber ( connection . totalSent ) , assetCode : connection . sourceAssetCode , assetScale : connection . sourceAssetScale } , received : { amount : new BigNumber ( connection . totalDelivered ) , assetCode : connection . destinationAssetCode , assetScale : connection . destinationAssetScale } } ; } } export { ILDCP , STREAM , SPSP , PluginApi , express , InvoiceReceiver , JsonInvoice , Receipt , createLogger , createPlugin , fetchConfig , receive , pay } ;