1
1
mirror of https://github.com/bumi/lntip synced 2025-07-18 13:36:44 +00:00

Formatting

This commit is contained in:
bumi 2021-08-27 11:18:49 +02:00
parent 5c8d604a20
commit 518a834066
2 changed files with 67 additions and 66 deletions

108
ln/lnd.go
View File

@ -1,13 +1,13 @@
package ln package ln
import ( import (
"fmt"
"context" "context"
"crypto/x509"
"encoding/hex" "encoding/hex"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"crypto/x509"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons" "github.com/lightningnetwork/lnd/macaroons"
@ -29,11 +29,11 @@ type Invoice struct {
// LNDoptions are the options for the connection to the lnd node. // LNDoptions are the options for the connection to the lnd node.
type LNDoptions struct { type LNDoptions struct {
Address string Address string
CertFile string CertFile string
CertHex string CertHex string
MacaroonFile string MacaroonFile string
MacaroonHex string MacaroonHex string
} }
type LNDclient struct { type LNDclient struct {
@ -46,7 +46,7 @@ type LNDclient struct {
func (c LNDclient) AddInvoice(value int64, memo string) (Invoice, error) { func (c LNDclient) AddInvoice(value int64, memo string) (Invoice, error) {
result := Invoice{} result := Invoice{}
stdOutLogger.Printf("Adding invoice: memo=%s amount=%v ", memo, value) stdOutLogger.Printf("Adding invoice: memo=%s value=%v ", memo, value)
invoice := lnrpc.Invoice{ invoice := lnrpc.Invoice{
Memo: memo, Memo: memo,
Value: value, Value: value,
@ -64,14 +64,14 @@ func (c LNDclient) AddInvoice(value int64, memo string) (Invoice, error) {
// NewAddress gets the next BTC onchain address. // NewAddress gets the next BTC onchain address.
func (c LNDclient) NewAddress() (string, error) { func (c LNDclient) NewAddress() (string, error) {
stdOutLogger.Printf("Getting a new BTC address") stdOutLogger.Printf("Getting a new BTC address")
request := lnrpc.NewAddressRequest{ request := lnrpc.NewAddressRequest{
Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH, Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH,
} }
res, err := c.lndClient.NewAddress(c.ctx, &request) res, err := c.lndClient.NewAddress(c.ctx, &request)
if err != nil { if err != nil {
return "", err return "", err
} }
return res.Address, nil return res.Address, nil
} }
// GetInvoice takes an invoice ID and returns the invoice details including settlement details // GetInvoice takes an invoice ID and returns the invoice details including settlement details
@ -107,50 +107,50 @@ func (c LNDclient) GetInvoice(paymentHashStr string) (Invoice, error) {
func NewLNDclient(lndOptions LNDoptions) (LNDclient, error) { func NewLNDclient(lndOptions LNDoptions) (LNDclient, error) {
result := LNDclient{} result := LNDclient{}
// Get credentials either from a hex string or a file // Get credentials either from a hex string or a file
var creds credentials.TransportCredentials var creds credentials.TransportCredentials
// if a hex string is provided // if a hex string is provided
if lndOptions.CertHex != "" { if lndOptions.CertHex != "" {
cp := x509.NewCertPool() cp := x509.NewCertPool()
cert, err := hex.DecodeString(lndOptions.CertHex) cert, err := hex.DecodeString(lndOptions.CertHex)
if err != nil { if err != nil {
return result, err return result, err
} }
cp.AppendCertsFromPEM(cert) cp.AppendCertsFromPEM(cert)
creds = credentials.NewClientTLSFromCert(cp, "") creds = credentials.NewClientTLSFromCert(cp, "")
// if a path to a cert file is provided // if a path to a cert file is provided
} else if lndOptions.CertFile != "" { } else if lndOptions.CertFile != "" {
credsFromFile, err := credentials.NewClientTLSFromFile(lndOptions.CertFile, "") credsFromFile, err := credentials.NewClientTLSFromFile(lndOptions.CertFile, "")
if err != nil { if err != nil {
return result, err return result, err
} }
creds = credsFromFile // make it available outside of the else if block creds = credsFromFile // make it available outside of the else if block
} else { } else {
return result, fmt.Errorf("LND credential is missing") return result, fmt.Errorf("LND credential is missing")
} }
opts := []grpc.DialOption{ opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds), grpc.WithTransportCredentials(creds),
} }
var macaroonData []byte var macaroonData []byte
if lndOptions.MacaroonHex != "" { if lndOptions.MacaroonHex != "" {
macBytes, err := hex.DecodeString(lndOptions.MacaroonHex) macBytes, err := hex.DecodeString(lndOptions.MacaroonHex)
if err != nil { if err != nil {
return result, err return result, err
} }
macaroonData = macBytes macaroonData = macBytes
} else if lndOptions.MacaroonFile != "" { } else if lndOptions.MacaroonFile != "" {
macBytes, err := ioutil.ReadFile(lndOptions.MacaroonFile) macBytes, err := ioutil.ReadFile(lndOptions.MacaroonFile)
if err != nil { if err != nil {
return result, err return result, err
} }
macaroonData = macBytes // make it available outside of the else if block macaroonData = macBytes // make it available outside of the else if block
} else { } else {
return result, fmt.Errorf("LND macaroon is missing") return result, fmt.Errorf("LND macaroon is missing")
} }
mac := &macaroon.Macaroon{} mac := &macaroon.Macaroon{}
if err := mac.UnmarshalBinary(macaroonData); err != nil { if err := mac.UnmarshalBinary(macaroonData); err != nil {
return result, err return result, err
} }
macCred := macaroons.NewMacaroonCredential(mac) macCred := macaroons.NewMacaroonCredential(mac)

25
lnme.go
View File

@ -2,7 +2,12 @@ package main
import ( import (
"flag" "flag"
"github.com/GeertJohan/go.rice" "log"
"net/http"
"os"
"strings"
rice "github.com/GeertJohan/go.rice"
"github.com/bumi/lnme/ln" "github.com/bumi/lnme/ln"
"github.com/didip/tollbooth/v6" "github.com/didip/tollbooth/v6"
"github.com/didip/tollbooth/v6/limiter" "github.com/didip/tollbooth/v6/limiter"
@ -13,10 +18,6 @@ import (
"github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/providers/file"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware" "github.com/labstack/echo/v4/middleware"
"log"
"net/http"
"os"
"strings"
) )
// Middleware for request limited to prevent too many requests // Middleware for request limited to prevent too many requests
@ -124,9 +125,9 @@ func main() {
}) })
// Check invoice status // Check invoice status
e.GET("/v1/invoice/:invoiceId", func(c echo.Context) error { e.GET("/v1/invoice/:paymentHash", func(c echo.Context) error {
invoiceId := c.Param("invoiceId") paymentHash := c.Param("paymentHash")
invoice, err := lnClient.GetInvoice(invoiceId) invoice, err := lnClient.GetInvoice(paymentHash)
if err != nil { if err != nil {
stdOutLogger.Printf("Error looking up invoice: %s", err) stdOutLogger.Printf("Error looking up invoice: %s", err)
@ -141,10 +142,10 @@ func main() {
return c.JSON(http.StatusOK, "pong") return c.JSON(http.StatusOK, "pong")
}) })
port := cfg.String("port") port := cfg.String("port")
if os.Getenv("PORT") != "" { if os.Getenv("PORT") != "" {
port = os.Getenv("PORT") port = os.Getenv("PORT")
} }
e.Logger.Fatal(e.Start(":" + port)) e.Logger.Fatal(e.Start(":" + port))
} }