Skip to content

Commit

Permalink
wallet: Add selectedutxos to txToOutputs
Browse files Browse the repository at this point in the history
Signed-off-by: Ononiwu Maureen <[email protected]>
  • Loading branch information
Ononiwu Maureen committed Mar 8, 2024
1 parent 10dd82a commit ed1195c
Show file tree
Hide file tree
Showing 3 changed files with 484 additions and 370 deletions.
69 changes: 48 additions & 21 deletions wallet/createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func (s secretSource) GetScript(addr btcutil.Address) ([]byte, error) {
func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
coinSelectKeyScope, changeKeyScope *waddrmgr.KeyScope,
account uint32, minconf int32, feeSatPerKb btcutil.Amount,
coinSelectionStrategy CoinSelectionStrategy, dryRun bool) (
*txauthor.AuthoredTx, error) {
coinSelectionStrategy CoinSelectionStrategy, dryRun bool,
selectedUtxos []wire.OutPoint) (*txauthor.AuthoredTx, error) {

chainClient, err := w.requireChainClient()
if err != nil {
Expand Down Expand Up @@ -174,27 +174,54 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
return err
}

// Wrap our coins in a type that implements the SelectableCoin
// interface, so we can arrange them according to the selected
// coin selection strategy.
wrappedEligible := make([]Coin, len(eligible))
for i := range eligible {
wrappedEligible[i] = Coin{
TxOut: wire.TxOut{
Value: int64(eligible[i].Amount),
PkScript: eligible[i].PkScript,
},
OutPoint: eligible[i].OutPoint,
var inputSource txauthor.InputSource
if len(selectedUtxos) > 0 {
mapEligibleToOutpoint := make(map[wire.OutPoint]wtxmgr.
Credit)

for _, e := range eligible {
mapEligibleToOutpoint[e.OutPoint] = e
}

var eligibleSelectedUtxo []wtxmgr.Credit
for _, outpoint := range selectedUtxos {
e, ok := mapEligibleToOutpoint[outpoint]

if !ok {
return fmt.Errorf(
"selected outpoint"+
"not eligible for "+
"spending: %v", outpoint)
}
eligibleSelectedUtxo = append(
eligibleSelectedUtxo, e)
}
}
arrangedCoins, err := coinSelectionStrategy.ArrangeCoins(
wrappedEligible, feeSatPerKb,
)
if err != nil {
return err
}

inputSource := makeInputSource(arrangedCoins)
inputSource = constantInputSource(eligibleSelectedUtxo)

} else {
// Wrap our coins in a type that implements the
// SelectableCoin interface, so we can arrange them
// according to the selected coin selection strategy.
wrappedEligible := make([]Coin, len(eligible))
for i := range eligible {
wrappedEligible[i] = Coin{
TxOut: wire.TxOut{
Value: int64(eligible[i].
Amount),
PkScript: eligible[i].PkScript,
},
OutPoint: eligible[i].OutPoint,
}
}

arrangedCoins, err := coinSelectionStrategy.
ArrangeCoins(wrappedEligible, feeSatPerKb)
if err != nil {
return err
}
inputSource = makeInputSource(arrangedCoins)
}

tx, err = txauthor.NewUnsignedTransaction(
outputs, feeSatPerKb, inputSource, changeSource,
Expand Down
Loading

0 comments on commit ed1195c

Please sign in to comment.