Skip to content

Commit

Permalink
wallet: add utxo filter function.
Browse files Browse the repository at this point in the history
Allows to attach a utxo filter function when creating a transaction
funded by the internal wallet.
  • Loading branch information
ziggie1984 committed Mar 30, 2024
1 parent 9a7dd24 commit 2323d8c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
19 changes: 18 additions & 1 deletion wallet/createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
coinSelectKeyScope, changeKeyScope *waddrmgr.KeyScope,
account uint32, minconf int32, feeSatPerKb btcutil.Amount,
strategy CoinSelectionStrategy, dryRun bool,
selectedUtxos []wire.OutPoint) (*txauthor.AuthoredTx, error) {
selectedUtxos []wire.OutPoint,
allowUtxo func(utxo wtxmgr.Credit, blockHeight int32) bool) (
*txauthor.AuthoredTx, error) {

chainClient, err := w.requireChainClient()
if err != nil {
Expand Down Expand Up @@ -181,6 +183,13 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
)

for _, e := range eligible {
// Restrict the selected utxos if a filter
// function is provided.
if allowUtxo != nil &&
!allowUtxo(e, bs.Height) {

continue
}
eligibleByOutpoint[e.OutPoint] = e
}

Expand All @@ -206,6 +215,14 @@ func (w *Wallet) txToOutputs(outputs []*wire.TxOut,
// according to the selected coin selection strategy.
wrappedEligible := make([]Coin, len(eligible))
for i := range eligible {
// Restrict the selected utxos if a filter
// function is provided.
if allowUtxo != nil &&
!allowUtxo(eligible[i], bs.Height) {

continue
}

wrappedEligible[i] = Coin{
TxOut: wire.TxOut{
Value: int64(
Expand Down
19 changes: 16 additions & 3 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ type (
dryRun bool
resp chan createTxResponse
selectUtxos []wire.OutPoint
allowUtxo func(wtxmgr.Credit, int32) bool
}
createTxResponse struct {
tx *txauthor.AuthoredTx
Expand Down Expand Up @@ -1239,9 +1240,10 @@ out:
}

tx, err := w.txToOutputs(
txr.outputs, txr.coinSelectKeyScope, txr.changeKeyScope,
txr.account, txr.minconf, txr.feeSatPerKB,
txr.coinSelectionStrategy, txr.dryRun, txr.selectUtxos,
txr.outputs, txr.coinSelectKeyScope,
txr.changeKeyScope, txr.account, txr.minconf,
txr.feeSatPerKB, txr.coinSelectionStrategy,
txr.dryRun, txr.selectUtxos, txr.allowUtxo,
)

release()
Expand All @@ -1259,6 +1261,7 @@ out:
type txCreateOptions struct {
changeKeyScope *waddrmgr.KeyScope
selectUtxos []wire.OutPoint
allowUtxo func(wtxmgr.Credit, int32) bool
}

// TxCreateOption is a set of optional arguments to modify the tx creation
Expand Down Expand Up @@ -1289,6 +1292,15 @@ func WithCustomSelectUtxos(utxos []wire.OutPoint) TxCreateOption {
}
}

// WithAllowUtxo is used to restrict the selection of the internal wallet inputs
// by further external conditions.
func WithAllowUtxo(allowUtxo func(utxo wtxmgr.Credit,
blockHeight int32) bool) TxCreateOption {
return func(opts *txCreateOptions) {
opts.allowUtxo = allowUtxo
}
}

// CreateSimpleTx creates a new signed transaction spending unspent outputs with
// at least minconf confirmations spending to any number of address/amount
// pairs. Only unspent outputs belonging to the given key scope and account will
Expand Down Expand Up @@ -1333,6 +1345,7 @@ func (w *Wallet) CreateSimpleTx(coinSelectKeyScope *waddrmgr.KeyScope,
dryRun: dryRun,
resp: make(chan createTxResponse),
selectUtxos: opts.selectUtxos,
allowUtxo: opts.allowUtxo,
}
w.createTxRequests <- req
resp := <-req.resp
Expand Down

0 comments on commit 2323d8c

Please sign in to comment.