Skip to content

Commit

Permalink
Draft Approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ziggie1984 committed Feb 13, 2023
1 parent 9398358 commit 1272414
Show file tree
Hide file tree
Showing 8 changed files with 811 additions and 220 deletions.
60 changes: 58 additions & 2 deletions cmd/sweepaccount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/btcsuite/btcwallet/wallet/txrules"
"github.com/btcsuite/btcwallet/wallet/txsizes"
"github.com/btcsuite/btcwallet/wtxmgr"
"github.com/jessevdk/go-flags"
)

Expand Down Expand Up @@ -187,6 +188,56 @@ func makeInputSource(outputs []btcjson.ListUnspentResult) txauthor.InputSource {
}
}

// fetchInputs fetches every unspent output with non-zero output values.
func fetchInputs(outputs []btcjson.ListUnspentResult) ([]wtxmgr.Credit, error) {
var (
totalInputValue btcutil.Amount
inputs = make([]wtxmgr.Credit, 0, len(outputs))
sourceErr error
)
for _, output := range outputs {
output := output

outputAmount, err := btcutil.NewAmount(output.Amount)
if err != nil {
sourceErr = fmt.Errorf(
"invalid amount `%v` in listunspent result",
output.Amount)
break
}
if outputAmount == 0 {
continue
}
if !saneOutputValue(outputAmount) {
sourceErr = fmt.Errorf(
"impossible output amount `%v` in listunspent result",
outputAmount)
break
}
totalInputValue += outputAmount

previousOutPoint, err := parseOutPoint(&output)
if err != nil {
sourceErr = fmt.Errorf(
"invalid data in listunspent result: %v",
err)
break
}

inputs = append(inputs, wtxmgr.Credit{
OutPoint: previousOutPoint,
Amount: outputAmount,
})
}

if sourceErr == nil && totalInputValue == 0 {
sourceErr = noInputValue{}
}

return inputs, sourceErr

}

// makeDestinationScriptSource creates a ChangeSource which is used to receive
// all correlated previous input value. A non-change address is created by this
// function.
Expand Down Expand Up @@ -277,10 +328,15 @@ func sweep() error {
numErrors++
}
for _, previousOutputs := range sourceOutputs {
inputSource := makeInputSource(previousOutputs)
// inputSource := makeInputSource(previousOutputs)
inputs, err := fetchInputs(previousOutputs)
if err != nil {
return err
}
destinationSource := makeDestinationScriptSource(rpcClient, opts.DestinationAccount)
// We are only selecting postive yieling outputs.
tx, err := txauthor.NewUnsignedTransaction(nil, opts.FeeRate.Amount,
inputSource, destinationSource)
inputs, txauthor.PositiveYieldingSelection, destinationSource)
if err != nil {
if err != (noInputValue{}) {
reportError("Failed to create unsigned transaction: %v", err)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ require (
)

go 1.16

replace github.com/btcsuite/btcwallet/wallet/txauthor v1.3.2 => /Users/ziggie-pro/working_freetime/btcwallet/wallet/txauthor

replace github.com/btcsuite/btcwallet/wallet/txsizes v1.3.2 => /Users/ziggie-pro/working_freetime/btcwallet/wallet/txsizes
12 changes: 7 additions & 5 deletions wallet/createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
return err
}

var inputSource txauthor.InputSource
// var inputSource txauthor.InputSource
// We need to define the Selection Strategy
var inputSelectionStrategy txauthor.InputSelectionStrategy = txauthor.ConstantSelection

switch coinSelectionStrategy {
// Pick largest outputs first.
case CoinSelectionLargest:
sort.Sort(sort.Reverse(byAmount(eligible)))
inputSource = makeInputSource(eligible)
inputSelectionStrategy = txauthor.PositiveYieldingSelection

// Select coins at random. This prevents the creation of ever
// smaller utxos over time that may never become economical to
Expand All @@ -170,12 +172,12 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
positivelyYielding[i], positivelyYielding[j] =
positivelyYielding[j], positivelyYielding[i]
})

inputSource = makeInputSource(positivelyYielding)
inputSelectionStrategy = txauthor.RandomSelection
eligible = positivelyYielding
}

tx, err = txauthor.NewUnsignedTransaction(
outputs, feeSatPerKb, inputSource, changeSource,
outputs, feeSatPerKb, eligible, inputSelectionStrategy, changeSource,
)
if err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions wallet/psbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (w *Wallet) FundPsbt(packet *psbt.Packet, keyScope *waddrmgr.KeyScope,
PkScript: utxo.PkScript,
}
}
inputSource := constantInputSource(credits)
// inputSource := constantInputSource(credits)

// Build the TxCreateOption to retrieve the change scope.
opts := defaultTxCreateOptions()
Expand All @@ -197,25 +197,25 @@ func (w *Wallet) FundPsbt(packet *psbt.Packet, keyScope *waddrmgr.KeyScope,
dbtx, opts.changeKeyScope, account,
)
if err != nil {
return err
return fmt.Errorf("could not add change address to "+
"database: %v", err)
}

// Ask the txauthor to create a transaction with our
// selected coins. This will perform fee estimation and
// add a change output if necessary.
tx, err = txauthor.NewUnsignedTransaction(
txOut, feeSatPerKB, inputSource, changeSource,
txOut, feeSatPerKB, credits, txauthor.ConstantSelection, changeSource,
)
if err != nil {
return fmt.Errorf("fee estimation not "+
"successful: %v", err)
"successful: %w", err)
}

return nil
})
if err != nil {
return 0, fmt.Errorf("could not add change address to "+
"database: %v", err)
return 0, err
}
}

Expand Down
Loading

0 comments on commit 1272414

Please sign in to comment.