2013-09-09 20:14:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcscript"
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwallet/tx"
|
2013-11-12 18:01:32 +01:00
|
|
|
"github.com/conformal/btcwallet/wallet"
|
2013-09-09 20:14:57 +02:00
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"sort"
|
2013-10-07 21:14:39 +02:00
|
|
|
"sync"
|
2013-09-09 20:14:57 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrInsufficientFunds represents an error where there are not enough
|
|
|
|
// funds from unspent tx outputs for a wallet to create a transaction.
|
|
|
|
var ErrInsufficientFunds = errors.New("insufficient funds")
|
|
|
|
|
|
|
|
// ErrUnknownBitcoinNet represents an error where the parsed or
|
|
|
|
// requested bitcoin network is invalid (neither mainnet nor testnet).
|
|
|
|
var ErrUnknownBitcoinNet = errors.New("unknown bitcoin network")
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
// ErrNonPositiveAmount represents an error where a bitcoin amount is
|
|
|
|
// not positive (either negative, or zero).
|
|
|
|
var ErrNonPositiveAmount = errors.New("amount is not positive")
|
|
|
|
|
|
|
|
// ErrNegativeFee represents an error where a fee is erroneously
|
|
|
|
// negative.
|
|
|
|
var ErrNegativeFee = errors.New("fee is negative")
|
|
|
|
|
2013-10-07 21:14:39 +02:00
|
|
|
// TxFee represents the global transaction fee added to newly-created
|
2013-10-29 07:17:49 +01:00
|
|
|
// transactions and sent as a reward to the block miner. i is measured
|
|
|
|
// in satoshis.
|
|
|
|
var TxFee = struct {
|
2013-10-07 21:14:39 +02:00
|
|
|
sync.Mutex
|
|
|
|
i int64
|
2013-10-29 07:17:49 +01:00
|
|
|
}{
|
|
|
|
i: 10000, // This is a fee of 0.0001 BTC.
|
2013-10-07 21:14:39 +02:00
|
|
|
}
|
|
|
|
|
2013-11-05 00:58:41 +01:00
|
|
|
// CreatedTx is a type holding information regarding a newly-created
|
|
|
|
// transaction, including the raw bytes, inputs, and an address and UTXO
|
|
|
|
// for change (if any).
|
|
|
|
type CreatedTx struct {
|
|
|
|
rawTx []byte
|
2013-11-22 19:42:25 +01:00
|
|
|
time time.Time
|
2013-11-05 00:58:41 +01:00
|
|
|
inputs []*tx.Utxo
|
2013-11-22 19:42:25 +01:00
|
|
|
outputs []tx.Pair
|
|
|
|
btcout int64
|
|
|
|
fee int64
|
2013-11-05 00:58:41 +01:00
|
|
|
changeAddr string
|
|
|
|
changeUtxo *tx.Utxo
|
|
|
|
}
|
|
|
|
|
|
|
|
// TXID is a transaction hash identifying a transaction.
|
2013-11-22 19:42:25 +01:00
|
|
|
type TXID btcwire.ShaHash
|
2013-11-05 00:58:41 +01:00
|
|
|
|
2013-10-24 00:23:20 +02:00
|
|
|
// UnminedTXs holds a map of transaction IDs as keys mapping to a
|
2013-11-22 19:42:25 +01:00
|
|
|
// CreatedTx structure. If sending a raw transaction succeeds, the
|
|
|
|
// tx is added to this map and checked again after each new block.
|
|
|
|
// If the new block contains a tx, it is removed from this map.
|
2013-10-24 00:23:20 +02:00
|
|
|
var UnminedTxs = struct {
|
|
|
|
sync.Mutex
|
2013-11-05 00:58:41 +01:00
|
|
|
m map[TXID]*CreatedTx
|
2013-10-24 00:23:20 +02:00
|
|
|
}{
|
2013-11-05 00:58:41 +01:00
|
|
|
m: make(map[TXID]*CreatedTx),
|
2013-10-24 00:23:20 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// ByAmount defines the methods needed to satisify sort.Interface to
|
|
|
|
// sort a slice of Utxos by their amount.
|
|
|
|
type ByAmount []*tx.Utxo
|
|
|
|
|
|
|
|
func (u ByAmount) Len() int {
|
|
|
|
return len(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u ByAmount) Less(i, j int) bool {
|
|
|
|
return u[i].Amt < u[j].Amt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u ByAmount) Swap(i, j int) {
|
|
|
|
u[i], u[j] = u[j], u[i]
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
// selectInputs selects the minimum number possible of unspent
|
2013-09-09 20:14:57 +02:00
|
|
|
// outputs to use to create a new transaction that spends amt satoshis.
|
2013-10-14 22:14:04 +02:00
|
|
|
// Previous outputs with less than minconf confirmations are ignored. btcout
|
|
|
|
// is the total number of satoshis which would be spent by the combination
|
|
|
|
// of all selected previous outputs. err will equal ErrInsufficientFunds if there
|
2013-09-09 20:14:57 +02:00
|
|
|
// are not enough unspent outputs to spend amt.
|
2013-10-14 22:14:04 +02:00
|
|
|
func selectInputs(s tx.UtxoStore, amt uint64, minconf int) (inputs []*tx.Utxo, btcout uint64, err error) {
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
// Create list of eligible unspent previous outputs to use as tx
|
|
|
|
// inputs, and sort by the amount in reverse order so a minimum number
|
|
|
|
// of inputs is needed.
|
2013-10-28 19:10:37 +01:00
|
|
|
eligible := make([]*tx.Utxo, 0, len(s))
|
2013-09-09 20:14:57 +02:00
|
|
|
for _, utxo := range s {
|
2013-10-28 19:10:37 +01:00
|
|
|
// TODO(jrick): if Height is -1, the UTXO is the result of spending
|
|
|
|
// to a change address, resulting in a UTXO not yet mined in a block.
|
|
|
|
// For now, disallow creating transactions until these UTXOs are mined
|
|
|
|
// into a block and show up as part of the balance.
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
if utxo.Height != -1 && int(bs.Height-utxo.Height) >= minconf {
|
2013-09-09 20:14:57 +02:00
|
|
|
eligible = append(eligible, utxo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(sort.Reverse(ByAmount(eligible)))
|
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
// Iterate throguh eligible transactions, appending to outputs and
|
2013-09-09 20:14:57 +02:00
|
|
|
// increasing btcout. This is finished when btcout is greater than the
|
|
|
|
// requested amt to spend.
|
|
|
|
for _, u := range eligible {
|
2013-10-14 22:14:04 +02:00
|
|
|
inputs = append(inputs, u)
|
2013-09-09 20:14:57 +02:00
|
|
|
if btcout += u.Amt; btcout >= amt {
|
2013-10-14 22:14:04 +02:00
|
|
|
return inputs, btcout, nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if btcout < amt {
|
|
|
|
return nil, 0, ErrInsufficientFunds
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:14:04 +02:00
|
|
|
return inputs, btcout, nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// txToPairs creates a raw transaction sending the amounts for each
|
|
|
|
// address/amount pair and fee to each address and the miner. minconf
|
|
|
|
// specifies the minimum number of confirmations required before an
|
|
|
|
// unspent output is eligible for spending. Leftover input funds not sent
|
|
|
|
// to addr or as a fee for the miner are sent to a newly generated
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
// address. If change is needed to return funds back to an owned
|
|
|
|
// address, changeUtxo will point to a unconfirmed (height = -1, zeroed
|
|
|
|
// block hash) Utxo. ErrInsufficientFunds is returned if there are not
|
|
|
|
// enough eligible unspent outputs to create the transaction.
|
2013-11-14 18:15:16 +01:00
|
|
|
func (w *Account) txToPairs(pairs map[string]int64, fee int64, minconf int) (*CreatedTx, error) {
|
2013-09-09 20:14:57 +02:00
|
|
|
// Recorded unspent transactions should not be modified until this
|
|
|
|
// finishes.
|
|
|
|
w.UtxoStore.RLock()
|
|
|
|
defer w.UtxoStore.RUnlock()
|
|
|
|
|
|
|
|
// Create a new transaction which will include all input scripts.
|
|
|
|
msgtx := btcwire.NewMsgTx()
|
|
|
|
|
|
|
|
// Calculate minimum amount needed for inputs.
|
2013-11-12 18:01:32 +01:00
|
|
|
var amt int64
|
2013-09-09 20:14:57 +02:00
|
|
|
for _, v := range pairs {
|
2013-11-12 18:01:32 +01:00
|
|
|
// Error out if any amount is negative.
|
|
|
|
if v <= 0 {
|
|
|
|
return nil, ErrNonPositiveAmount
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
amt += v
|
|
|
|
}
|
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
if fee < 0 {
|
|
|
|
return nil, ErrNegativeFee
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Select unspent outputs to be used in transaction.
|
2013-11-12 18:01:32 +01:00
|
|
|
inputs, btcout, err := selectInputs(w.UtxoStore.s, uint64(amt+fee),
|
|
|
|
minconf)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, err
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
2013-11-22 19:42:25 +01:00
|
|
|
// outputs is a tx.Pair slice representing each output that is created
|
|
|
|
// by the transaction.
|
|
|
|
outputs := make([]tx.Pair, 0, len(pairs)+1)
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Add outputs to new tx.
|
|
|
|
for addr, amt := range pairs {
|
|
|
|
addr160, _, err := btcutil.DecodeAddress(addr)
|
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot decode address: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spend amt to addr160
|
|
|
|
pkScript, err := btcscript.PayToPubKeyHashScript(addr160)
|
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot create txout script: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
txout := btcwire.NewTxOut(int64(amt), pkScript)
|
|
|
|
msgtx.AddTxOut(txout)
|
2013-11-22 19:42:25 +01:00
|
|
|
|
|
|
|
// Create amount, address pair and add to outputs.
|
|
|
|
out := tx.Pair{
|
|
|
|
Amount: amt,
|
|
|
|
PubkeyHash: addr160,
|
|
|
|
}
|
|
|
|
outputs = append(outputs, out)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if there are leftover unspent outputs, and return coins back to
|
|
|
|
// a new address we own.
|
2013-10-28 16:18:37 +01:00
|
|
|
var changeUtxo *tx.Utxo
|
|
|
|
var changeAddr string
|
2013-11-12 18:01:32 +01:00
|
|
|
if btcout > uint64(amt+fee) {
|
2013-09-09 20:14:57 +02:00
|
|
|
// Create a new address to spend leftover outputs to.
|
|
|
|
// TODO(jrick): use the next chained address, not the next unused.
|
2013-10-28 16:18:37 +01:00
|
|
|
var err error
|
2013-11-21 17:57:28 +01:00
|
|
|
// Get current block's height and hash.
|
|
|
|
bs, err := GetCurBlock()
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-11-21 17:57:28 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
changeAddr, err = w.NextChainedAddress(&bs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get next address: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spend change
|
2013-11-12 18:01:32 +01:00
|
|
|
change := btcout - uint64(amt+fee)
|
2013-10-28 16:18:37 +01:00
|
|
|
changeAddrHash, _, err := btcutil.DecodeAddress(changeAddr)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot decode new address: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-10-28 16:18:37 +01:00
|
|
|
pkScript, err := btcscript.PayToPubKeyHashScript(changeAddrHash)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot create txout script: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-10-11 16:53:55 +02:00
|
|
|
msgtx.AddTxOut(btcwire.NewTxOut(int64(change), pkScript))
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
|
|
|
|
changeUtxo = &tx.Utxo{
|
|
|
|
Amt: change,
|
|
|
|
Out: tx.OutPoint{
|
|
|
|
// Hash is unset (zeroed) here and must be filled in
|
|
|
|
// with the transaction hash of the complete
|
|
|
|
// transaction.
|
|
|
|
Index: uint32(len(pairs)),
|
|
|
|
},
|
|
|
|
Height: -1,
|
|
|
|
Subscript: pkScript,
|
|
|
|
}
|
2013-10-28 16:18:37 +01:00
|
|
|
copy(changeUtxo.AddrHash[:], changeAddrHash)
|
2013-11-22 19:42:25 +01:00
|
|
|
|
|
|
|
// Add change to outputs.
|
|
|
|
out := tx.Pair{
|
|
|
|
Amount: int64(change),
|
|
|
|
PubkeyHash: changeAddrHash,
|
|
|
|
}
|
|
|
|
outputs = append(outputs, out)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Selected unspent outputs become new transaction's inputs.
|
2013-10-14 22:14:04 +02:00
|
|
|
for _, ip := range inputs {
|
|
|
|
msgtx.AddTxIn(btcwire.NewTxIn((*btcwire.OutPoint)(&ip.Out), nil))
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-10-14 22:14:04 +02:00
|
|
|
for i, ip := range inputs {
|
|
|
|
addrstr, err := btcutil.EncodeAddress(ip.AddrHash[:], w.Wallet.Net())
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, err
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-12-02 20:56:06 +01:00
|
|
|
privkey, err := w.AddressKey(addrstr)
|
2013-11-12 18:01:32 +01:00
|
|
|
if err == wallet.ErrWalletLocked {
|
|
|
|
return nil, wallet.ErrWalletLocked
|
|
|
|
} else if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot get address key: %v", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-12-02 20:56:06 +01:00
|
|
|
ai, err := w.AddressInfo(addrstr)
|
2013-11-04 17:50:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get address info: %v", err)
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
|
|
|
sigscript, err := btcscript.SignatureScript(msgtx, i,
|
2013-11-04 17:50:32 +01:00
|
|
|
ip.Subscript, btcscript.SigHashAll, privkey, ai.Compressed)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot create sigscript: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
msgtx.TxIn[i].SignatureScript = sigscript
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate msgtx before returning the raw transaction.
|
2013-10-25 21:21:38 +02:00
|
|
|
flags := btcscript.ScriptCanonicalSignatures
|
2013-10-11 16:53:55 +02:00
|
|
|
bip16 := time.Now().After(btcscript.Bip16Activation)
|
2013-10-25 21:21:38 +02:00
|
|
|
if bip16 {
|
|
|
|
flags |= btcscript.ScriptBip16
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
for i, txin := range msgtx.TxIn {
|
2013-10-14 22:14:04 +02:00
|
|
|
engine, err := btcscript.NewScript(txin.SignatureScript, inputs[i].Subscript, i,
|
2013-10-25 21:21:38 +02:00
|
|
|
msgtx, flags)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot create script engine: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
if err = engine.Execute(); err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot validate transaction: %s", err)
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill Tx hash of change outpoint with transaction hash.
|
|
|
|
if changeUtxo != nil {
|
|
|
|
txHash, err := msgtx.TxSha()
|
|
|
|
if err != nil {
|
2013-10-28 16:18:37 +01:00
|
|
|
return nil, fmt.Errorf("cannot create transaction hash: %s", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
copy(changeUtxo.Out.Hash[:], txHash[:])
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
msgtx.BtcEncode(buf, btcwire.ProtocolVersion)
|
2013-11-22 19:42:25 +01:00
|
|
|
info := &CreatedTx{
|
|
|
|
rawTx: buf.Bytes(),
|
|
|
|
time: time.Now(),
|
|
|
|
inputs: inputs,
|
|
|
|
outputs: outputs,
|
|
|
|
btcout: int64(btcout),
|
|
|
|
fee: fee,
|
|
|
|
changeAddr: changeAddr,
|
|
|
|
changeUtxo: changeUtxo,
|
|
|
|
}
|
|
|
|
return info, nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|