Skip to content

Commit

Permalink
chore: remove opt to remote signer, should always use remote signer (#95
Browse files Browse the repository at this point in the history
)

* chore: remove opt to remote signer, should always use remote signer

* fix: test sign

* chore: removed unnecessary funcs

* chore: add #95 to changelog

* chore: add back the TestManager

* fix: lint removed unused var

* chore: address pr comments

* chore: add unlock wallet prior to send btc del

* chore: removed option to set remote signer

* chore: removed duplicated test with and without remote signer, all use the remote signer

* chore: add func to get open port to start covenant emulator
  • Loading branch information
RafilxTenfen authored Jan 15, 2025
1 parent 3cbcebb commit 0a7fd83
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 421 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Improvements

* [#83](https://github.com/babylonlabs-io/covenant-emulator/pull/83) covenant-signer: remove go.mod
* [#95](https://github.com/babylonlabs-io/covenant-emulator/pull/95) removed local signer option
as the covenant emulator should only connect to a remote signer

## v0.11.3

Expand Down
33 changes: 3 additions & 30 deletions cmd/covd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"path/filepath"

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"
Expand All @@ -23,11 +22,6 @@ var startCommand = cli.Command{
Usage: "Start the Covenant Emulator Daemon",
Description: "Start the Covenant Emulator Daemon. Note that the Covenant key pair should be created beforehand",
Flags: []cli.Flag{
cli.StringFlag{
Name: passphraseFlag,
Usage: "The pass phrase used to encrypt the keys",
Value: defaultPassphrase,
},
cli.StringFlag{
Name: homeFlag,
Usage: "The path to the covenant home directory",
Expand Down Expand Up @@ -59,20 +53,9 @@ func start(ctx *cli.Context) error {
return fmt.Errorf("failed to create rpc client for the consumer chain: %w", err)
}

pwd := ctx.String(passphraseFlag)

var signer covenant.Signer

if cfg.RemoteSignerEnabled {
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)
}
signer, err := newRemoteSignerFromConfig(cfg)
if err != nil {
return fmt.Errorf("failed to create remote signer from config: %w", err)
}

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

func newSignerFromConfig(cfg *covcfg.Config, passphrase string) (covenant.Signer, error) {
return keyring.NewKeyringSigner(
cfg.BabylonConfig.ChainID,
cfg.BabylonConfig.Key,
cfg.BabylonConfig.KeyDirectory,
cfg.BabylonConfig.KeyringBackend,
passphrase,
)
}

func newRemoteSignerFromConfig(cfg *covcfg.Config) (covenant.Signer, error) {
return remotesigner.NewRemoteSigner(cfg.RemoteSigner), nil
}
21 changes: 9 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ type Config struct {

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

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

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

Expand Down Expand Up @@ -134,16 +132,15 @@ func DefaultConfigWithHomePath(homePath string) Config {
metricsCfg := DefaultMetricsConfig()
remoteSignerCfg := DefaultRemoteSignerConfig()
cfg := Config{
LogLevel: defaultLogLevel,
QueryInterval: defaultQueryInterval,
DelegationLimit: defaultDelegationLimit,
SigsBatchSize: defaultSigsBatchSize,
BitcoinNetwork: defaultBitcoinNetwork,
BTCNetParams: defaultBTCNetParams,
Metrics: &metricsCfg,
BabylonConfig: &bbnCfg,
RemoteSignerEnabled: false,
RemoteSigner: &remoteSignerCfg,
LogLevel: defaultLogLevel,
QueryInterval: defaultQueryInterval,
DelegationLimit: defaultDelegationLimit,
SigsBatchSize: defaultSigsBatchSize,
BitcoinNetwork: defaultBitcoinNetwork,
BTCNetParams: defaultBTCNetParams,
Metrics: &metricsCfg,
BabylonConfig: &bbnCfg,
RemoteSigner: &remoteSignerCfg,
}

if err := cfg.Validate(); err != nil {
Expand Down
83 changes: 66 additions & 17 deletions covenant/covenant_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package covenant_test

import (
"context"
"encoding/hex"
"fmt"
"math/rand"
Expand All @@ -23,7 +24,13 @@ import (

covcfg "github.com/babylonlabs-io/covenant-emulator/config"
"github.com/babylonlabs-io/covenant-emulator/covenant"
signerCfg "github.com/babylonlabs-io/covenant-emulator/covenant-signer/config"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/keystore/cosmos"
signerMetrics "github.com/babylonlabs-io/covenant-emulator/covenant-signer/observability/metrics"
signerApp "github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerapp"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerservice"
"github.com/babylonlabs-io/covenant-emulator/keyring"
"github.com/babylonlabs-io/covenant-emulator/remotesigner"
"github.com/babylonlabs-io/covenant-emulator/testutil"
"github.com/babylonlabs-io/covenant-emulator/types"
)
Expand All @@ -37,30 +44,72 @@ var net = &chaincfg.SimNetParams

func FuzzAddCovenantSig(f *testing.F) {
testutil.AddRandomSeedsToFuzzer(f, 10)

// create a Covenant key pair in the keyring
covenantConfig := covcfg.DefaultConfig()

covenantConfig.BabylonConfig.KeyDirectory = f.TempDir()

signerConfig := signerCfg.DefaultConfig()
signerConfig.KeyStore.CosmosKeyStore.ChainID = covenantConfig.BabylonConfig.ChainID
signerConfig.KeyStore.CosmosKeyStore.KeyName = covenantConfig.BabylonConfig.Key
signerConfig.KeyStore.CosmosKeyStore.KeyringBackend = covenantConfig.BabylonConfig.KeyringBackend
signerConfig.KeyStore.CosmosKeyStore.KeyDirectory = covenantConfig.BabylonConfig.KeyDirectory
keyRetriever, err := cosmos.NewCosmosKeyringRetriever(signerConfig.KeyStore.CosmosKeyStore)
require.NoError(f, err)

covKeyPair, err := keyRetriever.Kr.CreateChainKey(
passphrase,
hdPath,
)
require.NoError(f, err)
require.NotNil(f, covKeyPair)

app := signerApp.NewSignerApp(
keyRetriever,
)

met := signerMetrics.NewCovenantSignerMetrics()
parsedConfig, err := signerConfig.Parse()
require.NoError(f, err)

server, err := signerservice.New(
context.Background(),
parsedConfig,
app,
met,
)
require.NoError(f, err)

signer := remotesigner.NewRemoteSigner(covenantConfig.RemoteSigner)

go func() {
_ = server.Start()
}()

// Give some time to launch server
time.Sleep(time.Second)

// unlock the signer before usage
err = signerservice.Unlock(
context.Background(),
covenantConfig.RemoteSigner.URL,
covenantConfig.RemoteSigner.Timeout,
passphrase,
)
require.NoError(f, err)

f.Cleanup(func() {
_ = server.Stop(context.TODO())
})

f.Fuzz(func(t *testing.T, seed int64) {
t.Log("Seed", seed)
r := rand.New(rand.NewSource(seed))

params := testutil.GenRandomParams(r, t)
mockClientController := testutil.PrepareMockedClientController(t, params)

// create a Covenant key pair in the keyring
covenantConfig := covcfg.DefaultConfig()
covenantConfig.BabylonConfig.KeyDirectory = t.TempDir()

covKeyPair, err := keyring.CreateCovenantKey(
covenantConfig.BabylonConfig.KeyDirectory,
covenantConfig.BabylonConfig.ChainID,
covenantConfig.BabylonConfig.Key,
covenantConfig.BabylonConfig.KeyringBackend,
passphrase,
hdPath,
)
require.NoError(t, err)

signer, err := keyring.NewKeyringSigner(covenantConfig.BabylonConfig.ChainID, covenantConfig.BabylonConfig.Key, covenantConfig.BabylonConfig.KeyDirectory, covenantConfig.BabylonConfig.KeyringBackend, passphrase)
require.NoError(t, err)

// create and start covenant emulator
ce, err := covenant.NewCovenantEmulator(&covenantConfig, mockClientController, zap.NewNop(), signer)
require.NoError(t, err)
Expand Down
41 changes: 17 additions & 24 deletions docs/covenant-emulator-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This document outlines the setup of the covenant-emulator
daemon program.

## Table of Contents
## Table of Contents

1. [Prerequesites](#1-prerequisites)
2. [Install Covenant Emulator Binary](#2-install-covenant-emulator-binary)
Expand All @@ -17,20 +17,20 @@ daemon program.

To successfully complete this guide, you will need:

1. A running instance of the [covenant signer](../covenant-signer)
with the url that you configured it to. Please follow the
[covenant signer setup guide](./covenant-signer-setup.md) to
1. A running instance of the [covenant signer](../covenant-signer)
with the url that you configured it to. Please follow the
[covenant signer setup guide](./covenant-signer-setup.md) to
complete the setup of the covenant signer with your keys before proceeding.
Note that the phase-2 covenant-signer program is a different one than the one
used doing phase-1
2. A connection to a Babylon node. To run your own node, please refer to the
2. A connection to a Babylon node. To run your own node, please refer to the
[Babylon Node Setup Guide](https://github.com/babylonlabs-io/networks/blob/main/bbn-test-5/bbn-test-5/babylon-node/README.md).

## 2. Install covenant emulator binary

If you haven't already, download [Golang 1.23](https://go.dev/dl).

Once installed run:
Once installed, run:

```shell
go version
Expand Down Expand Up @@ -69,8 +69,8 @@ echo 'export PATH=$HOME/go/bin:$PATH' >> ~/.profile

### 3.1. Initialize directories

Next, we initialize the node and home directory. It should generate all of the
necessary files such as `covd.config`, these files will live in the `<path>`
Next, initialize the node and home directory by generating all of the
necessary files such as `covd.conf`. These files will live in the `<path>`
that you set for the `--home` with the below command.

```shell
Expand All @@ -87,9 +87,6 @@ $ ls <path>

### 3.2. Configure the covenant emulator

As you have already set up the covenant signer, you can now configure the covenant
emulator to use it.

Use the following parameters to configure the `covd.conf` file.

```
Expand Down Expand Up @@ -127,9 +124,6 @@ URL = http://127.0.0.1:9792
; client when making requests to the remote signer
Timeout = 2s
; if true, covenant will use the remote signer to sign transactions
RemoteSignerEnabled = true
```

Below are brief explanations of the configuration entries:
Expand All @@ -144,15 +138,14 @@ Below are brief explanations of the configuration entries:
- `KeyringBackend` - Storage backend for the keyring (os, file, kwallet, pass, test, memory)
- `URL` - Endpoint where the remote signing service is running
- `Timeout` - Maximum time to wait for remote signer responses
- `RemoteSignerEnabled` - Whether to use the remote signing service

Ensure that the covenant signer is running and unlocked before proceeding
otherwise you will be unable to run the emulator.
Ensure that the covenant signer is running and unlocked before proceeding.
Otherwise, you will be unable to run the emulator.

## 4. Generate key pairs

The covenant emulator daemon requires the existence of a Babylon keyring that
signs signatures and interacts with Babylon. Use the following command to generate
The covenant emulator daemon requires the existence of a Babylon keyring that
signs signatures and interacts with Babylon. Use the following command to generate
the key:

```bash
Expand All @@ -167,20 +160,20 @@ covd create-key --key-name <name> --chain-id <chain-id> --keyring-backend <backe
Parameters:
- `--key-name`: Name for the key in the keyring
- `--chain-id`: ID of the Babylon chain (e.g., bbn-test-5)
- `--keyring-backend`: Backend for key storage, we will use `test`
- `--keyring-backend`: Backend for key storage, we will use `test`
for this guide.

After executing the above command, the key name will be saved in the config file
created in the last [step](#42-configure-the-covenant-emulator).

**⚡ Note:** that the `public-key` in the output should be used as one of the
**⚡ Note:** that the `public-key` in the output should be used as one of the
inputs of the genesis of the Babylon chain.

Also, this key will be used to pay for the fees due to the daemon submitting
Also, this key will be used to pay for the fees due to the daemon submitting
signatures to Babylon.

To check your balance, View your account on the
[Babylon Explorer](https://babylon-testnet.l2scan.co) by searching for your
To check your balance, View your account on the
[Babylon Explorer](https://babylon-testnet.l2scan.co) by searching for your
address.


Expand Down
Loading

0 comments on commit 0a7fd83

Please sign in to comment.