Skip to content

Commit 8451f29

Browse files
committed
loopd: add asset client to swap config
1 parent 20d5081 commit 8451f29

File tree

9 files changed

+72
-17
lines changed

9 files changed

+72
-17
lines changed

client.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/btcsuite/btcd/btcutil"
1515
"github.com/lightninglabs/aperture/l402"
1616
"github.com/lightninglabs/lndclient"
17+
"github.com/lightninglabs/loop/assets"
1718
"github.com/lightninglabs/loop/loopdb"
1819
"github.com/lightninglabs/loop/swap"
1920
"github.com/lightninglabs/loop/sweep"
@@ -94,6 +95,7 @@ type Client struct {
9495
lndServices *lndclient.LndServices
9596
sweeper *sweep.Sweeper
9697
executor *executor
98+
assetClient *assets.TapdClient
9799

98100
resumeReady chan struct{}
99101
wg sync.WaitGroup
@@ -121,6 +123,9 @@ type ClientConfig struct {
121123
// Lnd is an instance of the lnd proxy.
122124
Lnd *lndclient.LndServices
123125

126+
// AssetClient is an instance of the assets client.
127+
AssetClient *assets.TapdClient
128+
124129
// MaxL402Cost is the maximum price we are willing to pay to the server
125130
// for the token.
126131
MaxL402Cost btcutil.Amount
@@ -273,6 +278,7 @@ func NewClient(dbDir string, loopDB loopdb.SwapStore,
273278
errChan: make(chan error),
274279
clientConfig: *config,
275280
lndServices: cfg.Lnd,
281+
assetClient: cfg.AssetClient,
276282
sweeper: sweeper,
277283
executor: executor,
278284
resumeReady: make(chan struct{}),
@@ -453,7 +459,7 @@ func (s *Client) Run(ctx context.Context, statusChan chan<- SwapInfo) error {
453459
func (s *Client) resumeSwaps(ctx context.Context,
454460
loopOutSwaps []*loopdb.LoopOut, loopInSwaps []*loopdb.LoopIn) {
455461

456-
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
462+
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server, s.assetClient)
457463

458464
for _, pend := range loopOutSwaps {
459465
if pend.State().State.Type() != loopdb.StateTypePending {
@@ -523,7 +529,7 @@ func (s *Client) LoopOut(globalCtx context.Context,
523529
}
524530

525531
// Create a new swap object for this swap.
526-
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
532+
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server, s.assetClient)
527533
initResult, err := newLoopOutSwap(
528534
globalCtx, swapCfg, initiationHeight, request,
529535
)
@@ -682,7 +688,7 @@ func (s *Client) LoopIn(globalCtx context.Context,
682688

683689
// Create a new swap object for this swap.
684690
initiationHeight := s.executor.height()
685-
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server)
691+
swapCfg := newSwapConfig(s.lndServices, s.Store, s.Server, s.assetClient)
686692
initResult, err := newLoopInSwap(
687693
globalCtx, swapCfg, initiationHeight, request,
688694
)

loopd/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/btcsuite/btcd/btcutil"
1313
"github.com/lightninglabs/aperture/l402"
14+
"github.com/lightninglabs/loop/assets"
1415
"github.com/lightninglabs/loop/loopdb"
1516
"github.com/lightningnetwork/lnd/cert"
1617
"github.com/lightningnetwork/lnd/lncfg"
@@ -196,6 +197,8 @@ type Config struct {
196197

197198
Server *loopServerConfig `group:"server" namespace:"server"`
198199

200+
Tapd *assets.TapdConfig `group:"tapd" namespace:"tapd"`
201+
199202
View viewParameters `command:"view" alias:"v" description:"View all swaps in the database. This command can only be executed when loopd is not running."`
200203
}
201204

@@ -214,6 +217,7 @@ func DefaultConfig() Config {
214217
Server: &loopServerConfig{
215218
NoTLS: false,
216219
},
220+
Tapd: assets.DefaultTapdConfig(),
217221
LoopDir: LoopDirBase,
218222
ConfigFile: defaultConfigFile,
219223
DataDir: LoopDirBase,

loopd/daemon.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
proxy "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1717
"github.com/lightninglabs/lndclient"
1818
"github.com/lightninglabs/loop"
19+
"github.com/lightninglabs/loop/assets"
1920
"github.com/lightninglabs/loop/instantout"
2021
"github.com/lightninglabs/loop/instantout/reservation"
2122
"github.com/lightninglabs/loop/loopd/perms"
@@ -28,6 +29,7 @@ import (
2829
"github.com/lightninglabs/loop/staticaddr/withdraw"
2930
loop_swaprpc "github.com/lightninglabs/loop/swapserverrpc"
3031
"github.com/lightninglabs/loop/sweepbatcher"
32+
"github.com/lightninglabs/taproot-assets/taprpc"
3133
"github.com/lightningnetwork/lnd/clock"
3234
"github.com/lightningnetwork/lnd/lntypes"
3335
"github.com/lightningnetwork/lnd/macaroons"
@@ -82,6 +84,7 @@ type Daemon struct {
8284
internalErrChan chan error
8385

8486
lnd *lndclient.GrpcLndServices
87+
assetClient *assets.TapdClient
8588
clientCleanup func()
8689

8790
wg sync.WaitGroup
@@ -138,6 +141,14 @@ func (d *Daemon) Start() error {
138141
return err
139142
}
140143

144+
// Initialize the assets client.
145+
if d.cfg.Tapd.Activate {
146+
d.assetClient, err = assets.NewTapdClient(d.cfg.Tapd)
147+
if err != nil {
148+
return err
149+
}
150+
}
151+
141152
// With lnd connected, initialize everything else, such as the swap
142153
// server client, the swap client RPC server instance and our main swap
143154
// and error handlers. If this fails, then nothing has been started yet,
@@ -435,9 +446,26 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
435446
chainParams,
436447
)
437448

449+
// If we're running an asset client, we'll log something here.
450+
if d.assetClient != nil {
451+
getInfo, err := d.assetClient.GetInfo(
452+
d.mainCtx, &taprpc.GetInfoRequest{},
453+
)
454+
if err != nil {
455+
return fmt.Errorf("unable to get asset client info: %v", err)
456+
}
457+
if getInfo.LndIdentityPubkey != d.lnd.NodePubkey.String() {
458+
return fmt.Errorf("asset client pubkey %v does not match "+
459+
"lnd pubkey %v", getInfo.LndIdentityPubkey,
460+
d.lnd.NodePubkey)
461+
}
462+
463+
log.Infof("Using asset client with version %v", getInfo.Version)
464+
}
465+
438466
// Create an instance of the loop client library.
439467
swapClient, clientCleanup, err := getClient(
440-
d.cfg, swapDb, sweeperDb, &d.lnd.LndServices,
468+
d.cfg, swapDb, sweeperDb, &d.lnd.LndServices, d.assetClient,
441469
)
442470
if err != nil {
443471
return err
@@ -667,6 +695,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
667695
depositManager: depositManager,
668696
withdrawalManager: withdrawalManager,
669697
staticLoopInManager: staticLoopInManager,
698+
assetClient: d.assetClient,
670699
}
671700

672701
// Retrieve all currently existing swaps from the database.

loopd/swapclient_server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightninglabs/aperture/l402"
2020
"github.com/lightninglabs/lndclient"
2121
"github.com/lightninglabs/loop"
22+
"github.com/lightninglabs/loop/assets"
2223
"github.com/lightninglabs/loop/fsm"
2324
"github.com/lightninglabs/loop/instantout"
2425
"github.com/lightninglabs/loop/instantout/reservation"
@@ -93,6 +94,7 @@ type swapClientServer struct {
9394
depositManager *deposit.Manager
9495
withdrawalManager *withdraw.Manager
9596
staticLoopInManager *loopin.Manager
97+
assetClient *assets.TapdClient
9698
swaps map[lntypes.Hash]loop.SwapInfo
9799
subscribers map[int]chan<- interface{}
98100
statusChan chan loop.SwapInfo

loopd/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/lightninglabs/aperture/l402"
1010
"github.com/lightninglabs/lndclient"
1111
"github.com/lightninglabs/loop"
12+
"github.com/lightninglabs/loop/assets"
1213
"github.com/lightninglabs/loop/liquidity"
1314
"github.com/lightninglabs/loop/loopdb"
1415
"github.com/lightninglabs/loop/swap"
@@ -19,8 +20,8 @@ import (
1920

2021
// getClient returns an instance of the swap client.
2122
func getClient(cfg *Config, swapDb loopdb.SwapStore,
22-
sweeperDb sweepbatcher.BatcherStore, lnd *lndclient.LndServices) (
23-
*loop.Client, func(), error) {
23+
sweeperDb sweepbatcher.BatcherStore, lnd *lndclient.LndServices,
24+
assets *assets.TapdClient) (*loop.Client, func(), error) {
2425

2526
// Default is not set for MaxLSATCost and MaxLSATFee to distinguish
2627
// it from user explicitly setting the option to default value.
@@ -45,6 +46,7 @@ func getClient(cfg *Config, swapDb loopdb.SwapStore,
4546
SwapServerNoTLS: cfg.Server.NoTLS,
4647
TLSPathServer: cfg.Server.TLSPath,
4748
Lnd: lnd,
49+
AssetClient: assets,
4850
MaxL402Cost: btcutil.Amount(cfg.MaxL402Cost),
4951
MaxL402Fee: btcutil.Amount(cfg.MaxL402Fee),
5052
LoopOutMaxParts: cfg.LoopOutMaxParts,

loopd/view.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/btcsuite/btcd/chaincfg"
88
"github.com/lightninglabs/lndclient"
99
"github.com/lightninglabs/loop"
10+
"github.com/lightninglabs/loop/assets"
1011
"github.com/lightninglabs/loop/loopdb"
1112
"github.com/lightninglabs/loop/sweepbatcher"
1213
"github.com/lightninglabs/loop/utils"
@@ -37,8 +38,16 @@ func view(config *Config, lisCfg *ListenerCfg) error {
3738
chainParams,
3839
)
3940

41+
var assetClient *assets.TapdClient
42+
if config.Tapd.Host != "" {
43+
assetClient, err = assets.NewTapdClient(config.Tapd)
44+
if err != nil {
45+
return err
46+
}
47+
}
48+
4049
swapClient, cleanup, err := getClient(
41-
config, swapDb, sweeperDb, &lnd.LndServices,
50+
config, swapDb, sweeperDb, &lnd.LndServices, assetClient,
4251
)
4352
if err != nil {
4453
return err

loopin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func testLoopInSuccess(t *testing.T) {
4747

4848
height := int32(600)
4949

50-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
50+
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
5151

5252
expectedLastHop := &route.Vertex{0x02}
5353

@@ -200,7 +200,7 @@ func testLoopInTimeout(t *testing.T, externalValue int64) {
200200

201201
height := int32(600)
202202

203-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
203+
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
204204

205205
req := testLoopInRequest
206206
if externalValue != 0 {
@@ -414,7 +414,7 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
414414
ctxb := context.Background()
415415

416416
ctx := newLoopInTestContext(t)
417-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
417+
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
418418

419419
// Create sender and receiver keys.
420420
_, senderPubKey := test.CreateKey(1)
@@ -769,7 +769,7 @@ func advanceToPublishedHtlc(t *testing.T, ctx *loopInTestContext) SwapInfo {
769769
func startNewLoopIn(t *testing.T, ctx *loopInTestContext, height int32) (
770770
*swapConfig, error, *loopInSwap) {
771771

772-
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server)
772+
cfg := newSwapConfig(&ctx.lnd.LndServices, ctx.store, ctx.server, nil)
773773

774774
req := &testLoopInRequest
775775

loopout_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func testLateHtlcPublish(t *testing.T) {
181181

182182
height := int32(600)
183183

184-
cfg := newSwapConfig(&lnd.LndServices, store, server)
184+
cfg := newSwapConfig(&lnd.LndServices, store, server, nil)
185185

186186
testRequest.Expiry = height + testLoopOutMinOnChainCltvDelta
187187

@@ -282,7 +282,7 @@ func testCustomSweepConfTarget(t *testing.T) {
282282
ctx.Lnd.SetFeeEstimate(DefaultSweepConfTarget, 10000)
283283

284284
cfg := newSwapConfig(
285-
&lnd.LndServices, loopdb.NewStoreMock(t), server,
285+
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
286286
)
287287

288288
initResult, err := newLoopOutSwap(
@@ -522,7 +522,7 @@ func testPreimagePush(t *testing.T) {
522522
)
523523

524524
cfg := newSwapConfig(
525-
&lnd.LndServices, loopdb.NewStoreMock(t), server,
525+
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
526526
)
527527

528528
initResult, err := newLoopOutSwap(
@@ -786,7 +786,7 @@ func testFailedOffChainCancelation(t *testing.T) {
786786
testReq.Expiry = lnd.Height + 20
787787

788788
cfg := newSwapConfig(
789-
&lnd.LndServices, loopdb.NewStoreMock(t), server,
789+
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
790790
)
791791

792792
initResult, err := newLoopOutSwap(
@@ -940,7 +940,7 @@ func TestLoopOutMuSig2Sweep(t *testing.T) {
940940
)
941941

942942
cfg := newSwapConfig(
943-
&lnd.LndServices, loopdb.NewStoreMock(t), server,
943+
&lnd.LndServices, loopdb.NewStoreMock(t), server, nil,
944944
)
945945

946946
initResult, err := newLoopOutSwap(

swap.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/lightninglabs/lndclient"
8+
"github.com/lightninglabs/loop/assets"
89
"github.com/lightninglabs/loop/loopdb"
910
"github.com/lightninglabs/loop/swap"
1011
"github.com/lightninglabs/loop/utils"
@@ -79,14 +80,16 @@ type swapConfig struct {
7980
lnd *lndclient.LndServices
8081
store loopdb.SwapStore
8182
server swapServerClient
83+
assets *assets.TapdClient
8284
}
8385

8486
func newSwapConfig(lnd *lndclient.LndServices, store loopdb.SwapStore,
85-
server swapServerClient) *swapConfig {
87+
server swapServerClient, assets *assets.TapdClient) *swapConfig {
8688

8789
return &swapConfig{
8890
lnd: lnd,
8991
store: store,
9092
server: server,
93+
assets: assets,
9194
}
9295
}

0 commit comments

Comments
 (0)