Skip to content

Commit

Permalink
Merge pull request ElementsProject#217 from grubles/lbtconly
Browse files Browse the repository at this point in the history
Add CLN config option to disable BTC swaps, fixup LND swap config
  • Loading branch information
wtogami committed Aug 26, 2023
2 parents 19eeea4 + 3e7d3ee commit bb007f9
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 17 deletions.
2 changes: 1 addition & 1 deletion clightning/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (cl *ClightningClient) SetPeerswapConfig(config *Config) {
LiquidRpcHost: config.Liquid.RpcHost,
LiquidRpcPort: config.Liquid.RpcPort,
LiquidRpcWallet: config.Liquid.RpcWallet,
LiquidDisabled: config.Liquid.Disabled,
LiquidDisabled: *config.Liquid.LiquidSwaps,
PeerswapDir: config.PeerswapDir,
}
}
Expand Down
20 changes: 16 additions & 4 deletions clightning/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type BitcoinConf struct {
RpcPort uint
Network string
DataDir string
BitcoinSwaps *bool
}

type LiquidConf struct {
Expand All @@ -46,7 +47,7 @@ type LiquidConf struct {
RpcWallet string
Network string
DataDir string
Disabled bool
LiquidSwaps *bool
}

type Config struct {
Expand Down Expand Up @@ -157,6 +158,7 @@ func ReadFromFile() Processor {
c.Bitcoin.RpcPasswordFile = fileConf.Bitcoin.RpcPasswordFile
c.Bitcoin.RpcHost = fileConf.Bitcoin.RpcHost
c.Bitcoin.RpcPort = fileConf.Bitcoin.RpcPort
c.Bitcoin.BitcoinSwaps = fileConf.Bitcoin.BitcoinSwaps
}

if fileConf.Liquid != nil {
Expand All @@ -166,7 +168,7 @@ func ReadFromFile() Processor {
c.Liquid.RpcHost = fileConf.Liquid.RpcHost
c.Liquid.RpcPort = fileConf.Liquid.RpcPort
c.Liquid.RpcWallet = fileConf.Liquid.RpcWallet
c.Liquid.Disabled = fileConf.Liquid.Disabled
c.Liquid.LiquidSwaps = fileConf.Liquid.LiquidSwaps
}

return c, nil
Expand Down Expand Up @@ -296,7 +298,12 @@ func BitcoinFallback() Processor {
}
c.Bitcoin.DataDir = filepath.Join(home, defaultBitcoinSubDir)
}


if c.Bitcoin.BitcoinSwaps == nil {
var swapson = true
c.Bitcoin.BitcoinSwaps = &swapson
}

if c.Bitcoin.RpcHost == "" {
c.Bitcoin.RpcHost = defaultRpcHost
}
Expand Down Expand Up @@ -332,6 +339,11 @@ func ElementsFallback() Processor {
}
c.Liquid.DataDir = filepath.Join(home, defaultElementsSubDir)
}

if c.Liquid.LiquidSwaps == nil {
var swapson = true
c.Liquid.LiquidSwaps = &swapson
}

if c.Liquid.Network == "" {
c.Liquid.Network, err = liquidNetDir(c.Bitcoin.Network)
Expand Down Expand Up @@ -400,7 +412,7 @@ func ElementsCookieConnect() Processor {
return func(c *Config) (*Config, error) {
var err error
if c.Liquid.RpcUser == "" && c.Liquid.RpcPassword == "" &&
!c.Liquid.Disabled {
!*c.Liquid.LiquidSwaps == false {
if c.Liquid.RpcPasswordFile == "" {
return nil, fmt.Errorf("no liquid rpc configuration found")
}
Expand Down
3 changes: 1 addition & 2 deletions clightning/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func Test_ReadFromFile(t *testing.T) {
rpchost="rpchost"
rpcport=1234
rpcwallet="rpcwallet"
enabled=true
`

dir := t.TempDir()
Expand Down Expand Up @@ -59,7 +58,7 @@ func Test_ReadFromFile(t *testing.T) {
RpcWallet: "rpcwallet",
Network: "",
DataDir: "",
Disabled: false,
LiquidSwaps: nil,
},
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/peerswap-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
var liquidCli *gelements.Elements
var liquidEnabled bool

if !config.Liquid.Disabled && liquidWanted(config) {
if *config.Liquid.LiquidSwaps && liquidWanted(config) {
liquidEnabled = true
log.Infof("Starting elements client with rpcuser: %s, rpcpassword:******, rpccookie: %s, rpcport: %d, rpchost: %s",
config.Liquid.RpcUser,
Expand Down Expand Up @@ -228,7 +228,7 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
var bitcoinTxWatcher *txwatcher.BlockchainRpcTxWatcher
var bitcoinOnChainService *onchain.BitcoinOnChain
var bitcoinEnabled bool
if bitcoinCli != nil {
if bitcoinCli != nil && *config.Bitcoin.BitcoinSwaps {
supportedAssets = append(supportedAssets, "btc")
log.Infof("Bitcoin swaps enabled")
bitcoinEnabled = true
Expand Down Expand Up @@ -276,8 +276,12 @@ func run(ctx context.Context, lightningPlugin *clightning.ClightningClient) erro
log.Infof("Bitcoin swaps disabled")
}

if !*config.Bitcoin.BitcoinSwaps && !*config.Liquid.LiquidSwaps {
return errors.New("Disabling both BTC and L-BTC swaps is invalid.")
}

if !bitcoinEnabled && !liquidEnabled {
return errors.New("bad config, either liquid or bitcoin settings must be set")
return errors.New("Bad configuration or daemons are broken.")
}

// db
Expand Down
12 changes: 8 additions & 4 deletions cmd/peerswaplnd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ type PeerSwapConfig struct {
LndConfig *LndConfig `group:"Lnd Grpc config" namespace:"lnd"`
ElementsConfig *OnchainConfig `group:"Elements Rpc Config" namespace:"elementsd"`

LiquidEnabled bool
LiquidEnabled bool `long:"liquidswaps" description:"enable bitcoin peerswaps"`
BitcoinEnabled bool `long:"bitcoinswaps" description:"enable bitcoin peerswaps"`
}

func (p *PeerSwapConfig) String() string {
var liquidString string
if p.ElementsConfig != nil {
liquidString = fmt.Sprintf("elements: rpcuser: %s, rpchost: %s, rpcport %v, rpcwallet: %s", p.ElementsConfig.RpcUser, p.ElementsConfig.RpcHost, p.ElementsConfig.RpcPort, p.ElementsConfig.RpcWallet)
liquidString = fmt.Sprintf("elements: rpcuser: %s, rpchost: %s, rpcport %v, rpcwallet: %s, liquidswaps: %v", p.ElementsConfig.RpcUser, p.ElementsConfig.RpcHost, p.ElementsConfig.RpcPort, p.ElementsConfig.RpcWallet, p.ElementsConfig.LiquidSwaps)
}
var lndString string
if p.LndConfig != nil {
Expand All @@ -68,12 +68,14 @@ func (p *PeerSwapConfig) String() string {
}

func (p *PeerSwapConfig) Validate() error {
if p.ElementsConfig.RpcHost != "" {
if p.ElementsConfig.RpcHost != "" && p.ElementsConfig.LiquidSwaps != false {
err := p.ElementsConfig.Validate()
if err != nil {
return err
}
}
p.LiquidEnabled = true


}
return nil
}
Expand All @@ -86,6 +88,7 @@ type OnchainConfig struct {
RpcHost string `long:"rpchost" description:"host to connect to"`
RpcPort uint `long:"rpcport" description:"port to connect to"`
RpcWallet string `long:"rpcwallet" description:"wallet to use for swaps (elements only)"`
LiquidSwaps bool `long:"liquidswaps" description:"set to false to disable L-BTC swaps"`
}

func (o *OnchainConfig) Validate() error {
Expand Down Expand Up @@ -156,5 +159,6 @@ func defaultLiquidConfig() *OnchainConfig {
RpcHost: "",
RpcPort: 0,
RpcWallet: DefaultLiquidwallet,
LiquidSwaps: true,
}
}
8 changes: 7 additions & 1 deletion cmd/peerswaplnd/peerswapd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,14 @@ func run() error {
log.Infof("Liquid swaps disabled")
}

if !cfg.BitcoinEnabled && !cfg.ElementsConfig.LiquidSwaps {
log.Infof("Disabling both BTC and L-BTC swaps is invalid. Check PeerSwap and daemon configs. Exiting.")
os.Exit(1)
}

if !cfg.BitcoinEnabled && !cfg.LiquidEnabled {
return errors.New("bad config, either liquid or bitcoin settings must be set")
log.Infof("Bad configuration or daemons are broken. Exiting.")
os.Exit(1)
}
// Start lnd listeners and watchers.
messageListener, err := lnd_internal.NewMessageListener(ctx, cc)
Expand Down
3 changes: 2 additions & 1 deletion docs/setup_cln.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ rpcpassword="password"
rpchost="host"
rpcport=1234
cookiefilepath="/path/to/auth/.cookie" ## If set this will be used for authentication
bitcoinswaps=true ## If set to false, BTC mainchain swaps are disabled

# Liquid section
# Liquid rpc connection settings.
Expand All @@ -69,7 +70,7 @@ rpchost="host"
rpcport=1234
rpcpasswordfile="/path/to/auth/.cookie" ## If set this will be used for authentication
rpcwallet="swap-wallet" ## (default: peerswap)
enabled=false ## If set to true, peerswap connects to elementsd
liquidswaps=true ## If set to false, L-BTC swaps are disabled
```

In order to check if your daemon is setup correctly run
Expand Down
18 changes: 18 additions & 0 deletions docs/setup_lnd.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,26 @@ elementsd.rpcpass=<REPLACE_ME>
elementsd.rpchost=http://127.0.0.1
elementsd.rpcport=<REPLACE_ME>
elementsd.rpcwallet=peerswap
elementsd.liquidswaps=true # set to false to manually disable L-BTC swaps
EOF
```

L-BTC only config.

```bash
cat <<EOF > ~/.peerswap/peerswap.conf
bitcoinswaps=false # disables BTC swaps
lnd.tlscertpath=/home/<username>/.lnd/tls.cert
lnd.macaroonpath=/home/<username>/.lnd/data/chain/bitcoin/mainnet/admin.macaroon
elementsd.rpcuser=<REPLACE_ME>
elementsd.rpcpass=<REPLACE_ME>
elementsd.rpchost=http://127.0.0.1
elementsd.rpcport=<REPLACE_ME>
elementsd.rpcwallet=peerswap
EOF
```

### Policy

On first startup of the plugin a policy file will be generated (default path: `~/.peerswap/policy.conf`) in which trusted nodes will be specified.
Expand Down
2 changes: 1 addition & 1 deletion swap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *SwapService) RecoverSwaps() error {
}
}(sw)
}
log.Debugf("Waiting for all open swaps to recover.")
log.Debugf("Waiting for all pending swaps to recover.")
wg.Wait()
return nil
}
Expand Down

0 comments on commit bb007f9

Please sign in to comment.