From 32ca19322a5db259736eaa9766c0979152ec4d38 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 29 Jan 2016 21:10:10 -0800 Subject: [PATCH] Allow spending from the waddrmgr.ImportedAddrAccount via wallet.SpendPairs. Previously, when creating a change address during the process of creating a new transaction an error case would be hit in the waddrmgr triggered by attempting to derive a new internal address from under a waddrmgr.ImportedAddrAccount. To remedy this error, we now use the default account for change when spending outputs from an imported key. This approach allows funds under the control of imported private keys to be protected under the wallet's seed as soon as they've been partially spent. --- wallet/createtx.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/wallet/createtx.go b/wallet/createtx.go index 7e433e4..c60c0ec 100644 --- a/wallet/createtx.go +++ b/wallet/createtx.go @@ -216,6 +216,13 @@ func createTx(eligible []wtxmgr.Credit, feeEst = minimumFee(feeIncrement, szEst, msgtx.TxOut, inputs, bs.Height, disallowFree) } + // If we're spending the outputs of an imported address, we default + // to generating change addresses from the default account. + prevAccount := account + if account == waddrmgr.ImportedAddrAccount { + account = waddrmgr.DefaultAccountNum + } + var changeAddr btcutil.Address // changeIdx is -1 unless there's a change output. changeIdx := -1 @@ -241,6 +248,11 @@ func createTx(eligible []wtxmgr.Credit, } if feeForSize(feeIncrement, msgtx.SerializeSize()) <= feeEst { + if change > 0 && prevAccount == waddrmgr.ImportedAddrAccount { + log.Warnf("Spend from imported account produced change: moving"+ + " %v from imported account into default account.", change) + } + // The required fee for this size is less than or equal to what // we guessed, so we're done. break