-
Notifications
You must be signed in to change notification settings - Fork 40
/
config.go
160 lines (141 loc) · 5.27 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package peerswaplnd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/btcsuite/btcd/btcutil"
)
type LogLevel uint8
const (
LOGLEVEL_INFO = LogLevel(iota + 1)
LOGLEVEL_DEBUG
)
var (
DefaultPeerswapHost = "localhost:42069"
DefaultRestHost = "localhost:42070"
DefaultLndHost = "localhost:10009"
DefaultTlsCertPath = filepath.Join(defaultLndDir, "tls.cert")
DefaultMacaroonPath = filepath.Join(defaultLndDir, "data", "chain", "bitcoin", DefaultNetwork, "admin.macaroon")
DefaultNetwork = "signet"
DefaultConfigFile = filepath.Join(DefaultDatadir, "peerswap.conf")
DefaultDatadir = btcutil.AppDataDir("peerswap", false)
DefaultLiquidwallet = "swap"
DefaultBitcoinEnabled = true
DefaultLogLevel = LOGLEVEL_DEBUG
DefaultPolicyFile = filepath.Join(DefaultDatadir, "policy.conf")
defaultLndDir = btcutil.AppDataDir("lnd", false)
)
type PeerSwapConfig struct {
Host string `long:"host" description:"host to listen on for grpc connections"`
RestHost string `long:"resthost" description:"host to listen for rest connection"`
ConfigFile string `long:"configfile" description:"path to configfile"`
PolicyFile string `long:"policyfile" description:"path to policyfile"`
DataDir string `long:"datadir" description:"peerswap datadir"`
LogLevel LogLevel `long:"loglevel" description:"loglevel (1=Info, 2=Debug)"`
LndConfig *LndConfig `group:"Lnd Grpc config" namespace:"lnd"`
ElementsConfig *OnchainConfig `group:"Elements Rpc Config" namespace:"elementsd"`
LiquidEnabled bool
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)
}
var lndString string
if p.LndConfig != nil {
lndString = fmt.Sprintf("host: %s, macaroonpath %s, tlspath %s", p.LndConfig.LndHost, p.LndConfig.MacaroonPath, p.LndConfig.TlsCertPath)
}
if p.DataDir != DefaultDatadir && p.PolicyFile == DefaultPolicyFile {
p.PolicyFile = filepath.Join(p.DataDir, "policy.conf")
}
return fmt.Sprintf("Host %s, ConfigFile %s, Datadir %s, Bitcoin enabled: %v, Lnd Config: %s, elements: %s", p.Host, p.ConfigFile, p.DataDir, p.BitcoinEnabled, lndString, liquidString)
}
func (p *PeerSwapConfig) Validate() error {
if p.ElementsConfig.RpcHost != "" {
err := p.ElementsConfig.Validate()
if err != nil {
return err
}
p.LiquidEnabled = true
}
return nil
}
type OnchainConfig struct {
RpcUser string `long:"rpcuser" description:"rpc user"`
RpcPassword string `long:"rpcpass" description:"password for rpc user"`
RpcPasswordFile string `long:"rpcpasswordfile" description:"file that cointains password for rpc user"`
RpcCookieFilePath string `long:"rpccookiefilepath" description:"path to rpc cookie file"`
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)"`
}
func (o *OnchainConfig) Validate() error {
if (o.RpcCookieFilePath == "" && o.RpcUser == "") && o.RpcHost != "" {
return errors.New("either rpcuser or cookie file must be set")
}
if o.RpcUser == "" {
// look for cookie file
cookiePath := filepath.Join(o.RpcCookieFilePath)
if _, err := os.Stat(cookiePath); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("cannot find bitcoin cookie file at %s", cookiePath))
}
cookieBytes, err := os.ReadFile(cookiePath)
if err != nil {
return err
}
cookie := strings.Split(string(cookieBytes), ":")
// use cookie for auth
o.RpcUser = cookie[0]
o.RpcPassword = cookie[1]
} else {
if !(o.RpcPassword == "" || o.RpcPasswordFile == "") {
return errors.New("rpcpass or rpcpasswordfile must be set")
}
if o.RpcPasswordFile != "" {
passBytes, err := ioutil.ReadFile(o.RpcPasswordFile)
if err != nil {
return err
}
passString := strings.TrimRight(string(passBytes), "\r\n")
o.RpcPassword = passString
}
}
return nil
}
type LndConfig struct {
LndHost string `long:"host" description:"host:port for lnd connection"`
TlsCertPath string `long:"tlscertpath" description:"path to the lnd TLS cert."`
MacaroonPath string `long:"macaroonpath" description:"path to the macaroon (admin.macaroon or custom baked one)"`
}
func DefaultConfig() *PeerSwapConfig {
return &PeerSwapConfig{
Host: DefaultPeerswapHost,
RestHost: DefaultRestHost,
ConfigFile: DefaultConfigFile,
PolicyFile: DefaultPolicyFile,
DataDir: DefaultDatadir,
LndConfig: &LndConfig{
LndHost: DefaultLndHost,
TlsCertPath: DefaultTlsCertPath,
MacaroonPath: DefaultMacaroonPath,
},
BitcoinEnabled: DefaultBitcoinEnabled,
ElementsConfig: defaultLiquidConfig(),
LogLevel: DefaultLogLevel,
}
}
func defaultLiquidConfig() *OnchainConfig {
return &OnchainConfig{
RpcUser: "",
RpcPassword: "",
RpcPasswordFile: "",
RpcCookieFilePath: "",
RpcHost: "",
RpcPort: 0,
RpcWallet: DefaultLiquidwallet,
}
}