Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveblue committed Jun 30, 2023
1 parent cf36613 commit 570be29
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 45 deletions.
42 changes: 8 additions & 34 deletions aperture.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ type Aperture struct {

etcdClient *clientv3.Client
db *sql.DB
lncNodeConn *lnc.NodeConn
challenger challenger.Challenger
httpsServer *http.Server
torHTTPServer *http.Server
Expand Down Expand Up @@ -338,31 +337,15 @@ func (a *Aperture) Start(errChan chan error) error {
"session: %w", err)
}

nodeConn, err := lnc.NewNodeConn(session, lncStore)
if err != nil {
return fmt.Errorf("unable to connect to lnd "+
"using lnc: %w", err)
}

client, err := nodeConn.Client()
if err != nil {
return err
}

a.challenger, err = challenger.NewLndChallenger(
client, genInvoiceReq, nodeConn.CtxFunc,
errChan,
challenger, err := challenger.NewLNCChallenger(
session, lncStore, genInvoiceReq, errChan,
)
if err != nil {
return err
}

err = a.challenger.Start()
if err != nil {
return err
return fmt.Errorf("unable to create lnc "+
"challenger: %w", err)
}

a.lncNodeConn = nodeConn
a.challenger = challenger

case !a.cfg.Authenticator.Disable:
log.Infof("Using lnd's authenticator config")
Expand All @@ -378,18 +361,16 @@ func (a *Aperture) Start(errChan chan error) error {
if err != nil {
return err
}
a.challenger, err = challenger.NewLndChallenger(

challenger, err := challenger.NewLndChallenger(
client, genInvoiceReq, context.Background,
errChan,
)
if err != nil {
return err
}

err = a.challenger.Start()
if err != nil {
return err
}
a.challenger = challenger
}
}

Expand Down Expand Up @@ -497,13 +478,6 @@ func (a *Aperture) Stop() error {
a.challenger.Stop()
}

if a.lncNodeConn != nil {
if err := a.lncNodeConn.Stop(); err != nil {
log.Errorf("Error closing LNC connection: %v", err)
returnErr = err
}
}

// Stop everything that was started alongside the proxy, for example the
// gRPC and REST servers.
if a.proxyCleanup != nil {
Expand Down
84 changes: 84 additions & 0 deletions challenger/lnc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package challenger

import (
"fmt"
"time"

"github.com/lightninglabs/aperture/lnc"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
)

// LndChallenger is a challenger that uses an lnd backend to create new LSAT
// payment challenges.
type LNCChallenger struct {
lndChallenger *LndChallenger
nodeConn *lnc.NodeConn
}

// NewLndChallenger creates a new challenger that uses the given connection to
// an lnd backend to create payment challenges.
func NewLNCChallenger(session *lnc.Session, lncStore lnc.Store,
genInvoiceReq InvoiceRequestGenerator,
errChan chan<- error) (*LNCChallenger, error) {

nodeConn, err := lnc.NewNodeConn(session, lncStore)
if err != nil {
return nil, fmt.Errorf("unable to connect to lnd using lnc: %w",
err)
}

client, err := nodeConn.Client()
if err != nil {
return nil, err
}

lndChallenger, err := NewLndChallenger(
client, genInvoiceReq, nodeConn.CtxFunc, errChan,
)
if err != nil {
return nil, err
}

err = lndChallenger.Start()
if err != nil {
return nil, err
}

return &LNCChallenger{
lndChallenger: lndChallenger,
nodeConn: nodeConn,
}, nil
}

// Stop stops the challenger.
func (l *LNCChallenger) Stop() {
err := l.nodeConn.Stop()
if err != nil {
log.Errorf("unable to stop lnc node conn: %v", err)
}

l.lndChallenger.Stop()
}

// NewChallenge creates a new LSAT payment challenge, returning a payment
// request (invoice) and the corresponding payment hash.
//
// NOTE: This is part of the mint.Challenger interface.
func (l *LNCChallenger) NewChallenge(price int64) (string, lntypes.Hash,
error) {

return l.lndChallenger.NewChallenge(price)
}

// VerifyInvoiceStatus checks that an invoice identified by a payment
// hash has the desired status. To make sure we don't fail while the
// invoice update is still on its way, we try several times until either
// the desired status is set or the given timeout is reached.
//
// NOTE: This is part of the auth.InvoiceChecker interface.
func (l *LNCChallenger) VerifyInvoiceStatus(hash lntypes.Hash,
state lnrpc.Invoice_InvoiceState, timeout time.Duration) error {

return l.lndChallenger.VerifyInvoiceStatus(hash, state, timeout)
}
11 changes: 9 additions & 2 deletions challenger/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewLndChallenger(client InvoiceClient,
}

invoicesMtx := &sync.Mutex{}
return &LndChallenger{
challenger := &LndChallenger{
client: client,
clientCtx: ctxFunc,
genInvoiceReq: genInvoiceReq,
Expand All @@ -62,7 +62,14 @@ func NewLndChallenger(client InvoiceClient,
invoicesCond: sync.NewCond(invoicesMtx),
quit: make(chan struct{}),
errChan: errChan,
}, nil
}

err := challenger.Start()
if err != nil {
return nil, err
}

return challenger, nil
}

// Start starts the challenger's main work which is to keep track of all
Expand Down
12 changes: 3 additions & 9 deletions mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,14 @@ var (
// challenge that must be satisfied before an LSAT can be validated. This
// challenge takes the form of a Lightning payment request.
type Challenger interface {
// Start starts the challenger's main work which is to keep track of all
// invoices and their states. For that the backing lnd node is queried
// for all invoices on startup and the a subscription to all subsequent
// invoice updates is created.
Start() error

// Stop shuts down the challenger.
Stop()

// NewChallenge returns a new challenge in the form of a Lightning
// payment request. The payment hash is also returned as a convenience
// to avoid having to decode the payment request in order to retrieve
// its payment hash.
NewChallenge(price int64) (string, lntypes.Hash, error)

// Stop shuts down the challenger.
Stop()
}

// SecretStore is the store responsible for storing LSAT secrets. These secrets
Expand Down

0 comments on commit 570be29

Please sign in to comment.