Skip to content

Commit

Permalink
some basic validation of ethereum deposit data file
Browse files Browse the repository at this point in the history
  • Loading branch information
CluEleSsUK committed Jul 25, 2024
1 parent bbdac2d commit 4f2772e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cli/internal/cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func Sign(cmd *cobra.Command, _ []string) {
}

log := shared.QuietLogger{Quiet: shortFlag}
signingOutput, err := cli.Sign(shared.Uniq(append(args, operatorFlag...)), depositData, log)
// TODO: this should probably sign something more than just the deposit data root
signingOutput, err := cli.Sign(shared.Uniq(append(args, operatorFlag...)), depositData.DepositDataRoot, log)
if err != nil {
shared.Exit(fmt.Sprintf("%v", err))
}
Expand Down
21 changes: 18 additions & 3 deletions cli/internal/cmd/sign_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package cmd

import (
"encoding/json"
"math/big"
"os"
"path"
"strings"
"testing"

"github.com/randa-mu/ssv-dkg/shared/api"
"github.com/stretchr/testify/require"
)

func TestSignCommand(t *testing.T) {
tmp := t.TempDir()
filepath := path.Join(tmp, "testfile")
createJunkFile(t, filepath)
createdUnsignedDepositData(t, filepath)

tests := []struct {
name string
Expand Down Expand Up @@ -96,10 +99,22 @@ func TestSignCommand(t *testing.T) {
}
}

func createJunkFile(t *testing.T, filepath string) {
func createdUnsignedDepositData(t *testing.T, filepath string) {
data := api.UnsignedDepositData{
WithdrawalCredentials: []byte("hello world"),
DepositDataRoot: []byte("hello world"),
DepositMessageRoot: []byte("hello world"),
Amount: api.BigInt{Int: *big.NewInt(1)},
ForkVersion: "somefork",
NetworkName: "somenetwork",
DepositCLIVersion: "somecli",
}

bytes, err := json.Marshal(data)
require.NoError(t, err)
file, err := os.Create(filepath)
require.NoError(t, err)
_, err = file.Write([]byte("hello"))
_, err = file.Write(bytes)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
Expand Down
37 changes: 30 additions & 7 deletions shared/api/cli.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package api

import (
"fmt"
"math/big"

"github.com/randa-mu/ssv-dkg/shared/crypto"
)

type UnsignedDepositData struct {
WithdrawalCredentials []byte `json:"withdrawal_credentials"`
DepositDataRoot []byte `json:"deposit_data_root"`
DepositMessageRoot []byte `json:"deposit_message_root,omitempty"`
Amount big.Int `json:"amount,omitempty"`
ForkVersion string `json:"fork_version,omitempty"`
NetworkName string `json:"network_name,omitempty"`
DepositCLIVersion string `json:"deposit_cli_version,omitempty"`
WithdrawalCredentials []byte `json:"withdrawal_credentials"`
DepositDataRoot []byte `json:"deposit_data_root"`
DepositMessageRoot []byte `json:"deposit_message_root,omitempty"`
Amount BigInt `json:"amount,omitempty"`
ForkVersion string `json:"fork_version,omitempty"`
NetworkName string `json:"network_name,omitempty"`
DepositCLIVersion string `json:"deposit_cli_version,omitempty"`
}

type SignedDepositData struct {
Expand All @@ -38,3 +39,25 @@ type OperatorResponse struct {
Identity crypto.Identity
Response SignResponse
}

type BigInt struct {
big.Int
}

// MarshalJSON intentionally uses a value pointer or things go wrong
func (b BigInt) MarshalJSON() ([]byte, error) {
return []byte(b.String()), nil
}

func (b *BigInt) UnmarshalJSON(p []byte) error {
if string(p) == "null" {
return nil
}
var z big.Int
_, ok := z.SetString(string(p), 10)
if !ok {
return fmt.Errorf("not a valid big integer: %s", p)
}
b.Int = z
return nil
}

0 comments on commit 4f2772e

Please sign in to comment.