2017-01-17 01:19:02 +01:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2016 The btcsuite developers
|
2015-12-01 19:44:58 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2015-04-02 20:13:38 +02:00
|
|
|
package wallet
|
2013-09-09 20:14:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-23 04:16:50 +02:00
|
|
|
"sort"
|
|
|
|
|
2018-05-15 07:11:11 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
|
|
"github.com/btcsuite/btcwallet/wallet/txauthor"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
"github.com/btcsuite/btcwallet/wtxmgr"
|
2013-09-09 20:14:57 +02:00
|
|
|
)
|
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
// byAmount defines the methods needed to satisify sort.Interface to
|
|
|
|
// sort credits by their output amount.
|
|
|
|
type byAmount []wtxmgr.Credit
|
2014-09-25 19:27:18 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
func (s byAmount) Len() int { return len(s) }
|
|
|
|
func (s byAmount) Less(i, j int) bool { return s[i].Amount < s[j].Amount }
|
|
|
|
func (s byAmount) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
2014-09-25 19:27:18 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
func makeInputSource(eligible []wtxmgr.Credit) txauthor.InputSource {
|
|
|
|
// Pick largest outputs first. This is only done for compatibility with
|
|
|
|
// previous tx creation code, not because it's a good idea.
|
|
|
|
sort.Sort(sort.Reverse(byAmount(eligible)))
|
2014-09-25 19:27:18 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
// Current inputs and their total value. These are closed over by the
|
|
|
|
// returned input source and reused across multiple calls.
|
|
|
|
currentTotal := btcutil.Amount(0)
|
|
|
|
currentInputs := make([]*wire.TxIn, 0, len(eligible))
|
|
|
|
currentScripts := make([][]byte, 0, len(eligible))
|
2016-04-25 21:33:37 +02:00
|
|
|
currentInputValues := make([]btcutil.Amount, 0, len(eligible))
|
|
|
|
|
|
|
|
return func(target btcutil.Amount) (btcutil.Amount, []*wire.TxIn,
|
|
|
|
[]btcutil.Amount, [][]byte, error) {
|
2014-09-25 19:27:18 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
for currentTotal < target && len(eligible) != 0 {
|
|
|
|
nextCredit := &eligible[0]
|
|
|
|
eligible = eligible[1:]
|
2016-04-13 06:27:32 +02:00
|
|
|
nextInput := wire.NewTxIn(&nextCredit.OutPoint, nil, nil)
|
2016-02-28 05:30:56 +01:00
|
|
|
currentTotal += nextCredit.Amount
|
|
|
|
currentInputs = append(currentInputs, nextInput)
|
|
|
|
currentScripts = append(currentScripts, nextCredit.PkScript)
|
2016-04-25 21:33:37 +02:00
|
|
|
currentInputValues = append(currentInputValues, nextCredit.Amount)
|
2016-02-28 05:30:56 +01:00
|
|
|
}
|
2016-04-25 21:33:37 +02:00
|
|
|
return currentTotal, currentInputs, currentInputValues, currentScripts, nil
|
2016-02-28 05:30:56 +01:00
|
|
|
}
|
2014-09-25 19:27:18 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
// secretSource is an implementation of txauthor.SecretSource for the wallet's
|
|
|
|
// address manager.
|
|
|
|
type secretSource struct {
|
|
|
|
*waddrmgr.Manager
|
2017-01-17 01:19:02 +01:00
|
|
|
addrmgrNs walletdb.ReadBucket
|
2014-06-20 18:58:21 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
func (s secretSource) GetKey(addr btcutil.Address) (*btcec.PrivateKey, bool, error) {
|
2017-01-17 01:19:02 +01:00
|
|
|
ma, err := s.Address(s.addrmgrNs, addr)
|
2016-02-28 05:30:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2014-06-20 18:58:21 +02:00
|
|
|
}
|
2016-04-25 21:33:37 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
mpka, ok := ma.(waddrmgr.ManagedPubKeyAddress)
|
|
|
|
if !ok {
|
|
|
|
e := fmt.Errorf("managed address type for %v is `%T` but "+
|
|
|
|
"want waddrmgr.ManagedPubKeyAddress", addr, ma)
|
|
|
|
return nil, false, e
|
|
|
|
}
|
|
|
|
privKey, err := mpka.PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return privKey, ma.Compressed(), nil
|
2014-06-20 18:58:21 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
func (s secretSource) GetScript(addr btcutil.Address) ([]byte, error) {
|
2017-01-17 01:19:02 +01:00
|
|
|
ma, err := s.Address(s.addrmgrNs, addr)
|
2016-02-28 05:30:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-25 21:33:37 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
msa, ok := ma.(waddrmgr.ManagedScriptAddress)
|
|
|
|
if !ok {
|
|
|
|
e := fmt.Errorf("managed address type for %v is `%T` but "+
|
|
|
|
"want waddrmgr.ManagedScriptAddress", addr, ma)
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return msa.Script()
|
2013-10-24 00:23:20 +02:00
|
|
|
}
|
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
// txToOutputs creates a signed transaction which includes each output from
|
|
|
|
// outputs. Previous outputs to reedeem are chosen from the passed account's
|
|
|
|
// UTXO set and minconf policy. An additional output may be added to return
|
|
|
|
// change to the wallet. An appropriate fee is included based on the wallet's
|
|
|
|
// current relay fee. The wallet must be unlocked to create the transaction.
|
2017-11-19 00:22:46 +01:00
|
|
|
func (w *Wallet) txToOutputs(outputs []*wire.TxOut, account uint32,
|
|
|
|
minconf int32, feeSatPerKb btcutil.Amount) (tx *txauthor.AuthoredTx, err error) {
|
|
|
|
|
Modernize the RPC server.
This is a rather monolithic commit that moves the old RPC server to
its own package (rpc/legacyrpc), introduces a new RPC server using
gRPC (rpc/rpcserver), and provides the ability to defer wallet loading
until request at a later time by an RPC (--noinitialload).
The legacy RPC server remains the default for now while the new gRPC
server is not enabled by default. Enabling the new server requires
setting a listen address (--experimenalrpclisten). This experimental
flag is used to effectively feature gate the server until it is ready
to use as a default. Both RPC servers can be run at the same time,
but require binding to different listen addresses.
In theory, with the legacy RPC server now living in its own package it
should become much easier to unit test the handlers. This will be
useful for any future changes to the package, as compatibility with
Core's wallet is still desired.
Type safety has also been improved in the legacy RPC server. Multiple
handler types are now used for methods that do and do not require the
RPC client as a dependency. This can statically help prevent nil
pointer dereferences, and was very useful for catching bugs during
refactoring.
To synchronize the wallet loading process between the main package
(the default) and through the gRPC WalletLoader service (with the
--noinitialload option), as well as increasing the loose coupling of
packages, a new wallet.Loader type has been added. All creating and
loading of existing wallets is done through a single Loader instance,
and callbacks can be attached to the instance to run after the wallet
has been opened. This is how the legacy RPC server is associated with
a loaded wallet, even after the wallet is loaded by a gRPC method in a
completely unrelated package.
Documentation for the new RPC server has been added to the
rpc/documentation directory. The documentation includes a
specification for the new RPC API, addresses how to make changes to
the server implementation, and provides short example clients in
several different languages.
Some of the new RPC methods are not implementated exactly as described
by the specification. These are considered bugs with the
implementation, not the spec. Known bugs are commented as such.
2015-06-01 21:57:50 +02:00
|
|
|
chainClient, err := w.requireChainClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-25 01:07:39 +02:00
|
|
|
err = walletdb.Update(w.db, func(dbtx walletdb.ReadWriteTx) error {
|
|
|
|
addrmgrNs := dbtx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2013-12-04 01:22:47 +01:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
// Get current block's height and hash.
|
|
|
|
bs, err := chainClient.BlockStamp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-13 21:52:49 +02:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
eligible, err := w.findEligibleOutputs(dbtx, account, minconf, bs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-11-22 19:42:25 +01:00
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
|
|
|
|
inputSource := makeInputSource(eligible)
|
|
|
|
changeSource := func() ([]byte, error) {
|
2018-02-14 06:36:39 +01:00
|
|
|
// Derive the change output script. As a hack to allow
|
|
|
|
// spending from the imported account, change addresses
|
|
|
|
// are created from account 0.
|
2017-01-17 01:19:02 +01:00
|
|
|
var changeAddr btcutil.Address
|
|
|
|
var err error
|
|
|
|
if account == waddrmgr.ImportedAddrAccount {
|
2018-02-14 06:36:39 +01:00
|
|
|
changeAddr, err = w.newChangeAddress(addrmgrNs, 0)
|
2017-01-17 01:19:02 +01:00
|
|
|
} else {
|
2018-02-14 06:36:39 +01:00
|
|
|
changeAddr, err = w.newChangeAddress(addrmgrNs, account)
|
2017-01-17 01:19:02 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return txscript.PayToAddrScript(changeAddr)
|
|
|
|
}
|
2017-11-19 00:22:46 +01:00
|
|
|
tx, err = txauthor.NewUnsignedTransaction(outputs, feeSatPerKb,
|
2017-01-17 01:19:02 +01:00
|
|
|
inputSource, changeSource)
|
2016-02-28 05:30:56 +01:00
|
|
|
if err != nil {
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
2013-11-04 17:50:32 +01: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
|
|
|
|
2018-02-14 06:36:39 +01:00
|
|
|
// Randomize change position, if change exists, before signing.
|
|
|
|
// This doesn't affect the serialize size, so the change amount
|
|
|
|
// will still be valid.
|
2017-01-17 01:19:02 +01:00
|
|
|
if tx.ChangeIndex >= 0 {
|
|
|
|
tx.RandomizeChangePosition()
|
|
|
|
}
|
2013-12-04 01:22:47 +01:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
return tx.AddAllInputScripts(secretSource{w.Manager, addrmgrNs})
|
|
|
|
})
|
2014-09-25 19:27:18 +02:00
|
|
|
if err != nil {
|
2016-02-28 05:30:56 +01:00
|
|
|
return nil, err
|
2014-09-25 19:27:18 +02:00
|
|
|
}
|
|
|
|
|
2016-04-25 21:33:37 +02:00
|
|
|
err = validateMsgTx(tx.Tx, tx.PrevScripts, tx.PrevInputValues)
|
2016-02-28 05:30:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-29 00:12:01 +02:00
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
if tx.ChangeIndex >= 0 && account == waddrmgr.ImportedAddrAccount {
|
|
|
|
changeAmount := btcutil.Amount(tx.Tx.TxOut[tx.ChangeIndex].Value)
|
|
|
|
log.Warnf("Spend from imported account produced change: moving"+
|
|
|
|
" %v from imported account into default account.", changeAmount)
|
2014-07-29 00:12:01 +02:00
|
|
|
}
|
2016-02-28 05:30:56 +01:00
|
|
|
|
2018-10-31 00:20:43 +01:00
|
|
|
// Finally, we'll request the backend to notify us of the transaction
|
|
|
|
// that pays to the change address, if there is one, when it confirms.
|
|
|
|
if tx.ChangeIndex >= 0 {
|
|
|
|
changePkScript := tx.Tx.TxOut[tx.ChangeIndex].PkScript
|
|
|
|
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
|
|
|
|
changePkScript, w.chainParams,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := chainClient.NotifyReceived(addrs); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
return tx, nil
|
2014-07-29 00:12:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
func (w *Wallet) findEligibleOutputs(dbtx walletdb.ReadTx, account uint32, minconf int32, bs *waddrmgr.BlockStamp) ([]wtxmgr.Credit, error) {
|
|
|
|
addrmgrNs := dbtx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)
|
|
|
|
|
|
|
|
unspent, err := w.TxStore.UnspentOutputs(txmgrNs)
|
2014-07-29 00:12:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-06 21:03:24 +02:00
|
|
|
|
|
|
|
// TODO: Eventually all of these filters (except perhaps output locking)
|
|
|
|
// should be handled by the call to UnspentOutputs (or similar).
|
|
|
|
// Because one of these filters requires matching the output script to
|
|
|
|
// the desired account, this change depends on making wtxmgr a waddrmgr
|
|
|
|
// dependancy and requesting unspent outputs for a single account.
|
|
|
|
eligible := make([]wtxmgr.Credit, 0, len(unspent))
|
2014-07-29 00:12:01 +02:00
|
|
|
for i := range unspent {
|
2015-04-06 21:03:24 +02:00
|
|
|
output := &unspent[i]
|
2014-07-29 00:12:01 +02:00
|
|
|
|
2015-04-06 21:03:24 +02:00
|
|
|
// Only include this output if it meets the required number of
|
|
|
|
// confirmations. Coinbase transactions must have have reached
|
|
|
|
// maturity before their outputs may be spent.
|
|
|
|
if !confirmed(minconf, output.Height, bs.Height) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if output.FromCoinBase {
|
2016-08-13 02:27:51 +02:00
|
|
|
target := int32(w.chainParams.CoinbaseMaturity)
|
2015-04-06 21:03:24 +02:00
|
|
|
if !confirmed(target, output.Height, bs.Height) {
|
2014-07-29 00:12:01 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-04-06 21:03:24 +02:00
|
|
|
}
|
2014-07-29 00:12:01 +02:00
|
|
|
|
2015-04-06 21:03:24 +02:00
|
|
|
// Locked unspent outputs are skipped.
|
|
|
|
if w.LockedOutpoint(output.OutPoint) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
// Only include the output if it is associated with the passed
|
|
|
|
// account.
|
|
|
|
//
|
|
|
|
// TODO: Handle multisig outputs by determining if enough of the
|
|
|
|
// addresses are controlled.
|
|
|
|
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
|
2015-04-06 21:03:24 +02:00
|
|
|
output.PkScript, w.chainParams)
|
2016-02-28 05:30:56 +01:00
|
|
|
if err != nil || len(addrs) != 1 {
|
2015-04-06 21:03:24 +02:00
|
|
|
continue
|
2014-07-29 00:12:01 +02:00
|
|
|
}
|
2018-02-14 06:36:39 +01:00
|
|
|
_, addrAcct, err := w.Manager.AddrAccount(addrmgrNs, addrs[0])
|
2015-04-06 21:03:24 +02:00
|
|
|
if err != nil || addrAcct != account {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
eligible = append(eligible, *output)
|
2014-07-29 00:12:01 +02:00
|
|
|
}
|
|
|
|
return eligible, nil
|
|
|
|
}
|
|
|
|
|
2016-02-28 05:30:56 +01:00
|
|
|
// validateMsgTx verifies transaction input scripts for tx. All previous output
|
|
|
|
// scripts from outputs redeemed by the transaction, in the same order they are
|
|
|
|
// spent, must be passed in the prevScripts slice.
|
2016-04-25 21:33:37 +02:00
|
|
|
func validateMsgTx(tx *wire.MsgTx, prevScripts [][]byte, inputValues []btcutil.Amount) error {
|
2016-05-03 08:31:23 +02:00
|
|
|
hashCache := txscript.NewTxSigHashes(tx)
|
2016-02-28 05:30:56 +01:00
|
|
|
for i, prevScript := range prevScripts {
|
|
|
|
vm, err := txscript.NewEngine(prevScript, tx, i,
|
2016-05-03 08:31:23 +02:00
|
|
|
txscript.StandardVerifyFlags, nil, hashCache, int64(inputValues[i]))
|
2014-07-29 00:12:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create script engine: %s", err)
|
|
|
|
}
|
2016-02-28 05:30:56 +01:00
|
|
|
err = vm.Execute()
|
|
|
|
if err != nil {
|
2014-07-29 00:12:01 +02:00
|
|
|
return fmt.Errorf("cannot validate transaction: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|