1- package staticaddr
1+ package address
22
33import (
44 "bytes"
@@ -10,8 +10,9 @@ import (
1010 "github.com/btcsuite/btcd/btcec/v2/schnorr"
1111 "github.com/btcsuite/btcd/btcutil"
1212 "github.com/btcsuite/btcd/chaincfg"
13+ "github.com/btcsuite/btclog"
1314 "github.com/lightninglabs/lndclient"
14- "github.com/lightninglabs/loop"
15+ "github.com/lightninglabs/loop/staticaddr "
1516 "github.com/lightninglabs/loop/staticaddr/script"
1617 "github.com/lightninglabs/loop/swap"
1718 staticaddressrpc "github.com/lightninglabs/loop/swapserverrpc"
@@ -20,18 +21,26 @@ import (
2021 "github.com/lightningnetwork/lnd/lnwallet"
2122)
2223
24+ var (
25+ log btclog.Logger
26+ )
27+
28+ func init () {
29+ log = staticaddr .GetLogger ()
30+ }
31+
2332// ManagerConfig holds the configuration for the address manager.
2433type ManagerConfig struct {
2534 // AddressClient is the client that communicates with the loop server
2635 // to manage static addresses.
2736 AddressClient staticaddressrpc.StaticAddressServerClient
2837
29- // SwapClient provides loop rpc functionality .
30- SwapClient * loop. Client
38+ // FetchL402 is the function used to fetch the l402 token .
39+ FetchL402 func (context. Context ) error
3140
3241 // Store is the database store that is used to store static address
3342 // related records.
34- Store AddressStore
43+ Store Store
3544
3645 // WalletKit is the wallet client that is used to derive new keys from
3746 // lnd's wallet.
@@ -46,30 +55,19 @@ type ManagerConfig struct {
4655type Manager struct {
4756 cfg * ManagerConfig
4857
49- initChan chan struct {}
50-
5158 sync.Mutex
5259}
5360
54- // NewAddressManager creates a new address manager.
55- func NewAddressManager (cfg * ManagerConfig ) * Manager {
61+ // NewManager creates a new address manager.
62+ func NewManager (cfg * ManagerConfig ) * Manager {
5663 return & Manager {
57- cfg : cfg ,
58- initChan : make (chan struct {}),
64+ cfg : cfg ,
5965 }
6066}
6167
6268// Run runs the address manager.
6369func (m * Manager ) Run (ctx context.Context ) error {
64- log .Debugf ("Starting address manager." )
65- defer log .Debugf ("Address manager stopped." )
66-
67- // Communicate to the caller that the address manager has completed its
68- // initialization.
69- close (m .initChan )
70-
7170 <- ctx .Done ()
72-
7371 return nil
7472}
7573
@@ -99,7 +97,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
9997
10098 // We are fetching a new L402 token from the server. There is one static
10199 // address per L402 token allowed.
102- err = m .cfg .SwapClient . Server . FetchL402 (ctx )
100+ err = m .cfg .FetchL402 (ctx )
103101 if err != nil {
104102 return nil , err
105103 }
@@ -113,7 +111,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
113111
114112 // Send our clientPubKey to the server and wait for the server to
115113 // respond with he serverPubKey and the static address CSV expiry.
116- protocolVersion := CurrentRPCProtocolVersion ()
114+ protocolVersion := staticaddr . CurrentRPCProtocolVersion ()
117115 resp , err := m .cfg .AddressClient .ServerNewAddress (
118116 ctx , & staticaddressrpc.ServerNewAddressRequest {
119117 ProtocolVersion : protocolVersion ,
@@ -146,7 +144,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
146144
147145 // Create the static address from the parameters the server provided and
148146 // store all parameters in the database.
149- addrParams := & AddressParameters {
147+ addrParams := & Parameters {
150148 ClientPubkey : clientPubKey .PubKey ,
151149 ServerPubkey : serverPubKey ,
152150 PkScript : pkScript ,
@@ -155,7 +153,9 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
155153 Family : clientPubKey .Family ,
156154 Index : clientPubKey .Index ,
157155 },
158- ProtocolVersion : AddressProtocolVersion (protocolVersion ),
156+ ProtocolVersion : staticaddr .AddressProtocolVersion (
157+ protocolVersion ,
158+ ),
159159 }
160160 err = m .cfg .Store .CreateStaticAddress (ctx , addrParams )
161161 if err != nil {
@@ -172,7 +172,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
172172 return nil , err
173173 }
174174
175- log .Infof ("imported static address taproot script to lnd wallet: %v" ,
175+ log .Infof ("Imported static address taproot script to lnd wallet: %v" ,
176176 addr )
177177
178178 return m .getTaprootAddress (
@@ -197,12 +197,6 @@ func (m *Manager) getTaprootAddress(clientPubkey,
197197 )
198198}
199199
200- // WaitInitComplete waits until the address manager has completed its setup.
201- func (m * Manager ) WaitInitComplete () {
202- defer log .Debugf ("Address manager initiation complete." )
203- <- m .initChan
204- }
205-
206200// ListUnspentRaw returns a list of utxos at the static address.
207201func (m * Manager ) ListUnspentRaw (ctx context.Context , minConfs ,
208202 maxConfs int32 ) (* btcutil.AddressTaproot , []* lnwallet.Utxo , error ) {
@@ -213,7 +207,7 @@ func (m *Manager) ListUnspentRaw(ctx context.Context, minConfs,
213207 return nil , nil , err
214208
215209 case len (addresses ) == 0 :
216- return nil , nil , fmt . Errorf ( "no address found" )
210+ return nil , nil , nil
217211
218212 case len (addresses ) > 1 :
219213 return nil , nil , fmt .Errorf ("more than one address found" )
@@ -249,3 +243,52 @@ func (m *Manager) ListUnspentRaw(ctx context.Context, minConfs,
249243
250244 return taprootAddress , filteredUtxos , nil
251245}
246+
247+ // GetStaticAddressParameters returns the parameters of the static address.
248+ func (m * Manager ) GetStaticAddressParameters (ctx context.Context ) (* Parameters ,
249+ error ) {
250+
251+ params , err := m .cfg .Store .GetAllStaticAddresses (ctx )
252+ if err != nil {
253+ return nil , err
254+ }
255+
256+ if len (params ) == 0 {
257+ return nil , fmt .Errorf ("no static address parameters found" )
258+ }
259+
260+ return params [0 ], nil
261+ }
262+
263+ // GetStaticAddress returns a taproot address for the given client and server
264+ // public keys and expiry.
265+ func (m * Manager ) GetStaticAddress (ctx context.Context ) (* script.StaticAddress ,
266+ error ) {
267+
268+ params , err := m .GetStaticAddressParameters (ctx )
269+ if err != nil {
270+ return nil , err
271+ }
272+
273+ address , err := script .NewStaticAddress (
274+ input .MuSig2Version100RC2 , int64 (params .Expiry ),
275+ params .ClientPubkey , params .ServerPubkey ,
276+ )
277+ if err != nil {
278+ return nil , err
279+ }
280+
281+ return address , nil
282+ }
283+
284+ // ListUnspent returns a list of utxos at the static address.
285+ func (m * Manager ) ListUnspent (ctx context.Context , minConfs ,
286+ maxConfs int32 ) ([]* lnwallet.Utxo , error ) {
287+
288+ _ , utxos , err := m .ListUnspentRaw (ctx , minConfs , maxConfs )
289+ if err != nil {
290+ return nil , err
291+ }
292+
293+ return utxos , nil
294+ }
0 commit comments