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 2 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

### Improvements
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved

* [#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
22 changes: 22 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 Down Expand Up @@ -92,6 +94,26 @@ func saveKeyNameMapping(cmd *cobra.Command, keyName string) (*types.BIP340PubKey
return nil, fmt.Errorf("failed to load config: %w", err)
}

client, err := eotsclient.NewEOTSManagerGRpcClient(cfg.RPCListener)
if err == nil {
RafilxTenfen marked this conversation as resolved.
Show resolved Hide resolved
// if it can connect to the server, it means the server is running
// so the db is in use and we should send to the client to save the new key mapping
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
232 changes: 184 additions & 48 deletions eotsmanager/proto/eotsmanager.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading