Skip to content

Commit

Permalink
First changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 committed Oct 10, 2024
1 parent d05a6bd commit 1e56271
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ func getNetworkInfo(app *App, ctx sdk.Context, manifest *UpgradeManifest, expect
if app.cudosMigrationConfigPath != "" {
app.Logger().Info("cudos merge: loading network config", "file", app.cudosMigrationConfigPath, "expected sha256", app.cudosMigrationConfigSha256)

networkInfo, err = LoadNetworkConfigFromFile(app.cudosMigrationConfigPath, &app.cudosMigrationConfigSha256)
networkInfo, err = LoadAndVerifyNetworkConfigFromFile(app.cudosMigrationConfigPath, &app.cudosMigrationConfigSha256)
if err != nil {
return nil, err
}
Expand Down
32 changes: 21 additions & 11 deletions app/upgrade_v_11_4_network_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,24 +276,18 @@ type NetworkConfig struct {
CudosMerge *CudosMergeConfigJSON `json:"cudos_merge,omitempty"`
}

func LoadNetworkConfigFromFile(configFilePath string, expectedSha256Hex *string) (*NetworkConfig, error) {
func LoadNetworkConfigFromFile(configFilePath string) (*NetworkConfig, *[]byte, error) {
// Open the JSON file
file, err := os.Open(configFilePath)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %v", err)
return nil, nil, fmt.Errorf("failed to open config file: %v", err)
}
defer file.Close()

// Read the file contents
byteValue, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %v", err)
}

if isVerified, actualHashHex, err := VerifySha256(byteValue, expectedSha256Hex); err != nil {
return nil, err
} else if !isVerified {
return nil, fmt.Errorf("failed to verify sha256: NetworkConfig file \"%s\" hash \"%s\" does not match expected hash \"%s\"", configFilePath, actualHashHex, *expectedSha256Hex)
return nil, nil, fmt.Errorf("failed to read config file: %v", err)
}

// Initialize an empty struct to hold the JSON data
Expand All @@ -302,10 +296,26 @@ func LoadNetworkConfigFromFile(configFilePath string, expectedSha256Hex *string)
// Unmarshal the JSON data into the struct
err = json.Unmarshal(byteValue, &config)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %v", err)
return nil, nil, fmt.Errorf("failed to unmarshal JSON: %v", err)
}

return &config, &byteValue, nil
}

func LoadAndVerifyNetworkConfigFromFile(configFilePath string, expectedSha256Hex *string) (*NetworkConfig, error) {
config, byteValue, err := LoadNetworkConfigFromFile(configFilePath)

if err != nil {
return nil, err
}

if isVerified, actualHashHex, err := VerifySha256(*byteValue, expectedSha256Hex); err != nil {
return nil, err
} else if !isVerified {
return nil, fmt.Errorf("failed to verify sha256: NetworkConfig file \"%s\" hash \"%s\" does not match expected hash \"%s\"", configFilePath, actualHashHex, *expectedSha256Hex)
}

return &config, nil
return config, nil
}

type CudosMergeConfigJSON struct {
Expand Down
22 changes: 22 additions & 0 deletions app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/cosmos/cosmos-sdk/types/bech32"
"io"
"os"
)
Expand Down Expand Up @@ -32,6 +33,14 @@ func GenerateSHA256FromFile(filePath string) (string, error) {
return hex.EncodeToString(hashSum), nil
}

func GenerateSha256Hex(dataToVerify []byte) (actualHashHex string) {
actualHash32 := sha256.Sum256(dataToVerify)
actualHash := actualHash32[:]
actualHashHex = hex.EncodeToString(actualHash)

return actualHashHex
}

func VerifySha256(dataToVerify []byte, expectedSha256Hex *string) (isVerified bool, actualHashHex string, err error) {
if expectedSha256Hex == nil {
return true, "", nil
Expand Down Expand Up @@ -59,3 +68,16 @@ func VerifySha256(dataToVerify []byte, expectedSha256Hex *string) (isVerified bo
}
return isVerified, actualHashHex, nil
}

func VerifyAddressPrefix(addr string, expectedPrefix string) error {
prefix, _, err := bech32.DecodeAndConvert(addr)
if err != nil {
return err
}

if prefix != expectedPrefix {
return fmt.Errorf("invalid address prefix: expected %s, got %s", expectedPrefix, prefix)
}

return nil
}
67 changes: 67 additions & 0 deletions cmd/fetchd/cmd/cudos_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/fetchai/fetchd/app"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -40,3 +42,68 @@ func AddCudosFlags(startCmd *cobra.Command) {
}

}

func utilCudosCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cudos",
Short: "Cudos commands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmdVerify := &cobra.Command{
Use: "verify-config [config_json_file_path]",
Short: "Verifies the configuration JSON file",
Long: "This command verifies the structure and content of the configuration JSON file. It checks if all required fields are present and validates their values against predefined rules.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := client.GetClientContextFromCmd(cmd)

path := args[0]

// Read and verify the JSON file
var err error
if err = VerifyConfigFile(path, ctx); err != nil {
return err
}

return ctx.PrintString("Configuration is valid.")
},
}

cmd.AddCommand(cmdVerify)
return cmd
}

// VerifyConfigFile validates the content of a JSON configuration file.
func VerifyConfigFile(configFilePath string, ctx client.Context) error {
sourcePrefix := "cudos"

config, configBytes, err := app.LoadNetworkConfigFromFile(configFilePath)
if err != nil {
return err
}

if config.MergeSourceChainID == "" {
return fmt.Errorf("merge source chain id is empty")
}
if config.DestinationChainID == "" {
return fmt.Errorf("destination chain id is empty")
}

// Verify addresses
err = app.VerifyAddressPrefix(config.CudosMerge.RemainingDistributionBalanceAddr, sourcePrefix)
if err != nil {
return fmt.Errorf("remaining distribution balance address prefix error: %v", err)
}

err = ctx.PrintString(fmt.Sprintf("Config hash: %s", app.GenerateSha256Hex(*configBytes)))
if err != nil {
return err
}

println(config)

return nil
}
1 change: 1 addition & 0 deletions cmd/fetchd/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func utilCommand() *cobra.Command {
cmd.AddCommand(
utilJsonCommand(),
utilAddressCommand(),
utilCudosCommand(),
)

return cmd
Expand Down

0 comments on commit 1e56271

Please sign in to comment.