Skip to content

Commit

Permalink
Fix issue #44, #46, #54
Browse files Browse the repository at this point in the history
  • Loading branch information
klim-sidorov-ledger authored and adrienlacombe committed May 17, 2022
1 parent 7ee85c5 commit 106bc18
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 121 deletions.
12 changes: 11 additions & 1 deletion bus/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bus
import (
"context"
"fmt"
"os"
"strings"

"github.com/btcsuite/btcd/chaincfg"
Expand Down Expand Up @@ -85,7 +86,7 @@ type descriptor struct {
}

// New initializes a Bus struct that embeds a btcd RPC client.
func New(host string, user string, pass string, proxy string, noTLS bool) (*Bus, error) {
func New(host string, user string, pass string, proxy string, noTLS bool, unloadWallet bool) (*Bus, error) {
log.Info("Warming up...")

// Prepare the connection config to initialize the rpcclient.Client
Expand Down Expand Up @@ -144,6 +145,15 @@ func New(host string, user string, pass string, proxy string, noTLS bool) (*Bus,
return nil, err
}

if unloadWallet {
if err = mainClient.UnloadWallet(nil); err != nil {
return nil, err
}

log.Info("Unload wallet: done")
os.Exit(1)
}

isNewWallet, err := loadOrCreateWallet(mainClient)
if err != nil {
return nil, err
Expand Down
150 changes: 150 additions & 0 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package cli

import (
"context"
"net/http"
"os"
"os/signal"
"time"

"github.com/ledgerhq/satstack/bus"
"github.com/ledgerhq/satstack/config"
"github.com/ledgerhq/satstack/fortunes"
"github.com/ledgerhq/satstack/httpd"
"github.com/ledgerhq/satstack/httpd/svc"
"github.com/ledgerhq/satstack/version"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

func init() {
rootCmd.PersistentFlags().String("port", "20000", "Port")
rootCmd.PersistentFlags().Bool("unload-wallet", false, "Whether SatStack should unload wallet")
}

var rootCmd = &cobra.Command{
Use: "lss",
Short: "Bitcoin full node with Ledger Live.",
Long: `Ledger SatStack is a lightweight bridge to connect Ledger Live with your personal Bitcoin full node. It's designed to allow Ledger Live users use Bitcoin without compromising on privacy, or relying on Ledger's infrastructure.`,
Run: func(cmd *cobra.Command, args []string) {
port, _ := cmd.Flags().GetString("port")
unloadWallet, _ := cmd.Flags().GetBool("unload-wallet")

s := startup(unloadWallet)
if s == nil {
return
}

engine := httpd.GetRouter(s)

srv := &http.Server{
Addr: ":" + port,
Handler: engine,
}

go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Failed to listen and serve")
}
}()

// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)

<-quit

log.Info("Shutdown server: in progress")

{
// Scoped block to disconnect all connections, and stop all goroutines.
// If not successful within 5s, drop a nuclear bomb and fail with a
// FATAL error.

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

s.Bus.Close(ctx)
}

{
// Scoped block to gracefully shutdown Gin-Gonic server within 10s.

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
log.WithField("error", err).Fatal("Failed to shutdown server")
}

log.Info("Shutdown server: done")
}
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

func startup(unloadWallet bool) *svc.Service {
log.SetFormatter(&prefixed.TextFormatter{
TimestampFormat: "2006/01/02 - 15:04:05",
FullTimestamp: true,
QuoteEmptyFields: true,
SpacePadding: 45,
})

log.WithFields(log.Fields{
"build": version.Build,
"commit": version.GitCommit,
"runtime": version.GoVersion,
"arch": version.OsArch,
}).Infof("Ledger SatStack (lss) %s", version.Version)

configuration, err := config.Load()
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatal("Failed to load config")
return nil
}

b, err := bus.New(
*configuration.RPCURL,
*configuration.RPCUser,
*configuration.RPCPassword,
configuration.TorProxy,
configuration.NoTLS,
unloadWallet,
)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Failed to initialize Bus")
return nil
}

log.WithFields(log.Fields{
"chain": b.Chain,
"pruned": b.Pruned,
"txindex": b.TxIndex,
"blockFilter": b.BlockFilter,
}).Info("RPC connection established")

s := &svc.Service{
Bus: b,
}

fortunes.Fortune()

s.Bus.Worker(configuration)

return s
}
120 changes: 2 additions & 118 deletions cmd/lss.go
Original file line number Diff line number Diff line change
@@ -1,125 +1,9 @@
package main

import (
"context"
"net/http"
"os"
"os/signal"
"time"

"github.com/ledgerhq/satstack/bus"
"github.com/ledgerhq/satstack/config"
"github.com/ledgerhq/satstack/fortunes"
"github.com/ledgerhq/satstack/httpd"
"github.com/ledgerhq/satstack/httpd/svc"
"github.com/ledgerhq/satstack/version"
log "github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"github.com/ledgerhq/satstack/cmd/cli"
)

func startup() *svc.Service {
log.SetFormatter(&prefixed.TextFormatter{
TimestampFormat: "2006/01/02 - 15:04:05",
FullTimestamp: true,
QuoteEmptyFields: true,
SpacePadding: 45,
})

log.WithFields(log.Fields{
"build": version.Build,
"commit": version.GitCommit,
"runtime": version.GoVersion,
"arch": version.OsArch,
}).Infof("Ledger SatStack (lss) %s", version.Version)

configuration, err := config.Load()
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatal("Failed to load config")
return nil
}

b, err := bus.New(
*configuration.RPCURL,
*configuration.RPCUser,
*configuration.RPCPassword,
configuration.TorProxy,
configuration.NoTLS,
)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Failed to initialize Bus")
return nil
}

log.WithFields(log.Fields{
"chain": b.Chain,
"pruned": b.Pruned,
"txindex": b.TxIndex,
"blockFilter": b.BlockFilter,
}).Info("RPC connection established")

s := &svc.Service{
Bus: b,
}

fortunes.Fortune()

s.Bus.Worker(configuration)

return s
}

func main() {
s := startup()
engine := httpd.GetRouter(s)

srv := &http.Server{
Addr: ":20000",
Handler: engine,
}

go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.WithFields(log.Fields{
"error": err,
}).Fatal("Failed to listen and serve")
}
}()

// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)

<-quit

log.Info("Shutdown server: in progress")

{
// Scoped block to disconnect all connections, and stop all goroutines.
// If not successful within 5s, drop a nuclear bomb and fail with a
// FATAL error.

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

s.Bus.Close(ctx)
}

{
// Scoped block to gracefully shutdown Gin-Gonic server within 10s.

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
log.WithField("error", err).Fatal("Failed to shutdown server")
}

log.Info("Shutdown server: done")
}
cli.Execute()
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.4.0
github.com/x-cray/logrus-prefixed-formatter v0.5.2
)

Expand All @@ -24,6 +25,7 @@ require (
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
Expand All @@ -32,13 +34,14 @@ require (
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9 // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 // indirect
google.golang.org/protobuf v1.23.0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401 => github.com/onyb/btcd v0.20.1-beta.0.20201116101952-848ee6a30375
11 changes: 10 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 h1:R8vQdOQdZ9Y3SkEwmHoWBmX1DNXhXZqlTpq6s4tyJGc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -50,6 +51,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
Expand Down Expand Up @@ -92,8 +95,13 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down Expand Up @@ -150,5 +158,6 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

0 comments on commit 106bc18

Please sign in to comment.