diff --git a/contract/contract.go b/contract/contract.go index bf1c372..d610b72 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/openweb3/web3go" "github.com/zero-gravity-labs/zerog-storage-client/common/blockchain" ) @@ -37,3 +38,21 @@ func (submission Submission) String() string { return fmt.Sprintf("{ Size: %v, Heights: %v }", submission.Length, heights) } + +func (submission Submission) Root() common.Hash { + numNodes := len(submission.Nodes) + + // should be never occur + if numNodes == 0 { + return common.Hash{} + } + + // calculate root in reverse order + root := submission.Nodes[numNodes-1].Root + for i := 1; i < numNodes; i++ { + left := submission.Nodes[numNodes-1-i] + root = crypto.Keccak256Hash(left.Root[:], root[:]) + } + + return root +}