Skip to content

Commit fa4a632

Browse files
committed
staticaddr: multi-address loop-in
1 parent e120ffc commit fa4a632

File tree

15 files changed

+303
-178
lines changed

15 files changed

+303
-178
lines changed

loopd/daemon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
677677
LndClient: d.lnd.Client,
678678
InvoicesClient: d.lnd.Invoices,
679679
NodePubkey: d.lnd.NodePubkey,
680+
AddressManager: staticAddressManager,
680681
DepositManager: depositManager,
681682
Store: staticAddressLoopInStore,
682683
WalletKit: d.lnd.WalletKit,
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,
@@ -22,7 +23,8 @@ INSERT INTO static_address_swaps (
2223
$8,
2324
$9,
2425
$10,
25-
$11
26+
$11,
27+
$12
2628
);
2729

2830
-- 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/deposit/manager_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
312312
},
313313
}
314314
require.NoError(t, err)
315+
316+
_, clientPub := test.CreateKey(1)
317+
_, serverPub := test.CreateKey(2)
315318
storedDeposits := []*Deposit{
316319
{
317320
ID: ID,
@@ -324,6 +327,9 @@ func newManagerTestContext(t *testing.T) *ManagerTestContext {
324327
AddressParams: &address.Parameters{
325328
ProtocolVersion: version.ProtocolVersion_V0,
326329
Expiry: 100,
330+
ClientPubkey: clientPub,
331+
ServerPubkey: serverPub,
332+
PkScript: utxo.PkScript,
327333
},
328334
},
329335
}

staticaddr/deposit/sql_store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ func (s *SqlStore) DepositForOutpoint(ctx context.Context,
232232
ExpirySweepTxid: row.ExpirySweepTxid,
233233
FinalizedWithdrawalTx: row.FinalizedWithdrawalTx,
234234
SwapHash: row.SwapHash,
235+
ClientKeyFamily: row.ClientKeyFamily,
236+
ClientKeyIndex: row.ClientKeyIndex,
235237
StaticAddressID: row.StaticAddressID,
236238
ClientPubkey: row.ClientPubkey,
237239
ServerPubkey: row.ServerPubkey,

staticaddr/loopin/actions.go

Lines changed: 46 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,55 @@ 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+
changeAddress string
75+
)
7076
if f.loopIn.SelectedAmount > 0 {
7177
swapAmount = f.loopIn.SelectedAmount
7278
remainingAmount := f.loopIn.TotalDepositAmount() - swapAmount
7379
hasChange = remainingAmount > 0 && remainingAmount <
7480
f.loopIn.TotalDepositAmount()
81+
82+
if hasChange {
83+
changeAddrParams, err = f.cfg.AddressManager.NewAddress(
84+
ctx,
85+
)
86+
if err != nil {
87+
err = fmt.Errorf("unable to create change "+
88+
"address: %w", err)
89+
90+
return f.HandleError(err)
91+
}
92+
93+
taprootAddress, err := changeAddrParams.TaprootAddress(
94+
f.cfg.ChainParams,
95+
)
96+
if err != nil {
97+
err = fmt.Errorf("unable to create taproot "+
98+
"address: %w", err)
99+
100+
return f.HandleError(err)
101+
}
102+
103+
changeAddr, err := btcutil.DecodeAddress(
104+
taprootAddress, f.cfg.ChainParams,
105+
)
106+
if err != nil {
107+
err = fmt.Errorf("unable to decode change "+
108+
"address: %w", err)
109+
110+
return f.HandleError(err)
111+
}
112+
113+
f.loopIn.ChangeAddress = changeAddr
114+
changeAddress = changeAddr.String()
115+
}
75116
}
76117
swapInvoiceAmt := swapAmount - f.loopIn.QuotedSwapFee
77118

@@ -124,6 +165,7 @@ func (f *FSM) InitHtlcAction(ctx context.Context,
124165
SwapHash: f.loopIn.SwapHash[:],
125166
DepositOutpoints: f.loopIn.DepositOutpoints,
126167
Amount: uint64(f.loopIn.SelectedAmount),
168+
ChangeAddress: changeAddress,
127169
HtlcClientPubKey: f.loopIn.ClientPubkey.SerializeCompressed(),
128170
SwapInvoice: f.loopIn.SwapInvoice,
129171
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"
@@ -32,6 +33,9 @@ type (
3233

3334
// AddressManager handles fetching of address parameters.
3435
type AddressManager interface {
36+
// NewAddress returns a new static address.
37+
NewAddress(ctx context.Context) (*address.Parameters, error)
38+
3539
// IsOurPkScript returns true if the given pkScript is our static
3640
// address script.
3741
IsOurPkScript(pkScript []byte) bool

0 commit comments

Comments
 (0)