From 24a0a2f50b8e2705db99e047b6842a6359d5b943 Mon Sep 17 00:00:00 2001 From: KonradStaniec Date: Fri, 15 Nov 2024 10:33:40 +0100 Subject: [PATCH] different signers --- cmd/signerCmd.go | 12 +++-- config/config.go | 15 ++++--- config/signer_config.go | 99 +++++++++++++++++++++++++++++++++++++++++ example/config.toml | 2 + 4 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 config/signer_config.go diff --git a/cmd/signerCmd.go b/cmd/signerCmd.go index a0552be..25c8aea 100644 --- a/cmd/signerCmd.go +++ b/cmd/signerCmd.go @@ -49,14 +49,20 @@ var runSignerCmd = &cobra.Command{ chainInfo := signerapp.NewBitcoindChainInfo(fullNodeClient) - signerClient, err := btcclient.NewBtcClient(parsedConfig.BtcSignerConfig) + signerClient, err := btcclient.NewBtcClient(parsedConfig.BtcSignerConfig.ToBtcConfig()) if err != nil { return err } - // TODO: Add options to use customn remote signers - signer := signerapp.NewPsbtSigner(signerClient) + var signer signerapp.ExternalBtcSigner + if parsedConfig.BtcSignerConfig.SignerType == config.PsbtSigner { + fmt.Println("using psbt signer") + signer = signerapp.NewPsbtSigner(signerClient) + } else if parsedConfig.BtcSignerConfig.SignerType == config.PrivKeySigner { + fmt.Println("using privkey signer") + signer = signerapp.NewPrivKeySigner(signerClient) + } app := signerapp.NewSignerApp( signer, diff --git a/config/config.go b/config/config.go index a046f37..0f34811 100644 --- a/config/config.go +++ b/config/config.go @@ -16,17 +16,16 @@ const ( ) type Config struct { - // TODO: Separate config for signing node and for full node - BtcNodeConfig BtcConfig `mapstructure:"btc-config"` - BtcSignerConfig BtcConfig `mapstructure:"btc-signer-config"` - Server ServerConfig `mapstructure:"server-config"` - Metrics MetricsConfig `mapstructure:"metrics"` + BtcNodeConfig BtcConfig `mapstructure:"btc-config"` + BtcSignerConfig BtcSignerConfig `mapstructure:"btc-signer-config"` + Server ServerConfig `mapstructure:"server-config"` + Metrics MetricsConfig `mapstructure:"metrics"` } func DefaultConfig() *Config { return &Config{ BtcNodeConfig: *DefaultBtcConfig(), - BtcSignerConfig: *DefaultBtcConfig(), + BtcSignerConfig: *DefaultBtcSignerConfig(), Server: *DefaultServerConfig(), Metrics: *DefaultMetricsConfig(), } @@ -34,7 +33,7 @@ func DefaultConfig() *Config { type ParsedConfig struct { BtcNodeConfig *ParsedBtcConfig - BtcSignerConfig *ParsedBtcConfig + BtcSignerConfig *ParsedBtcSignerConfig ServerConfig *ParsedServerConfig MetricsConfig *ParsedMetricsConfig } @@ -100,6 +99,8 @@ user = "{{ .BtcSignerConfig.User }}" pass = "{{ .BtcSignerConfig.Pass }}" # Btc network (testnet3|mainnet|regtest|simnet|signet) network = "{{ .BtcSignerConfig.Network }}" +# Signer type (psbt|privkey) +signer-type = "{{ .BtcSignerConfig.SignerType }}" [server-config] # The address to listen on diff --git a/config/signer_config.go b/config/signer_config.go new file mode 100644 index 0000000..8c4e094 --- /dev/null +++ b/config/signer_config.go @@ -0,0 +1,99 @@ +package config + +import ( + "fmt" + + "github.com/btcsuite/btcd/chaincfg" +) + +type SignerType int + +const ( + PsbtSigner SignerType = iota + PrivKeySigner +) + +func SignerFromString(s string) (SignerType, error) { + switch s { + case "psbt": + return PsbtSigner, nil + case "privkey": + return PrivKeySigner, nil + default: + return -1, fmt.Errorf("unknown signer type %s", s) + } +} + +type BtcSignerConfig struct { + Host string `mapstructure:"host"` + User string `mapstructure:"user"` + Pass string `mapstructure:"pass"` + Network string `mapstructure:"network"` + SignerType string `mapstructure:"signer-type"` +} + +type ParsedBtcSignerConfig struct { + Host string + User string + Pass string + Network *chaincfg.Params + SignerType SignerType +} + +func DefaultBtcSignerConfig() *BtcSignerConfig { + return &BtcSignerConfig{ + Host: "localhost:18556", + User: "user", + Pass: "pass", + Network: "regtest", + SignerType: "psbt", + } +} + +func (c *ParsedBtcSignerConfig) ToBtcConfig() *ParsedBtcConfig { + return &ParsedBtcConfig{ + Host: c.Host, + User: c.User, + Pass: c.Pass, + Network: c.Network, + } +} + +func (c *BtcSignerConfig) Parse() (*ParsedBtcSignerConfig, error) { + params, err := c.getBtcNetworkParams() + + if err != nil { + return nil, err + } + + signerType, err := SignerFromString(c.SignerType) + + if err != nil { + return nil, err + } + + return &ParsedBtcSignerConfig{ + Host: c.Host, + User: c.User, + Pass: c.Pass, + Network: params, + SignerType: signerType, + }, nil +} + +func (cfg *BtcSignerConfig) getBtcNetworkParams() (*chaincfg.Params, error) { + switch cfg.Network { + case "testnet3": + return &chaincfg.TestNet3Params, nil + case "mainnet": + return &chaincfg.MainNetParams, nil + case "regtest": + return &chaincfg.RegressionNetParams, nil + case "simnet": + return &chaincfg.SimNetParams, nil + case "signet": + return &chaincfg.SigNetParams, nil + default: + return nil, fmt.Errorf("unknown network %s", cfg.Network) + } +} diff --git a/example/config.toml b/example/config.toml index 7ca6145..4b0c9ff 100644 --- a/example/config.toml +++ b/example/config.toml @@ -27,6 +27,8 @@ user = "user" pass = "pass" # Btc network (testnet3|mainnet|regtest|simnet|signet) network = "regtest" +# Signer type (psbt|privkey) +signer-type = "psbt" [server-config] # The address to listen on