Skip to content

Commit 8dab512

Browse files
committed
walletrpc: make use of custom lock ID and lock duration in FundPsbt
1 parent a62e410 commit 8dab512

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

lnrpc/walletrpc/psbt.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package walletrpc
66
import (
77
"fmt"
88
"math"
9+
"time"
910

1011
"github.com/btcsuite/btcd/wire"
1112
base "github.com/btcsuite/btcwallet/wallet"
@@ -40,31 +41,41 @@ func verifyInputsUnspent(inputs []*wire.TxIn, utxos []*lnwallet.Utxo) error {
4041
return nil
4142
}
4243

43-
// lockInputs requests a lock lease for all inputs specified in a PSBT packet
44-
// by using the internal, static lock ID of lnd's wallet.
45-
func lockInputs(w lnwallet.WalletController,
46-
outpoints []wire.OutPoint) ([]*base.ListLeasedOutputResult, error) {
44+
// lockInputs requests lock leases for all inputs specified in a PSBT packet
45+
// (the passed outpoints), using either the optional custom lock ID and duration
46+
// or the wallet's internal static lock ID with the default 10-minute duration.
47+
func lockInputs(w lnwallet.WalletController, outpoints []wire.OutPoint,
48+
customLockID *wtxmgr.LockID, customLockDuration time.Duration) (
49+
[]*base.ListLeasedOutputResult, error) {
4750

4851
locks := make(
4952
[]*base.ListLeasedOutputResult, len(outpoints),
5053
)
5154
for idx := range outpoints {
5255
lock := &base.ListLeasedOutputResult{
5356
LockedOutput: &wtxmgr.LockedOutput{
54-
LockID: chanfunding.LndInternalLockID,
5557
Outpoint: outpoints[idx],
5658
},
5759
}
5860

61+
lock.LockID = chanfunding.LndInternalLockID
62+
if customLockID != nil {
63+
lock.LockID = *customLockID
64+
}
65+
66+
lockDuration := chanfunding.DefaultLockDuration
67+
if customLockDuration != 0 {
68+
lockDuration = customLockDuration
69+
}
70+
5971
// Get the details about this outpoint.
6072
utxo, err := w.FetchOutpointInfo(&lock.Outpoint)
6173
if err != nil {
6274
return nil, fmt.Errorf("fetch outpoint info: %w", err)
6375
}
6476

6577
expiration, err := w.LeaseOutput(
66-
lock.LockID, lock.Outpoint,
67-
chanfunding.DefaultLockDuration,
78+
lock.LockID, lock.Outpoint, lockDuration,
6879
)
6980
if err != nil {
7081
// If we run into a problem with locking one output, we

lnrpc/walletrpc/walletkit_server.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,24 @@ func (w *WalletKit) FundPsbt(_ context.Context,
16001600
account = req.Account
16011601
}
16021602

1603+
var customLockID *wtxmgr.LockID
1604+
if len(req.CustomLockId) > 0 {
1605+
lockID := wtxmgr.LockID{}
1606+
if len(req.CustomLockId) != len(lockID) {
1607+
return nil, fmt.Errorf("custom lock ID must be " +
1608+
"exactly 32 bytes")
1609+
}
1610+
1611+
copy(lockID[:], req.CustomLockId)
1612+
customLockID = &lockID
1613+
}
1614+
1615+
var customLockDuration time.Duration
1616+
if req.LockExpirationSeconds != 0 {
1617+
customLockDuration = time.Duration(req.LockExpirationSeconds) *
1618+
time.Second
1619+
}
1620+
16031621
// There are three ways a user can specify what we call the template (a
16041622
// list of inputs and outputs to use in the PSBT): Either as a PSBT
16051623
// packet directly with no coin selection, a PSBT with coin selection or
@@ -1619,6 +1637,7 @@ func (w *WalletKit) FundPsbt(_ context.Context,
16191637
return w.fundPsbtInternalWallet(
16201638
account, keyScopeFromChangeAddressType(req.ChangeType),
16211639
packet, minConfs, feeSatPerKW, coinSelectionStrategy,
1640+
customLockID, customLockDuration,
16221641
)
16231642

16241643
// The template is specified as a PSBT with the intention to perform
@@ -1701,6 +1720,7 @@ func (w *WalletKit) FundPsbt(_ context.Context,
17011720
return w.fundPsbtCoinSelect(
17021721
account, changeIndex, packet, minConfs, changeType,
17031722
feeSatPerKW, coinSelectionStrategy, maxFeeRatio,
1723+
customLockID, customLockDuration,
17041724
)
17051725

17061726
// The template is specified as a RPC message. We need to create a new
@@ -1758,6 +1778,7 @@ func (w *WalletKit) FundPsbt(_ context.Context,
17581778
return w.fundPsbtInternalWallet(
17591779
account, keyScopeFromChangeAddressType(req.ChangeType),
17601780
packet, minConfs, feeSatPerKW, coinSelectionStrategy,
1781+
customLockID, customLockDuration,
17611782
)
17621783

17631784
default:
@@ -1770,8 +1791,9 @@ func (w *WalletKit) FundPsbt(_ context.Context,
17701791
// wallet that does not allow specifying custom inputs while selecting coins.
17711792
func (w *WalletKit) fundPsbtInternalWallet(account string,
17721793
keyScope *waddrmgr.KeyScope, packet *psbt.Packet, minConfs int32,
1773-
feeSatPerKW chainfee.SatPerKWeight,
1774-
strategy base.CoinSelectionStrategy) (*FundPsbtResponse, error) {
1794+
feeSatPerKW chainfee.SatPerKWeight, strategy base.CoinSelectionStrategy,
1795+
customLockID *wtxmgr.LockID, customLockDuration time.Duration) (
1796+
*FundPsbtResponse, error) {
17751797

17761798
// The RPC parsing part is now over. Several of the following operations
17771799
// require us to hold the global coin selection lock, so we do the rest
@@ -1885,7 +1907,8 @@ func (w *WalletKit) fundPsbtInternalWallet(account string,
18851907
}
18861908

18871909
response, err = w.lockAndCreateFundingResponse(
1888-
packet, outpoints, changeIndex,
1910+
packet, outpoints, changeIndex, customLockID,
1911+
customLockDuration,
18891912
)
18901913

18911914
return err
@@ -1904,7 +1927,8 @@ func (w *WalletKit) fundPsbtCoinSelect(account string, changeIndex int32,
19041927
packet *psbt.Packet, minConfs int32,
19051928
changeType chanfunding.ChangeAddressType,
19061929
feeRate chainfee.SatPerKWeight, strategy base.CoinSelectionStrategy,
1907-
maxFeeRatio float64) (*FundPsbtResponse, error) {
1930+
maxFeeRatio float64, customLockID *wtxmgr.LockID,
1931+
customLockDuration time.Duration) (*FundPsbtResponse, error) {
19081932

19091933
// We want to make sure we don't select any inputs that are already
19101934
// specified in the template. To do that, we require those inputs to
@@ -2019,7 +2043,10 @@ func (w *WalletKit) fundPsbtCoinSelect(account string, changeIndex int32,
20192043
}
20202044

20212045
// We're done. Let's serialize and return the updated package.
2022-
return w.lockAndCreateFundingResponse(packet, nil, changeIndex)
2046+
return w.lockAndCreateFundingResponse(
2047+
packet, nil, changeIndex, customLockID,
2048+
customLockDuration,
2049+
)
20232050
}
20242051

20252052
// The RPC parsing part is now over. Several of the following operations
@@ -2091,7 +2118,8 @@ func (w *WalletKit) fundPsbtCoinSelect(account string, changeIndex int32,
20912118
}
20922119

20932120
response, err = w.lockAndCreateFundingResponse(
2094-
packet, addedOutpoints, changeIndex,
2121+
packet, addedOutpoints, changeIndex, customLockID,
2122+
customLockDuration,
20952123
)
20962124

20972125
return err
@@ -2136,8 +2164,9 @@ func (w *WalletKit) assertNotAvailable(inputs []*wire.TxIn, minConfs int32,
21362164
// lockAndCreateFundingResponse locks the given outpoints and creates a funding
21372165
// response with the serialized PSBT, the change index and the locked UTXOs.
21382166
func (w *WalletKit) lockAndCreateFundingResponse(packet *psbt.Packet,
2139-
newOutpoints []wire.OutPoint, changeIndex int32) (*FundPsbtResponse,
2140-
error) {
2167+
newOutpoints []wire.OutPoint, changeIndex int32,
2168+
customLockID *wtxmgr.LockID, customLockDuration time.Duration) (
2169+
*FundPsbtResponse, error) {
21412170

21422171
// Make sure we can properly serialize the packet. If this goes wrong
21432172
// then something isn't right with the inputs, and we probably shouldn't
@@ -2148,7 +2177,9 @@ func (w *WalletKit) lockAndCreateFundingResponse(packet *psbt.Packet,
21482177
return nil, fmt.Errorf("error serializing funded PSBT: %w", err)
21492178
}
21502179

2151-
locks, err := lockInputs(w.cfg.Wallet, newOutpoints)
2180+
locks, err := lockInputs(
2181+
w.cfg.Wallet, newOutpoints, customLockID, customLockDuration,
2182+
)
21522183
if err != nil {
21532184
return nil, fmt.Errorf("could not lock inputs: %w", err)
21542185
}

lnrpc/walletrpc/walletkit_server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ func TestFundPsbtCoinSelect(t *testing.T) {
660660
"", tc.changeIndex, copiedPacket, 0,
661661
tc.changeType, tc.feeRate,
662662
rpcServer.cfg.CoinSelectionStrategy,
663-
tc.maxFeeRatio,
663+
tc.maxFeeRatio, nil, 0,
664664
)
665665

666666
switch {

0 commit comments

Comments
 (0)