1
- package staticaddr
1
+ package address
2
2
3
3
import (
4
4
"bytes"
@@ -10,8 +10,9 @@ import (
10
10
"github.com/btcsuite/btcd/btcec/v2/schnorr"
11
11
"github.com/btcsuite/btcd/btcutil"
12
12
"github.com/btcsuite/btcd/chaincfg"
13
+ "github.com/btcsuite/btclog"
13
14
"github.com/lightninglabs/lndclient"
14
- "github.com/lightninglabs/loop"
15
+ "github.com/lightninglabs/loop/staticaddr "
15
16
"github.com/lightninglabs/loop/staticaddr/script"
16
17
"github.com/lightninglabs/loop/swap"
17
18
staticaddressrpc "github.com/lightninglabs/loop/swapserverrpc"
@@ -20,18 +21,22 @@ import (
20
21
"github.com/lightningnetwork/lnd/lnwallet"
21
22
)
22
23
24
+ var (
25
+ log btclog.Logger
26
+ )
27
+
23
28
// ManagerConfig holds the configuration for the address manager.
24
29
type ManagerConfig struct {
25
30
// AddressClient is the client that communicates with the loop server
26
31
// to manage static addresses.
27
32
AddressClient staticaddressrpc.StaticAddressServerClient
28
33
29
- // SwapClient provides loop rpc functionality .
30
- SwapClient * loop. Client
34
+ // FetchL402 is the function used to fetch the l402 token .
35
+ FetchL402 func (context. Context ) error
31
36
32
37
// Store is the database store that is used to store static address
33
38
// related records.
34
- Store AddressStore
39
+ Store Store
35
40
36
41
// WalletKit is the wallet client that is used to derive new keys from
37
42
// lnd's wallet.
@@ -46,30 +51,20 @@ type ManagerConfig struct {
46
51
type Manager struct {
47
52
cfg * ManagerConfig
48
53
49
- initChan chan struct {}
50
-
51
54
sync.Mutex
52
55
}
53
56
54
- // NewAddressManager creates a new address manager.
55
- func NewAddressManager (cfg * ManagerConfig ) * Manager {
57
+ // NewManager creates a new address manager.
58
+ func NewManager (cfg * ManagerConfig ) * Manager {
59
+ log = staticaddr .GetLogger ()
56
60
return & Manager {
57
- cfg : cfg ,
58
- initChan : make (chan struct {}),
61
+ cfg : cfg ,
59
62
}
60
63
}
61
64
62
65
// Run runs the address manager.
63
66
func (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
-
71
67
<- ctx .Done ()
72
-
73
68
return nil
74
69
}
75
70
@@ -99,7 +94,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
99
94
100
95
// We are fetching a new L402 token from the server. There is one static
101
96
// address per L402 token allowed.
102
- err = m .cfg .SwapClient . Server . FetchL402 (ctx )
97
+ err = m .cfg .FetchL402 (ctx )
103
98
if err != nil {
104
99
return nil , err
105
100
}
@@ -113,7 +108,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
113
108
114
109
// Send our clientPubKey to the server and wait for the server to
115
110
// respond with he serverPubKey and the static address CSV expiry.
116
- protocolVersion := CurrentRPCProtocolVersion ()
111
+ protocolVersion := staticaddr . CurrentRPCProtocolVersion ()
117
112
resp , err := m .cfg .AddressClient .ServerNewAddress (
118
113
ctx , & staticaddressrpc.ServerNewAddressRequest {
119
114
ProtocolVersion : protocolVersion ,
@@ -146,7 +141,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
146
141
147
142
// Create the static address from the parameters the server provided and
148
143
// store all parameters in the database.
149
- addrParams := & AddressParameters {
144
+ addrParams := & Parameters {
150
145
ClientPubkey : clientPubKey .PubKey ,
151
146
ServerPubkey : serverPubKey ,
152
147
PkScript : pkScript ,
@@ -155,7 +150,9 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
155
150
Family : clientPubKey .Family ,
156
151
Index : clientPubKey .Index ,
157
152
},
158
- ProtocolVersion : AddressProtocolVersion (protocolVersion ),
153
+ ProtocolVersion : staticaddr .AddressProtocolVersion (
154
+ protocolVersion ,
155
+ ),
159
156
}
160
157
err = m .cfg .Store .CreateStaticAddress (ctx , addrParams )
161
158
if err != nil {
@@ -172,7 +169,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
172
169
return nil , err
173
170
}
174
171
175
- log .Infof ("imported static address taproot script to lnd wallet: %v" ,
172
+ log .Infof ("Imported static address taproot script to lnd wallet: %v" ,
176
173
addr )
177
174
178
175
return m .getTaprootAddress (
@@ -197,12 +194,6 @@ func (m *Manager) getTaprootAddress(clientPubkey,
197
194
)
198
195
}
199
196
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
-
206
197
// ListUnspentRaw returns a list of utxos at the static address.
207
198
func (m * Manager ) ListUnspentRaw (ctx context.Context , minConfs ,
208
199
maxConfs int32 ) (* btcutil.AddressTaproot , []* lnwallet.Utxo , error ) {
@@ -213,7 +204,7 @@ func (m *Manager) ListUnspentRaw(ctx context.Context, minConfs,
213
204
return nil , nil , err
214
205
215
206
case len (addresses ) == 0 :
216
- return nil , nil , fmt . Errorf ( "no address found" )
207
+ return nil , nil , nil
217
208
218
209
case len (addresses ) > 1 :
219
210
return nil , nil , fmt .Errorf ("more than one address found" )
@@ -249,3 +240,52 @@ func (m *Manager) ListUnspentRaw(ctx context.Context, minConfs,
249
240
250
241
return taprootAddress , filteredUtxos , nil
251
242
}
243
+
244
+ // GetStaticAddressParameters returns the parameters of the static address.
245
+ func (m * Manager ) GetStaticAddressParameters (ctx context.Context ) (* Parameters ,
246
+ error ) {
247
+
248
+ params , err := m .cfg .Store .GetAllStaticAddresses (ctx )
249
+ if err != nil {
250
+ return nil , err
251
+ }
252
+
253
+ if len (params ) == 0 {
254
+ return nil , fmt .Errorf ("no static address parameters found" )
255
+ }
256
+
257
+ return params [0 ], nil
258
+ }
259
+
260
+ // GetStaticAddress returns a taproot address for the given client and server
261
+ // public keys and expiry.
262
+ func (m * Manager ) GetStaticAddress (ctx context.Context ) (* script.StaticAddress ,
263
+ error ) {
264
+
265
+ params , err := m .GetStaticAddressParameters (ctx )
266
+ if err != nil {
267
+ return nil , err
268
+ }
269
+
270
+ address , err := script .NewStaticAddress (
271
+ input .MuSig2Version100RC2 , int64 (params .Expiry ),
272
+ params .ClientPubkey , params .ServerPubkey ,
273
+ )
274
+ if err != nil {
275
+ return nil , err
276
+ }
277
+
278
+ return address , nil
279
+ }
280
+
281
+ // ListUnspent returns a list of utxos at the static address.
282
+ func (m * Manager ) ListUnspent (ctx context.Context , minConfs ,
283
+ maxConfs int32 ) ([]* lnwallet.Utxo , error ) {
284
+
285
+ _ , utxos , err := m .ListUnspentRaw (ctx , minConfs , maxConfs )
286
+ if err != nil {
287
+ return nil , err
288
+ }
289
+
290
+ return utxos , nil
291
+ }
0 commit comments