From f7be814c824ca54664e23a0f6f13c71ab66e5bcb Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Tue, 18 Jul 2023 10:19:52 -0400 Subject: [PATCH] feat(manifest): implement Version function Signed-off-by: Artur Troian --- go/manifest/v2beta2/manifest.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/go/manifest/v2beta2/manifest.go b/go/manifest/v2beta2/manifest.go index 43480448..69b7ce95 100644 --- a/go/manifest/v2beta2/manifest.go +++ b/go/manifest/v2beta2/manifest.go @@ -1,9 +1,13 @@ package v2beta2 import ( + "crypto/sha256" + "encoding/json" "fmt" "regexp" + sdk "github.com/cosmos/cosmos-sdk/types" + dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3" ) @@ -43,3 +47,21 @@ func (m Manifest) CheckAgainstDeployment(dgroups []dtypes.Group) error { func (m Manifest) CheckAgainstGSpecs(gspecs dtypes.GroupSpecs) error { return m.GetGroups().CheckAgainstGSpecs(gspecs) } + +// Version calculates the identifying deterministic hash for an SDL. +// Sha256 returns 32 byte sum of the SDL. +func (m Manifest) Version() ([]byte, error) { + data, err := json.Marshal(m) + if err != nil { + return nil, err + } + + sortedBytes, err := sdk.SortJSON(data) + if err != nil { + return nil, err + } + + sum := sha256.Sum256(sortedBytes) + + return sum[:], nil +}