Chore: refactor generic wallet and wallet plugins

This commit is contained in:
csrapr 2021-03-16 18:41:44 +00:00 committed by Josh Harvey
parent d2b7224c73
commit b6d91f94bf
11 changed files with 110 additions and 87 deletions

View file

@ -21,6 +21,10 @@ function computeSeed (masterSeed) {
return hkdf(masterSeed, 32, { salt: 'lamassu-server-salt', info: 'wallet-seed' })
}
function computeOperatorId (masterSeed) {
return hkdf(masterSeed, 16, { salt: 'lamassu-server-salt', info: 'operator-id' }).toString('hex')
}
function fetchWallet (settings, cryptoCode) {
return fs.readFile(options.mnemonicPath, 'utf8')
.then(mnemonic => {
@ -30,8 +34,8 @@ function fetchWallet (settings, cryptoCode) {
const rawAccount = settings.accounts[plugin]
const account = _.set('seed', computeSeed(masterSeed), rawAccount)
if (_.isFunction(wallet.run)) wallet.run(account)
return { wallet, account }
const operatorId = computeOperatorId(masterSeed)
return { wallet, account, operatorId }
})
}
@ -39,7 +43,7 @@ const lastBalance = {}
function _balance (settings, cryptoCode) {
return fetchWallet(settings, cryptoCode)
.then(r => r.wallet.balance(r.account, cryptoCode))
.then(r => r.wallet.balance(r.account, cryptoCode, settings, r.operatorId))
.then(balance => ({ balance, timestamp: Date.now() }))
.then(r => {
lastBalance[cryptoCode] = r
@ -51,10 +55,10 @@ function _balance (settings, cryptoCode) {
})
}
function sendCoins (settings, toAddress, cryptoAtoms, cryptoCode) {
return fetchWallet(settings, cryptoCode)
function sendCoins (settings, tx) {
return fetchWallet(settings, tx.cryptoCode)
.then(r => {
return r.wallet.sendCoins(r.account, toAddress, cryptoAtoms, cryptoCode)
return r.wallet.sendCoins(r.account, tx, settings, r.operatorId)
.then(res => {
mem.clear(module.exports.balance)
return res
@ -69,9 +73,9 @@ function sendCoins (settings, toAddress, cryptoAtoms, cryptoCode) {
})
}
function newAddress (settings, info) {
function newAddress (settings, info, tx) {
const walletAddressPromise = fetchWallet(settings, info.cryptoCode)
.then(r => r.wallet.newAddress(r.account, info))
.then(r => r.wallet.newAddress(r.account, info, tx, settings, r.operatorId))
return Promise.all([
walletAddressPromise,
@ -89,7 +93,7 @@ function newFunding (settings, cryptoCode, address) {
const wallet = r.wallet
const account = r.account
return wallet.newFunding(account, cryptoCode)
return wallet.newFunding(account, cryptoCode, settings, r.operatorId)
})
}
@ -117,9 +121,10 @@ function mergeStatusMode (a, b) {
function getWalletStatus (settings, tx) {
const fudgeFactorEnabled = configManager.getWalletSettings(tx.cryptoCode, settings.config).fudgeFactorActive
const fudgeFactor = fudgeFactorEnabled ? 100 : 0
const requested = tx.cryptoAtoms.minus(fudgeFactor)
const walletStatusPromise = fetchWallet(settings, tx.cryptoCode)
.then(r => r.wallet.getStatus(r.account, tx.toAddress, tx.cryptoAtoms.minus(fudgeFactor), tx.cryptoCode))
.then(r => r.wallet.getStatus(r.account, tx, requested, settings, r.operatorId))
return Promise.all([
walletStatusPromise,
@ -176,21 +181,21 @@ function getStatus (settings, tx, machineId) {
function sweep (settings, cryptoCode, hdIndex) {
return fetchWallet(settings, cryptoCode)
.then(r => r.wallet.sweep(r.account, cryptoCode, hdIndex))
.then(r => r.wallet.sweep(r.account, cryptoCode, hdIndex, settings, r.operatorId))
}
function isHd (settings, cryptoCode) {
return fetchWallet(settings, cryptoCode)
function isHd (settings, tx) {
return fetchWallet(settings, tx.cryptoCode)
.then(r => r.wallet.supportsHd)
}
function cryptoNetwork (settings, cryptoCode) {
const plugin = configManager.getWalletSettings(cryptoCode, settings.config).wallet
const wallet = ph.load(ph.WALLET, plugin)
const account = settings.accounts[plugin]
if (!wallet.cryptoNetwork) return Promise.resolve(false)
return wallet.cryptoNetwork(account, cryptoCode)
return fetchWallet(settings, cryptoCode).then(r => {
if (!r.wallet.cryptoNetwork) return Promise.resolve(false)
return r.wallet.cryptoNetwork(account, cryptoCode, settings, r.operatorId)
})
}
function isStrictAddress (settings, cryptoCode, toAddress) {
@ -199,7 +204,7 @@ function isStrictAddress (settings, cryptoCode, toAddress) {
return fetchWallet(settings, cryptoCode)
.then(r => {
if (!r.wallet.isStrictAddress) return true
return r.wallet.isStrictAddress(cryptoCode, toAddress)
return r.wallet.isStrictAddress(cryptoCode, toAddress, settings, r.operatorId)
})
}