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

@ -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')
}

View file

@ -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')
}

View file

@ -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')
}

View file

@ -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 = [

View file

@ -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)

View file

@ -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 = [

View file

@ -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' }
})
})
})

View file

@ -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('<Fake address, don\'t send>')
}
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))

View file

@ -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 = [