Skip to content

Commit

Permalink
chore: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jovijovi committed Dec 15, 2021
1 parent 685d8d6 commit 7e82142
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ import (
"golang.org/x/crypto/sha3"
)

// HashProvider hash provider
type HashProvider func() hash.Hash

// IHashFunc hash interface
type IHashFunc interface {
Hash(msg []byte) ([]byte, error)
}

// ITree tree interface
type ITree interface {
BuildTree(opt ...OptionFunc) error
}

// HashFunc hash function
type HashFunc struct {
Provider HashProvider
}

// Hash returns message digest
func (h *HashFunc) Hash(msg []byte) ([]byte, error) {
provider := h.Provider()
if _, err := provider.Write(msg); err != nil {
Expand All @@ -33,16 +38,20 @@ func (h *HashFunc) Hash(msg []byte) ([]byte, error) {
return provider.Sum(nil), nil
}

// DefaultHashFunc returns default hash interface
func DefaultHashFunc() IHashFunc {
hashFunc := new(HashFunc)
hashFunc.Provider = sha3.NewLegacyKeccak256
return hashFunc
}

// Hash node hash
type Hash = []byte

// Tree merkle tree
type Tree [][]Hash

// Node merkle tree node
type Node struct {
Height int
Hash []byte
Expand All @@ -51,10 +60,13 @@ type Node struct {
Payload []byte
}

// Leaf merkle tree leaf
type Leaf = Node

// Root merkle tree root
type Root = Node

// NewLeaf returns a new leaf
func NewLeaf() Leaf {
return Leaf{
Height: 0,
Expand Down Expand Up @@ -82,6 +94,7 @@ func (node *Root) Marshal() ([]byte, error) {
return json.Marshal(node)
}

// Leaves merkle tree leaves
type Leaves []Leaf

// Length returns length of leaves
Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
)

// Hex returns a hex string
func Hex(b []byte) string {
return fmt.Sprintf("%x", b)
}

0 comments on commit 7e82142

Please sign in to comment.