Skip to content

staticaddr: add expiry to NewAddress response #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/loop/staticaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ func newStaticAddress(ctx *cli.Context) error {
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: commit message is not really fitting for what it does

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

fmt.Printf("Received a new static loop-in address from the server: "+
"%s\n", resp.Address)
printRespJSON(resp)

return nil
}
Expand Down
3 changes: 2 additions & 1 deletion loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 0 additions & 5 deletions staticaddr/address/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 27 additions & 14 deletions staticaddr/address/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -101,31 +102,38 @@ 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()

// We are fetching a new L402 token from the server. There is one static
// 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
Expand All @@ -138,27 +146,27 @@ 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(
input.MuSig2Version100RC2, int64(serverParams.Expiry),
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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion staticaddr/address/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down