Skip to content

Instantly share code, notes, and snippets.

@WietseWind WietseWind/index.js
Last active Jun 28, 2019

Embed
What would you like to do?
Simple ILP Payment Pointer
#!/usr/bin/env node
const makePlugin = require('ilp-plugin')
const { Server } = require('ilp-protocol-stream')
const Koa = require('koa')
const app = new Koa()
const crypto = require('crypto')
process.on('SIGINT', () => {
console.log('Hi')
})
async function run () {
console.log('Connecting to moneyd...')
const streamPlugin = makePlugin()
await streamPlugin.connect()
const streamServer = new Server({
plugin: streamPlugin,
serverSecret: crypto.randomBytes(32)
})
streamServer.on('connection', connection => {
let thisConnectionTimeout
let totalDrops = 0
console.log('+ New connection')
connection.on('stream', stream => {
stream.setReceiveMax(1000000000)
stream.on('money', amount => {
if (totalDrops === 0) {
console.log(`<<< Stream ON ${connection.connectionTag} >>>>`)
}
clearTimeout(thisConnectionTimeout)
thisConnectionTimeout = setTimeout(() => {
totalDrops = Math.max(connection.totalReceived, totalDrops)
console.log(`<<< Stream OFF ${connection.connectionTag} >>>>`)
console.log(`# Connection closed`)
console.log(' > value:', `${totalDrops} = ${totalDrops/1000000} XRP`)
console.log(' > sourceAccount:', connection.sourceAccount)
console.log(' > destinationAccount:', connection.destinationAccount)
console.log(' > connectionTag:', connection.connectionTag)
connection.destroy().then(() => {
console.log(` - Connection ${connection.connectionTag} cleaned up`)
}).catch(console.error)
}, timeoutSeconds * 1000)
totalDrops += parseInt(amount)
console.log(` » Got packet for ${amount} units - Sum: ${Math.max(connection.totalReceived, totalDrops)} drops (${Math.max(connection.totalReceived, totalDrops)/1000000} XRP)`)
})
})
})
await streamServer.listen()
console.log('Created Receiver...')
async function handleSPSP (ctx, next) {
console.log(`Request at domain ${ctx.host} with path ${ctx.originalUrl}`)
if (ctx.get('Accept').indexOf('application/spsp4+json') !== -1) {
const details = streamServer.generateAddressAndSecret('someconnectiontag')
/**
* Parse the domain / path / ... and assign a connection tag, used to match the received drops.
**/
ctx.body = {
destination_account: details.destinationAccount,
shared_secret: details.sharedSecret.toString('base64')
}
ctx.set('Content-Type', 'application/spsp4+json')
ctx.set('Access-Control-Allow-Origin', '*')
} else {
ctx.status = 404
ctx.body = `ILP Deposit Endpoint\n\nPlease send ILP payments here.`;
return next()
}
}
app
.use(handleSPSP)
.listen(1337)
console.log('Listening at localhost:1337 :)')
}
run()
.catch(e => {
console.error('##ERROR', e)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.