@@ -40,13 +40,19 @@ type ManagerConfig struct {
4040 // ChainParams is the chain configuration(mainnet, testnet...) this
4141 // manager uses.
4242 ChainParams * chaincfg.Params
43+
44+ // ChainNotifier is the chain notifier that is used to listen for new
45+ // blocks.
46+ ChainNotifier lndclient.ChainNotifierClient
4347}
4448
4549// Manager manages the address state machines.
4650type Manager struct {
4751 cfg * ManagerConfig
4852
4953 sync.Mutex
54+
55+ currentHeight int32
5056}
5157
5258// NewManager creates a new address manager.
@@ -58,8 +64,26 @@ func NewManager(cfg *ManagerConfig) *Manager {
5864
5965// Run runs the address manager.
6066func (m * Manager ) Run (ctx context.Context ) error {
61- <- ctx .Done ()
62- return nil
67+ newBlockChan , newBlockErrChan , err :=
68+ m .cfg .ChainNotifier .RegisterBlockEpochNtfn (ctx )
69+
70+ if err != nil {
71+ return err
72+ }
73+
74+ for {
75+ select {
76+ case currentHeight := <- newBlockChan :
77+ m .currentHeight = currentHeight
78+
79+ case err = <- newBlockErrChan :
80+ return err
81+
82+ case <- ctx .Done ():
83+ // Signal subroutines that the manager is exiting.
84+ return ctx .Err ()
85+ }
86+ }
6387}
6488
6589// NewAddress starts a new address creation flow.
@@ -86,6 +110,11 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
86110 }
87111 m .Unlock ()
88112
113+ // Ensure that we have that we have a sane current block height.
114+ if m .currentHeight == 0 {
115+ return nil , fmt .Errorf ("current block height is unknown" )
116+ }
117+
89118 // We are fetching a new L402 token from the server. There is one static
90119 // address per L402 token allowed.
91120 err = m .cfg .FetchL402 (ctx )
@@ -147,6 +176,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
147176 ProtocolVersion : version .AddressProtocolVersion (
148177 protocolVersion ,
149178 ),
179+ InitiationHeight : m .currentHeight ,
150180 }
151181 err = m .cfg .Store .CreateStaticAddress (ctx , addrParams )
152182 if err != nil {
0 commit comments