Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

different signers #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions cmd/signerCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@ 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(),
}
}

type ParsedConfig struct {
BtcNodeConfig *ParsedBtcConfig
BtcSignerConfig *ParsedBtcConfig
BtcSignerConfig *ParsedBtcSignerConfig
ServerConfig *ParsedServerConfig
MetricsConfig *ParsedMetricsConfig
}
Expand Down Expand Up @@ -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
Expand Down
99 changes: 99 additions & 0 deletions config/signer_config.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions example/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading