Skip to content

Commit

Permalink
add basic wireing of the remote signer
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Nov 25, 2024
1 parent 433d1a2 commit 84035c7
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 4 deletions.
25 changes: 21 additions & 4 deletions cmd/covd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
covcfg "github.com/babylonlabs-io/covenant-emulator/config"
"github.com/babylonlabs-io/covenant-emulator/keyring"
"github.com/babylonlabs-io/covenant-emulator/log"
"github.com/babylonlabs-io/covenant-emulator/remotesigner"
"github.com/babylonlabs-io/covenant-emulator/util"

"github.com/lightningnetwork/lnd/signal"
Expand Down Expand Up @@ -60,9 +61,21 @@ func start(ctx *cli.Context) error {

pwd := ctx.String(passphraseFlag)

signer, err := newSignerFromConfig(cfg, pwd)
if err != nil {
return fmt.Errorf("failed to create signer from config: %w", err)
var signer covenant.Signer

if cfg.UseRemoteSigner {
signer, err = newRemoteSignerFromConfig(cfg)

if err != nil {
return fmt.Errorf("failed to create remote signer from config: %w", err)

} else {
signer, err = newSignerFromConfig(cfg, pwd)

if err != nil {
return fmt.Errorf("failed to create keyring signer from config: %w", err)
}
}
}

ce, err := covenant.NewCovenantEmulator(cfg, bbnClient, logger, signer)
Expand All @@ -84,7 +97,7 @@ func start(ctx *cli.Context) error {
return srv.RunUntilShutdown()
}

func newSignerFromConfig(cfg *covcfg.Config, passphrase string) (*keyring.KeyringSigner, error) {
func newSignerFromConfig(cfg *covcfg.Config, passphrase string) (covenant.Signer, error) {
return keyring.NewKeyringSigner(
cfg.BabylonConfig.ChainID,
cfg.BabylonConfig.Key,
Expand All @@ -93,3 +106,7 @@ func newSignerFromConfig(cfg *covcfg.Config, passphrase string) (*keyring.Keyrin
passphrase,
)
}

func newRemoteSignerFromConfig(cfg *covcfg.Config) (covenant.Signer, error) {
return remotesigner.NewRemoteSigner(cfg.RemoteSigner), nil
}
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type Config struct {
Metrics *MetricsConfig `group:"metrics" namespace:"metrics"`

BabylonConfig *BBNConfig `group:"babylon" namespace:"babylon"`

UseRemoteSigner bool `long:"use-remotesigner" description:"if true, covenant will use the remote signer to sign transactions"`

RemoteSigner *RemoteSignerCfg `group:"remotesigner" namespace:"remotesigner"`
}

// LoadConfig initializes and parses the config using a config file and command
Expand Down Expand Up @@ -128,6 +132,7 @@ func DefaultConfigWithHomePath(homePath string) Config {
bbnCfg.Key = defaultCovenantKeyName
bbnCfg.KeyDirectory = homePath
metricsCfg := DefaultMetricsConfig()
remoteSignerCfg := DefaultRemoteSignerConfig()
cfg := Config{
LogLevel: defaultLogLevel,
QueryInterval: defaultQueryInterval,
Expand All @@ -137,6 +142,8 @@ func DefaultConfigWithHomePath(homePath string) Config {
BTCNetParams: defaultBTCNetParams,
Metrics: &metricsCfg,
BabylonConfig: &bbnCfg,
UseRemoteSigner: false,
RemoteSigner: &remoteSignerCfg,
}

if err := cfg.Validate(); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions config/remotesigner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package config

import "time"

const (
defaultUrl = "http://127.0.0.1:9791"
defaultTimeout = 2 * time.Second
)

type RemoteSignerCfg struct {
URL string `long:"url" description:"URL of the remote signer"`
Timeout time.Duration `long:"timeout" description:"client when making requests to the remote signer"`
}

func DefaultRemoteSignerConfig() RemoteSignerCfg {
return RemoteSignerCfg{
URL: defaultUrl,
Timeout: defaultTimeout,
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require (
cosmossdk.io/math v1.4.0
github.com/avast/retry-go/v4 v4.5.1
github.com/babylonlabs-io/babylon v0.17.1
// TODO: Release covenant-signer
github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.0.0-20241122072853-f24b47aaa46b
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.6
Expand Down Expand Up @@ -105,6 +107,7 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-kit/kit v0.13.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,8 @@ github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/babylonlabs-io/babylon v0.17.1 h1:lyWGdR7B49qDw5pllLyTW/HAM5uQWXXPZefjFzy/Xy0=
github.com/babylonlabs-io/babylon v0.17.1/go.mod h1:sT+KG2U+M0tDMNZZ2L5CwlXX0OpagGEs56BiWXqaZFw=
github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.0.0-20241122072853-f24b47aaa46b h1:/v0YgkWITwFArI6/ovU1QyfbeGaE8GiojmY+z/rTdq8=
github.com/babylonlabs-io/covenant-emulator/covenant-signer v0.0.0-20241122072853-f24b47aaa46b/go.mod h1:9lAyEcdpfS21bMLMEa8WjTyLVfwHJABRh5TmoxC9LKU=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down Expand Up @@ -1674,6 +1676,8 @@ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=
Expand Down
64 changes: 64 additions & 0 deletions remotesigner/signer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package remotesigner

import (
"context"

"github.com/babylonlabs-io/covenant-emulator/config"
"github.com/babylonlabs-io/covenant-emulator/covenant"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerapp"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerservice"
"github.com/btcsuite/btcd/btcec/v2"
)

var _ covenant.Signer = RemoteSigner{}

func covenantRequestToSignerRequest(req covenant.SigningRequest) *signerapp.ParsedSigningRequest {
return &signerapp.ParsedSigningRequest{
StakingTx: req.StakingTx,
SlashingTx: req.SlashingTx,
UnbondingTx: req.UnbondingTx,
SlashUnbondingTx: req.SlashUnbondingTx,
StakingOutputIdx: req.StakingOutputIdx,
SlashingScript: req.SlashingPkScriptPath,
UnbondingScript: req.StakingTxUnbondingPkScriptPath,
UnbondingSlashingScript: req.UnbondingTxSlashingPkScriptPath,
FpEncKeys: req.FpEncKeys,
}
}

func signerResponseToCovenantResponse(resp *signerapp.ParsedSigningResponse) *covenant.SignaturesResponse {
return &covenant.SignaturesResponse{
SlashSigs: resp.SlashAdaptorSigs,
UnbondingSig: resp.UnbondingSig,
SlashUnbondingSigs: resp.SlashUnbondingAdaptorSigs,
}
}

type RemoteSigner struct {
cfg *config.RemoteSignerCfg
}

func NewRemoteSigner(cfg *config.RemoteSignerCfg) RemoteSigner {
return RemoteSigner{
cfg: cfg,
}
}

func (rs RemoteSigner) PubKey() (*btcec.PublicKey, error) {
return signerservice.GetPublicKey(context.Background(), rs.cfg.URL, rs.cfg.Timeout)
}

func (rs RemoteSigner) SignTransactions(req covenant.SigningRequest) (*covenant.SignaturesResponse, error) {
resp, err := signerservice.RequestCovenantSignaure(
context.Background(),
rs.cfg.URL,
rs.cfg.Timeout,
covenantRequestToSignerRequest(req),
)

if err != nil {
return nil, err
}

return signerResponseToCovenantResponse(resp), nil
}

0 comments on commit 84035c7

Please sign in to comment.