Report correct change address after composing txs.

This commit is contained in:
Josh Rickmar 2014-04-08 17:49:02 -05:00
parent eb567f2590
commit 902bbd1111
2 changed files with 12 additions and 22 deletions

View file

@ -61,10 +61,9 @@ var TxFeeIncrement = struct {
}
type CreatedTx struct {
tx *btcutil.Tx
time time.Time
haschange bool
changeIdx uint32
tx *btcutil.Tx
time time.Time
changeAddr btcutil.Address
}
// ByAmount defines the methods needed to satisify sort.Interface to
@ -185,14 +184,11 @@ func (a *Account) txToPairs(pairs map[string]int64, minconf int) (*CreatedTx, er
// a higher fee if not enough was originally chosen.
txNoInputs := msgtx.Copy()
var selectedInputs []*tx.RecvTxOut
// These are nil/zeroed until a change address is needed, and reused
// again in case a change utxo has already been chosen.
var changeAddr btcutil.Address
var selectedInputs []*tx.RecvTxOut
hasChange := false
changeIndex := uint32(0)
// Get the number of satoshis to increment fee by when searching for
// the minimum tx fee needed.
fee := int64(0)
@ -209,13 +205,11 @@ func (a *Account) txToPairs(pairs map[string]int64, minconf int) (*CreatedTx, er
// Check if there are leftover unspent outputs, and return coins back to
// a new address we own.
//
// TODO: change needs to be inserted into a random txout index, or else
// this is a privacy risk.
change := btcin - amt - fee
if change > 0 {
hasChange = true
// TODO: this needs to be randomly inserted into the
// tx, or else this is a privacy risk
changeIndex = 0
// Get a new change address if one has not already been found.
if changeAddr == nil {
changeAddr, err = a.ChangeAddress(&bs, cfg.KeypoolSize)
@ -301,10 +295,9 @@ func (a *Account) txToPairs(pairs map[string]int64, minconf int) (*CreatedTx, er
buf := new(bytes.Buffer)
msgtx.BtcEncode(buf, btcwire.ProtocolVersion)
info := &CreatedTx{
tx: btcutil.NewTx(msgtx),
time: time.Now(),
haschange: hasChange,
changeIdx: changeIndex,
tx: btcutil.NewTx(msgtx),
time: time.Now(),
changeAddr: changeAddr,
}
return info, nil
}

View file

@ -1316,10 +1316,7 @@ func sendPairs(icmd btcjson.Cmd, account string, amounts map[string]int64,
// If a change address was added, sync wallet to disk and request
// transaction notifications to the change address.
if createdTx.haschange {
script := createdTx.tx.MsgTx().TxOut[createdTx.changeIdx].PkScript
_, addrs, _, _ := btcscript.ExtractPkScriptAddrs(script, cfg.Net())
if createdTx.changeAddr != nil {
AcctMgr.ds.ScheduleWalletWrite(a)
if err := AcctMgr.ds.FlushAccount(a); err != nil {
e := btcjson.Error{
@ -1328,7 +1325,7 @@ func sendPairs(icmd btcjson.Cmd, account string, amounts map[string]int64,
}
return nil, &e
}
a.ReqNewTxsForAddress(addrs[0])
a.ReqNewTxsForAddress(createdTx.changeAddr)
}
serializedTx := new(bytes.Buffer)