lndclient: add preimage to invoice subscription

pull/190/head
carla 4 years ago
parent ac096132b0
commit fe960a4382
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -30,6 +30,10 @@ type InvoicesClient interface {
type InvoiceUpdate struct {
State channeldb.ContractState
AmtPaid btcutil.Amount
// Preimage is the preimage for the invoice that has just been
// paid. This field will be nil for updates that are not settles.
Preimage *lntypes.Preimage
}
type invoicesClient struct {
@ -111,11 +115,26 @@ func (s *invoicesClient) SubscribeSingleInvoice(ctx context.Context,
return
}
select {
case updateChan <- InvoiceUpdate{
update := InvoiceUpdate{
State: state,
AmtPaid: btcutil.Amount(invoice.AmtPaidSat),
}:
}
// If the invoice is settled, we expect the preimage
// to be present so we include it.
if state == channeldb.ContractSettled {
preimage, err := lntypes.MakePreimage(
invoice.RPreimage,
)
if err != nil {
errChan <- err
return
}
update.Preimage = &preimage
}
select {
case updateChan <- update:
case <-ctx.Done():
return
}

@ -10,6 +10,7 @@ import (
"github.com/lightninglabs/loop/test"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
@ -82,11 +83,17 @@ func TestLoopInSuccess(t *testing.T) {
t.Fatal("client subscribing to wrong invoice")
}
typedPreimage, err := lntypes.MakePreimage(testPreimage[:])
if err != nil {
t.Fatalf("could not make preimage: %v", err)
}
// Server has already paid invoice before spending the htlc. Signal
// settled.
subscription.Update <- lndclient.InvoiceUpdate{
State: channeldb.ContractSettled,
AmtPaid: 49000,
State: channeldb.ContractSettled,
AmtPaid: 49000,
Preimage: &typedPreimage,
}
// Swap is expected to move to the state InvoiceSettled
@ -393,11 +400,17 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
t.Fatal("client subscribing to wrong invoice")
}
typedPreimage, err := lntypes.MakePreimage(testPreimage[:])
if err != nil {
t.Fatalf("could not make preimage: %v", err)
}
// Server has already paid invoice before spending the htlc. Signal
// settled.
subscription.Update <- lndclient.InvoiceUpdate{
State: channeldb.ContractSettled,
AmtPaid: 49000,
State: channeldb.ContractSettled,
AmtPaid: 49000,
Preimage: &typedPreimage,
}
// Swap is expected to move to the state InvoiceSettled

Loading…
Cancel
Save