Skip to content

Commit

Permalink
feat(cli): Tool to display all formats of public key (#2426)
Browse files Browse the repository at this point in the history
  • Loading branch information
calbera authored Jan 29, 2025
1 parent 8a04847 commit f87d1d5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/commands/deposit/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Commands(chainSpec chain.Spec) *cobra.Command {
cmd.AddCommand(
NewValidateDeposit(chainSpec),
NewCreateValidator(chainSpec),
GetValidatorKeysCmd(),
)

return cmd
Expand Down
90 changes: 90 additions & 0 deletions cli/commands/deposit/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package deposit

import (
"encoding/base64"

"github.com/berachain/beacon-kit/cli/context"
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/node-core/components"
"github.com/berachain/beacon-kit/node-core/components/signer"
"github.com/cometbft/cometbft/crypto/bls12381"
"github.com/spf13/cobra"
)

// GetValidatorKeysCmd returns a command that returns the validator public key in different formats
// for the given private key files.
//
//nolint:lll // reads better if long description is one line.
func GetValidatorKeysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validator-keys",
Short: "Outputs the validator public key in different formats.",
Long: `Outputs the validator public key in formats of Comet address, Comet pubkey, and Eth/Beacon pubkey. Uses the private key file specified as the value of "priv_validator_key_file" in the config.toml file in the beacond HOMEDIR.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
// Get the BLS signer.
blsSignerI, err := components.ProvideBlsSigner(
components.BlsSignerInput{
AppOpts: context.GetViperFromCmd(cmd),
},
)
if err != nil {
return errors.Wrap(err, "failed to initialize BLS signer from validator files")
}
blsSigner, ok := blsSignerI.(*signer.BLSSigner)
if !ok {
return errors.New("failed to assert BLS signer type")
}

// Get the comet public key.
cometKey, err := blsSigner.PrivValidator.GetPubKey()
if err != nil {
return errors.Wrap(err, "failed to get comet public key from bls signer")
}

// Get the comet BLS public key.
blsKey, err := bls12381.NewPublicKeyFromBytes(cometKey.Bytes())
if err != nil {
return errors.Wrap(err, "failed to create BLS key from bytes")
}

// Output the validator public key in different formats.
cmd.Printf(
"Comet Address:\n%s\n\n",
cometKey.Address(),
)
cmd.Printf(
"Comet Pubkey (Uncompressed Base64):\n%s\n\n",
base64.StdEncoding.EncodeToString(blsKey.Bytes()),
)
cmd.Printf(
"Eth/Beacon Pubkey (Compressed 48-byte Hex):\n%s\n",
blsSigner.PublicKey().String(),
)

return nil
},
}

return cmd
}

0 comments on commit f87d1d5

Please sign in to comment.