@@ -3,13 +3,15 @@ package address
33import (
44 "bytes"
55 "context"
6+ "errors"
67 "fmt"
78 "sync/atomic"
89
910 "github.com/btcsuite/btcd/btcec/v2"
1011 "github.com/btcsuite/btcd/btcec/v2/schnorr"
1112 "github.com/btcsuite/btcd/btcutil"
1213 "github.com/btcsuite/btcd/chaincfg"
14+ "github.com/lightninglabs/aperture/l402"
1315 "github.com/lightninglabs/lndclient"
1416 "github.com/lightninglabs/loop/staticaddr/script"
1517 "github.com/lightninglabs/loop/staticaddr/version"
@@ -26,10 +28,14 @@ type ManagerConfig struct {
2628 // to manage static addresses.
2729 AddressClient staticaddressrpc.StaticAddressServerClient
2830
31+ // CurrentToken returns the token currently contained in the store or a
32+ // l402.ErrNoToken error if there is none.
33+ CurrentToken func () (* l402.Token , error )
34+
2935 // FetchL402 is the function used to fetch the l402 token.
3036 FetchL402 func (context.Context ) error
3137
32- // Store is the database store that is used to store static address
38+ // Store is the database store that is used to store static address-
3339 // related records.
3440 Store Store
3541
@@ -123,6 +129,20 @@ func (m *Manager) Run(ctx context.Context, initChan chan struct{}) error {
123129 return err
124130 }
125131
132+ // Check if we need to fetch a new L402 token.
133+ _ , err = m .cfg .CurrentToken ()
134+ if err != nil {
135+ if ! errors .Is (err , l402 .ErrNoToken ) {
136+ return err
137+ }
138+
139+ // We are fetching a new L402 token from the server.
140+ err = m .cfg .FetchL402 (ctx )
141+ if err != nil {
142+ return err
143+ }
144+ }
145+
126146 // The address worker offloads the address creation with the server to a
127147 // separate go routine.
128148 go m .addrWorker (ctx )
@@ -181,12 +201,6 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
181201func (m * Manager ) newAddress (ctx context.Context ) (* btcutil.AddressTaproot ,
182202 int64 , error ) {
183203
184- // We are fetching a new L402 token from the server.
185- err := m .cfg .FetchL402 (ctx )
186- if err != nil {
187- return nil , 0 , err
188- }
189-
190204 clientPubKey , err := m .cfg .WalletKit .DeriveNextKey (
191205 ctx , swap .StaticAddressKeyFamily ,
192206 )
@@ -197,10 +211,11 @@ func (m *Manager) newAddress(ctx context.Context) (*btcutil.AddressTaproot,
197211 // Send our clientPubKey to the server and wait for the server to
198212 // respond with he serverPubKey and the static address CSV expiry.
199213 protocolVersion := version .CurrentRPCProtocolVersion ()
214+ serializedPubKey := clientPubKey .PubKey .SerializeCompressed ()
200215 resp , err := m .cfg .AddressClient .ServerNewAddress (
201216 ctx , & staticaddressrpc.ServerNewAddressRequest {
202217 ProtocolVersion : protocolVersion ,
203- ClientKey : clientPubKey . PubKey . SerializeCompressed (), //nolint:lll
218+ ClientKey : serializedPubKey ,
204219 },
205220 )
206221 if err != nil {
0 commit comments