Skip to content

Commit

Permalink
lncli: keysend acceptance [poc]
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Jun 25, 2020
1 parent 913cc02 commit 6cbbc93
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
126 changes: 126 additions & 0 deletions cmd/lncli/cmd_tlvshop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"context"
"fmt"
"time"

"github.com/lightningnetwork/lnd/lntypes"

"github.com/lightningnetwork/lnd/lnrpc"

"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
"github.com/urfave/cli"
)

var ctxb = context.Background()

var tlvshopCommand = cli.Command{
Name: "tlvshop",
Action: actionDecorator(tlvshop),
}

func tlvshop(ctx *cli.Context) error {
conn := getClientConn(ctx, false)
defer conn.Close()

client := lnrpc.NewLightningClient(conn)
invoicesClient := invoicesrpc.NewInvoicesClient(conn)

allCtx, cancel := context.WithCancel(ctxb)
defer cancel()

stream, err := client.SubscribeInvoices(allCtx, &lnrpc.InvoiceSubscription{})
if err != nil {
return err
}

invoices := make(map[lntypes.Hash]struct{})
for {
invoice, err := stream.Recv()
if err != nil {
return err
}

// Don't need to track invoices without preimage.
if invoice.RPreimage == nil {
continue
}

hash, err := lntypes.MakeHash(invoice.RHash)
if err != nil {
return err
}

if _, ok := invoices[hash]; ok {
continue
}

invoices[hash] = struct{}{}

go func() {
err := trackSingle(allCtx, invoicesClient, hash)
if err != nil {
fmt.Printf("error: %v\n", err)
cancel()
}
}()
}
}

func trackSingle(ctx context.Context, invoicesClient invoicesrpc.InvoicesClient,
hash lntypes.Hash) error {

singleStream, err := invoicesClient.SubscribeSingleInvoice(
ctx,
&invoicesrpc.SubscribeSingleInvoiceRequest{
RHash: hash[:],
},
)
if err != nil {
return err
}

for {
invoice, err := singleStream.Recv()
if err != nil {
return err
}

fmt.Printf("%x: state=%v, preimage=%x\n", invoice.RHash,
invoice.State, invoice.RPreimage)

if invoice.State == lnrpc.Invoice_ACCEPTED {
// Check amount paid
amtOk := invoice.Value > 500

// Check webshop inventory here.
time.Sleep(2 * time.Second)
stockOk := true

settlePayment := amtOk && stockOk

if settlePayment {
_, err := invoicesClient.SettleInvoice(
ctxb,
&invoicesrpc.SettleInvoiceMsg{
Preimage: invoice.RPreimage,
},
)
if err != nil {
return err
}
} else {
_, err := invoicesClient.CancelInvoice(
ctxb,
&invoicesrpc.CancelInvoiceMsg{
PaymentHash: hash[:],
},
)
if err != nil {
return err
}
}
}
}
}
1 change: 1 addition & 0 deletions cmd/lncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func main() {
bakeMacaroonCommand,
trackPaymentCommand,
versionCommand,
tlvshopCommand,
}

// Add any extra commands determined by build flags.
Expand Down

0 comments on commit 6cbbc93

Please sign in to comment.