Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Last active June 28, 2019 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WietseWind/272e7e62b2bf6d18b12e130da45180dd to your computer and use it in GitHub Desktop.
Save WietseWind/272e7e62b2bf6d18b12e130da45180dd to your computer and use it in GitHub Desktop.
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