@@ -153,6 +153,10 @@ type (
153
153
Send (T ) error
154
154
grpc.ServerStream
155
155
}
156
+
157
+ // coinSelectExistingIndex is a type alias for the existing output index
158
+ // used in the PSBT coin selection.
159
+ coinSelectExistingIndex = walletrpc.PsbtCoinSelect_ExistingOutputIndex
156
160
)
157
161
158
162
// rpcServer is the main RPC server for the Taproot Assets daemon that handles
@@ -2570,80 +2574,92 @@ func (r *rpcServer) CommitVirtualPsbts(ctx context.Context,
2570
2574
return nil , fmt .Errorf ("error serializing packet: %w" , err )
2571
2575
}
2572
2576
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
+ }
2588
2610
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
+ }
2598
2615
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" )
2602
2618
}
2603
2619
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
+ }
2607
2626
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
+ }
2613
2632
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" )
2617
2635
}
2618
2636
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
+ }
2622
2644
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
+ )
2630
2655
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
2638
2661
}
2639
- },
2640
- )
2641
2662
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 {
2647
2663
for idx , utxo := range lockedUTXO {
2648
2664
var lockID wtxmgr.LockID
2649
2665
copy (lockID [:], utxo .Id )
@@ -2655,8 +2671,8 @@ func (r *rpcServer) CommitVirtualPsbts(ctx context.Context,
2655
2671
"UTXO %v: %v" , op , err )
2656
2672
}
2657
2673
}
2658
- }
2659
- }()
2674
+ }()
2675
+ }
2660
2676
2661
2677
// We can now update the anchor outputs as we have the final
2662
2678
// commitments.
0 commit comments