From d41cd218ff1ebab07f2bf8e84ffa0ca7deeb1a04 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 12 Feb 2018 19:35:34 -0800 Subject: generate lightning deposit address on user create --- db.js | 90 +++++++++++++++++++++++++++++++----------------------------- index.js | 6 ++-- mutations.js | 3 +- 3 files changed, 51 insertions(+), 48 deletions(-) diff --git a/db.js b/db.js index 04822a94..e4c18a83 100644 --- a/db.js +++ b/db.js @@ -44,56 +44,58 @@ const db = new Sequelize( const gqlfields = {} const gqltypes = {} -const p = new Promise((resolve, reject) => { - return auto.run(() => { - Object.keys(tables).forEach((k) => { - let t = tables[k] - let fields = {} - - Object.keys(auto.tables[t]).forEach(f => { - let isKey = f === 'id' - let rawtype = auto.tables[t][f].type.toLowerCase() - let type = Sequelize.STRING - - if (rawtype.match(/^int/)) type = Sequelize.INTEGER - if (rawtype.match(/^date/)) type = Sequelize.DATE - - fields[f] = { - type: type, - field: f, - primaryKey: isKey, - autoIncrement: isKey, +const p = (lnrpc) => { + return new Promise((resolve, reject) => { + return auto.run(() => { + Object.keys(tables).forEach((k) => { + let t = tables[k] + let fields = {} + + Object.keys(auto.tables[t]).forEach(f => { + let isKey = f === 'id' + let rawtype = auto.tables[t][f].type.toLowerCase() + let type = Sequelize.STRING + + if (rawtype.match(/^int/)) type = Sequelize.INTEGER + if (rawtype.match(/^date/)) type = Sequelize.DATE + + fields[f] = { + type: type, + field: f, + primaryKey: isKey, + autoIncrement: isKey, + } + }) + + db[k] = db.define(k, fields, { tableName: t }) + + let typefields = {} + let options = {} + + gqltypes[t] = new GraphQLObjectType({ + name: t, + desc: t, + fields: Object.assign(typefields, attributeFields(db[k])), + }) + + gqlfields[t] = { + type: new GraphQLList(gqltypes[t]), + args: Object.assign({}, defaultArgs(db[k]), defaultListArgs()), + resolve: resolver(db[k], options), } }) - db[k] = db.define(k, fields, { tableName: t }) - - let typefields = {} - let options = {} - - gqltypes[t] = new GraphQLObjectType({ - name: t, - desc: t, - fields: Object.assign(typefields, attributeFields(db[k])), + db.gqlschema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'RootQueryType', + fields: gqlfields + }), + mutation: require('./mutations')(db, gqltypes, lnrpc) }) - gqlfields[t] = { - type: new GraphQLList(gqltypes[t]), - args: Object.assign({}, defaultArgs(db[k]), defaultListArgs()), - resolve: resolver(db[k], options), - } - }) - - db.gqlschema = new GraphQLSchema({ - query: new GraphQLObjectType({ - name: 'RootQueryType', - fields: gqlfields - }), - mutation: require('./mutations')(db, gqltypes) + resolve(db) }) - - resolve(db) }) -}) +} module.exports = p diff --git a/index.js b/index.js index cad9a57f..0aa5038e 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,6 @@ const l = console.log ;(async () => { const app = express() - const db = await require('./db.js') app.enable('trust proxy') app.use(bodyParser.urlencoded({ extended: true })) app.use(bodyParser.json()) @@ -32,10 +31,9 @@ const l = console.log const restClient = ba.restfulClient(process.env.BITCOINAVERAGE_PUBLIC, process.env.BITCOINAVERAGE_SECRET) const lnrpc = await require('lnrpc')({ server: 'localhost:10001' }) const adminMacaroon = fs.readFileSync('/home/adam/.lnd.testa/admin.macaroon') - - const meta = new grpc.Metadata() meta.add('macaroon', adminMacaroon.toString('hex')) + lnrpc.meta = meta const invoices = lnrpc.subscribeInvoices({}, meta) invoices.on('data', msg => { @@ -44,6 +42,8 @@ const l = console.log ds.event.emit('invoices', msg) }) + const db = await require('./db.js')(lnrpc) + app.get('/balance', async (req, res) => { res.json(await lnrpc.walletBalance({witness_only: true}, meta)) }) diff --git a/mutations.js b/mutations.js index 795c885a..57332b7d 100644 --- a/mutations.js +++ b/mutations.js @@ -10,7 +10,7 @@ import { GraphQLList } from 'graphql' -module.exports = (db, gqltypes) => { +module.exports = (db, gqltypes, lnrpc) => { const UserInputType = new GraphQLInputObjectType({ name: 'UserInput', desc: 'UserInput', @@ -30,6 +30,7 @@ module.exports = (db, gqltypes) => { } }, resolve: async (root, { user }) => { + user.address = (await lnrpc.newAddress({ type: 1 }, lnrpc.meta)).address return db['User'].create(user) } }, -- cgit v1.2.3