Compare commits

...

4 Commits

Author SHA1 Message Date
Jim McDonald
394b4a7cd2 Bump version. 2022-05-19 13:00:38 +01:00
Jim McDonald
fd574aae34 Tidy up tests. 2022-05-19 13:00:05 +01:00
Jim McDonald
7fe503f51d Add ropsten support. 2022-05-19 12:59:48 +01:00
Jim McDonald
6bfb0ef098 Add validator credentials get command. 2022-05-10 15:02:41 +01:00
17 changed files with 545 additions and 13 deletions

View File

@@ -1,3 +1,9 @@
1.22.0:
- add "ropsten" to the list of supported networks
1.21.0:
- add "validator credentials get"
1.20.0:
- add "chain queues"

View File

@@ -111,6 +111,8 @@ func includeCommandBindings(cmd *cobra.Command) {
synccommitteeInclusionBindings()
case "synccommittee/members":
synccommitteeMembersBindings()
case "validator/credentials/get":
validatorCredentialsGetBindings()
case "validator/depositdata":
validatorDepositdataBindings()
case "validator/duties":

View File

@@ -0,0 +1,89 @@
// Copyright © 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validatorcredentialsget
import (
"context"
"time"
eth2client "github.com/attestantio/go-eth2-client"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type command struct {
quiet bool
verbose bool
debug bool
// Input.
account string
index string
pubKey string
// Beacon node connection.
timeout time.Duration
connection string
allowInsecureConnections bool
// Data access.
consensusClient eth2client.Service
validatorsProvider eth2client.ValidatorsProvider
// Output.
validator *apiv1.Validator
}
func newCommand(ctx context.Context) (*command, error) {
c := &command{
quiet: viper.GetBool("quiet"),
verbose: viper.GetBool("verbose"),
debug: viper.GetBool("debug"),
}
// Timeout.
if viper.GetDuration("timeout") == 0 {
return nil, errors.New("timeout is required")
}
c.timeout = viper.GetDuration("timeout")
if viper.GetString("connection") == "" {
return nil, errors.New("connection is required")
}
c.connection = viper.GetString("connection")
c.allowInsecureConnections = viper.GetBool("allow-insecure-connections")
c.account = viper.GetString("account")
c.index = viper.GetString("index")
c.pubKey = viper.GetString("pubkey")
nonNil := 0
if c.account != "" {
nonNil++
}
if c.index != "" {
nonNil++
}
if c.pubKey != "" {
nonNil++
}
if nonNil == 0 {
return nil, errors.New("one of account, index or pubkey required")
}
if nonNil > 1 {
return nil, errors.New("only one of account, index and pubkey allowed")
}
return c, nil
}

View File

@@ -0,0 +1,91 @@
// Copyright © 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validatorcredentialsget
import (
"context"
"os"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestInput(t *testing.T) {
if os.Getenv("ETHDO_TEST_CONNECTION") == "" {
t.Skip("ETHDO_TEST_CONNECTION not configured; cannot run tests")
}
tests := []struct {
name string
vars map[string]interface{}
err string
}{
{
name: "TimeoutMissing",
vars: map[string]interface{}{},
err: "timeout is required",
},
{
name: "ConnectionMissing",
vars: map[string]interface{}{
"timeout": "5s",
"index": "1",
},
err: "connection is required",
},
{
name: "NoValidatorInfo",
vars: map[string]interface{}{
"timeout": "5s",
"connection": os.Getenv("ETHDO_TEST_CONNECTION"),
},
err: "one of account, index or pubkey required",
},
{
name: "MultipleValidatorInfo",
vars: map[string]interface{}{
"timeout": "5s",
"connection": os.Getenv("ETHDO_TEST_CONNECTION"),
"index": "1",
"pubkey": "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
},
err: "only one of account, index and pubkey allowed",
},
{
name: "Good",
vars: map[string]interface{}{
"timeout": "5s",
"connection": os.Getenv("ETHDO_TEST_CONNECTION"),
"index": "1",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
viper.Reset()
for k, v := range test.vars {
viper.Set(k, v)
}
_, err := newCommand(context.Background())
if test.err != "" {
require.EqualError(t, err, test.err)
} else {
require.NoError(t, err)
}
})
}
}

View File

@@ -0,0 +1,44 @@
// Copyright © 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validatorcredentialsget
import (
"context"
"fmt"
"strings"
)
func (c *command) output(ctx context.Context) (string, error) {
if c.quiet {
return "", nil
}
builder := strings.Builder{}
switch c.validator.Validator.WithdrawalCredentials[0] {
case 0:
builder.WriteString("BLS credentials: ")
builder.WriteString(fmt.Sprintf("%#x", c.validator.Validator.WithdrawalCredentials))
case 1:
builder.WriteString("Ethereum execution address: ")
builder.WriteString(fmt.Sprintf("%#x", c.validator.Validator.WithdrawalCredentials[12:]))
if c.verbose {
builder.WriteString("\n")
builder.WriteString("Withdrawal credentials: ")
builder.WriteString(fmt.Sprintf("%#x", c.validator.Validator.WithdrawalCredentials))
}
}
return builder.String(), nil
}

View File

@@ -0,0 +1,139 @@
// Copyright © 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validatorcredentialsget
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/pkg/errors"
"github.com/wealdtech/ethdo/util"
)
func (c *command) process(ctx context.Context) error {
// Obtain information we need to process.
if err := c.setup(ctx); err != nil {
return err
}
// Work out which validator we are dealing with.
if err := c.fetchValidator(ctx); err != nil {
return err
}
if c.debug {
data, err := json.Marshal(c.validator)
if err == nil {
fmt.Println(string(data))
}
}
return nil
}
func (c *command) setup(ctx context.Context) error {
var err error
// Connect to the consensus node.
c.consensusClient, err = util.ConnectToBeaconNode(ctx, c.connection, c.timeout, c.allowInsecureConnections)
if err != nil {
return errors.Wrap(err, "failed to connect to consensus node")
}
// Obtain the validators provider.
var isProvider bool
c.validatorsProvider, isProvider = c.consensusClient.(eth2client.ValidatorsProvider)
if !isProvider {
return errors.New("consensu node does not provide validator information")
}
return nil
}
func (c *command) fetchValidator(ctx context.Context) error {
if c.account != "" {
_, account, err := util.WalletAndAccountFromInput(ctx)
if err != nil {
return errors.Wrap(err, "unable to obtain account")
}
accPubKey, err := util.BestPublicKey(account)
if err != nil {
return errors.Wrap(err, "unable to obtain public key for account")
}
pubKey := phase0.BLSPubKey{}
copy(pubKey[:], accPubKey.Marshal())
validators, err := c.validatorsProvider.ValidatorsByPubKey(ctx,
"head",
[]phase0.BLSPubKey{pubKey},
)
if err != nil {
return errors.Wrap(err, "failed to obtain validator information")
}
if len(validators) == 0 {
return errors.New("unknown validator")
}
for _, validator := range validators {
c.validator = validator
}
}
if c.index != "" {
tmp, err := strconv.ParseUint(c.index, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid validator index")
}
index := phase0.ValidatorIndex(tmp)
validators, err := c.validatorsProvider.Validators(ctx,
"head",
[]phase0.ValidatorIndex{index},
)
if err != nil {
return errors.Wrap(err, "failed to obtain validator information")
}
if _, exists := validators[index]; !exists {
return errors.New("unknown validator")
}
c.validator = validators[index]
}
if c.pubKey != "" {
bytes, err := hex.DecodeString(strings.TrimPrefix(c.pubKey, "0x"))
if err != nil {
return errors.Wrap(err, "invalid validator public key")
}
pubKey := phase0.BLSPubKey{}
copy(pubKey[:], bytes)
validators, err := c.validatorsProvider.ValidatorsByPubKey(ctx,
"head",
[]phase0.BLSPubKey{pubKey},
)
if err != nil {
return errors.Wrap(err, "failed to obtain validator information")
}
if len(validators) == 0 {
return errors.New("unknown validator")
}
for _, validator := range validators {
c.validator = validator
}
}
return nil
}

View File

@@ -0,0 +1,50 @@
// Copyright © 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validatorcredentialsget
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// Run runs the command.
func Run(cmd *cobra.Command) (string, error) {
ctx := context.Background()
c, err := newCommand(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to set up command")
}
// Further errors do not need a usage report.
cmd.SilenceUsage = true
if err := c.process(ctx); err != nil {
return "", errors.Wrap(err, "failed to process")
}
if viper.GetBool("quiet") {
return "", nil
}
results, err := c.output(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to obtain output")
}
return results, nil
}

View File

@@ -1,4 +1,4 @@
// Copyright © 2019, 2020 Weald Technology Trading
// Copyright © 2019 - 2022 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
@@ -110,6 +110,7 @@ func validatorDepositDataOutputLaunchpad(datum *dataOut) (string, error) {
[4]byte{0x00, 0x00, 0x00, 0x00}: "mainnet",
[4]byte{0x00, 0x00, 0x20, 0x09}: "pyrmont",
[4]byte{0x00, 0x00, 0x10, 0x20}: "prater",
[4]byte{0x80, 0x00, 0x00, 0x69}: "ropsten",
}
if datum.validatorPubKey == nil {

View File

@@ -0,0 +1,32 @@
// Copyright © 2022 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"github.com/spf13/cobra"
)
// validatorCredentialsCmd represents the validator credentials command
var validatorCredentialsCmd = &cobra.Command{
Use: "credentials",
Short: "Manage Ethereum consensu validator credentials",
Long: `Manage Ethereum consensu validator credentials.`,
}
func init() {
validatorCmd.AddCommand(validatorCredentialsCmd)
}
func validatorCredentialsFlags(cmd *cobra.Command) {
}

View File

@@ -0,0 +1,65 @@
// Copyright © 2022 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
validatorcredentialsget "github.com/wealdtech/ethdo/cmd/validator/credentials/get"
)
var validatorCredentialsGetCmd = &cobra.Command{
Use: "get",
Short: "Obtain withdrawal credentials for an Ethereum consensus validator",
Long: `Obtain withdrawal credentials for an Ethereum consensus validator. For example:
ethdo validator credentials get --account=primary/validator
In quiet mode this will return 0 if the validator exists, otherwise 1.`,
RunE: func(cmd *cobra.Command, args []string) error {
res, err := validatorcredentialsget.Run(cmd)
if err != nil {
return err
}
if viper.GetBool("quiet") {
return nil
}
if res != "" {
fmt.Println(res)
}
return nil
},
}
func init() {
validatorCredentialsCmd.AddCommand(validatorCredentialsGetCmd)
validatorCredentialsFlags(validatorCredentialsGetCmd)
validatorCredentialsGetCmd.Flags().String("account", "", "Account for which to fetch validator credentials")
validatorCredentialsGetCmd.Flags().String("index", "", "Validator index for which to fetch validator credentials")
validatorCredentialsGetCmd.Flags().String("pubkey", "", "Validator public key for which to fetch validator credentials")
}
func validatorCredentialsGetBindings() {
if err := viper.BindPFlag("account", validatorCredentialsGetCmd.Flags().Lookup("account")); err != nil {
panic(err)
}
if err := viper.BindPFlag("index", validatorCredentialsGetCmd.Flags().Lookup("index")); err != nil {
panic(err)
}
if err := viper.BindPFlag("pubkey", validatorCredentialsGetCmd.Flags().Lookup("pubkey")); err != nil {
panic(err)
}
}

View File

@@ -1,4 +1,4 @@
// Copyright © 2019, 2020 Weald Technology Trading
// Copyright © 2019 - 2022 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
@@ -55,7 +55,7 @@ func init() {
validatorDepositDataCmd.Flags().String("withdrawaladdress", "", "Ethereum 1 address of the account to which the validator funds will be withdrawn")
validatorDepositDataCmd.Flags().String("depositvalue", "", "Value of the amount to be deposited")
validatorDepositDataCmd.Flags().Bool("raw", false, "Print raw deposit data transaction data")
validatorDepositDataCmd.Flags().String("forkversion", "", "Use a hard-coded fork version (default is to fetch it from the node)")
validatorDepositDataCmd.Flags().String("forkversion", "", "Use a hard-coded fork version (default is to use mainnet value)")
validatorDepositDataCmd.Flags().Bool("launchpad", false, "Print launchpad-compatible JSON")
}

View File

@@ -24,7 +24,7 @@ import (
// ReleaseVersion is the release version of the codebase.
// Usually overridden by tag names when building binaries.
var ReleaseVersion = "local build (latest release 1.20.0)"
var ReleaseVersion = "local build (latest release 1.22.0)"
// versionCmd represents the version command
var versionCmd = &cobra.Command{

View File

@@ -35,11 +35,11 @@ import (
func TestInput(t *testing.T) {
require.NoError(t, e2types.InitBLS())
dir := os.TempDir()
datFile := filepath.Join(dir, "backup.dat")
err := ioutil.WriteFile(datFile, []byte("dummy"), 0600)
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
// defer os.RemoveAll(dir)
datFile := filepath.Join(dir, "backup.dat")
require.NoError(t, ioutil.WriteFile(datFile, []byte("dummy"), 0600))
defer os.RemoveAll(dir)
store := scratch.New()
require.NoError(t, e2wallet.UseStore(store))

View File

@@ -37,10 +37,10 @@ func TestProcess(t *testing.T) {
"ed2166659f7b5412a169ec83627386bc6ff1a31e67735d405b2bf7cb122ad7ced35c87e42c8e8f7ba90b5899a94be506687a9c5b353af2a216018d9f1bf61745a5",
}
dir := os.TempDir()
datFile := filepath.Join(dir, "backup.dat")
err := ioutil.WriteFile(datFile, export, 0600)
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
datFile := filepath.Join(dir, "backup.dat")
require.NoError(t, ioutil.WriteFile(datFile, export, 0600))
defer os.RemoveAll(dir)
tests := []struct {

View File

@@ -560,14 +560,26 @@ $ ethdo synccommittee members
Validator commands focus on interaction with Ethereum 2 validators.
#### `credentials get`
`ethdo validator credentials get` provides information about the withdrawal credentials for the provided validator. Options include:
- `account` the account for which to obtain the withdrawal credentials (in format "wallet/account")
- `pubkey` the public key of the validator for which to obtain the withdrawal credentials
- `index` the index of the validator for which to obtain the withdrawal credentials
```sh
$ ethdo validator credentials get --account=Validators/1
```
#### `depositdata`
`ethdo validator depositdata` generates the data required to deposit one or more Ethereum 2 validators. Options include:
- `withdrawalaccount` specify the account to be used for the withdrawal credentials (if withdrawalpubkey is not supplied)
- `withdrawaladdress` specify the Ethereum execution address to be used for the withdrawal credentials (if withdrawalpubkey is not supplied)
- `withdrawalpubkey` specify the public key to be used for the withdrawal credentials (if withdrawalaccount is not supplied)
- `validatoraccount` specify the account to be used for the validator
- `depositvalue` specify the amount of the deposit
- `forkversion` specify the fork version for the deposit signature; this should not be included unless the deposit is being generated offline. Note that supplying an incorrect value could result in the loss of your deposit, so only supply this value if you are sure you know what you are doing
- `forkversion` specify the fork version for the deposit signature; this defaults to mainnet. Note that supplying an incorrect value could result in the loss of your deposit, so only supply this value if you are sure you know what you are doing. You can find the value for other chains by fetching the value supplied in "Genesis fork version" of the `ethdo chain info` command
- `raw` generate raw hex output that can be supplied as the data to an Ethereum 1 deposit transaction
#### `exit`

View File

@@ -1,4 +1,4 @@
// Copyright © 2020 Weald Technology Trading
// Copyright © 2020, 2022 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

View File

@@ -27,6 +27,7 @@ var networks = map[string]string{
"07b39f4fde4a38bace212b546dac87c58dfe3fdc": "Medalla",
"8c5fecdc472e27bc447696f431e425d02dd46a8c": "Pyrmont",
"ff50ed3d0ec03ac01d4c79aad74928bff48a7b2b": "Prater",
"6f22ffbc56eff051aecf839396dd1ed9ad6bba9d": "Ropsten",
}
// Network returns the name of the network., calculated from the deposit contract information.