coinUtils fixes

This commit is contained in:
Josh Harvey 2017-07-09 13:31:01 +03:00
parent b7aa655587
commit 5a26f718c5
9 changed files with 63 additions and 71 deletions

View file

@ -6,7 +6,6 @@ const makeDir = require('make-dir')
const inquirer = require('inquirer') const inquirer = require('inquirer')
const _ = require('lodash/fp') const _ = require('lodash/fp')
const options = require('../options')
const coinUtils = require('../coin-utils') const coinUtils = require('../coin-utils')
const common = require('./common') const common = require('./common')
@ -27,7 +26,7 @@ const PLUGINS = {
module.exports = {run} module.exports = {run}
function installedFilePath (crypto) { function installedFilePath (crypto) {
return path.resolve(options.blockchainDir, crypto.code, '.installed') return path.resolve(coinUtils.cryptoDir(crypto), '.installed')
} }
function isInstalled (crypto) { function isInstalled (crypto) {
@ -57,7 +56,7 @@ function processCryptos (codes) {
function setupCrypto (crypto) { function setupCrypto (crypto) {
logger.info(`Installing ${crypto.display}...`) logger.info(`Installing ${crypto.display}...`)
const cryptoDir = path.resolve(options.blockchainDir, crypto.code) const cryptoDir = coinUtils.cryptoConfigDir(crypto.cryptoCode)
makeDir.sync(cryptoDir) makeDir.sync(cryptoDir)
const cryptoPlugin = plugin(crypto) const cryptoPlugin = plugin(crypto)
const oldDir = process.cwd() const oldDir = process.cwd()

View file

@ -1,5 +1,9 @@
const path = require('path')
const _ = require('lodash/fp') const _ = require('lodash/fp')
const options = require('./options')
const CRYPTO_CURRENCIES = [ const CRYPTO_CURRENCIES = [
{ {
cryptoCode: 'BTC', cryptoCode: 'BTC',
@ -48,7 +52,7 @@ const CRYPTO_CURRENCIES = [
} }
] ]
module.exports = {buildUrl, cryptoCurrencies, getCryptoCurrency} module.exports = {buildUrl, cryptoDir, cryptoConfigDir, cryptoCurrencies, getCryptoCurrency}
function getCryptoCurrency (cryptoCode) { function getCryptoCurrency (cryptoCode) {
const cryptoCurrency = _.find(['cryptoCode', cryptoCode], CRYPTO_CURRENCIES) const cryptoCurrency = _.find(['cryptoCode', cryptoCode], CRYPTO_CURRENCIES)
@ -70,3 +74,14 @@ function buildUrl (cryptoCode, address) {
default: throw new Error(`Unsupported crypto: ${cryptoCode}`) default: throw new Error(`Unsupported crypto: ${cryptoCode}`)
} }
} }
function cryptoDir (cryptoRec) {
const code = cryptoRec.code
const blockchainDir = options.blockchainDir
return path.resolve(blockchainDir, code)
}
function cryptoConfigDir (cryptoRec) {
const configFile = cryptoRec.configFile
return path.resolve(cryptoDir(cryptoRec), configFile)
}

View file

@ -1,9 +1,5 @@
const BigNumber = require('bignumber.js')
const coinUtils = require('../../coin-utils') const coinUtils = require('../../coin-utils')
const TEN = new BigNumber(10)
const PAIRS = { const PAIRS = {
BTC: { BTC: {
USD: 'XXBTZUSD', USD: 'XXBTZUSD',
@ -29,7 +25,8 @@ const PAIRS = {
module.exports = {PAIRS, toUnit} module.exports = {PAIRS, toUnit}
function toUnit (cryptoAtoms, cryptoCoin) { function toUnit (cryptoAtoms, cryptoCode) {
var scale = TEN.pow(coinUtils.unitScale(cryptoCoin)) const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
return cryptoAtoms.div(scale) const unitScale = cryptoRec.unitScale
return cryptoAtoms.shift(-unitScale)
} }

View file

@ -1,20 +1,18 @@
const jsonRpc = require('../../common/json-rpc') const jsonRpc = require('../../common/json-rpc')
const path = require('path')
const options = require('../../../options')
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error') const E = require('../../../error')
const coinUtils = require('../../../coin-utils')
const DEFAULT_PORT = 8332 const cryptoRec = coinUtils.getCryptoCurrency('BTC')
const SATOSHI_SHIFT = 8 const configPath = coinUtils.cryptoDir(cryptoRec)
const unitScale = cryptoRec.unitScale
const configPath = path.resolve(options.blockchainDir, 'bitcoin.conf')
const config = jsonRpc.parseConf(configPath) const config = jsonRpc.parseConf(configPath)
const rpcConfig = { const rpcConfig = {
username: config.rpcuser, username: config.rpcuser,
password: config.rpcpassword, password: config.rpcpassword,
port: config.rpcport || DEFAULT_PORT port: config.rpcport || cryptoRec.defaultPort
} }
function fetch (method, params) { function fetch (method, params) {
@ -29,7 +27,7 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (acount, cryptoCode, confirmations) { function accountBalance (acount, cryptoCode, confirmations) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalance', ['', confirmations])) .then(() => fetch('getbalance', ['', confirmations]))
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
// 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
@ -39,7 +37,7 @@ function balance (account, cryptoCode) {
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, address, cryptoAtoms, cryptoCode) {
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [address, coins]))
@ -56,7 +54,7 @@ function newAddress (account, info) {
function addressBalance (address, confs) { function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs]) return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
function confirmedBalance (address, cryptoCode) { function confirmedBalance (address, cryptoCode) {

View file

@ -1,22 +1,19 @@
const jsonRpc = require('../../common/json-rpc') const jsonRpc = require('../../common/json-rpc')
const path = require('path') const coinUtils = require('../../../coin-utils')
const options = require('../../../options')
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error') const E = require('../../../error')
const DEFAULT_PORT = 9998 const cryptoRec = coinUtils.getCryptoCurrency('DASH')
const SATOSHI_SHIFT = 8 const configPath = coinUtils.cryptoDir(cryptoRec)
const unitScale = cryptoRec.unitScale
const configPath = path.resolve(options.blockchainDir, 'dash.conf')
const config = jsonRpc.parseConf(configPath) const config = jsonRpc.parseConf(configPath)
const rpcConfig = { const rpcConfig = {
username: config.rpcuser, username: config.rpcuser,
password: config.rpcpassword, password: config.rpcpassword,
port: config.rpcport || DEFAULT_PORT port: config.rpcport || cryptoRec.defaultPort
} }
function fetch (method, params) { function fetch (method, params) {
@ -31,7 +28,7 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (acount, cryptoCode, confirmations) { function accountBalance (acount, cryptoCode, confirmations) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalance', ['', confirmations])) .then(() => fetch('getbalance', ['', confirmations]))
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
// 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
@ -41,7 +38,7 @@ function balance (account, cryptoCode) {
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, address, cryptoAtoms, cryptoCode) {
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [address, coins]))
@ -58,7 +55,7 @@ function newAddress (account, info) {
function addressBalance (address, confs) { function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs]) return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
function confirmedBalance (address, cryptoCode) { function confirmedBalance (address, cryptoCode) {

View file

@ -1,18 +1,22 @@
'use strict' 'use strict'
const _ = require('lodash/fp')
const Web3 = require('web3') const Web3 = require('web3')
const web3 = new Web3() const web3 = new Web3()
const hdkey = require('ethereumjs-wallet/hdkey') const hdkey = require('ethereumjs-wallet/hdkey')
const Tx = require('ethereumjs-tx') const Tx = require('ethereumjs-tx')
const pify = require('pify') const pify = require('pify')
const coinUtils = require('../../../coin-utils')
const NAME = 'geth' const NAME = 'geth'
exports.SUPPORTED_MODULES = ['wallet'] exports.SUPPORTED_MODULES = ['wallet']
const paymentPrefixPath = "m/44'/60'/0'/0'" const paymentPrefixPath = "m/44'/60'/0'/0'"
const defaultPrefixPath = "m/44'/60'/1'/0'" const defaultPrefixPath = "m/44'/60'/1'/0'"
const cryptoRec = coinUtils.getCryptoCurrency('ETH')
const defaultPort = cryptoRec.defaultPort
module.exports = { module.exports = {
NAME, NAME,
balance, balance,
@ -27,7 +31,7 @@ module.exports = {
} }
if (!web3.isConnected()) { if (!web3.isConnected()) {
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')) web3.setProvider(new web3.providers.HttpProvider(`http://localhost:${defaultPort}`))
} }
const hex = bigNum => '0x' + bigNum.truncated().toString(16) const hex = bigNum => '0x' + bigNum.truncated().toString(16)
@ -38,7 +42,6 @@ function privateKey (account) {
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) { function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false) return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false)
.then(_.tap(r => console.log('DEBUG113: %s', r)))
.then(pify(web3.eth.sendRawTransaction)) .then(pify(web3.eth.sendRawTransaction))
} }
@ -58,7 +61,6 @@ const confirmedBalance = address => _balance(false, address)
function _balance (includePending, address) { function _balance (includePending, address) {
const block = includePending ? 'pending' : undefined const block = includePending ? 'pending' : undefined
console.log('DEBUG140: %s', address)
return pify(web3.eth.getBalance)(address.toLowerCase(), block) return pify(web3.eth.getBalance)(address.toLowerCase(), block)
} }
@ -80,7 +82,6 @@ function generateTx (_toAddress, wallet, amount, includesFee) {
return Promise.all(promises) return Promise.all(promises)
.then(arr => { .then(arr => {
console.log('DEBUG111')
const gas = arr[0] const gas = arr[0]
const gasPrice = arr[1] const gasPrice = arr[1]
const txCount = arr[2] const txCount = arr[2]
@ -97,7 +98,6 @@ function generateTx (_toAddress, wallet, amount, includesFee) {
from: fromAddress, from: fromAddress,
value: hex(toSend) value: hex(toSend)
} }
console.log('DEBUG112: %j', rawTx)
const tx = new Tx(rawTx) const tx = new Tx(rawTx)
const privateKey = wallet.getPrivateKey() const privateKey = wallet.getPrivateKey()
@ -117,26 +117,19 @@ function defaultAddress (account) {
} }
function sweep (account, cryptoCode, hdIndex) { function sweep (account, cryptoCode, hdIndex) {
console.log('DEBUG115: %d', hdIndex)
const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet() const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet()
const fromAddress = wallet.getChecksumAddressString() const fromAddress = wallet.getChecksumAddressString()
console.log('DEBUG115.1: %s', fromAddress)
return confirmedBalance(fromAddress) return confirmedBalance(fromAddress)
.then(r => { .then(r => {
console.log('DEBUG116.0: %j', r)
if (r.eq(0)) return if (r.eq(0)) return
console.log('DEBUG116')
return generateTx(defaultAddress(account), wallet, r, true) return generateTx(defaultAddress(account), wallet, r, true)
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx)) .then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
}) })
} }
function newAddress (account, info) { function newAddress (account, info) {
console.log('DEBUG120: %d', info.hdIndex)
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())
} }
@ -149,7 +142,6 @@ function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
return pendingBalance(toAddress) return pendingBalance(toAddress)
.then(pending => { .then(pending => {
console.log('DEBUG114: %s', pending.toString())
if (pending.gte(cryptoAtoms)) return {status: 'published'} if (pending.gte(cryptoAtoms)) return {status: 'published'}
if (pending.gt(0)) return {status: 'insufficientFunds'} if (pending.gt(0)) return {status: 'insufficientFunds'}
return {status: 'notSeen'} return {status: 'notSeen'}

View file

@ -1,26 +1,21 @@
const jsonRpc = require('../../common/json-rpc') const jsonRpc = require('../../common/json-rpc')
const path = require('path') const coinUtils = require('../../../coin-utils')
const options = require('../../../options')
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error') const E = require('../../../error')
const DEFAULT_PORT = 9332 const cryptoRec = coinUtils.getCryptoCurrency('LTC')
const SATOSHI_SHIFT = 8 const configPath = coinUtils.cryptoDir(cryptoRec)
const unitScale = cryptoRec.unitScale
const configPath = path.resolve(options.blockchainDir, 'litecoin.conf')
const config = jsonRpc.parseConf(configPath) const config = jsonRpc.parseConf(configPath)
const rpcConfig = { const rpcConfig = {
username: config.rpcuser, username: config.rpcuser,
password: config.rpcpassword, password: config.rpcpassword,
port: config.rpcport || DEFAULT_PORT port: config.rpcport || cryptoRec.defaultPort
} }
console.log('DEBUG101: %j', configPath)
console.log('DEBUG100: %j', rpcConfig)
function fetch (method, params) { function fetch (method, params) {
return jsonRpc.fetch(rpcConfig, method, params) return jsonRpc.fetch(rpcConfig, method, params)
} }
@ -33,7 +28,7 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (acount, cryptoCode, confirmations) { function accountBalance (acount, cryptoCode, confirmations) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalance', ['', confirmations])) .then(() => fetch('getbalance', ['', confirmations]))
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
// 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
@ -43,7 +38,7 @@ function balance (account, cryptoCode) {
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, address, cryptoAtoms, cryptoCode) {
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [address, coins]))
@ -60,7 +55,7 @@ function newAddress (account, info) {
function addressBalance (address, confs) { function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs]) return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
function confirmedBalance (address, cryptoCode) { function confirmedBalance (address, cryptoCode) {

View file

@ -12,8 +12,9 @@ const CONFIRM_TIME = 180 * SECONDS
let t0 let t0
function _balance (cryptoCode) { function _balance (cryptoCode) {
const unitScale = coinUtils.unitScale(cryptoCode) const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
return BN(10).pow(unitScale).mul(10) const unitScale = cryptoRec.unitScale
BN(10).shift(unitScale).round()
} }
function balance (account, cryptoCode) { function balance (account, cryptoCode) {

View file

@ -1,21 +1,19 @@
const jsonRpc = require('../../common/json-rpc') const jsonRpc = require('../../common/json-rpc')
const path = require('path') const coinUtils = require('../../../coin-utils')
const options = require('../../../options')
const BN = require('../../../bn') const BN = require('../../../bn')
const E = require('../../../error') const E = require('../../../error')
const DEFAULT_PORT = 8232 const cryptoRec = coinUtils.getCryptoCurrency('ZEC')
const SATOSHI_SHIFT = 8 const configPath = coinUtils.cryptoDir(cryptoRec)
const unitScale = cryptoRec.unitScale
const configPath = path.resolve(options.blockchainDir, 'zcash.conf')
const config = jsonRpc.parseConf(configPath) const config = jsonRpc.parseConf(configPath)
const rpcConfig = { const rpcConfig = {
username: config.rpcuser, username: config.rpcuser,
password: config.rpcpassword, password: config.rpcpassword,
port: config.rpcport || DEFAULT_PORT port: config.rpcport || cryptoRec.defaultPort
} }
function fetch (method, params) { function fetch (method, params) {
@ -30,7 +28,7 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (acount, cryptoCode, confirmations) { function accountBalance (acount, cryptoCode, confirmations) {
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalance', ['', confirmations])) .then(() => fetch('getbalance', ['', confirmations]))
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
// 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
@ -40,7 +38,7 @@ function balance (account, cryptoCode) {
} }
function sendCoins (account, address, cryptoAtoms, cryptoCode) { function sendCoins (account, address, cryptoAtoms, cryptoCode) {
const coins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [address, coins])) .then(() => fetch('sendtoaddress', [address, coins]))
@ -57,7 +55,7 @@ function newAddress (account, info) {
function addressBalance (address, confs) { function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs]) return fetch('getreceivedbyaddress', [address, confs])
.then(r => BN(r).shift(SATOSHI_SHIFT).round()) .then(r => BN(r).shift(unitScale).round())
} }
function confirmedBalance (address, cryptoCode) { function confirmedBalance (address, cryptoCode) {