diff --git a/cmd/loop/staticaddr.go b/cmd/loop/staticaddr.go index 665e5cd41..5b3b86e29 100644 --- a/cmd/loop/staticaddr.go +++ b/cmd/loop/staticaddr.go @@ -75,8 +75,7 @@ func newStaticAddress(ctx *cli.Context) error { return err } - fmt.Printf("Received a new static loop-in address from the server: "+ - "%s\n", resp.Address) + printRespJSON(resp) return nil } diff --git a/loopd/swapclient_server.go b/loopd/swapclient_server.go index e8dbae007..651a8107c 100644 --- a/loopd/swapclient_server.go +++ b/loopd/swapclient_server.go @@ -1505,13 +1505,14 @@ func (s *swapClientServer) NewStaticAddress(ctx context.Context, _ *looprpc.NewStaticAddressRequest) ( *looprpc.NewStaticAddressResponse, error) { - staticAddress, err := s.staticAddressManager.NewAddress(ctx) + staticAddress, expiry, err := s.staticAddressManager.NewAddress(ctx) if err != nil { return nil, err } return &looprpc.NewStaticAddressResponse{ Address: staticAddress.String(), + Expiry: uint32(expiry), }, nil } diff --git a/staticaddr/address/interface.go b/staticaddr/address/interface.go index 5e39abdf7..54d785de2 100644 --- a/staticaddr/address/interface.go +++ b/staticaddr/address/interface.go @@ -2,17 +2,12 @@ package address import ( "context" - "fmt" "github.com/btcsuite/btcd/btcec/v2" "github.com/lightninglabs/loop/staticaddr/version" "github.com/lightningnetwork/lnd/keychain" ) -var ( - ErrAddressAlreadyExists = fmt.Errorf("address already exists") -) - // Store is the database interface that is used to store and retrieve // static addresses. type Store interface { diff --git a/staticaddr/address/manager.go b/staticaddr/address/manager.go index cc6012738..578904501 100644 --- a/staticaddr/address/manager.go +++ b/staticaddr/address/manager.go @@ -90,9 +90,10 @@ func (m *Manager) Run(ctx context.Context) error { } } -// NewAddress starts a new address creation flow. +// NewAddress creates a new static address with the server or returns an +// existing one. func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot, - error) { + int64, error) { // If there's already a static address in the database, we can return // it. @@ -101,16 +102,23 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot, if err != nil { m.Unlock() - return nil, err + return nil, 0, err } if len(addresses) > 0 { clientPubKey := addresses[0].ClientPubkey serverPubKey := addresses[0].ServerPubkey expiry := int64(addresses[0].Expiry) - m.Unlock() + defer m.Unlock() + + address, err := m.GetTaprootAddress( + clientPubKey, serverPubKey, expiry, + ) + if err != nil { + return nil, 0, err + } - return m.GetTaprootAddress(clientPubKey, serverPubKey, expiry) + return address, expiry, nil } m.Unlock() @@ -118,14 +126,14 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot, // address per L402 token allowed. err = m.cfg.FetchL402(ctx) if err != nil { - return nil, err + return nil, 0, err } clientPubKey, err := m.cfg.WalletKit.DeriveNextKey( ctx, swap.StaticAddressKeyFamily, ) if err != nil { - return nil, err + return nil, 0, err } // Send our clientPubKey to the server and wait for the server to @@ -138,14 +146,14 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot, }, ) if err != nil { - return nil, err + return nil, 0, err } serverParams := resp.GetParams() serverPubKey, err := btcec.ParsePubKey(serverParams.ServerKey) if err != nil { - return nil, err + return nil, 0, err } staticAddress, err := script.NewStaticAddress( @@ -153,12 +161,12 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot, clientPubKey.PubKey, serverPubKey, ) if err != nil { - return nil, err + return nil, 0, err } pkScript, err := staticAddress.StaticAddressScript() if err != nil { - return nil, err + return nil, 0, err } // Create the static address from the parameters the server provided and @@ -179,7 +187,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot, } err = m.cfg.Store.CreateStaticAddress(ctx, addrParams) if err != nil { - return nil, err + return nil, 0, err } // Import the static address tapscript into our lnd wallet, so we can @@ -189,15 +197,20 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot, ) addr, err := m.cfg.WalletKit.ImportTaprootScript(ctx, tapScript) if err != nil { - return nil, err + return nil, 0, err } log.Infof("Imported static address taproot script to lnd wallet: %v", addr) - return m.GetTaprootAddress( + address, err := m.GetTaprootAddress( clientPubKey.PubKey, serverPubKey, int64(serverParams.Expiry), ) + if err != nil { + return nil, 0, err + } + + return address, int64(serverParams.Expiry), nil } // GetTaprootAddress returns a taproot address for the given client and server diff --git a/staticaddr/address/manager_test.go b/staticaddr/address/manager_test.go index 37ba83c9b..f1eb0adaf 100644 --- a/staticaddr/address/manager_test.go +++ b/staticaddr/address/manager_test.go @@ -105,11 +105,14 @@ func TestManager(t *testing.T) { require.NoError(t, err) // Create a new static address. - taprootAddress, err := testContext.manager.NewAddress(ctxb) + taprootAddress, expiry, err := testContext.manager.NewAddress(ctxb) require.NoError(t, err) // The addresses have to match. require.Equal(t, expectedAddress.String(), taprootAddress.String()) + + // The expiry has to match. + require.EqualValues(t, defaultExpiry, expiry) } // GenerateExpectedTaprootAddress generates the expected taproot address that