From 902bbd1111eeef273e393375a155b7abc303da47 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 8 Apr 2014 17:49:02 -0500 Subject: [PATCH] Report correct change address after composing txs. --- createtx.go | 27 ++++++++++----------------- rpcserver.go | 7 ++----- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/createtx.go b/createtx.go index d08a4ed..e4e3c97 100644 --- a/createtx.go +++ b/createtx.go @@ -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 } diff --git a/rpcserver.go b/rpcserver.go index f667929..49f74a5 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -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)