From b6d91f94bfcc5638ba34c8931d50de62da122479 Mon Sep 17 00:00:00 2001 From: csrapr <26280794+csrapr@users.noreply.github.com> Date: Tue, 16 Mar 2021 18:41:44 +0000 Subject: [PATCH] Chore: refactor generic wallet and wallet plugins --- lib/plugins.js | 6 +-- .../wallet/bitcoincashd/bitcoincashd.js | 18 ++++---- lib/plugins/wallet/bitcoind/bitcoind.js | 16 ++++---- lib/plugins/wallet/bitgo/bitgo.js | 16 ++++---- lib/plugins/wallet/dashd/dashd.js | 14 ++++--- lib/plugins/wallet/geth/base.js | 20 +++++---- lib/plugins/wallet/litecoind/litecoind.js | 14 ++++--- lib/plugins/wallet/lnd/lnd.js | 20 +++++---- lib/plugins/wallet/mock-wallet/mock-wallet.js | 18 ++++---- lib/plugins/wallet/zcashd/zcashd.js | 14 ++++--- lib/wallet.js | 41 +++++++++++-------- 11 files changed, 110 insertions(+), 87 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 78730cf7..87e8b3b2 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -259,7 +259,7 @@ function plugins (settings, deviceId) { } function sendCoins (tx) { - return wallet.sendCoins(settings, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode) + return wallet.sendCoins(settings, tx) } function recordPing (deviceTime, version, model) { @@ -279,7 +279,7 @@ function plugins (settings, deviceId) { } function isHd (tx) { - return wallet.isHd(settings, tx.cryptoCode) + return wallet.isHd(settings, tx) } function getStatus (tx) { @@ -295,7 +295,7 @@ function plugins (settings, deviceId) { cryptoAtoms: tx.cryptoAtoms, isLightning: tx.isLightning } - return wallet.newAddress(settings, info) + return wallet.newAddress(settings, info, tx) } function dispenseAck (tx) { diff --git a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js index 418af0a2..a14dc639 100644 --- a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js +++ b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js @@ -45,15 +45,15 @@ function accountUnconfirmedBalance (cryptoCode) { // 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. -function balance (account, cryptoCode) { +function balance (account, cryptoCode, settings, operatorId) { 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) - return checkCryptoCode(cryptoCode) - .then(() => fetch('sendtoaddress', [address, coins])) + .then(() => fetch('sendtoaddress', [toAddress, coins])) .then((txId) => fetch('gettransaction', [txId])) .then((res) => _.pick(['fee', 'txid'], res)) .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) .then(() => fetch('getnewaddress')) } @@ -88,7 +88,9 @@ function pendingBalance (address, cryptoCode) { .then(() => addressBalance(address, 0)) } -function getStatus (account, toAddress, requested, cryptoCode) { +function getStatus (account, tx, requested, settings, operatorId) { + const { toAddress, cryptoCode } = tx + return checkCryptoCode(cryptoCode) .then(() => confirmedBalance(toAddress, cryptoCode)) .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) .then(() => { const promises = [ @@ -121,7 +123,7 @@ function newFunding (account, cryptoCode) { })) } -function cryptoNetwork (account, cryptoCode) { +function cryptoNetwork (account, cryptoCode, settings, operatorId) { return checkCryptoCode(cryptoCode) .then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main') } diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index d68a27c3..61f9c733 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -45,15 +45,16 @@ function accountUnconfirmedBalance (cryptoCode) { // 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. -function balance (account, cryptoCode) { +function balance (account, cryptoCode, settings, operatorId) { 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) return checkCryptoCode(cryptoCode) - .then(() => fetch('sendtoaddress', [address, coins])) + .then(() => fetch('sendtoaddress', [toAddress, coins])) .then((txId) => fetch('gettransaction', [txId])) .then((res) => _.pick(['fee', 'txid'], res)) .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) .then(() => fetch('getnewaddress')) } @@ -88,7 +89,8 @@ function pendingBalance (address, cryptoCode) { .then(() => addressBalance(address, 0)) } -function getStatus (account, toAddress, requested, cryptoCode) { +function getStatus (account, tx, requested, settings, operatorId) { + const { toAddress, cryptoCode } = tx return checkCryptoCode(cryptoCode) .then(() => confirmedBalance(toAddress, cryptoCode)) .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) .then(() => { const promises = [ @@ -121,7 +123,7 @@ function newFunding (account, cryptoCode) { })) } -function cryptoNetwork (account, cryptoCode) { +function cryptoNetwork (account, cryptoCode, settings, operatorId) { return checkCryptoCode(cryptoCode) .then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main') } diff --git a/lib/plugins/wallet/bitgo/bitgo.js b/lib/plugins/wallet/bitgo/bitgo.js index 26b1c9db..b5a36eec 100644 --- a/lib/plugins/wallet/bitgo/bitgo.js +++ b/lib/plugins/wallet/bitgo/bitgo.js @@ -53,12 +53,13 @@ function formatToGetStatus (address, cryptoCode) { return part2 || part1 } -function sendCoins (account, address, cryptoAtoms, cryptoCode) { +function sendCoins (account, tx, settings, operatorId) { + const { toAddress, cryptoAtoms, cryptoCode } = tx return checkCryptoCode(cryptoCode) .then(() => getWallet(account, cryptoCode)) .then(wallet => { const params = { - address: getLegacyAddress(address, cryptoCode), + address: getLegacyAddress(toAddress, cryptoCode), amount: cryptoAtoms.toNumber(), walletPassphrase: account[`${cryptoCode}WalletPassphrase`], 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) .then(() => getWallet(account, cryptoCode)) .then(wallet => BN(wallet._wallet.spendableBalanceString)) } -function newAddress (account, info) { +function newAddress (account, info, tx, settings, operatorId) { return checkCryptoCode(info.cryptoCode) .then(() => getWallet(account, info.cryptoCode)) .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) .then(() => getWallet(account, cryptoCode)) .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) .then(() => { 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) .then(() => account.environment === 'test' ? 'test' : 'main') } diff --git a/lib/plugins/wallet/dashd/dashd.js b/lib/plugins/wallet/dashd/dashd.js index 1ef6ee66..449786e9 100644 --- a/lib/plugins/wallet/dashd/dashd.js +++ b/lib/plugins/wallet/dashd/dashd.js @@ -46,15 +46,16 @@ function accountUnconfirmedBalance (cryptoCode) { // 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. -function balance (account, cryptoCode) { +function balance (account, cryptoCode, settings, operatorId) { 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) return checkCryptoCode(cryptoCode) - .then(() => fetch('sendtoaddress', [address, coins])) + .then(() => fetch('sendtoaddress', [toAddress, coins])) .then((txId) => fetch('gettransaction', [txId])) .then((res) => _.pick(['fee', 'txid'], res)) .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) .then(() => fetch('getnewaddress')) } @@ -89,7 +90,8 @@ function pendingBalance (address, cryptoCode) { .then(() => addressBalance(address, 0)) } -function getStatus (account, toAddress, requested, cryptoCode) { +function getStatus (account, tx, requested, settings, operatorId) { + const { toAddress, cryptoCode } = tx return checkCryptoCode(cryptoCode) .then(() => confirmedBalance(toAddress, cryptoCode)) .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) .then(() => { const promises = [ diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index 0aabacb8..a24246c9 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -42,11 +42,12 @@ function privateKey (account) { return defaultWallet(account).getPrivateKey() } -function isStrictAddress (cryptoCode, toAddress) { +function isStrictAddress (cryptoCode, toAddress, settings, operatorId) { 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) .then(pify(web3.eth.sendRawTransaction)) .then(txid => { @@ -66,7 +67,7 @@ function checkCryptoCode (cryptoCode) { return Promise.reject(new Error('cryptoCode must be ETH')) } -function balance (account, cryptoCode) { +function balance (account, cryptoCode, settings, operatorId) { return checkCryptoCode(cryptoCode) .then(() => confirmedBalance(defaultAddress(account))) } @@ -139,7 +140,7 @@ function defaultAddress (account) { return defaultWallet(account).getChecksumAddressString() } -function sweep (account, cryptoCode, hdIndex) { +function sweep (account, cryptoCode, hdIndex, settings, operatorId) { const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet() 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) 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) .then(() => confirmedBalance(toAddress)) .then(confirmed => { - if (confirmed.gte(cryptoAtoms)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' } + if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' } return pendingBalance(toAddress) .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' } return { receivedCryptoAtoms: pending, status: 'notSeen' } }) @@ -186,7 +188,7 @@ function defaultHdNode (account) { return key.derivePath(defaultPrefixPath) } -function newFunding (account, cryptoCode) { +function newFunding (account, cryptoCode, settings, operatorId) { return checkCryptoCode(cryptoCode) .then(() => { const fundingAddress = defaultAddress(account) diff --git a/lib/plugins/wallet/litecoind/litecoind.js b/lib/plugins/wallet/litecoind/litecoind.js index 5601c6e4..9ba22b34 100644 --- a/lib/plugins/wallet/litecoind/litecoind.js +++ b/lib/plugins/wallet/litecoind/litecoind.js @@ -45,15 +45,16 @@ function accountUnconfirmedBalance (cryptoCode) { // 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. -function balance (account, cryptoCode) { +function balance (account, cryptoCode, settings, operatorId) { 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) return checkCryptoCode(cryptoCode) - .then(() => fetch('sendtoaddress', [address, coins])) + .then(() => fetch('sendtoaddress', [toAddress, coins])) .then((txId) => fetch('gettransaction', [txId])) .then((res) => _.pick(['fee', 'txid'], res)) .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) .then(() => fetch('getnewaddress')) } @@ -88,7 +89,8 @@ function pendingBalance (address, cryptoCode) { .then(() => addressBalance(address, 0)) } -function getStatus (account, toAddress, requested, cryptoCode) { +function getStatus (account, tx, requested, settings, operatorId) { + const { toAddress, cryptoCode } = tx return checkCryptoCode(cryptoCode) .then(() => confirmedBalance(toAddress, cryptoCode)) .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) .then(() => { const promises = [ diff --git a/lib/plugins/wallet/lnd/lnd.js b/lib/plugins/wallet/lnd/lnd.js index 5e9d82f9..a70fe002 100644 --- a/lib/plugins/wallet/lnd/lnd.js +++ b/lib/plugins/wallet/lnd/lnd.js @@ -23,7 +23,7 @@ function connect () { return lnd.connect(options.lnd || {}) } -function cryptoNetwork (account, cryptoCode) { +function cryptoNetwork (account, cryptoCode, settings, operatorId) { return Promise.resolve('main') } @@ -32,7 +32,7 @@ function checkCryptoCode (cryptoCode) { return Promise.resolve() } -function balance (acount, cryptoCode) { +function balance (acount, cryptoCode, settings, operatorId) { return checkCryptoCode(cryptoCode) .then(connect) .then(c => c.channelBalance({})) @@ -41,17 +41,18 @@ function balance (acount, cryptoCode) { .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 return Promise.reject(new E.NotImplementedError()) } -function newFunding (account, cryptoCode) { +function newFunding (account, cryptoCode, settings, operatorId) { // Not implemented yet return Promise.reject(new E.NotImplementedError()) } -function newAddress (account, info) { +function newAddress (account, info, tx, settings, operatorId) { return checkCryptoCode(info.cryptoCode) .then(connect) .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) .then(() => { const parts = _.split(':', toAddress) @@ -74,10 +76,10 @@ function getStatus (account, toAddress, requested, cryptoCode) { return connect() .then(c => { - return c.lookupInvoice({r_hash_str: rHashStr}) + return c.lookupInvoice({ r_hash_str: rHashStr }) .then(r => { - if (r.settled) return {status: 'confirmed'} - return {status: 'notSeen'} + if (r.settled) return { status: 'confirmed' } + return { status: 'notSeen' } }) }) }) diff --git a/lib/plugins/wallet/mock-wallet/mock-wallet.js b/lib/plugins/wallet/mock-wallet/mock-wallet.js index abaff6f7..9f1248f3 100644 --- a/lib/plugins/wallet/mock-wallet/mock-wallet.js +++ b/lib/plugins/wallet/mock-wallet/mock-wallet.js @@ -17,7 +17,7 @@ function _balance (cryptoCode) { return BN(10).shift(unitScale).round() } -function balance (account, cryptoCode) { +function balance (account, cryptoCode, settings, operatorId) { return Promise.resolve() .then(() => _balance(cryptoCode)) } @@ -39,7 +39,8 @@ function isInsufficient (cryptoAtoms, cryptoCode) { 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++ return new Promise((resolve, reject) => { setTimeout(() => { @@ -56,16 +57,16 @@ function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) { }) } -function newAddress () { +function newAddress (account, info, tx, settings, operatorId) { t0 = Date.now() return Promise.resolve('') } -function newFunding (account, cryptoCode) { +function newFunding (account, cryptoCode, settings, operatorId) { const promises = [ pendingBalance(account, cryptoCode), confirmedBalance(account, cryptoCode), - newAddress(account, {cryptoCode}) + newAddress(account, { cryptoCode }) ] 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 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 < CONFIRM_TIME) return Promise.resolve({ receivedCryptoAtoms: cryptoAtoms, status: 'authorized' }) + if (elapsed < AUTHORIZE_TIME) return Promise.resolve({ receivedCryptoAtoms: requested, status: 'published' }) + 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)) diff --git a/lib/plugins/wallet/zcashd/zcashd.js b/lib/plugins/wallet/zcashd/zcashd.js index 459bd5a3..1cb82e06 100644 --- a/lib/plugins/wallet/zcashd/zcashd.js +++ b/lib/plugins/wallet/zcashd/zcashd.js @@ -47,11 +47,12 @@ function accountUnconfirmedBalance (cryptoCode) { // 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. -function balance (account, cryptoCode) { +function balance (account, cryptoCode, settings, operatorId) { 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 checkSendStatus = function (opid) { 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 }) return checkCryptoCode(cryptoCode) - .then(() => fetch('z_sendmany', ['ANY_TADDR', [{ address, amount: coins }]])) + .then(() => fetch('z_sendmany', ['ANY_TADDR', [{ toAddress, amount: coins }]])) .then(checker) .then((res) => { 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) .then(() => fetch('getnewaddress')) } @@ -115,7 +116,8 @@ function pendingBalance (address, cryptoCode) { .then(() => addressBalance(address, 0)) } -function getStatus (account, toAddress, requested, cryptoCode) { +function getStatus (account, tx, requested, settings, operatorId) { + const { toAddress, cryptoCode } = tx return checkCryptoCode(cryptoCode) .then(() => confirmedBalance(toAddress, cryptoCode)) .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) .then(() => { const promises = [ diff --git a/lib/wallet.js b/lib/wallet.js index f454e1a5..85a0a8f4 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -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) }) }