1
1
mirror of https://github.com/bumi/lntip synced 2025-07-18 05:26:45 +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
import (
"fmt"
"context"
"crypto/x509"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
"crypto/x509"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
@ -29,11 +29,11 @@ type Invoice struct {
// LNDoptions are the options for the connection to the lnd node.
type LNDoptions struct {
Address string
CertFile string
CertHex string
Address string
CertFile string
CertHex string
MacaroonFile string
MacaroonHex string
MacaroonHex string
}
type LNDclient struct {
@ -46,7 +46,7 @@ type LNDclient struct {
func (c LNDclient) AddInvoice(value int64, memo string) (Invoice, error) {
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{
Memo: memo,
Value: value,
@ -64,14 +64,14 @@ func (c LNDclient) AddInvoice(value int64, memo string) (Invoice, error) {
// NewAddress gets the next BTC onchain address.
func (c LNDclient) NewAddress() (string, error) {
stdOutLogger.Printf("Getting a new BTC address")
request := lnrpc.NewAddressRequest{
Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH,
}
res, err := c.lndClient.NewAddress(c.ctx, &request)
if err != nil {
return "", err
}
return res.Address, nil
request := lnrpc.NewAddressRequest{
Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH,
}
res, err := c.lndClient.NewAddress(c.ctx, &request)
if err != nil {
return "", err
}
return res.Address, nil
}
// 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) {
result := LNDclient{}
// Get credentials either from a hex string or a file
var creds credentials.TransportCredentials
// if a hex string is provided
if lndOptions.CertHex != "" {
cp := x509.NewCertPool()
cert, err := hex.DecodeString(lndOptions.CertHex)
if err != nil {
return result, err
}
cp.AppendCertsFromPEM(cert)
creds = credentials.NewClientTLSFromCert(cp, "")
// if a path to a cert file is provided
} else if lndOptions.CertFile != "" {
credsFromFile, err := credentials.NewClientTLSFromFile(lndOptions.CertFile, "")
if err != nil {
return result, err
}
creds = credsFromFile // make it available outside of the else if block
} else {
return result, fmt.Errorf("LND credential is missing")
}
// Get credentials either from a hex string or a file
var creds credentials.TransportCredentials
// if a hex string is provided
if lndOptions.CertHex != "" {
cp := x509.NewCertPool()
cert, err := hex.DecodeString(lndOptions.CertHex)
if err != nil {
return result, err
}
cp.AppendCertsFromPEM(cert)
creds = credentials.NewClientTLSFromCert(cp, "")
// if a path to a cert file is provided
} else if lndOptions.CertFile != "" {
credsFromFile, err := credentials.NewClientTLSFromFile(lndOptions.CertFile, "")
if err != nil {
return result, err
}
creds = credsFromFile // make it available outside of the else if block
} else {
return result, fmt.Errorf("LND credential is missing")
}
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
}
var macaroonData []byte
if lndOptions.MacaroonHex != "" {
macBytes, err := hex.DecodeString(lndOptions.MacaroonHex)
if err != nil {
return result, err
}
macaroonData = macBytes
} else if lndOptions.MacaroonFile != "" {
macBytes, err := ioutil.ReadFile(lndOptions.MacaroonFile)
if err != nil {
return result, err
}
macaroonData = macBytes // make it available outside of the else if block
} else {
return result, fmt.Errorf("LND macaroon is missing")
}
var macaroonData []byte
if lndOptions.MacaroonHex != "" {
macBytes, err := hex.DecodeString(lndOptions.MacaroonHex)
if err != nil {
return result, err
}
macaroonData = macBytes
} else if lndOptions.MacaroonFile != "" {
macBytes, err := ioutil.ReadFile(lndOptions.MacaroonFile)
if err != nil {
return result, err
}
macaroonData = macBytes // make it available outside of the else if block
} else {
return result, fmt.Errorf("LND macaroon is missing")
}
mac := &macaroon.Macaroon{}
if err := mac.UnmarshalBinary(macaroonData); err != nil {
mac := &macaroon.Macaroon{}
if err := mac.UnmarshalBinary(macaroonData); err != nil {
return result, err
}
macCred := macaroons.NewMacaroonCredential(mac)

25
lnme.go
View File

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