Skip to content

Commit 9e7408a

Browse files
committed
loop: add stubs and CLI commands for the asset deposit subserver
This commit adds a placeholder asset deposit subserver along with the corresponding CLI commands to the Loop daemon and CLI.
1 parent a927d0f commit 9e7408a

7 files changed

Lines changed: 384 additions & 1 deletion

File tree

assets/deposit/log.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package deposit
2+
3+
import (
4+
"github.com/btcsuite/btclog/v2"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
// Subsystem defines the sub system name of this package.
9+
const Subsystem = "ADEP"
10+
11+
// log is a logger that is initialized with no output filters. This
12+
// means the package will not perform any logging by default until the caller
13+
// requests it.
14+
var log btclog.Logger
15+
16+
// The default amount of logging is none.
17+
func init() {
18+
UseLogger(build.NewSubLogger(Subsystem, nil))
19+
}
20+
21+
// UseLogger uses a specified Logger to output package logging info.
22+
// This should be used in preference to SetLogWriter if the caller is also
23+
// using btclog.
24+
func UseLogger(logger btclog.Logger) {
25+
log = logger
26+
}

assets/deposit/server.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package deposit
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/looprpc"
7+
"google.golang.org/grpc/codes"
8+
"google.golang.org/grpc/status"
9+
)
10+
11+
// Server is the grpc server that serves the reservation service.
12+
type Server struct {
13+
looprpc.UnimplementedAssetDepositClientServer
14+
}
15+
16+
func NewServer() *Server {
17+
return &Server{}
18+
}
19+
20+
// NewAssetDeposit is the rpc endpoint for loop clients to request a new asset
21+
// deposit.
22+
func (s *Server) NewAssetDeposit(ctx context.Context,
23+
in *looprpc.NewAssetDepositRequest) (*looprpc.NewAssetDepositResponse,
24+
error) {
25+
26+
return nil, status.Error(codes.Unimplemented, "unimplemented")
27+
}
28+
29+
// ListAssetDeposits is the rpc endpoint for loop clients to list their asset
30+
// deposits.
31+
func (s *Server) ListAssetDeposits(ctx context.Context,
32+
in *looprpc.ListAssetDepositsRequest) (
33+
*looprpc.ListAssetDepositsResponse, error) {
34+
35+
return nil, status.Error(codes.Unimplemented, "unimplemented")
36+
}
37+
38+
// RevealAssetDepositKey is the rpc endpoint for loop clients to reveal the
39+
// asset deposit key for a specific asset deposit.
40+
func (s *Server) RevealAssetDepositKey(ctx context.Context,
41+
in *looprpc.RevealAssetDepositKeyRequest) (
42+
*looprpc.RevealAssetDepositKeyResponse, error) {
43+
44+
return nil, status.Error(codes.Unimplemented, "unimplemented")
45+
}
46+
47+
// WithdrawAssetDeposits is the rpc endpoint for loop clients to withdraw their
48+
// asset deposits.
49+
func (s *Server) WithdrawAssetDeposits(ctx context.Context,
50+
in *looprpc.WithdrawAssetDepositsRequest) (
51+
*looprpc.WithdrawAssetDepositsResponse, error) {
52+
53+
return nil, status.Error(codes.Unimplemented, "unimplemented")
54+
}
55+
56+
// TestCoSignAssetDepositHTLC is the rpc endpoint for loop clients to test
57+
// co-signing an asset deposit HTLC.
58+
func (s *Server) TestCoSignAssetDepositHTLC(ctx context.Context,
59+
in *looprpc.TestCoSignAssetDepositHTLCRequest) (
60+
*looprpc.TestCoSignAssetDepositHTLCResponse, error) {
61+
62+
return nil, status.Error(codes.Unimplemented, "unimplemented")
63+
}

cmd/loop/asset_deposits.go

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/looprpc"
7+
"github.com/urfave/cli"
8+
)
9+
10+
var (
11+
assetDepositsCommands = cli.Command{
12+
Name: "asset deposits",
13+
ShortName: "ad",
14+
Usage: "TAP asset deposit commands.",
15+
Subcommands: []cli.Command{
16+
newAssetDepositCommand,
17+
listAssetDepositsCommand,
18+
withdrawAssetDepositCommand,
19+
testCoSignCommand,
20+
testKeyRevealCommand,
21+
},
22+
}
23+
24+
newAssetDepositCommand = cli.Command{
25+
Name: "new",
26+
ShortName: "n",
27+
Usage: "Create a new TAP asset deposit.",
28+
Description: "Create a new TAP asset deposit.",
29+
Action: newAssetDeposit,
30+
Flags: []cli.Flag{
31+
cli.StringFlag{
32+
Name: "asset_id",
33+
Usage: "The asset id of the asset to deposit.",
34+
},
35+
cli.Uint64Flag{
36+
Name: "amt",
37+
Usage: "the amount to deposit (in asset " +
38+
"units).",
39+
},
40+
cli.UintFlag{
41+
Name: "expiry",
42+
Usage: "the deposit expiry in blocks.",
43+
},
44+
},
45+
}
46+
47+
listAssetDepositsCommand = cli.Command{
48+
Name: "list",
49+
ShortName: "l",
50+
Usage: "List TAP asset deposits.",
51+
Description: "List TAP asset deposits.",
52+
Flags: []cli.Flag{
53+
cli.UintFlag{
54+
Name: "min_confs",
55+
Usage: "The minimum amount of confirmations " +
56+
"an anchor output should have to be " +
57+
"listed.",
58+
},
59+
cli.UintFlag{
60+
Name: "max_confs",
61+
Usage: "The maximum number of confirmations " +
62+
"an anchor output could have to be " +
63+
"listed.",
64+
},
65+
},
66+
Action: listAssetDeposits,
67+
}
68+
69+
withdrawAssetDepositCommand = cli.Command{
70+
Name: "withdraw",
71+
ShortName: "w",
72+
Usage: "Withdraw TAP asset deposits.",
73+
Description: "Withdraw TAP asset deposits.",
74+
Action: withdrawAssetDeposit,
75+
Flags: []cli.Flag{
76+
cli.StringSliceFlag{
77+
Name: "deposit_ids",
78+
Usage: "The deposit ids of the asset " +
79+
"deposits to withdraw.",
80+
},
81+
},
82+
}
83+
84+
testKeyRevealCommand = cli.Command{
85+
Name: "testkeyreveal",
86+
ShortName: "tkr",
87+
Usage: "Test revealing the key of a deposit to the server.",
88+
Action: testKeyReveal,
89+
Flags: []cli.Flag{
90+
cli.StringFlag{
91+
Name: "deposit_id",
92+
Usage: "The deposit id of the asset deposit.",
93+
},
94+
},
95+
}
96+
97+
testCoSignCommand = cli.Command{
98+
Name: "testcosign",
99+
ShortName: "tcs",
100+
Usage: "Test co-signing a deposit to spend to an HTLC.",
101+
Action: testCoSign,
102+
Flags: []cli.Flag{
103+
cli.StringFlag{
104+
Name: "deposit_id",
105+
Usage: "The deposit id of the asset deposit.",
106+
},
107+
},
108+
}
109+
)
110+
111+
func init() {
112+
commands = append(commands, assetDepositsCommands)
113+
}
114+
115+
func newAssetDeposit(ctx *cli.Context) error {
116+
ctxb := context.Background()
117+
if ctx.NArg() > 0 {
118+
return cli.ShowCommandHelp(ctx, "newdeposit")
119+
}
120+
121+
client, cleanup, err := getAssetDepositsClient(ctx)
122+
if err != nil {
123+
return err
124+
}
125+
defer cleanup()
126+
127+
assetID := ctx.String("asset_id")
128+
amt := ctx.Uint64("amt")
129+
expiry := int32(ctx.Uint("expiry"))
130+
131+
resp, err := client.NewAssetDeposit(
132+
ctxb, &looprpc.NewAssetDepositRequest{
133+
AssetId: assetID,
134+
Amount: amt,
135+
CsvExpiry: expiry,
136+
},
137+
)
138+
if err != nil {
139+
return err
140+
}
141+
142+
printJSON(resp)
143+
144+
return nil
145+
}
146+
147+
func listAssetDeposits(ctx *cli.Context) error {
148+
ctxb := context.Background()
149+
if ctx.NArg() > 0 {
150+
return cli.ShowCommandHelp(ctx, "list")
151+
}
152+
153+
client, cleanup, err := getAssetDepositsClient(ctx)
154+
if err != nil {
155+
return err
156+
}
157+
defer cleanup()
158+
159+
resp, err := client.ListAssetDeposits(
160+
ctxb, &looprpc.ListAssetDepositsRequest{
161+
MinConfs: uint32(ctx.Int("min_confs")),
162+
MaxConfs: uint32(ctx.Int("max_confs")),
163+
})
164+
if err != nil {
165+
return err
166+
}
167+
168+
printRespJSON(resp)
169+
170+
return nil
171+
}
172+
173+
func withdrawAssetDeposit(ctx *cli.Context) error {
174+
ctxb := context.Background()
175+
if ctx.NArg() > 0 {
176+
return cli.ShowCommandHelp(ctx, "withdraw")
177+
}
178+
179+
client, cleanup, err := getAssetDepositsClient(ctx)
180+
if err != nil {
181+
return err
182+
}
183+
defer cleanup()
184+
185+
depositIDs := ctx.StringSlice("deposit_ids")
186+
187+
resp, err := client.WithdrawAssetDeposits(
188+
ctxb, &looprpc.WithdrawAssetDepositsRequest{
189+
DepositIds: depositIDs,
190+
},
191+
)
192+
if err != nil {
193+
return err
194+
}
195+
196+
printRespJSON(resp)
197+
198+
return nil
199+
}
200+
201+
func testKeyReveal(ctx *cli.Context) error {
202+
ctxb := context.Background()
203+
if ctx.NArg() > 0 {
204+
return cli.ShowCommandHelp(ctx, "testkeyreveal")
205+
}
206+
207+
client, cleanup, err := getAssetDepositsClient(ctx)
208+
if err != nil {
209+
return err
210+
}
211+
defer cleanup()
212+
213+
depositID := ctx.String("deposit_id")
214+
215+
resp, err := client.RevealAssetDepositKey(
216+
ctxb, &looprpc.RevealAssetDepositKeyRequest{
217+
DepositId: depositID,
218+
},
219+
)
220+
if err != nil {
221+
return err
222+
}
223+
224+
printRespJSON(resp)
225+
226+
return nil
227+
}
228+
229+
func testCoSign(ctx *cli.Context) error {
230+
ctxb := context.Background()
231+
if ctx.NArg() > 0 {
232+
return cli.ShowCommandHelp(ctx, "testcosign")
233+
}
234+
235+
client, cleanup, err := getAssetDepositsClient(ctx)
236+
if err != nil {
237+
return err
238+
}
239+
defer cleanup()
240+
241+
depositID := ctx.String("deposit_id")
242+
243+
resp, err := client.TestCoSignAssetDepositHTLC(
244+
ctxb, &looprpc.TestCoSignAssetDepositHTLCRequest{
245+
DepositId: depositID,
246+
},
247+
)
248+
if err != nil {
249+
return err
250+
}
251+
252+
printRespJSON(resp)
253+
254+
return nil
255+
}

cmd/loop/main.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,44 @@ func main() {
185185
}
186186
}
187187

188-
func getClient(ctx *cli.Context) (looprpc.SwapClientClient, func(), error) {
188+
func getConn(ctx *cli.Context) (*grpc.ClientConn, func(), error) {
189189
rpcServer := ctx.GlobalString("rpcserver")
190190
tlsCertPath, macaroonPath, err := extractPathArgs(ctx)
191191
if err != nil {
192192
return nil, nil, err
193193
}
194+
194195
conn, err := getClientConn(rpcServer, tlsCertPath, macaroonPath)
195196
if err != nil {
196197
return nil, nil, err
197198
}
198199
cleanup := func() { conn.Close() }
199200

201+
return conn, cleanup, nil
202+
}
203+
204+
func getClient(ctx *cli.Context) (looprpc.SwapClientClient, func(), error) {
205+
conn, cleanup, err := getConn(ctx)
206+
if err != nil {
207+
return nil, nil, err
208+
}
209+
200210
loopClient := looprpc.NewSwapClientClient(conn)
201211
return loopClient, cleanup, nil
202212
}
203213

214+
func getAssetDepositsClient(ctx *cli.Context) (
215+
looprpc.AssetDepositClientClient, func(), error) {
216+
217+
conn, cleanup, err := getConn(ctx)
218+
if err != nil {
219+
return nil, nil, err
220+
}
221+
222+
assetDepositsClient := looprpc.NewAssetDepositClientClient(conn)
223+
return assetDepositsClient, cleanup, nil
224+
}
225+
204226
func getMaxRoutingFee(amt btcutil.Amount) btcutil.Amount {
205227
return swap.CalcFee(amt, maxRoutingFeeBase, maxRoutingFeeRate)
206228
}

0 commit comments

Comments
 (0)