Skip to content

Commit a6ff4a9

Browse files
committed
challenger: add LNCChallenger
1 parent 4e81f5d commit a6ff4a9

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

challenger/lnc.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package challenger
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/lightninglabs/aperture/lnc"
8+
"github.com/lightningnetwork/lnd/lnrpc"
9+
"github.com/lightningnetwork/lnd/lntypes"
10+
)
11+
12+
// LNCChallenger is a challenger that uses LNC to connect to an lnd backend to
13+
// create new LSAT payment challenges.
14+
type LNCChallenger struct {
15+
lndChallenger *LndChallenger
16+
nodeConn *lnc.NodeConn
17+
}
18+
19+
// NewLNCChallenger creates a new challenger that uses the given LNC session to
20+
// connect to an lnd backend to create payment challenges.
21+
func NewLNCChallenger(session *lnc.Session, lncStore lnc.Store,
22+
genInvoiceReq InvoiceRequestGenerator,
23+
errChan chan<- error) (*LNCChallenger, error) {
24+
25+
nodeConn, err := lnc.NewNodeConn(session, lncStore)
26+
if err != nil {
27+
return nil, fmt.Errorf("unable to connect to lnd using lnc: %w",
28+
err)
29+
}
30+
31+
client, err := nodeConn.Client()
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
lndChallenger, err := NewLndChallenger(
37+
client, genInvoiceReq, nodeConn.CtxFunc, errChan,
38+
)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
err = lndChallenger.Start()
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return &LNCChallenger{
49+
lndChallenger: lndChallenger,
50+
nodeConn: nodeConn,
51+
}, nil
52+
}
53+
54+
// Stop stops the challenger.
55+
func (l *LNCChallenger) Stop() {
56+
err := l.nodeConn.Stop()
57+
if err != nil {
58+
log.Errorf("unable to stop lnc node conn: %v", err)
59+
}
60+
61+
l.lndChallenger.Stop()
62+
}
63+
64+
// NewChallenge creates a new LSAT payment challenge, returning a payment
65+
// request (invoice) and the corresponding payment hash.
66+
//
67+
// NOTE: This is part of the mint.Challenger interface.
68+
func (l *LNCChallenger) NewChallenge(price int64) (string, lntypes.Hash,
69+
error) {
70+
71+
return l.lndChallenger.NewChallenge(price)
72+
}
73+
74+
// VerifyInvoiceStatus checks that an invoice identified by a payment
75+
// hash has the desired status. To make sure we don't fail while the
76+
// invoice update is still on its way, we try several times until either
77+
// the desired status is set or the given timeout is reached.
78+
//
79+
// NOTE: This is part of the auth.InvoiceChecker interface.
80+
func (l *LNCChallenger) VerifyInvoiceStatus(hash lntypes.Hash,
81+
state lnrpc.Invoice_InvoiceState, timeout time.Duration) error {
82+
83+
return l.lndChallenger.VerifyInvoiceStatus(hash, state, timeout)
84+
}

0 commit comments

Comments
 (0)