Skip to content

Commit

Permalink
fix: save key name mapping verifies if there is a client running (#244)
Browse files Browse the repository at this point in the history
Closes: #243
  • Loading branch information
RafilxTenfen authored and Lazar955 committed Dec 23, 2024
1 parent f57fbdd commit 93e886e
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 53 deletions.
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

* [#240](https://github.com/babylonlabs-io/finality-provider/pull/240) fix removed printf in cmd command
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

0 comments on commit 93e886e

Please sign in to comment.