Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Make bitcoind optional
Browse files Browse the repository at this point in the history
  • Loading branch information
meeDamian committed Aug 29, 2019
1 parent bb82b40 commit ca7f309
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
4 changes: 3 additions & 1 deletion common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ type (
}

Info struct {
Uris []string `json:"uris"`
OnChain bool `json:"on-chain"`
OffChain bool `json:"off-chain"`
Uris []string `json:"uris"`
}

AddrStatus struct {
Expand Down
3 changes: 3 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type (
// Currently only `lnd` supported
LnClient string `toml:"ln-client"`

// Allows for disabling the possibility of on-chain payments.
OffChainOnly bool `toml:"off-chain-only"`

// [bitcoind] section in the `--config` file that defines Bitcoind's setup
Bitcoind Bitcoind `toml:"bitcoind"`

Expand Down
34 changes: 34 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package common

import (
"fmt"

"github.com/gin-gonic/gin"
)

func Max(a, b int) int {
if a > b {
return a
}

return b
}

func FormatRoutes(ginRoutes gin.RoutesInfo) (routes []string) {
var maxM, maxP int
for _, r := range ginRoutes {
maxM = Max(maxM, len(r.Method))
maxP = Max(maxP, len(r.Path))
}

for _, r := range ginRoutes {
routes = append(routes, fmt.Sprintf(
fmt.Sprintf("%%-%ds %%-%ds -> %%s", maxM, maxP),
r.Method,
r.Path,
r.Handler,
))
}

return
}
2 changes: 2 additions & 0 deletions invoicer.example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ static-dir = "static/"
log-file = "~/.lncm/invoicer.log"
ln-client = "lnd"

off-chain-only = false

[bitcoind]
host = "localhost"
port = 8332
Expand Down
67 changes: 40 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ var (

configFilePath = flag.String("config", common.DefaultConfigFile, "Path to a config file in TOML format")
showVersion = flag.Bool("version", false, "Show version and exit")

accounts gin.Accounts
)

func init() {
Expand Down Expand Up @@ -114,14 +112,11 @@ func init() {
}

// Init BTC client for monitoring on-chain payments
btcClient, err = bitcoind.New(conf.Bitcoind)
if err != nil {
panic(err)
}

// If user credentials specified in config file, pass them to gin, which enables `/api/history` endpoint
if len(conf.Users) > 0 {
accounts = gin.Accounts(conf.Users)
if !conf.OffChainOnly {
btcClient, err = bitcoind.New(conf.Bitcoind)
if err != nil {
panic(err)
}
}

if conf.LogFile == "" {
Expand Down Expand Up @@ -177,6 +172,11 @@ func newPayment(c *gin.Context) {
return
}

// Force LN-only, no matter what the request was
if conf.OffChainOnly {
data.Only = "ln"
}

var payment common.NewPayment

if data.Only != "btc" {
Expand Down Expand Up @@ -394,7 +394,7 @@ func status(c *gin.Context) {
}

// keep polling for status update every N seconds
if len(addr) > 0 {
if !conf.OffChainOnly && len(addr) > 0 {
go func() {
paymentStatus <- checkBtcStatus(ctx, fin, addr, len(hash) > 0, flexible, desiredAmount)
}()
Expand Down Expand Up @@ -461,17 +461,20 @@ func history(c *gin.Context) {
}

var warning string
// fetch bitcoin history
btcAllAddresses, err := btcClient.CheckAddress("")
if err != nil {
warning = "Unable to fetch Bitcoin history. Only showing LN."
}

// Convert Bitcoin history from list to easily addressable map
btcHistory := make(map[string]common.AddrStatus)
for _, payment := range btcAllAddresses {
if payment.Label != "" {
btcHistory[payment.Label] = payment

if !conf.OffChainOnly {
// fetch bitcoin history
btcAllAddresses, err := btcClient.CheckAddress("")
if err != nil {
warning = "Unable to fetch Bitcoin history. Only showing LN."
}

// Convert Bitcoin history from list to easily addressable map
for _, payment := range btcAllAddresses {
if payment.Label != "" {
btcHistory[payment.Label] = payment
}
}
}

Expand All @@ -491,8 +494,11 @@ func history(c *gin.Context) {
var payment common.Payment
payment.ApplyLn(invoice)

if btcStatus, ok := btcHistory[payment.Hash]; ok {
payment.ApplyBtc(btcStatus)
// this check is probably unnecessary, as the map is empty anyway…
if !conf.OffChainOnly {
if btcStatus, ok := btcHistory[payment.Hash]; ok {
payment.ApplyBtc(btcStatus)
}
}

switch queryParams.OnlyStatus {
Expand Down Expand Up @@ -539,7 +545,14 @@ func info(c *gin.Context) {
return
}

c.JSON(200, info.Uris)
info.OnChain = true
info.OffChain = true

if conf.OffChainOnly {
info.OnChain = false
}

c.JSON(200, info)
}

func main() {
Expand All @@ -555,8 +568,8 @@ func main() {
r.GET("/info", info)

// history only available if Basic Auth is enabled
if len(accounts) > 0 {
r.GET("/history", gin.BasicAuth(accounts), history)
if len(conf.Users) > 0 {
r.GET("/history", gin.BasicAuth(conf.Users), history)
}

var staticFilePath string
Expand All @@ -570,7 +583,7 @@ func main() {
}

log.WithFields(log.Fields{
"routes": router.Routes(),
"routes": common.FormatRoutes(router.Routes()),
"port": conf.Port,
"static-file": staticFilePath,
}).Println("gin router defined")
Expand Down

0 comments on commit ca7f309

Please sign in to comment.