Skip to content

Commit 66657bd

Browse files
committed
continue loop-in
1 parent 0bbdb3f commit 66657bd

File tree

10 files changed

+211
-128
lines changed

10 files changed

+211
-128
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE static_address_swaps DROP COLUMN change_address;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- change_address stores the change output address for the HTLC transaction of a
2+
-- static address loop-in swap.
3+
ALTER TABLE static_address_swaps ADD change_address TEXT NOT NULL DEFAULT '';

loopdb/sqlc/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/static_address_loopin.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ INSERT INTO static_address_swaps (
77
quoted_swap_fee_satoshis,
88
deposit_outpoints,
99
selected_amount,
10+
change_address,
1011
htlc_tx_fee_rate_sat_kw,
1112
htlc_timeout_sweep_tx_id,
1213
htlc_timeout_sweep_address
@@ -20,7 +21,8 @@ INSERT INTO static_address_swaps (
2021
$7,
2122
$8,
2223
$9,
23-
$10
24+
$10,
25+
$11
2426
);
2527

2628
-- name: UpdateStaticAddressLoopIn :exec

loopdb/sqlc/static_address_loopin.sql.go

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staticaddr/loopin/actions.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lightninglabs/lndclient"
1717
"github.com/lightninglabs/loop"
1818
"github.com/lightninglabs/loop/fsm"
19+
"github.com/lightninglabs/loop/staticaddr/address"
1920
"github.com/lightninglabs/loop/staticaddr/deposit"
2021
"github.com/lightninglabs/loop/staticaddr/version"
2122
"github.com/lightninglabs/loop/swap"
@@ -63,15 +64,53 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
6364

6465
// Calculate the swap invoice amount. The server needs to pay us the
6566
// swap amount minus the fees that the server charges for the swap. The
66-
// swap amount is either the total value of the selected deposits, or
67-
// the selected amount if a specific amount was requested.
68-
swapAmount := f.loopIn.TotalDepositAmount()
69-
var hasChange bool
67+
// swap amount is either the total value of the selected deposits or the
68+
// selected amount if a specific amount was requested. If the selection
69+
// results in change, we create a new static address for it.
70+
var (
71+
swapAmount = f.loopIn.TotalDepositAmount()
72+
hasChange bool
73+
changeAddrParams *address.Parameters
74+
)
7075
if f.loopIn.SelectedAmount > 0 {
7176
swapAmount = f.loopIn.SelectedAmount
7277
remainingAmount := f.loopIn.TotalDepositAmount() - swapAmount
7378
hasChange = remainingAmount > 0 && remainingAmount <
7479
f.loopIn.TotalDepositAmount()
80+
81+
if hasChange {
82+
changeAddrParams, err = f.cfg.AddressManager.NewAddress(
83+
ctx,
84+
)
85+
if err != nil {
86+
err = fmt.Errorf("unable to create change "+
87+
"address: %w", err)
88+
89+
return f.HandleError(err)
90+
}
91+
92+
taprootAddress, err := changeAddrParams.TaprootAddress(
93+
f.cfg.ChainParams,
94+
)
95+
if err != nil {
96+
err = fmt.Errorf("unable to create taproot "+
97+
"address: %w", err)
98+
99+
return f.HandleError(err)
100+
}
101+
102+
changeAddress, err := btcutil.DecodeAddress(
103+
taprootAddress, f.cfg.ChainParams,
104+
)
105+
if err != nil {
106+
err = fmt.Errorf("unable to decode change "+
107+
"address: %w", err)
108+
109+
return f.HandleError(err)
110+
}
111+
112+
f.loopIn.ChangeAddress = changeAddress
113+
}
75114
}
76115
swapInvoiceAmt := swapAmount - f.loopIn.QuotedSwapFee
77116

@@ -124,6 +163,7 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
124163
SwapHash: f.loopIn.SwapHash[:],
125164
DepositOutpoints: f.loopIn.DepositOutpoints,
126165
Amount: uint64(f.loopIn.SelectedAmount),
166+
ChangeAddress: f.loopIn.ChangeAddress.String(),
127167
HtlcClientPubKey: f.loopIn.ClientPubkey.SerializeCompressed(),
128168
SwapInvoice: f.loopIn.SwapInvoice,
129169
ProtocolVersion: version.CurrentRPCProtocolVersion(),

staticaddr/loopin/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/btcsuite/btcd/btcutil"
77
"github.com/lightninglabs/loop"
88
"github.com/lightninglabs/loop/fsm"
9+
"github.com/lightninglabs/loop/staticaddr/address"
910
"github.com/lightninglabs/loop/staticaddr/deposit"
1011
"github.com/lightninglabs/loop/swapserverrpc"
1112
"github.com/lightningnetwork/lnd/lntypes"
@@ -21,6 +22,9 @@ type (
2122

2223
// AddressManager handles fetching of address parameters.
2324
type AddressManager interface {
25+
// NewAddress returns a new static address.
26+
NewAddress(ctx context.Context) (*address.Parameters, error)
27+
2428
// IsOutPkScript returns true if the given pkScript is our static
2529
// address script.
2630
IsOutPkScript(pkScript []byte) (bool, error)

staticaddr/loopin/sql_store.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ func (s *SqlStore) CreateLoopIn(ctx context.Context,
277277
HtlcTxFeeRateSatKw: int64(loopIn.HtlcTxFeeRate),
278278
DepositOutpoints: joinedOutpoints,
279279
SelectedAmount: int64(loopIn.SelectedAmount),
280+
ChangeAddress: loopIn.ChangeAddress.String(),
280281
PaymentTimeoutSeconds: int32(loopIn.PaymentTimeoutSeconds),
281282
}
282283

@@ -564,6 +565,11 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
564565
depositList = append(depositList, deposit)
565566
}
566567

568+
changeAddress, err := btcutil.DecodeAddress(swap.ChangeAddress, network)
569+
if err != nil {
570+
return nil, err
571+
}
572+
567573
loopIn := &StaticAddressLoopIn{
568574
SwapHash: swapHash,
569575
SwapPreimage: swapPreImage,
@@ -587,6 +593,7 @@ func toStaticAddressLoopIn(_ context.Context, network *chaincfg.Params,
587593
QuotedSwapFee: btcutil.Amount(swap.QuotedSwapFeeSatoshis),
588594
DepositOutpoints: depositOutpoints,
589595
SelectedAmount: btcutil.Amount(swap.SelectedAmount),
596+
ChangeAddress: changeAddress,
590597
HtlcTxFeeRate: chainfee.SatPerKWeight(
591598
swap.HtlcTxFeeRateSatKw,
592599
),

0 commit comments

Comments
 (0)