dymensionxyz / dymension-rdk

Framework for building highly scalable RollApps
Apache License 2.0
98 stars 54 forks source link

checksum validation on genesis bridge #569

Closed mtsitrin closed 4 weeks ago

mtsitrin commented 1 month ago
          possibly a quick win to validate using json checksum.

golang:

package main

import (
    "crypto/sha256"
    "encoding/hex"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "sort"
)

func getJSONChecksum(filePath string) (string, error) {
    fileContent, err := ioutil.ReadFile(filePath)
    if err != nil {
        return "", err
    }

    var jsonObject map[string]interface{}
    err = json.Unmarshal(fileContent, &jsonObject)
    if err != nil {
        return "", err
    }

    keys := make([]string, 0, len(jsonObject))
    for k := range jsonObject {
        keys = append(keys, k)
    }
    sort.Strings(keys)

    sortedJSON, err := json.Marshal(jsonObject)
    if err != nil {
        return "", err
    }

    hash := sha256.Sum256(sortedJSON)
    return hex.EncodeToString(hash[:]), nil
}

func main() {
    checksum, err := getJSONChecksum("path/to/your/file.json")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Checksum:", checksum)
}

and roller can use same method for vadlidate it or through cli:

jq -cS . file.json | sha256sum

_Originally posted by @omritoptix in https://github.com/dymensionxyz/dymension-rdk/pull/565#discussion_r1787713387_