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

@ -259,7 +259,7 @@ function plugins (settings, deviceId) {
} }
function sendCoins (tx) { function sendCoins (tx) {
return wallet.sendCoins(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode) return wallet.sendCoins(settings, tx)
} }
function recordPing (deviceTime, version, model) { function recordPing (deviceTime, version, model) {
@ -279,7 +279,7 @@ function plugins (settings, deviceId) {
} }
function isHd (tx) { function isHd (tx) {
return wallet.isHd(settings, tx.cryptoCode) return wallet.isHd(settings, tx)
} }
function getStatus (tx) { function getStatus (tx) {
@ -295,7 +295,7 @@ function plugins (settings, deviceId) {
cryptoAtoms: tx.cryptoAtoms, cryptoAtoms: tx.cryptoAtoms,
isLightning: tx.isLightning isLightning: tx.isLightning
} }
return wallet.newAddress(settings, info) return wallet.newAddress(settings, info, tx)
} }
function dispenseAck (tx) { function dispenseAck (tx) {

View file

@ -45,15 +45,15 @@ function accountUnconfirmedBalance (cryptoCode) {
// We want a balance that includes all spends (0 conf) but only deposits that // We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically. // have at least 1 confirmation. getbalance does this for us automatically.
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return accountBalance(cryptoCode) return accountBalance(cryptoCode)
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId])) .then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res)) .then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => { .then((pickedObj) => {
@ -68,7 +68,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
}) })
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress')) .then(() => fetch('getnewaddress'))
} }
@ -88,7 +88,9 @@ function pendingBalance (address, cryptoCode) {
.then(() => addressBalance(address, 0)) .then(() => addressBalance(address, 0))
} }
function getStatus (account, toAddress, requested, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode)) .then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => { .then(confirmed => {
@ -103,7 +105,7 @@ function getStatus (account, toAddress, requested, cryptoCode) {
}) })
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
const promises = [ const promises = [
@ -121,7 +123,7 @@ function newFunding (account, cryptoCode) {
})) }))
} }
function cryptoNetwork (account, cryptoCode) { function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main') .then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
} }

View file

@ -45,15 +45,16 @@ function accountUnconfirmedBalance (cryptoCode) {
// We want a balance that includes all spends (0 conf) but only deposits that // We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically. // have at least 1 confirmation. getbalance does this for us automatically.
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return accountBalance(cryptoCode) return accountBalance(cryptoCode)
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId])) .then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res)) .then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => { .then((pickedObj) => {
@ -68,7 +69,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
}) })
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress')) .then(() => fetch('getnewaddress'))
} }
@ -88,7 +89,8 @@ function pendingBalance (address, cryptoCode) {
.then(() => addressBalance(address, 0)) .then(() => addressBalance(address, 0))
} }
function getStatus (account, toAddress, requested, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode)) .then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => { .then(confirmed => {
@ -103,7 +105,7 @@ function getStatus (account, toAddress, requested, cryptoCode) {
}) })
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
const promises = [ const promises = [
@ -121,7 +123,7 @@ function newFunding (account, cryptoCode) {
})) }))
} }
function cryptoNetwork (account, cryptoCode) { function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main') .then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
} }

View file

@ -53,12 +53,13 @@ function formatToGetStatus (address, cryptoCode) {
return part2 || part1 return part2 || part1
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode)) .then(() => getWallet(account, cryptoCode))
.then(wallet => { .then(wallet => {
const params = { const params = {
address: getLegacyAddress(address, cryptoCode), address: getLegacyAddress(toAddress, cryptoCode),
amount: cryptoAtoms.toNumber(), amount: cryptoAtoms.toNumber(),
walletPassphrase: account[`${cryptoCode}WalletPassphrase`], walletPassphrase: account[`${cryptoCode}WalletPassphrase`],
enforceMinConfirmsForChange: false enforceMinConfirmsForChange: false
@ -77,13 +78,13 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
}) })
} }
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode)) .then(() => getWallet(account, cryptoCode))
.then(wallet => BN(wallet._wallet.spendableBalanceString)) .then(wallet => BN(wallet._wallet.spendableBalanceString))
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(() => getWallet(account, info.cryptoCode)) .then(() => getWallet(account, info.cryptoCode))
.then(wallet => { .then(wallet => {
@ -102,7 +103,8 @@ function newAddress (account, info) {
}) })
} }
function getStatus (account, toAddress, requested, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode)) .then(() => getWallet(account, cryptoCode))
.then(wallet => wallet.transfers({ .then(wallet => wallet.transfers({
@ -131,7 +133,7 @@ function getStatus (account, toAddress, requested, cryptoCode) {
}) })
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
return getWallet(account, cryptoCode) return getWallet(account, cryptoCode)
@ -150,7 +152,7 @@ function newFunding (account, cryptoCode) {
}) })
} }
function cryptoNetwork (account, cryptoCode) { function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => account.environment === 'test' ? 'test' : 'main') .then(() => account.environment === 'test' ? 'test' : 'main')
} }

View file

@ -46,15 +46,16 @@ function accountUnconfirmedBalance (cryptoCode) {
// We want a balance that includes all spends (0 conf) but only deposits that // We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically. // have at least 1 confirmation. getbalance does this for us automatically.
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return accountBalance(cryptoCode) return accountBalance(cryptoCode)
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId])) .then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res)) .then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => { .then((pickedObj) => {
@ -69,7 +70,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
}) })
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress')) .then(() => fetch('getnewaddress'))
} }
@ -89,7 +90,8 @@ function pendingBalance (address, cryptoCode) {
.then(() => addressBalance(address, 0)) .then(() => addressBalance(address, 0))
} }
function getStatus (account, toAddress, requested, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode)) .then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => { .then(confirmed => {
@ -104,7 +106,7 @@ function getStatus (account, toAddress, requested, cryptoCode) {
}) })
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
const promises = [ const promises = [

View file

@ -42,11 +42,12 @@ function privateKey (account) {
return defaultWallet(account).getPrivateKey() return defaultWallet(account).getPrivateKey()
} }
function isStrictAddress (cryptoCode, toAddress) { function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {
return cryptoCode === 'ETH' && util.isValidChecksumAddress(toAddress) return cryptoCode === 'ETH' && util.isValidChecksumAddress(toAddress)
} }
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms } = tx
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false) return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false)
.then(pify(web3.eth.sendRawTransaction)) .then(pify(web3.eth.sendRawTransaction))
.then(txid => { .then(txid => {
@ -66,7 +67,7 @@ function checkCryptoCode (cryptoCode) {
return Promise.reject(new Error('cryptoCode must be ETH')) return Promise.reject(new Error('cryptoCode must be ETH'))
} }
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(defaultAddress(account))) .then(() => confirmedBalance(defaultAddress(account)))
} }
@ -139,7 +140,7 @@ function defaultAddress (account) {
return defaultWallet(account).getChecksumAddressString() return defaultWallet(account).getChecksumAddressString()
} }
function sweep (account, cryptoCode, hdIndex) { function sweep (account, cryptoCode, hdIndex, settings, operatorId) {
const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet() const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet()
const fromAddress = wallet.getChecksumAddressString() const fromAddress = wallet.getChecksumAddressString()
@ -152,20 +153,21 @@ function sweep (account, cryptoCode, hdIndex) {
}) })
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
const childNode = paymentHdNode(account).deriveChild(info.hdIndex) const childNode = paymentHdNode(account).deriveChild(info.hdIndex)
return Promise.resolve(childNode.getWallet().getChecksumAddressString()) return Promise.resolve(childNode.getWallet().getChecksumAddressString())
} }
function getStatus (account, toAddress, cryptoAtoms, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress)) .then(() => confirmedBalance(toAddress))
.then(confirmed => { .then(confirmed => {
if (confirmed.gte(cryptoAtoms)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' } if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress) return pendingBalance(toAddress)
.then(pending => { .then(pending => {
if (pending.gte(cryptoAtoms)) return { receivedCryptoAtoms: pending, status: 'published' } if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'published' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' } if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' } return { receivedCryptoAtoms: pending, status: 'notSeen' }
}) })
@ -186,7 +188,7 @@ function defaultHdNode (account) {
return key.derivePath(defaultPrefixPath) return key.derivePath(defaultPrefixPath)
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
const fundingAddress = defaultAddress(account) const fundingAddress = defaultAddress(account)

View file

@ -45,15 +45,16 @@ function accountUnconfirmedBalance (cryptoCode) {
// We want a balance that includes all spends (0 conf) but only deposits that // We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically. // have at least 1 confirmation. getbalance does this for us automatically.
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return accountBalance(cryptoCode) return accountBalance(cryptoCode)
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId])) .then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res)) .then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => { .then((pickedObj) => {
@ -68,7 +69,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
}) })
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress')) .then(() => fetch('getnewaddress'))
} }
@ -88,7 +89,8 @@ function pendingBalance (address, cryptoCode) {
.then(() => addressBalance(address, 0)) .then(() => addressBalance(address, 0))
} }
function getStatus (account, toAddress, requested, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode)) .then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => { .then(confirmed => {
@ -103,7 +105,7 @@ function getStatus (account, toAddress, requested, cryptoCode) {
}) })
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
const promises = [ const promises = [

View file

@ -23,7 +23,7 @@ function connect () {
return lnd.connect(options.lnd || {}) return lnd.connect(options.lnd || {})
} }
function cryptoNetwork (account, cryptoCode) { function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return Promise.resolve('main') return Promise.resolve('main')
} }
@ -32,7 +32,7 @@ function checkCryptoCode (cryptoCode) {
return Promise.resolve() return Promise.resolve()
} }
function balance (acount, cryptoCode) { function balance (acount, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(connect) .then(connect)
.then(c => c.channelBalance({})) .then(c => c.channelBalance({}))
@ -41,17 +41,18 @@ function balance (acount, cryptoCode) {
.then(r => r.shift(unitScale).round()) .then(r => r.shift(unitScale).round())
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
// const { toAddress, cryptoAtoms, cryptoCode } = tx
// Not implemented yet // Not implemented yet
return Promise.reject(new E.NotImplementedError()) return Promise.reject(new E.NotImplementedError())
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
// Not implemented yet // Not implemented yet
return Promise.reject(new E.NotImplementedError()) return Promise.reject(new E.NotImplementedError())
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(connect) .then(connect)
.then(c => { .then(c => {
@ -65,7 +66,8 @@ function newAddress (account, info) {
}) })
} }
function getStatus (account, toAddress, requested, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
const parts = _.split(':', toAddress) const parts = _.split(':', toAddress)
@ -74,10 +76,10 @@ function getStatus (account, toAddress, requested, cryptoCode) {
return connect() return connect()
.then(c => { .then(c => {
return c.lookupInvoice({r_hash_str: rHashStr}) return c.lookupInvoice({ r_hash_str: rHashStr })
.then(r => { .then(r => {
if (r.settled) return {status: 'confirmed'} if (r.settled) return { status: 'confirmed' }
return {status: 'notSeen'} return { status: 'notSeen' }
}) })
}) })
}) })

View file

@ -17,7 +17,7 @@ function _balance (cryptoCode) {
return BN(10).shift(unitScale).round() return BN(10).shift(unitScale).round()
} }
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return Promise.resolve() return Promise.resolve()
.then(() => _balance(cryptoCode)) .then(() => _balance(cryptoCode))
} }
@ -39,7 +39,8 @@ function isInsufficient (cryptoAtoms, cryptoCode) {
return cryptoAtoms.gt(b.div(1000).mul(sendCount)) return cryptoAtoms.gt(b.div(1000).mul(sendCount))
} }
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
sendCount++ sendCount++
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
@ -56,16 +57,16 @@ function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
}) })
} }
function newAddress () { function newAddress (account, info, tx, settings, operatorId) {
t0 = Date.now() t0 = Date.now()
return Promise.resolve('<Fake address, don\'t send>') return Promise.resolve('<Fake address, don\'t send>')
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
const promises = [ const promises = [
pendingBalance(account, cryptoCode), pendingBalance(account, cryptoCode),
confirmedBalance(account, cryptoCode), confirmedBalance(account, cryptoCode),
newAddress(account, {cryptoCode}) newAddress(account, { cryptoCode })
] ]
return Promise.all(promises) return Promise.all(promises)
@ -76,12 +77,13 @@ function newFunding (account, cryptoCode) {
})) }))
} }
function getStatus (account, toAddress, cryptoAtoms, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
const elapsed = Date.now() - t0 const elapsed = Date.now() - t0
if (elapsed < PUBLISH_TIME) return Promise.resolve({ receivedCryptoAtoms: BN(0), status: 'notSeen' }) if (elapsed < PUBLISH_TIME) return Promise.resolve({ receivedCryptoAtoms: BN(0), status: 'notSeen' })
if (elapsed < AUTHORIZE_TIME) return Promise.resolve({ receivedCryptoAtoms: cryptoAtoms, status: 'published' }) if (elapsed < AUTHORIZE_TIME) return Promise.resolve({ receivedCryptoAtoms: requested, status: 'published' })
if (elapsed < CONFIRM_TIME) return Promise.resolve({ receivedCryptoAtoms: cryptoAtoms, status: 'authorized' }) if (elapsed < CONFIRM_TIME) return Promise.resolve({ receivedCryptoAtoms: requested, status: 'authorized' })
console.log('[%s] DEBUG: Mock wallet has confirmed transaction [%s]', cryptoCode, toAddress.slice(0, 5)) console.log('[%s] DEBUG: Mock wallet has confirmed transaction [%s]', cryptoCode, toAddress.slice(0, 5))

View file

@ -47,11 +47,12 @@ function accountUnconfirmedBalance (cryptoCode) {
// We want a balance that includes all spends (0 conf) but only deposits that // We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically. // have at least 1 confirmation. getbalance does this for us automatically.
function balance (account, cryptoCode) { function balance (account, cryptoCode, settings, operatorId) {
return accountBalance(cryptoCode) return accountBalance(cryptoCode)
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, tx, settings, operatorId) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shift(-unitScale).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
const checkSendStatus = function (opid) { const checkSendStatus = function (opid) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -75,7 +76,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
const checker = opid => pRetry(() => checkSendStatus(opid), { retries: 20, minTimeout: 300, factor: 1.05 }) const checker = opid => pRetry(() => checkSendStatus(opid), { retries: 20, minTimeout: 300, factor: 1.05 })
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('z_sendmany', ['ANY_TADDR', [{ address, amount: coins }]])) .then(() => fetch('z_sendmany', ['ANY_TADDR', [{ toAddress, amount: coins }]]))
.then(checker) .then(checker)
.then((res) => { .then((res) => {
return { return {
@ -95,7 +96,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
}) })
} }
function newAddress (account, info) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress')) .then(() => fetch('getnewaddress'))
} }
@ -115,7 +116,8 @@ function pendingBalance (address, cryptoCode) {
.then(() => addressBalance(address, 0)) .then(() => addressBalance(address, 0))
} }
function getStatus (account, toAddress, requested, cryptoCode) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode)) .then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => { .then(confirmed => {
@ -130,7 +132,7 @@ function getStatus (account, toAddress, requested, cryptoCode) {
}) })
} }
function newFunding (account, cryptoCode) { function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => { .then(() => {
const promises = [ const promises = [

View file

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