Lots of development

This commit is contained in:
Josh Harvey 2017-03-31 16:45:14 +03:00
parent 5cbec6bd23
commit 3a244f691e
19 changed files with 594 additions and 837 deletions

View file

@ -1,27 +1,38 @@
const _ = require('lodash/fp')
const mem = require('mem')
const HKDF = require('node-hkdf-sync')
const configManager = require('./config-manager')
const pify = require('pify')
const fs = pify(require('fs'))
const options = require('./options')
const FETCH_INTERVAL = 5000
const INSUFFICIENT_FUNDS_CODE = 570
const INSUFFICIENT_FUNDS_NAME = 'InsufficientFunds'
function httpError (msg, code) {
const err = new Error(msg)
err.name = 'HTTPError'
err.code = code || 500
return err
}
function computeSeed (masterSeed) {
const hkdf = new HKDF('sha256', 'lamassu-server-salt', masterSeed)
return hkdf.derive('wallet-seed', 32)
}
function fetchWallet (settings, cryptoCode) {
return Promise.resolve()
.then(() => {
console.log('DEBUG44')
console.log('DEBUG44.0.0: %j', cryptoCode)
try {
console.log('DEBUG44.0: %j', configManager.cryptoScoped(cryptoCode, settings.config).wallet)
} catch (err) {
console.log('DEBUG44.0.e: %s', err.stack)
}
return fs.readFile(options.seedPath, 'utf8')
.then(hex => {
const masterSeed = Buffer.from(hex.trim(), 'hex')
const plugin = configManager.cryptoScoped(cryptoCode, settings.config).wallet
console.log('DEBUG44.1')
const account = settings.accounts[plugin]
console.log('DEBUG44.2')
const wallet = require('lamassu-' + plugin)
console.log('DEBUG45: %j', {wallet, account})
return {wallet, account}
return {wallet, account: _.set('seed', computeSeed(masterSeed), account)}
})
}
@ -44,11 +55,18 @@ function sendCoins (settings, toAddress, cryptoAtoms, cryptoCode) {
return res
})
})
.catch(err => {
if (err.name === INSUFFICIENT_FUNDS_NAME) {
throw httpError(INSUFFICIENT_FUNDS_NAME, INSUFFICIENT_FUNDS_CODE)
}
throw err
})
}
function newAddress (settings, cryptoCode, info) {
return fetchWallet(settings, cryptoCode)
.then(r => r.wallet.newAddress(r.account, cryptoCode, info))
function newAddress (settings, info) {
return fetchWallet(settings, info.cryptoCode)
.then(r => r.wallet.newAddress(r.account, info))
}
function getStatus (settings, toAddress, cryptoAtoms, cryptoCode) {
@ -56,9 +74,21 @@ function getStatus (settings, toAddress, cryptoAtoms, cryptoCode) {
.then(r => r.wallet.getStatus(r.account, toAddress, cryptoAtoms, cryptoCode))
}
function sweep (settings, cryptoCode, hdIndex) {
return fetchWallet(settings, cryptoCode)
.then(r => r.wallet.sweep(r.account, cryptoCode, hdIndex))
}
function isHd (settings, cryptoCode) {
return fetchWallet(settings, cryptoCode)
.then(r => r.wallet.supportsHd)
}
module.exports = {
balance: mem(balance, {maxAge: FETCH_INTERVAL}),
sendCoins,
newAddress,
getStatus
getStatus,
sweep,
isHd
}