fix geth issues

This commit is contained in:
Josh Harvey 2017-05-13 14:50:32 +03:00
parent f7856c7db7
commit efd8e8220d
3 changed files with 64 additions and 9 deletions

View file

@ -1,10 +1,14 @@
'use strict'
const _ = require('lodash/fp')
const Web3 = require('web3')
const web3 = new Web3()
const hdkey = require('hdkey')
const hdkey = require('ethereumjs-wallet/hdkey')
const Tx = require('ethereumjs-tx')
const pify = require('pify')
const NAME = 'geth'
exports.SUPPORTED_MODULES = ['wallet']
const paymentPrefixPath = "m/44'/60'/0'/0'"
const defaultPrefixPath = "m/44'/60'/1'/0'"
@ -28,6 +32,7 @@ const hex = bigNum => '0x' + bigNum.truncated().toString(16)
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false)
.then(_.tap(r => console.log('DEBUG113: %s', r)))
.then(pify(web3.eth.sendRawTransaction))
}
@ -47,6 +52,7 @@ const confirmedBalance = address => _balance(false, address)
function _balance (includePending, address) {
const block = includePending ? 'pending' : undefined
console.log('DEBUG140: %s', address)
return pify(web3.eth.getBalance)(address.toLowerCase(), block)
}
@ -68,6 +74,7 @@ function generateTx (_toAddress, wallet, amount, includesFee) {
return Promise.all(promises)
.then(arr => {
console.log('DEBUG111')
const gas = arr[0]
const gasPrice = arr[1]
const txCount = arr[2]
@ -84,6 +91,7 @@ function generateTx (_toAddress, wallet, amount, includesFee) {
from: fromAddress,
value: hex(toSend)
}
console.log('DEBUG112: %j', rawTx)
const tx = new Tx(rawTx)
const privateKey = wallet.getPrivateKey()
@ -103,19 +111,26 @@ function defaultAddress (account) {
}
function sweep (account, cryptoCode, hdIndex) {
console.log('DEBUG115: %d', hdIndex)
const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet()
const fromAddress = wallet.getChecksumAddressString()
console.log('DEBUG115.1: %s', fromAddress)
return confirmedBalance(fromAddress)
.then(r => {
console.log('DEBUG116.0: %j', r)
if (r.eq(0)) return
console.log('DEBUG116')
return generateTx(defaultAddress(account), wallet, r, true)
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
})
}
function newAddress (account, info) {
console.log('DEBUG120: %d', info.hdIndex)
const childNode = paymentHdNode(account).deriveChild(info.hdIndex)
return Promise.resolve(childNode.getWallet().getChecksumAddressString())
}
@ -128,6 +143,7 @@ function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
return pendingBalance(toAddress)
.then(pending => {
console.log('DEBUG114: %s', pending.toString())
if (pending.gte(cryptoAtoms)) return {status: 'published'}
if (pending.gt(0)) return {status: 'insufficientFunds'}
return {status: 'notSeen'}
@ -139,12 +155,12 @@ function paymentHdNode (account) {
const masterSeed = account.seed
if (!masterSeed) throw new Error('No master seed!')
const key = hdkey.fromMasterSeed(masterSeed)
return key.derive(paymentPrefixPath)
return key.derivePath(paymentPrefixPath)
}
function defaultHdNode (account) {
const masterSeed = account.seed
if (!masterSeed) throw new Error('No master seed!')
const key = hdkey.fromMasterSeed(masterSeed)
return key.derive(defaultPrefixPath)
return key.derivePath(defaultPrefixPath)
}