Change seed file with a bip39 mnemonic (#207)
* Change seed file with a bip39 mnemonic * Create helper for bip39 mnemonic * Prod mode on lamassu-update-to-mnemonic script * Fix standard styling issues
This commit is contained in:
parent
809bf5a2a9
commit
d97a33565f
10 changed files with 98 additions and 34 deletions
|
|
@ -6,6 +6,7 @@ const pify = require('pify')
|
|||
const fs = pify(require('fs'))
|
||||
|
||||
const db = require('../db')
|
||||
const mnemonicHelpers = require('../mnemonic-helpers')
|
||||
const configManager = require('../config-manager')
|
||||
const options = require('../options')
|
||||
const logger = require('../logger')
|
||||
|
|
@ -129,17 +130,19 @@ function sendRadar (data) {
|
|||
|
||||
function mapRecord (info) {
|
||||
const timestamp = new Date().toISOString()
|
||||
return Promise.all([getMachines(info), fs.readFile(options.seedPath, 'utf8')])
|
||||
.then(([machines, hex]) => ({
|
||||
operatorId: computeOperatorId(Buffer.from(hex.trim(), 'hex')),
|
||||
operator: {
|
||||
name: null,
|
||||
phone: null,
|
||||
email: null
|
||||
},
|
||||
timestamp,
|
||||
machines
|
||||
}))
|
||||
return Promise.all([getMachines(info), fs.readFile(options.mnemonicPath, 'utf8')])
|
||||
.then(([machines, mnemonic]) => {
|
||||
return {
|
||||
operatorId: computeOperatorId(mnemonicHelpers.toEntropyBuffer(mnemonic)),
|
||||
operator: {
|
||||
name: null,
|
||||
phone: null,
|
||||
email: null
|
||||
},
|
||||
timestamp,
|
||||
machines
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function update (info) {
|
||||
|
|
|
|||
19
lib/mnemonic-helpers.js
Normal file
19
lib/mnemonic-helpers.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
const bip39 = require('bip39')
|
||||
const os = require('os')
|
||||
|
||||
function fromSeed (seed) {
|
||||
const words = bip39.entropyToMnemonic(seed).split(' ')
|
||||
|
||||
let mnemonic = ''
|
||||
for (let i = 0; i < words.length; i += 6) {
|
||||
mnemonic += words.slice(i, i + 6).join(' ') + os.EOL
|
||||
}
|
||||
return mnemonic
|
||||
}
|
||||
|
||||
function toEntropyBuffer (mnemonic) {
|
||||
const hex = bip39.mnemonicToEntropy(mnemonic.split('\n').join(' ').trim())
|
||||
return Buffer.from(hex.trim(), 'hex')
|
||||
}
|
||||
|
||||
module.exports = { toEntropyBuffer, fromSeed }
|
||||
|
|
@ -6,6 +6,7 @@ const configManager = require('./config-manager')
|
|||
const pify = require('pify')
|
||||
const fs = pify(require('fs'))
|
||||
|
||||
const mnemonicHelpers = require('./mnemonic-helpers')
|
||||
const options = require('./options')
|
||||
const ph = require('./plugin-helper')
|
||||
const layer2 = require('./layer2')
|
||||
|
|
@ -24,20 +25,20 @@ function httpError (msg, code) {
|
|||
}
|
||||
|
||||
function computeSeed (masterSeed) {
|
||||
return hkdf(masterSeed, 32, {salt: 'lamassu-server-salt', info: 'wallet-seed'}).toString('hex')
|
||||
return hkdf(masterSeed, 32, { salt: 'lamassu-server-salt', info: 'wallet-seed' }).toString('hex')
|
||||
}
|
||||
|
||||
function fetchWallet (settings, cryptoCode) {
|
||||
return fs.readFile(options.seedPath, 'utf8')
|
||||
.then(hex => {
|
||||
const masterSeed = Buffer.from(hex.trim(), 'hex')
|
||||
return fs.readFile(options.mnemonicPath, 'utf8')
|
||||
.then(mnemonic => {
|
||||
const masterSeed = mnemonicHelpers.toEntropyBuffer(mnemonic)
|
||||
const plugin = configManager.cryptoScoped(cryptoCode, settings.config).wallet
|
||||
const wallet = ph.load(ph.WALLET, plugin)
|
||||
const rawAccount = settings.accounts[plugin]
|
||||
const account = _.set('seed', computeSeed(masterSeed), rawAccount)
|
||||
if (_.isFunction(wallet.run)) wallet.run(account)
|
||||
|
||||
return {wallet, account}
|
||||
return { wallet, account }
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +47,7 @@ const lastBalance = {}
|
|||
function _balance (settings, cryptoCode) {
|
||||
return fetchWallet(settings, cryptoCode)
|
||||
.then(r => r.wallet.balance(r.account, cryptoCode))
|
||||
.then(balance => ({balance, timestamp: Date.now()}))
|
||||
.then(balance => ({ balance, timestamp: Date.now() }))
|
||||
.then(r => {
|
||||
lastBalance[cryptoCode] = r
|
||||
return r
|
||||
|
|
@ -103,7 +104,7 @@ function mergeStatus (a, b) {
|
|||
if (!a) return b
|
||||
if (!b) return a
|
||||
|
||||
return {status: mergeStatusMode(a.status, b.status)}
|
||||
return { status: mergeStatusMode(a.status, b.status) }
|
||||
}
|
||||
|
||||
function mergeStatusMode (a, b) {
|
||||
|
|
@ -169,7 +170,7 @@ function getStatus (settings, tx, machineId) {
|
|||
|
||||
const status = isAuthorized ? 'authorized' : unauthorizedStatus
|
||||
|
||||
return {status}
|
||||
return { status }
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue