Skip to content

Commit 0310a7e

Browse files
committed
staticaddr: multi-address loop-in
1 parent 4a45853 commit 0310a7e

File tree

13 files changed

+297
-175
lines changed

13 files changed

+297
-175
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/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"
@@ -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

staticaddr/loopin/selected_amount_migration_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/btcsuite/btcd/chaincfg/chainhash"
1111
"github.com/btcsuite/btcd/wire"
1212
"github.com/lightninglabs/loop/loopdb"
13+
"github.com/lightninglabs/loop/staticaddr/address"
1314
"github.com/lightninglabs/loop/staticaddr/deposit"
1415
"github.com/lightninglabs/loop/test"
1516
"github.com/lightningnetwork/lnd/clock"
@@ -26,6 +27,7 @@ func TestMigrateSelectedSwapAmount(t *testing.T) {
2627
defer testDb.Close()
2728

2829
db := loopdb.NewStoreMock(t)
30+
addressStore := address.NewSqlStore(testDb.BaseDB)
2931
depositStore := deposit.NewSqlStore(testDb.BaseDB)
3032
swapStore := NewSqlStore(
3133
loopdb.NewTypedStore[Querier](testDb), testClock,
@@ -38,7 +40,8 @@ func TestMigrateSelectedSwapAmount(t *testing.T) {
3840

3941
return did
4042
}
41-
43+
_, pubClient := test.CreateKey(1)
44+
_, pubServer := test.CreateKey(2)
4245
d1, d2 := &deposit.Deposit{
4346
ID: newID(),
4447
OutPoint: wire.OutPoint{
@@ -49,6 +52,13 @@ func TestMigrateSelectedSwapAmount(t *testing.T) {
4952
TimeOutSweepPkScript: []byte{
5053
0x00, 0x14, 0x1a, 0x2b, 0x3c, 0x41,
5154
},
55+
AddressID: 1,
56+
AddressParams: &address.Parameters{
57+
ClientPubkey: pubClient,
58+
ServerPubkey: pubServer,
59+
PkScript: []byte{0x00, 0x14, 0x1a, 0x2b, 0x3c},
60+
Expiry: 10_000,
61+
},
5262
},
5363
&deposit.Deposit{
5464
ID: newID(),
@@ -60,9 +70,22 @@ func TestMigrateSelectedSwapAmount(t *testing.T) {
6070
TimeOutSweepPkScript: []byte{
6171
0x00, 0x14, 0x1a, 0x2b, 0x3c, 0x4d,
6272
},
73+
AddressID: 2,
74+
AddressParams: &address.Parameters{
75+
ClientPubkey: pubClient,
76+
ServerPubkey: pubServer,
77+
PkScript: []byte{0x00, 0x14, 0x1a, 0x2b, 0x3d},
78+
Expiry: 20_000,
79+
},
6380
}
6481

65-
err := depositStore.CreateDeposit(ctxb, d1)
82+
err := addressStore.CreateStaticAddress(ctxb, d1.AddressParams)
83+
require.NoError(t, err)
84+
85+
err = addressStore.CreateStaticAddress(ctxb, d2.AddressParams)
86+
require.NoError(t, err)
87+
88+
err = depositStore.CreateDeposit(ctxb, d1)
6689
require.NoError(t, err)
6790
err = depositStore.CreateDeposit(ctxb, d2)
6891
require.NoError(t, err)

0 commit comments

Comments
 (0)