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

chore: Use sign schnorr instead of getting private key from EOTS manager #154

Merged
merged 2 commits into from
Nov 26, 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
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Improvements

* [149](https://github.com/babylonlabs-io/finality-provider/pull/149) Remove update of config after `fpd keys add`
* [148](https://github.com/babylonlabs-io/finality-provider/pull/148) Allow command `eotsd keys add` to use
* [#149](https://github.com/babylonlabs-io/finality-provider/pull/149) Remove update of config after `fpd keys add`
* [#148](https://github.com/babylonlabs-io/finality-provider/pull/148) Allow command `eotsd keys add` to use
empty HD path to derive new key and use master private key.
* [#154](https://github.com/babylonlabs-io/finality-provider/pull/154) Use sign schnorr instead of getting private key from EOTS manager

## v0.12.0

### Bug Fixes

* [139](https://github.com/babylonlabs-io/finality-provider/pull/139) Ignore voting power not updated error
* [#139](https://github.com/babylonlabs-io/finality-provider/pull/139) Ignore voting power not updated error

### Improvements

Expand Down
83 changes: 22 additions & 61 deletions finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
bbntypes "github.com/babylonlabs-io/babylon/types"
bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/cometbft/cometbft/crypto/tmhash"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -424,13 +425,8 @@ func (app *FinalityProviderApp) handleCreateFinalityProviderRequest(req *createF
}
}

fpRecord, err := app.eotsManager.KeyRecord(fpPk.MustMarshal(), req.passPhrase)
if err != nil {
return nil, fmt.Errorf("failed to get finality-provider record: %w", err)
}

// 3. create proof-of-possession
pop, err := kr.CreatePop(fpAddr, fpRecord.PrivKey)
pop, err := app.CreatePop(fpAddr, fpPk, req.passPhrase)
if err != nil {
return nil, fmt.Errorf("failed to create proof-of-possession of the finality-provider: %w", err)
}
Expand All @@ -456,6 +452,26 @@ func (app *FinalityProviderApp) handleCreateFinalityProviderRequest(req *createF
}, nil
}

func (app *FinalityProviderApp) CreatePop(fpAddress sdk.AccAddress, fpPk *bbntypes.BIP340PubKey, passphrase string) (*bstypes.ProofOfPossessionBTC, error) {
pop := &bstypes.ProofOfPossessionBTC{
BtcSigType: bstypes.BTCSigType_BIP340, // by default, we use BIP-340 encoding for BTC signature
}

// generate pop.BtcSig = schnorr_sign(sk_BTC, hash(bbnAddress))
// NOTE: *schnorr.Sign has to take the hash of the message.
// So we have to hash the address before signing
hash := tmhash.Sum(fpAddress.Bytes())

sig, err := app.eotsManager.SignSchnorrSig(fpPk.MustMarshal(), hash, passphrase)
if err != nil {
return nil, fmt.Errorf("failed to get schnorr signature from the EOTS manager: %w", err)
}

pop.BtcSig = bbntypes.NewBIP340SignatureFromBTCSig(sig).MustMarshal()

return pop, nil
}

// SignRawMsg loads the keyring private key and signs a message.
func (app *FinalityProviderApp) SignRawMsg(
keyName, passPhrase, hdPath string,
Expand Down Expand Up @@ -497,61 +513,6 @@ func (app *FinalityProviderApp) UpdateClientController(cc clientcontroller.Clien
app.cc = cc
}

// StoreFinalityProvider stores a new finality provider in the fp store.
func (app *FinalityProviderApp) StoreFinalityProvider(
keyName, passPhrase, hdPath, chainID string,
description *stakingtypes.Description,
commission *sdkmath.LegacyDec,
) (*store.StoredFinalityProvider, error) {
// 1. check if the chain key exists
kr, _, err := app.loadChainKeyring(keyName, passPhrase, hdPath)
if err != nil {
return nil, err
}
fpAddr, err := kr.Address(passPhrase)
if err != nil {
return nil, err
}

// 2. create EOTS key
fpPkBytes, err := app.eotsManager.CreateKey(keyName, passPhrase, hdPath)
if err != nil {
return nil, err
}
fpPk, err := bbntypes.NewBIP340PubKey(fpPkBytes)
if err != nil {
return nil, err
}
fpRecord, err := app.eotsManager.KeyRecord(fpPk.MustMarshal(), passPhrase)
if err != nil {
return nil, fmt.Errorf("failed to get finality-provider record: %w", err)
}

// 3. create proof-of-possession
pop, err := kr.CreatePop(fpAddr, fpRecord.PrivKey)
if err != nil {
return nil, fmt.Errorf("failed to create proof-of-possession of the finality provider: %w", err)
}

if err := app.fps.CreateFinalityProvider(fpAddr, fpPk.MustToBTCPK(), description, commission, keyName, chainID, pop.BtcSig); err != nil {
return nil, fmt.Errorf("failed to save finality-provider: %w", err)
}
app.fpManager.metrics.RecordFpStatus(fpPk.MarshalHex(), proto.FinalityProviderStatus_CREATED)

app.logger.Info("successfully created a finality-provider",
zap.String("btc_pk", fpPk.MarshalHex()),
zap.String("fp_addr", fpAddr.String()),
zap.String("key_name", keyName),
)

storedFp, err := app.fps.GetFinalityProvider(fpPk.MustToBTCPK())
if err != nil {
return nil, err
}

return storedFp, nil
}

func CreateChainKey(keyringDir, chainID, keyName, backend, passphrase, hdPath, mnemonic string) (*types.ChainKeyInfo, error) {
sdkCtx, err := fpkr.CreateClientCtx(
keyringDir, chainID,
Expand Down
Loading