aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2018-02-27 15:54:07 -0800
committerAdam Soltys <[email protected]>2018-02-27 15:54:07 -0800
commit0096f7c25f24df8959f8c0c4e469b4e70ff5db79 (patch)
tree709092fd415085723d096dc3035671487f043636
parentf18ed646b0e056179650118c1b02420809de0405 (diff)
downloadcoinos-server-0096f7c25f24df8959f8c0c4e469b4e70ff5db79.tar.gz
coinos-server-0096f7c25f24df8959f8c0c4e469b4e70ff5db79.zip
add faucet endpoint
-rw-r--r--index.js97
-rw-r--r--package.json5
-rw-r--r--yarn.lock75
3 files changed, 131 insertions, 46 deletions
diff --git a/index.js b/index.js
index b1be1871..d8840474 100644
--- a/index.js
+++ b/index.js
@@ -4,6 +4,7 @@ import bitcoin from 'bitcoinjs-lib'
import bodyParser from 'body-parser'
import compression from 'compression'
import cookieParser from 'cookie-parser'
+import core from 'bitcoin-core'
import cors from 'cors'
import dotenv from 'dotenv'
import express from 'express'
@@ -39,18 +40,23 @@ const l = console.log
const passport = require('./passport')(db)
const auth = passport.authenticate('jwt', { session: false })
+ const bc = new core({
+ username: 'adam',
+ password: 'MPJzfq97',
+ network: 'testnet',
+ })
+
const app = express()
app.enable('trust proxy')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cookieParser())
- app.use(cors())
+ app.use(cors({ credentials: true, origin: 'http://*:*' }))
app.use(compression())
app.use(passport.initialize())
- const server = require('http').createServer(app)
- const io = require('socket.io').listen(server)
- io.origins('http://localhost:8085')
+ const server = require('http').Server(app)
+ const io = require('socket.io')(server, { origins: '*:*' })
const invoices = lna.subscribeInvoices({})
invoices.on('data', msg => {
@@ -68,6 +74,12 @@ const l = console.log
zmqSock.subscribe('rawtx')
const seen = []
+ const channelpeers = [
+ '02fa77e0f4ca666f7d158c4bb6675d1436e339903a9feeeaacbd6e55021b98e7ee',
+ '039cc950286a8fa99218283d1adc2456e0d5e81be558da77dd6e85ba9a1fff5ad3',
+ '0231eee2441073c86d38f6085aedaf2bb7ad3d43af4c0e2669c1edd1a7d566ce31',
+ '022ea315e5052b152579e70a90bacfd6aa7420f2ce94674d4ca8da29d709bc70fd',
+ ]
zmqSock.on('message', async (topic, message, sequence) => {
const addresses = {}
@@ -116,36 +128,47 @@ const l = console.log
}
})
- app.post('/openchannel', async (req, res) => {
- let user = await db['User'].findOne({
- where: {
- username: req.body.username,
- }
- })
-
- let channel = lna.openChannelSync({
- node_pubkey_string: '022ea315e5052b152579e70a90bacfd6aa7420f2ce94674d4ca8da29d709bc70fd',
- local_funding_amount: user.balance,
- }, async (err, data) => {
- if (err) {
- l(err)
- return res.status(500).send(err)
- }
-
- user.balance = 0
- user.channel = reverse(data.funding_txid).toString('hex')
+ app.post('/openchannel', auth, async (req, res) => {
+ let pending = await lna.pendingChannels({}, meta)
+ let busypeers = pending.pending_open_channels.map(c => c.channel.remote_node_pub)
+ let peer = channelpeers.find(p => !busypeers.includes(p))
+
+ if (!peer) {
+ return res.status(500).send(
+ { error: 'All peers have pending channel opens' }
+ )
+ }
+
+ try {
+ let channel = await lna.openChannel({
+ node_pubkey: new Buffer(peer, 'hex'),
+ local_funding_amount: req.user.balance,
+ })
+
+ channel.on('data', async data => {
+ l(data)
+ req.user.channelbalance += req.user.balance
+ req.user.balance = 0
+ req.user.channel = reverse(data.chan_pending.txid).toString('hex')
+ await req.user.save()
+ res.send(data)
+ })
+ } catch (err) {
+ l(err)
+ return res.status(500).send(err)
+ }
+ })
- let list = await lnrpc.listChannels({}, meta)
- let channels = list.channels.filter(c => c.channel_point.includes(user.channel))
+ app.post('/closechannels', auth, async (req, res) => {
+ req.user.balance += req.user.channelbalance
+ req.user.channelbalance = 0
+ await req.user.save()
+ res.send(req.user)
+ })
- if (channels.length) {
- user.channelbalance = channels[0].local_balance
- }
-
- await user.save()
- res.json(data)
- })
+ app.post('/faucet', auth, async (req, res) => {
+ req.user.balance += bc.sendToAddress(req.user.address, 0.001)
})
app.post('/sendPayment', auth, (req, res) => {
@@ -154,7 +177,7 @@ const l = console.log
payments.on('data', async m => {
req.user.channelbalance -= m.value
await req.user.save()
- res.json(m)
+ res.send(m)
})
})
@@ -166,7 +189,7 @@ const l = console.log
payreq: invoice.payment_request,
})
- res.json(invoice)
+ res.send(invoice)
})
let fetchRates
@@ -178,7 +201,7 @@ const l = console.log
})()
app.get('/rates', (req, res) => {
- res.json(app.get('rates'))
+ res.send(app.get('rates'))
})
app.post('/login', async (req, res) => {
@@ -194,7 +217,7 @@ const l = console.log
let payload = { username: user.username }
let token = jwt.sign(payload, process.env.SECRET)
res.cookie('token', token, { expires: new Date(Date.now() + 9999999) })
- res.json({ user, token })
+ res.send({ user, token })
} else {
res.status(401).end()
}
@@ -204,13 +227,13 @@ const l = console.log
})
app.get('/secret', passport.authenticate('jwt', { session: false }), (req, res) => {
- res.json({message: 'Success! You can not see this without a token'})
+ res.send({message: 'Success! You can not see this without a token'})
})
app.get('/api/users/me',
passport.authenticate('basic', { session: false }),
function(req, res) {
- res.json({ id: req.user.id, username: req.user.username });
+ res.send({ id: req.user.id, username: req.user.username });
});
app.use('/graphql', bodyParser.json(), graphqlExpress({
diff --git a/package.json b/package.json
index 98199648..4eddb255 100644
--- a/package.json
+++ b/package.json
@@ -41,7 +41,7 @@
"http-proxy-middleware": "^0.17.2",
"jsonwebtoken": "^8.1.1",
"less": "2.7.1",
- "lnrpc": "https://github.com/asoltys/lnrpc#b32f349",
+ "lnrpc": "https://github.com/asoltys/lnrpc#ab71ae4",
"lodash": "^4.17.4",
"moment": "^2.10.6",
"mysql": "^2.15.0",
@@ -56,7 +56,8 @@
"sequelize": "^4.32.2",
"sequelize-auto": "^0.4.29",
"sequelize-cli": "^3.2.0",
- "socket.io-client": "^2.0.3",
+ "socket.io": "^2.0.4",
+ "socket.io-client": "^2.0.4",
"sql-template-strings": "^2.2.2",
"twilio": "^2.11.1",
"zeromq": "^4.6.0",
diff --git a/yarn.lock b/yarn.lock
index 948544a9..7c552c7e 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -233,6 +233,10 @@ [email protected]:
version "0.0.6"
resolved "https://registry.yarnpkg.com/arraybuffer.slice/-/arraybuffer.slice-0.0.6.tgz#f33b2159f0532a3f3107a272c0ccfbd1ad2979ca"
+arraybuffer.slice@~0.0.7:
+ version "0.0.7"
+ resolved "https://registry.yarnpkg.com/arraybuffer.slice/-/arraybuffer.slice-0.0.7.tgz#3bbc4275dd584cc1b10809b89d4e8b63a69e7675"
+
arrify@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
@@ -276,6 +280,10 @@ async-each@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"
+async-limiter@~1.0.0:
+ version "1.0.0"
+ resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
+
async.ensureasync@^0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/async.ensureasync/-/async.ensureasync-0.5.2.tgz#c3c7e4a4e9b31d96875d56b8504598446e1e305d"
@@ -1016,6 +1024,10 @@ [email protected]:
version "0.1.0"
resolved "https://registry.yarnpkg.com/base64id/-/base64id-0.1.0.tgz#02ce0fdeee0cef4f40080e1e73e834f0b1bfce3f"
+ version "1.0.0"
+ resolved "https://registry.yarnpkg.com/base64id/-/base64id-1.0.0.tgz#47688cb99bb6804f0e06d3e763b1c32e57d8e6b6"
+
[email protected], base64url@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/base64url/-/base64url-2.0.0.tgz#eac16e03ea1438eff9423d69baa36262ed1f70bb"
@@ -1896,13 +1908,13 @@ [email protected]:
dependencies:
ms "0.7.2"
[email protected], debug@^2.6.8, debug@^2.6.9:
[email protected], debug@^2.6.8, debug@^2.6.9, debug@~2.6.6:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
dependencies:
ms "2.0.0"
-debug@^3.0.0, debug@^3.1.0:
+debug@^3.0.0, debug@^3.1.0, debug@~3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
dependencies:
@@ -2227,6 +2239,16 @@ [email protected]:
has-binary "0.1.6"
wtf-8 "1.0.0"
+engine.io-parser@~2.1.0:
+ version "2.1.2"
+ resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-2.1.2.tgz#4c0f4cff79aaeecbbdcfdea66a823c6085409196"
+ dependencies:
+ after "0.8.2"
+ arraybuffer.slice "~0.0.7"
+ base64-arraybuffer "0.1.5"
+ blob "0.0.4"
+ has-binary2 "~1.0.2"
+
engine.io-parser@~2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/engine.io-parser/-/engine.io-parser-2.1.1.tgz#e0fb3f0e0462f7f58bb77c1a52e9f5a7e26e4668"
@@ -2248,6 +2270,19 @@ [email protected]:
engine.io-parser "1.3.1"
ws "1.1.1"
+engine.io@~3.1.0:
+ version "3.1.5"
+ resolved "https://registry.yarnpkg.com/engine.io/-/engine.io-3.1.5.tgz#0e7ef9d690eb0b35597f1d4ad02a26ca2dba3845"
+ dependencies:
+ accepts "~1.3.4"
+ base64id "1.0.0"
+ cookie "0.3.1"
+ debug "~3.1.0"
+ engine.io-parser "~2.1.0"
+ ws "~3.3.1"
+ optionalDependencies:
+ uws "~9.14.0"
+
errno@^0.1.1:
version "0.1.4"
resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.4.tgz#b896e23a9e5e8ba33871fc996abd3635fc9a1c7d"
@@ -3769,9 +3804,9 @@ limiter@^1.0.5:
version "1.1.0"
resolved "https://registry.yarnpkg.com/limiter/-/limiter-1.1.0.tgz#6e2bd12ca3fcdaa11f224e2e53c896df3f08d913"
-"lnrpc@https://github.com/asoltys/lnrpc#b32f349":
+"lnrpc@https://github.com/asoltys/lnrpc#ab71ae4":
version "0.0.3"
- resolved "https://github.com/asoltys/lnrpc#b32f34914a735e25064365eb036fae620ba13dc7"
+ resolved "https://github.com/asoltys/lnrpc#ab71ae4ea79da3e9ea74d47ab50f8dba31c41322"
dependencies:
grpc "^1.6.0"
napa "^3.0.0"
@@ -5638,6 +5673,10 @@ [email protected]:
debug "2.3.3"
socket.io-parser "2.3.1"
+socket.io-adapter@~1.1.0:
+ version "1.1.1"
+ resolved "https://registry.yarnpkg.com/socket.io-adapter/-/socket.io-adapter-1.1.1.tgz#2a805e8a14d6372124dd9159ad4502f8cb07f06b"
+
version "1.6.0"
resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-client-1.6.0.tgz#5b668f4f771304dfeed179064708386fa6717853"
@@ -5654,9 +5693,9 @@ [email protected]:
socket.io-parser "2.3.1"
to-array "0.1.4"
-socket.io-client@^2.0.3:
- version "2.0.3"
- resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-client-2.0.3.tgz#6caf4aff9f85b19fd91b6ce13d69adb564f8873b"
[email protected], socket.io-client@^2.0.4:
+ version "2.0.4"
+ resolved "https://registry.yarnpkg.com/socket.io-client/-/socket.io-client-2.0.4.tgz#0918a552406dc5e540b380dcd97afc4a64332f8e"
dependencies:
backo2 "1.0.2"
base64-arraybuffer "0.1.5"
@@ -5702,6 +5741,16 @@ [email protected]:
socket.io-client "1.6.0"
socket.io-parser "2.3.1"
+socket.io@^2.0.4:
+ version "2.0.4"
+ resolved "https://registry.yarnpkg.com/socket.io/-/socket.io-2.0.4.tgz#c1a4590ceff87ecf13c72652f046f716b29e6014"
+ dependencies:
+ debug "~2.6.6"
+ engine.io "~3.1.0"
+ socket.io-adapter "~1.1.0"
+ socket.io-client "2.0.4"
+ socket.io-parser "~3.1.1"
+
sort-keys-length@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/sort-keys-length/-/sort-keys-length-1.0.1.tgz#9cb6f4f4e9e48155a6aa0671edd336ff1479a188"
@@ -6193,6 +6242,10 @@ uuid@^3.0.1, uuid@^3.1.0, uuid@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"
+uws@~9.14.0:
+ version "9.14.0"
+ resolved "https://registry.yarnpkg.com/uws/-/uws-9.14.0.tgz#fac8386befc33a7a3705cbd58dc47b430ca4dd95"
+
v8flags@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/v8flags/-/v8flags-2.1.1.tgz#aab1a1fa30d45f88dd321148875ac02c0b55e5b4"
@@ -6359,6 +6412,14 @@ ws@~2.3.1:
safe-buffer "~5.0.1"
ultron "~1.1.0"
+ws@~3.3.1:
+ version "3.3.3"
+ resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.3.tgz#f1cf84fe2d5e901ebce94efaece785f187a228f2"
+ dependencies:
+ async-limiter "~1.0.0"
+ safe-buffer "~5.1.0"
+ ultron "~1.1.0"
+
version "1.0.0"
resolved "https://registry.yarnpkg.com/wtf-8/-/wtf-8-1.0.0.tgz#392d8ba2d0f1c34d1ee2d630f15d0efb68e1048a"