Skip to content

Commit b69e8cb

Browse files
committed
multi: allow loop in last hop restriction
1 parent 535e964 commit b69e8cb

File tree

6 files changed

+111
-73
lines changed

6 files changed

+111
-73
lines changed

cmd/loop/loopin.go

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,36 @@ import (
77
"github.com/btcsuite/btcutil"
88
"github.com/lightninglabs/loop/looprpc"
99
"github.com/lightninglabs/loop/swap"
10+
"github.com/lightningnetwork/lnd/routing/route"
1011
"github.com/urfave/cli"
1112
)
1213

13-
var loopInCommand = cli.Command{
14-
Name: "in",
15-
Usage: "perform an on-chain to off-chain swap (loop in)",
16-
ArgsUsage: "amt",
17-
Description: `
14+
var (
15+
lastHopFlag = cli.StringFlag{
16+
Name: "last_hop",
17+
Usage: "the pubkey of the last hop to use for this swap",
18+
}
19+
20+
loopInCommand = cli.Command{
21+
Name: "in",
22+
Usage: "perform an on-chain to off-chain swap (loop in)",
23+
ArgsUsage: "amt",
24+
Description: `
1825
Send the amount in satoshis specified by the amt argument off-chain.`,
19-
Flags: []cli.Flag{
20-
cli.Uint64Flag{
21-
Name: "amt",
22-
Usage: "the amount in satoshis to loop in",
23-
},
24-
cli.BoolFlag{
25-
Name: "external",
26-
Usage: "expect htlc to be published externally",
26+
Flags: []cli.Flag{
27+
cli.Uint64Flag{
28+
Name: "amt",
29+
Usage: "the amount in satoshis to loop in",
30+
},
31+
cli.BoolFlag{
32+
Name: "external",
33+
Usage: "expect htlc to be published externally",
34+
},
35+
lastHopFlag,
2736
},
28-
},
29-
Action: loopIn,
30-
}
37+
Action: loopIn,
38+
}
39+
)
3140

3241
func loopIn(ctx *cli.Context) error {
3342
args := ctx.Args()
@@ -73,12 +82,25 @@ func loopIn(ctx *cli.Context) error {
7382
return err
7483
}
7584

76-
resp, err := client.LoopIn(context.Background(), &looprpc.LoopInRequest{
85+
req := &looprpc.LoopInRequest{
7786
Amt: int64(amt),
7887
MaxMinerFee: int64(limits.maxMinerFee),
7988
MaxSwapFee: int64(limits.maxSwapFee),
8089
ExternalHtlc: external,
81-
})
90+
}
91+
92+
if ctx.IsSet(lastHopFlag.Name) {
93+
lastHop, err := route.NewVertexFromStr(
94+
ctx.String(lastHopFlag.Name),
95+
)
96+
if err != nil {
97+
return err
98+
}
99+
100+
req.LastHop = lastHop[:]
101+
}
102+
103+
resp, err := client.LoopIn(context.Background(), req)
82104
if err != nil {
83105
return err
84106
}

loopin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
114114
// htlc.
115115
log.Infof("Initiating swap request at height %v", currentHeight)
116116
swapResp, err := cfg.server.NewLoopInSwap(globalCtx, swapHash,
117-
request.Amount, senderKey, swapInvoice,
117+
request.Amount, senderKey, swapInvoice, request.LastHop,
118118
)
119119
if err != nil {
120120
return nil, fmt.Errorf("cannot initiate swap: %v", err)

looprpc/server.pb.go

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

looprpc/server.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ message ServerLoopInRequest {
7878
bytes swap_hash = 2;
7979
uint64 amt = 3;
8080
string swap_invoice = 4;
81+
bytes last_hop = 5;
8182
}
8283

8384
message ServerLoopInResponse {

server_mock_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightninglabs/loop/test"
1212
"github.com/lightningnetwork/lnd/lntypes"
1313
"github.com/lightningnetwork/lnd/lnwire"
14+
"github.com/lightningnetwork/lnd/routing/route"
1415
"github.com/lightningnetwork/lnd/zpay32"
1516
)
1617

@@ -127,7 +128,7 @@ func getInvoice(hash lntypes.Hash, amt btcutil.Amount, memo string) (string, err
127128

128129
func (s *serverMock) NewLoopInSwap(ctx context.Context,
129130
swapHash lntypes.Hash, amount btcutil.Amount,
130-
senderKey [33]byte, swapInvoice string) (
131+
senderKey [33]byte, swapInvoice string, lastHop *route.Vertex) (
131132
*newLoopInResponse, error) {
132133

133134
_, receiverKey := test.CreateKey(101)

swap_server_client.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/lightninglabs/loop/looprpc"
1515
"github.com/lightninglabs/loop/lsat"
1616
"github.com/lightningnetwork/lnd/lntypes"
17+
"github.com/lightningnetwork/lnd/routing/route"
1718
"google.golang.org/grpc"
1819
"google.golang.org/grpc/credentials"
1920
)
@@ -40,7 +41,7 @@ type swapServerClient interface {
4041

4142
NewLoopInSwap(ctx context.Context,
4243
swapHash lntypes.Hash, amount btcutil.Amount,
43-
senderKey [33]byte, swapInvoice string) (
44+
senderKey [33]byte, swapInvoice string, lastHop *route.Vertex) (
4445
*newLoopInResponse, error)
4546
}
4647

@@ -203,18 +204,22 @@ func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context,
203204

204205
func (s *grpcSwapServerClient) NewLoopInSwap(ctx context.Context,
205206
swapHash lntypes.Hash, amount btcutil.Amount, senderKey [33]byte,
206-
swapInvoice string) (*newLoopInResponse, error) {
207+
swapInvoice string, lastHop *route.Vertex) (*newLoopInResponse, error) {
207208

208209
rpcCtx, rpcCancel := context.WithTimeout(ctx, globalCallTimeout)
209210
defer rpcCancel()
210-
swapResp, err := s.server.NewLoopInSwap(rpcCtx,
211-
&looprpc.ServerLoopInRequest{
212-
SwapHash: swapHash[:],
213-
Amt: uint64(amount),
214-
SenderKey: senderKey[:],
215-
SwapInvoice: swapInvoice,
216-
},
217-
)
211+
212+
req := &looprpc.ServerLoopInRequest{
213+
SwapHash: swapHash[:],
214+
Amt: uint64(amount),
215+
SenderKey: senderKey[:],
216+
SwapInvoice: swapInvoice,
217+
}
218+
if lastHop != nil {
219+
req.LastHop = lastHop[:]
220+
}
221+
222+
swapResp, err := s.server.NewLoopInSwap(rpcCtx, req)
218223
if err != nil {
219224
return nil, err
220225
}

0 commit comments

Comments
 (0)