feat: create add machine page

This commit is contained in:
Taranto 2020-03-15 21:51:19 +00:00 committed by Josh Harvey
parent b1b8b82260
commit 2b71c08444
7 changed files with 350 additions and 5 deletions

View file

@ -16,6 +16,7 @@ const transactions = require('../transactions')
const funding = require('../funding')
const supervisor = require('../supervisor')
const serverLogs = require('../server-logs')
const pairing = require('../pairing')
const { accounts, coins, countries, currencies, languages } = require('../config')
// TODO why does server logs messages can be null?
@ -186,6 +187,7 @@ const typeDefs = gql`
machineSupportLogs(deviceId: ID!): SupportLogsResponse
serverSupportLogs: SupportLogsResponse
saveConfig(config: JSONObject): JSONObject
createPairingTotem(name: String!): String
}
`
@ -215,6 +217,7 @@ const resolvers = {
Mutation: {
machineAction: (...[, { deviceId, action }]) => machineAction({ deviceId, action }),
machineSupportLogs: (...[, { deviceId }]) => supportLogs.insert(deviceId),
createPairingTotem: (...[, { name }]) => pairing.totem(name),
serverSupportLogs: () => serverLogs.insert(),
saveConfig: (...[, { config }]) => settingsLoader.saveConfig(config)
.then(it => {

33
lib/new-admin/pairing.js Normal file
View file

@ -0,0 +1,33 @@
const fs = require('fs')
const pify = require('pify')
const readFile = pify(fs.readFile)
const crypto = require('crypto')
const baseX = require('base-x')
const options = require('../options')
const db = require('../db')
const pairing = require('../pairing')
const ALPHA_BASE = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'
const bsAlpha = baseX(ALPHA_BASE)
const unpair = pairing.unpair
function totem (name) {
const caPath = options.caPath
return readFile(caPath)
.then(data => {
const caHash = crypto.createHash('sha256').update(data).digest()
const token = crypto.randomBytes(32)
const hexToken = token.toString('hex')
const caHexToken = crypto.createHash('sha256').update(hexToken).digest('hex')
const buf = Buffer.concat([caHash, token, Buffer.from(options.hostname)])
const sql = 'insert into pairing_tokens (token, name) values ($1, $3), ($2, $3)'
return db.none(sql, [hexToken, caHexToken, name])
.then(() => bsAlpha.encode(buf))
})
}
module.exports = { totem, unpair }