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

fix: save key name mapping verifies if there is a client running #244

Merged
merged 4 commits into from
Dec 23, 2024
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Bug Fixes

* [#244](https://github.com/babylonlabs-io/finality-provider/pull/244) fix: save key name mapping
verifies if there is a eots client running

## v0.14.1

### Bug Fixes
Expand Down
9 changes: 9 additions & 0 deletions eotsmanager/client/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func (c *EOTSManagerGRpcClient) CreateRandomnessPairList(uid, chainID []byte, st
return pubRandFieldValList, nil
}

func (c *EOTSManagerGRpcClient) SaveEOTSKeyName(pk *btcec.PublicKey, keyName string) error {
req := &proto.SaveEOTSKeyNameRequest{
KeyName: keyName,
EotsPk: pk.SerializeUncompressed(),
}
_, err := c.client.SaveEOTSKeyName(context.Background(), req)
return err
}

func (c *EOTSManagerGRpcClient) KeyRecord(uid []byte, passphrase string) (*types.KeyRecord, error) {
req := &proto.KeyRecordRequest{Uid: uid, Passphrase: passphrase}

Expand Down
1 change: 1 addition & 0 deletions eotsmanager/cmd/eotsd/daemon/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
const (
forceFlag = "force"
rpcListenerFlag = "rpc-listener"
rpcClientFlag = "rpc-client"
flagInteractive = "interactive"
flagNoBackup = "no-backup"
flagMultisig = "multisig"
Expand Down
34 changes: 34 additions & 0 deletions eotsmanager/cmd/eotsd/daemon/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"encoding/json"
"fmt"
"io"
"strings"

"github.com/btcsuite/btcd/btcec/v2/schnorr"

"github.com/babylonlabs-io/babylon/types"
"github.com/babylonlabs-io/finality-provider/eotsmanager"
eotsclient "github.com/babylonlabs-io/finality-provider/eotsmanager/client"
"github.com/babylonlabs-io/finality-provider/eotsmanager/config"
"github.com/babylonlabs-io/finality-provider/log"
"github.com/babylonlabs-io/finality-provider/util"
Expand All @@ -35,6 +37,8 @@ func NewKeysCmd() *cobra.Command {
panic("failed to find keys add command")
}

addCmd.Flags().String(rpcClientFlag, "", "The RPC address of a running eotsd to connect and save new key")

// Override the original RunE function to run almost the same as
// the sdk, but it allows empty hd path and allow to save the key
// in the name mapping
Expand Down Expand Up @@ -73,6 +77,8 @@ func saveKeyOnPostRun(cmd *cobra.Command, commandName string) {
panic(fmt.Sprintf("failed to find keys %s command", commandName))
}

subCmd.Flags().String(rpcClientFlag, "", "The RPC address of a running eotsd to connect and save new key")

subCmd.PostRunE = func(cmd *cobra.Command, args []string) error {
keyName := args[0]
_, err := saveKeyNameMapping(cmd, keyName)
Expand All @@ -92,6 +98,34 @@ func saveKeyNameMapping(cmd *cobra.Command, keyName string) (*types.BIP340PubKey
return nil, fmt.Errorf("failed to load config: %w", err)
}

rpcListener, err := cmd.Flags().GetString(rpcClientFlag)
if err != nil {
return nil, err
}

if len(rpcListener) > 0 {
client, err := eotsclient.NewEOTSManagerGRpcClient(rpcListener)
if err != nil {
return nil, err
}

kr, err := eotsmanager.InitKeyring(clientCtx.HomeDir, clientCtx.Keyring.Backend(), strings.NewReader(""))
if err != nil {
return nil, fmt.Errorf("failed to init keyring: %w", err)
}

eotsPk, err := eotsmanager.LoadBIP340PubKeyFromKeyName(kr, keyName)
if err != nil {
return nil, fmt.Errorf("failed to get public key for key %s: %w", keyName, err)
}

if err := client.SaveEOTSKeyName(eotsPk.MustToBTCPK(), keyName); err != nil {
return nil, fmt.Errorf("failed to save key name mapping: %w", err)
}

return eotsPk, nil
}

// Setup logger
logger, err := log.NewRootLoggerWithFile(config.LogFile(clientCtx.HomeDir), cfg.LogLevel)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions eotsmanager/eotsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ type EOTSManager interface {
// or passPhrase is incorrect
SignSchnorrSig(uid []byte, msg []byte, passphrase string) (*schnorr.Signature, error)

// SaveEOTSKeyName saves a new key under the EOTS key name mapping
SaveEOTSKeyName(pk *btcec.PublicKey, keyName string) error

Close() error
}
10 changes: 7 additions & 3 deletions eotsmanager/localmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewLocalEOTSManager(homeDir, keyringBackend string, dbbackend kvdb.Backend,
return nil, fmt.Errorf("failed to initialize store: %w", err)
}

kr, err := initKeyring(homeDir, keyringBackend, inputReader)
kr, err := InitKeyring(homeDir, keyringBackend, inputReader)
if err != nil {
return nil, fmt.Errorf("failed to initialize keyring: %w", err)
}
Expand All @@ -66,7 +66,7 @@ func NewLocalEOTSManager(homeDir, keyringBackend string, dbbackend kvdb.Backend,
}, nil
}

func initKeyring(homeDir, keyringBackend string, inputReader *strings.Reader) (keyring.Keyring, error) {
func InitKeyring(homeDir, keyringBackend string, inputReader *strings.Reader) (keyring.Keyring, error) {
return keyring.New(
"eots-manager",
keyringBackend,
Expand Down Expand Up @@ -149,7 +149,11 @@ func (lm *LocalEOTSManager) SaveEOTSKeyName(pk *btcec.PublicKey, keyName string)
}

func (lm *LocalEOTSManager) LoadBIP340PubKeyFromKeyName(keyName string) (*bbntypes.BIP340PubKey, error) {
info, err := lm.kr.Key(keyName)
return LoadBIP340PubKeyFromKeyName(lm.kr, keyName)
}

func LoadBIP340PubKeyFromKeyName(kr keyring.Keyring, keyName string) (*bbntypes.BIP340PubKey, error) {
info, err := kr.Key(keyName)
if err != nil {
return nil, fmt.Errorf("failed to load keyring record for key %s: %w", keyName, err)
}
Expand Down
Loading
Loading