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 { type CreatedTx struct {
tx *btcutil.Tx tx *btcutil.Tx
time time.Time time time.Time
haschange bool changeAddr btcutil.Address
changeIdx uint32
} }
// ByAmount defines the methods needed to satisify sort.Interface to // 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. // a higher fee if not enough was originally chosen.
txNoInputs := msgtx.Copy() txNoInputs := msgtx.Copy()
var selectedInputs []*tx.RecvTxOut
// These are nil/zeroed until a change address is needed, and reused // These are nil/zeroed until a change address is needed, and reused
// again in case a change utxo has already been chosen. // again in case a change utxo has already been chosen.
var changeAddr btcutil.Address 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 // Get the number of satoshis to increment fee by when searching for
// the minimum tx fee needed. // the minimum tx fee needed.
fee := int64(0) 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 // Check if there are leftover unspent outputs, and return coins back to
// a new address we own. // 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 change := btcin - amt - fee
if change > 0 { 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. // Get a new change address if one has not already been found.
if changeAddr == nil { if changeAddr == nil {
changeAddr, err = a.ChangeAddress(&bs, cfg.KeypoolSize) 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) buf := new(bytes.Buffer)
msgtx.BtcEncode(buf, btcwire.ProtocolVersion) msgtx.BtcEncode(buf, btcwire.ProtocolVersion)
info := &CreatedTx{ info := &CreatedTx{
tx: btcutil.NewTx(msgtx), tx: btcutil.NewTx(msgtx),
time: time.Now(), time: time.Now(),
haschange: hasChange, changeAddr: changeAddr,
changeIdx: changeIndex,
} }
return info, nil 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 // If a change address was added, sync wallet to disk and request
// transaction notifications to the change address. // transaction notifications to the change address.
if createdTx.haschange { if createdTx.changeAddr != nil {
script := createdTx.tx.MsgTx().TxOut[createdTx.changeIdx].PkScript
_, addrs, _, _ := btcscript.ExtractPkScriptAddrs(script, cfg.Net())
AcctMgr.ds.ScheduleWalletWrite(a) AcctMgr.ds.ScheduleWalletWrite(a)
if err := AcctMgr.ds.FlushAccount(a); err != nil { if err := AcctMgr.ds.FlushAccount(a); err != nil {
e := btcjson.Error{ e := btcjson.Error{
@ -1328,7 +1325,7 @@ func sendPairs(icmd btcjson.Cmd, account string, amounts map[string]int64,
} }
return nil, &e return nil, &e
} }
a.ReqNewTxsForAddress(addrs[0]) a.ReqNewTxsForAddress(createdTx.changeAddr)
} }
serializedTx := new(bytes.Buffer) serializedTx := new(bytes.Buffer)