-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·114 lines (102 loc) · 3.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env node
const chalk = require('chalk')
const getPort = require('get-port')
const makePlugin = require('ilp-plugin')
const localtunnel = require('localtunnel')
const { Server } = require('ilp-protocol-stream')
const PSK2 = require('ilp-protocol-psk2')
const Koa = require('koa')
const app = new Koa()
const crypto = require('crypto')
const name = crypto.randomBytes(8).toString('hex')
const argv = require('yargs')
.option('subdomain', {
default: name,
description: 'subdomain for localtunnel'
})
.option('localtunnel', {
default: true,
type: 'boolean',
description: 'use localtunnel'
})
.option('port', {
description: 'port to listen locally'
})
.help('help')
.argv
async function run () {
console.log('connecting...')
const pskPlugin = makePlugin()
const streamPlugin = makePlugin()
await streamPlugin.connect()
await pskPlugin.connect()
const port = argv.port || await getPort()
const streamServer = new Server({
plugin: streamPlugin,
serverSecret: crypto.randomBytes(32)
})
streamServer.on('connection', connection => {
connection.on('stream', stream => {
stream.setReceiveMax(10000000000000)
stream.on('money', amount => {
console.log('got packet for', amount, 'units')
})
})
})
await streamServer.listen()
// PSK2 is included for backwards compatibility
const pskReceiver = await PSK2.createReceiver({
plugin: pskPlugin,
paymentHandler: async params => {
console.log('got packet for', params.prepare.amount, 'units')
return params.acceptSingleChunk()
}
})
console.log('created receiver...')
async function handleSPSP (ctx, next) {
if (ctx.get('Accept').indexOf('application/spsp4+json') !== -1) {
const receiptNonce = ctx.headers['receipt-nonce'] ? Buffer.from(ctx.headers['receipt-nonce'], 'base64') : undefined
const receiptSecret = ctx.headers['receipt-secret'] ? Buffer.from(ctx.headers['receipt-secret'], 'base64') : undefined
const details = streamServer.generateAddressAndSecret({
receiptNonce,
receiptSecret
})
ctx.body = {
destination_account: details.destinationAccount,
shared_secret: details.sharedSecret.toString('base64'),
receipts_enabled: details.receiptsEnabled
}
ctx.set('Content-Type', 'application/spsp4+json')
ctx.set('Access-Control-Allow-Origin', '*')
} else if (ctx.get('Accept').indexOf('application/spsp+json') !== -1) {
const details = pskReceiver.generateAddressAndSecret()
ctx.body = {
destination_account: details.destinationAccount,
shared_secret: details.sharedSecret.toString('base64')
}
ctx.set('Content-Type', 'application/spsp+json')
ctx.set('Access-Control-Allow-Origin', '*')
} else {
return next()
}
}
app
.use(handleSPSP)
.listen(port)
console.log('listening on ' + port)
if (argv.localtunnel) {
localtunnel(port, { subdomain: argv.subdomain }, (err, tunnel) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(chalk.green('public at:', tunnel.url))
console.log(chalk.green('payment pointer is:', '$' + argv.subdomain + '.localtunnel.me'))
})
}
}
run()
.catch(e => {
console.error(e)
process.exit(1)
})