Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Aug 9, 2024
1 parent b19c5b3 commit c4b5c68
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
7 changes: 5 additions & 2 deletions clientcontroller/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package api

import (
"cosmossdk.io/math"
"github.com/babylonlabs-io/finality-provider/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"

"github.com/babylonlabs-io/finality-provider/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
babylonConsumerChainName = "babylon"
)

type ClientController interface {
// GetKeyAddress returns the address of the Babylon key in the keyring
GetKeyAddress() sdk.AccAddress

// RegisterFinalityProvider registers a finality provider to the consumer chain
// it returns tx hash and error. The address of the finality provider will be
// the signer of the msg.
Expand Down
14 changes: 6 additions & 8 deletions finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,12 @@ func (app *FinalityProviderApp) handleCreateFinalityProviderRequest(req *createF
return nil, err
}

fpAddr, err := kr.Address(req.passPhrase)
if err != nil {
if !kr.HasKey(req.passPhrase) {
// the chain key does not exist, should create the chain key first
keyInfo, err := kr.CreateChainKey(req.passPhrase, req.hdPath, "")
_, err := kr.CreateChainKey(req.passPhrase, req.hdPath, "")
if err != nil {
return nil, fmt.Errorf("failed to create chain key %s: %w", req.keyName, err)
}
fpAddr = keyInfo.AccAddress
}

// 2. create EOTS key
Expand All @@ -410,6 +408,9 @@ func (app *FinalityProviderApp) handleCreateFinalityProviderRequest(req *createF
return nil, fmt.Errorf("failed to get finality-provider record: %w", err)
}

// get the Babylon address used by the finality provider
fpAddr := app.cc.GetKeyAddress()

// 3. create proof-of-possession
pop, err := kr.CreatePop(fpAddr, fpRecord.PrivKey)
if err != nil {
Expand Down Expand Up @@ -489,10 +490,7 @@ func (app *FinalityProviderApp) StoreFinalityProvider(
if err != nil {
return nil, err
}
fpAddr, err := kr.Address(passPhrase)
if err != nil {
return nil, err
}
fpAddr := app.cc.GetKeyAddress()

// 2. create EOTS key
fpPkBytes, err := app.eotsManager.CreateKey(keyName, passPhrase, hdPath)
Expand Down
5 changes: 2 additions & 3 deletions finality-provider/service/fp_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ func newFinalityProviderManagerWithRegisteredFp(t *testing.T, r *rand.Rand, cc c
require.NoError(t, err)
btcPk, err := bbntypes.NewBIP340PubKey(btcPkBytes)
require.NoError(t, err)
keyInfo, err := kc.CreateChainKey(passphrase, hdPath, "")
require.NoError(t, err)
fpAddr := keyInfo.AccAddress

fpAddr := datagen.GenRandomAccount().GetAddress()
fpRecord, err := em.KeyRecord(btcPk.MustMarshal(), passphrase)
require.NoError(t, err)
pop, err := kc.CreatePop(fpAddr, fpRecord.PrivKey)
Expand Down
16 changes: 4 additions & 12 deletions keyring/keyringcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,12 @@ func (kc *ChainKeyringController) CreateChainKey(passphrase, hdPath, mnemonic st
}

privKey := record.GetLocal().PrivKey.GetCachedValue()
accAddress, err := record.GetAddress()
if err != nil {
return nil, err
}

switch v := privKey.(type) {
case *sdksecp256k1.PrivKey:
sk, pk := btcec.PrivKeyFromBytes(v.Key)
return &types.ChainKeyInfo{
Name: kc.fpName,
AccAddress: accAddress,
PublicKey: pk,
PrivateKey: sk,
Mnemonic: mnemonic,
Expand All @@ -126,15 +121,12 @@ func (kc *ChainKeyringController) CreatePop(fpAddr sdk.AccAddress, btcPrivKey *b
return bstypes.NewPoPBTC(fpAddr, btcPrivKey)
}

// Address returns the address from the keyring
func (kc *ChainKeyringController) Address(passphrase string) (sdk.AccAddress, error) {
// HasKey returns whether the key with the given passphrase exists in the keyring
func (kc *ChainKeyringController) HasKey(passphrase string) bool {
kc.input.Reset(passphrase)
k, err := kc.kr.Key(kc.fpName)
if err != nil {
return nil, fmt.Errorf("failed to get address: %w", err)
}
_, err := kc.kr.Key(kc.fpName)

return k.GetAddress()
return err == nil
}

func (kc *ChainKeyringController) GetChainPrivKey(passphrase string) (*sdksecp256k1.PrivKey, error) {
Expand Down
5 changes: 2 additions & 3 deletions keyring/keyringcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"go.uber.org/zap"

"github.com/babylonlabs-io/babylon/testutil/datagen"
"github.com/babylonlabs-io/babylon/types"
"github.com/btcsuite/btcd/chaincfg"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -54,10 +55,8 @@ func FuzzCreatePoP(f *testing.F) {
require.NoError(t, err)
btcPk, err := types.NewBIP340PubKey(btcPkBytes)
require.NoError(t, err)
keyInfo, err := kc.CreateChainKey(passphrase, hdPath, "")
require.NoError(t, err)

fpAddr := keyInfo.AccAddress
fpAddr := datagen.GenRandomAccount().GetAddress()
fpRecord, err := em.KeyRecord(btcPk.MustMarshal(), passphrase)
require.NoError(t, err)
pop, err := kc.CreatePop(fpAddr, fpRecord.PrivKey)
Expand Down
15 changes: 15 additions & 0 deletions testutil/mocks/clientcontroller.go

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

2 changes: 0 additions & 2 deletions types/chainkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package types

import (
"github.com/btcsuite/btcd/btcec/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type ChainKeyInfo struct {
Name string
Mnemonic string
AccAddress sdk.AccAddress
PublicKey *btcec.PublicKey
PrivateKey *btcec.PrivateKey
}

0 comments on commit c4b5c68

Please sign in to comment.