-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): Re-add create validator (#2004)
- Loading branch information
1 parent
76943ea
commit 685fbf2
Showing
5 changed files
with
228 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2024, 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 ( | ||
"os" | ||
|
||
"cosmossdk.io/log" | ||
"github.com/berachain/beacon-kit/mod/cli/pkg/utils/parser" | ||
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" | ||
"github.com/berachain/beacon-kit/mod/node-core/pkg/components" | ||
"github.com/berachain/beacon-kit/mod/node-core/pkg/components/signer" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/common" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/constraints" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewCreateValidator creates a new command to create a validator deposit. | ||
func NewCreateValidator[ | ||
ExecutionPayloadT constraints.EngineType[ExecutionPayloadT], | ||
]( | ||
chainSpec common.ChainSpec, | ||
) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create-validator", | ||
Short: "Creates a validator deposit", | ||
Long: `Creates a validator deposit with the necessary credentials. The | ||
arguments are expected in the order of withdrawal credentials, deposit | ||
amount, current version, and genesis validator root. If the broadcast | ||
flag is set to true, a private key must be provided to sign the transaction.`, | ||
Args: cobra.ExactArgs(4), //nolint:mnd // The number of arguments. | ||
RunE: createValidatorCmd[ExecutionPayloadT](chainSpec), | ||
} | ||
cmd.Flags().BoolP( | ||
overrideNodeKey, overrideNodeKeyShorthand, | ||
defaultOverrideNodeKey, overrideNodeKeyMsg, | ||
) | ||
cmd.Flags(). | ||
String(valPrivateKey, defaultValidatorPrivateKey, valPrivateKeyMsg) | ||
|
||
return cmd | ||
} | ||
|
||
// createValidatorCmd returns a command that builds a create validator request. | ||
// | ||
|
||
func createValidatorCmd[ | ||
ExecutionPayloadT constraints.EngineType[ExecutionPayloadT], | ||
]( | ||
chainSpec common.ChainSpec, | ||
) func(*cobra.Command, []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
logger := log.NewLogger(os.Stdout) | ||
|
||
// Get the BLS signer. | ||
blsSigner, err := getBLSSigner(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
credentials, err := parser.ConvertWithdrawalCredentials(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
amount, err := parser.ConvertAmount(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
currentVersion, err := parser.ConvertVersion(args[2]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
genesisValidatorRoot, err := parser.ConvertGenesisValidatorRoot(args[3]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create and sign the deposit message. | ||
depositMsg, signature, err := types.CreateAndSignDepositMessage( | ||
types.NewForkData(currentVersion, genesisValidatorRoot), | ||
chainSpec.DomainTypeDeposit(), | ||
blsSigner, | ||
credentials, | ||
amount, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Verify the deposit message. | ||
if err = depositMsg.VerifyCreateValidator( | ||
types.NewForkData(currentVersion, genesisValidatorRoot), | ||
signature, | ||
chainSpec.DomainTypeDeposit(), | ||
signer.BLSSigner{}.VerifySignature, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
// If the broadcast flag is not set, output the deposit message and | ||
// signature and return early. | ||
logger.Info( | ||
"Deposit Message CallData", | ||
"pubkey", depositMsg.Pubkey.String(), | ||
"withdrawal credentials", depositMsg.Credentials.String(), | ||
"amount", depositMsg.Amount, | ||
"signature", signature.String(), | ||
) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// getBLSSigner returns a BLS signer based on the override commands key flag. | ||
func getBLSSigner( | ||
cmd *cobra.Command, | ||
) (crypto.BLSSigner, error) { | ||
var legacyKey components.LegacyKey | ||
overrideFlag, err := cmd.Flags().GetBool(overrideNodeKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Build the BLS signer. | ||
if overrideFlag { | ||
var validatorPrivKey string | ||
validatorPrivKey, err = cmd.Flags().GetString(valPrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if validatorPrivKey == "" { | ||
return nil, ErrValidatorPrivateKeyRequired | ||
} | ||
legacyKey, err = signer.LegacyKeyFromString(validatorPrivKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return components.ProvideBlsSigner( | ||
components.BlsSignerInput{ | ||
AppOpts: client.GetViperFromCmd(cmd), | ||
PrivKey: legacyKey, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2024, 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 | ||
|
||
const ( | ||
// overrideNodeKey is the flag for overriding the node key. | ||
overrideNodeKey = "override-node-key" | ||
|
||
// validatorPrivateKey is the flag for the validator private key. | ||
valPrivateKey = "validator-private-key" | ||
) | ||
|
||
const ( | ||
|
||
// overrideNodeKeyShorthand is the shorthand flag for the overrideNodeKey | ||
// flag. | ||
overrideNodeKeyShorthand = "o" | ||
) | ||
|
||
const ( | ||
// defaultOverrideNodeKey is the default value for the overrideNodeKey flag. | ||
defaultOverrideNodeKey = false | ||
|
||
// defaultValidatorPrivateKey is the default value for the | ||
// validatorPrivateKey flag. | ||
defaultValidatorPrivateKey = "" | ||
) | ||
|
||
const ( | ||
// overrideNodeKeyFlagMsg is the usage description for the overrideNodeKey | ||
// flag. | ||
overrideNodeKeyMsg = "override the node private key" | ||
|
||
// valPrivateKeyMsg is the usage description for the | ||
// valPrivateKey flag. | ||
valPrivateKeyMsg = `validator private key. This is required if the | ||
override-node-key flag is set.` | ||
) |