github.com/decred/politeia@v1.4.0/util/merkle.go (about)

     1  // Copyright (c) 2020-2021 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"crypto/sha256"
     9  	"encoding/hex"
    10  
    11  	"github.com/decred/dcrtime/merkle"
    12  )
    13  
    14  // MerkleRoot computes and returns the merkle root of the provided digests.
    15  // The digests should be hex encoded SHA256 digests.
    16  func MerkleRoot(digests []string) (*[sha256.Size]byte, error) {
    17  	sha := make([]*[sha256.Size]byte, 0, len(digests))
    18  	for _, v := range digests {
    19  		// Decode digest
    20  		d, err := hex.DecodeString(v)
    21  		if err != nil {
    22  			return nil, err
    23  		}
    24  
    25  		// Save digest
    26  		var s [sha256.Size]byte
    27  		copy(s[:], d)
    28  		sha = append(sha, &s)
    29  	}
    30  
    31  	// Calc merkle root
    32  	return merkle.Root(sha), nil
    33  }