Skip to content

Commit 608bed8

Browse files
authored
Merge pull request #1563 from bhandras/commitvirtualpsbt-skip-funding
tapd: allow skipping the funding step in CommitVirtualPsbts
2 parents 324bce0 + 91f5663 commit 608bed8

File tree

4 files changed

+321
-282
lines changed

4 files changed

+321
-282
lines changed

rpcserver.go

Lines changed: 80 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ type (
153153
Send(T) error
154154
grpc.ServerStream
155155
}
156+
157+
// coinSelectExistingIndex is a type alias for the existing output index
158+
// used in the PSBT coin selection.
159+
coinSelectExistingIndex = walletrpc.PsbtCoinSelect_ExistingOutputIndex
156160
)
157161

158162
// rpcServer is the main RPC server for the Taproot Assets daemon that handles
@@ -2570,80 +2574,92 @@ func (r *rpcServer) CommitVirtualPsbts(ctx context.Context,
25702574
return nil, fmt.Errorf("error serializing packet: %w", err)
25712575
}
25722576

2573-
// The change output and fee parameters of this RPC are identical to the
2574-
// walletrpc.FundPsbt, so we just map them 1:1 and let lnd do the
2575-
// validation.
2576-
coinSelect := &walletrpc.PsbtCoinSelect{
2577-
Psbt: packetBytes,
2578-
}
2579-
fundRequest := &walletrpc.FundPsbtRequest{
2580-
Template: &walletrpc.FundPsbtRequest_CoinSelect{
2581-
CoinSelect: coinSelect,
2582-
},
2583-
MinConfs: 1,
2584-
ChangeType: P2TRChangeType,
2585-
CustomLockId: req.CustomLockId,
2586-
LockExpirationSeconds: req.LockExpirationSeconds,
2587-
}
2577+
var (
2578+
lockedUTXO []*walletrpc.UtxoLease
2579+
lockedOutpoints []wire.OutPoint
2580+
fundedPacket *psbt.Packet = pkt
2581+
changeIndex int32 = -1
2582+
success bool
2583+
)
2584+
2585+
if !req.SkipFunding {
2586+
// The change output and fee parameters of this RPC are
2587+
// identical to the walletrpc.FundPsbt, so we just map them 1:1
2588+
// and let lnd do the validation.
2589+
coinSelect := &walletrpc.PsbtCoinSelect{
2590+
Psbt: packetBytes,
2591+
}
2592+
fundRequest := &walletrpc.FundPsbtRequest{
2593+
Template: &walletrpc.FundPsbtRequest_CoinSelect{
2594+
CoinSelect: coinSelect,
2595+
},
2596+
MinConfs: 1,
2597+
ChangeType: P2TRChangeType,
2598+
CustomLockId: req.CustomLockId,
2599+
LockExpirationSeconds: req.LockExpirationSeconds,
2600+
}
2601+
2602+
// Unfortunately we can't use the same RPC types, so we have to
2603+
// do a 1:1 mapping to the walletrpc types for the anchor change
2604+
// output and fee "oneof" fields.
2605+
switch change := req.AnchorChangeOutput.(type) {
2606+
case *wrpc.CommitVirtualPsbtsRequest_ExistingOutputIndex:
2607+
coinSelect.ChangeOutput = &coinSelectExistingIndex{
2608+
ExistingOutputIndex: change.ExistingOutputIndex,
2609+
}
25882610

2589-
// Unfortunately we can't use the same RPC types, so we have to do a
2590-
// 1:1 mapping to the walletrpc types for the anchor change output and
2591-
// fee "oneof" fields.
2592-
type existingIndex = walletrpc.PsbtCoinSelect_ExistingOutputIndex
2593-
switch change := req.AnchorChangeOutput.(type) {
2594-
case *wrpc.CommitVirtualPsbtsRequest_ExistingOutputIndex:
2595-
coinSelect.ChangeOutput = &existingIndex{
2596-
ExistingOutputIndex: change.ExistingOutputIndex,
2597-
}
2611+
case *wrpc.CommitVirtualPsbtsRequest_Add:
2612+
coinSelect.ChangeOutput = &walletrpc.PsbtCoinSelect_Add{
2613+
Add: change.Add,
2614+
}
25982615

2599-
case *wrpc.CommitVirtualPsbtsRequest_Add:
2600-
coinSelect.ChangeOutput = &walletrpc.PsbtCoinSelect_Add{
2601-
Add: change.Add,
2616+
default:
2617+
return nil, fmt.Errorf("unknown change output type")
26022618
}
26032619

2604-
default:
2605-
return nil, fmt.Errorf("unknown change output type")
2606-
}
2620+
switch fee := req.Fees.(type) {
2621+
case *wrpc.CommitVirtualPsbtsRequest_TargetConf:
2622+
fundRequest.Fees =
2623+
&walletrpc.FundPsbtRequest_TargetConf{
2624+
TargetConf: fee.TargetConf,
2625+
}
26072626

2608-
switch fee := req.Fees.(type) {
2609-
case *wrpc.CommitVirtualPsbtsRequest_TargetConf:
2610-
fundRequest.Fees = &walletrpc.FundPsbtRequest_TargetConf{
2611-
TargetConf: fee.TargetConf,
2612-
}
2627+
case *wrpc.CommitVirtualPsbtsRequest_SatPerVbyte:
2628+
fundRequest.Fees =
2629+
&walletrpc.FundPsbtRequest_SatPerVbyte{
2630+
SatPerVbyte: fee.SatPerVbyte,
2631+
}
26132632

2614-
case *wrpc.CommitVirtualPsbtsRequest_SatPerVbyte:
2615-
fundRequest.Fees = &walletrpc.FundPsbtRequest_SatPerVbyte{
2616-
SatPerVbyte: fee.SatPerVbyte,
2633+
default:
2634+
return nil, fmt.Errorf("unknown fee type")
26172635
}
26182636

2619-
default:
2620-
return nil, fmt.Errorf("unknown fee type")
2621-
}
2637+
lndWallet := r.cfg.Lnd.WalletKit
2638+
fundedPacket, changeIndex, lockedUTXO, err = lndWallet.FundPsbt(
2639+
ctx, fundRequest,
2640+
)
2641+
if err != nil {
2642+
return nil, fmt.Errorf("error funding packet: %w", err)
2643+
}
26222644

2623-
lndWallet := r.cfg.Lnd.WalletKit
2624-
fundedPacket, changeIndex, lockedUTXO, err := lndWallet.FundPsbt(
2625-
ctx, fundRequest,
2626-
)
2627-
if err != nil {
2628-
return nil, fmt.Errorf("error funding packet: %w", err)
2629-
}
2645+
lockedOutpoints = fn.Map(lockedUTXO,
2646+
func(utxo *walletrpc.UtxoLease) wire.OutPoint {
2647+
var hash chainhash.Hash
2648+
copy(hash[:], utxo.Outpoint.TxidBytes)
2649+
return wire.OutPoint{
2650+
Hash: hash,
2651+
Index: utxo.Outpoint.OutputIndex,
2652+
}
2653+
},
2654+
)
26302655

2631-
lockedOutpoints := fn.Map(
2632-
lockedUTXO, func(utxo *walletrpc.UtxoLease) wire.OutPoint {
2633-
var hash chainhash.Hash
2634-
copy(hash[:], utxo.Outpoint.TxidBytes)
2635-
return wire.OutPoint{
2636-
Hash: hash,
2637-
Index: utxo.Outpoint.OutputIndex,
2656+
// From now on, if we error out, we need to make sure we unlock
2657+
// the UTXOs that lnd just locked for us.
2658+
defer func() {
2659+
if success {
2660+
return
26382661
}
2639-
},
2640-
)
26412662

2642-
// From now on, if we error out, we need to make sure we unlock the
2643-
// UTXOs that lnd just locked for us.
2644-
success := false
2645-
defer func() {
2646-
if !success {
26472663
for idx, utxo := range lockedUTXO {
26482664
var lockID wtxmgr.LockID
26492665
copy(lockID[:], utxo.Id)
@@ -2655,8 +2671,8 @@ func (r *rpcServer) CommitVirtualPsbts(ctx context.Context,
26552671
"UTXO %v: %v", op, err)
26562672
}
26572673
}
2658-
}
2659-
}()
2674+
}()
2675+
}
26602676

26612677
// We can now update the anchor outputs as we have the final
26622678
// commitments.

0 commit comments

Comments
 (0)