gerkle

package module
v0.0.0-...-006a3fe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2022 License: MIT Imports: 4 Imported by: 0

README

Gerkle

A go library for working with merkle trees.

To build the merkle tree:

// Build a merkle tree out of raw bytes, using 0x31 as a separator
// leaves is an array of Stringers, the result of which is hashed using hasher
tree, err := gerkle.Build(hash, false, 0x31, leaves)

// Build a merkle tree out of hexidecimal strings separated by ':', using sha256
// For example, this may be more user-friendly if someone is comparing proofs
tree, err := gerkle.Build(sha256.New(), true, ':', leaves)

To enumerate the leaves

tree.EnumerateLeaves(func(t *MerkleTree) bool { return true })
tree.EnumerateWithProofs(func(t *MerkleTree, p []MerkleStep) (bool, error) { return true, nil })

To get a proof for a specific leaf, and validate that proof

steps, err := tree.FindProofFor(leaf)
if err := tree.CheckProof(steps); err != nil {
  fmt.Printf("proof invalid: %w", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MerkleStep

type MerkleStep struct {
	Type string
	Hash []byte
}

type MerkleTree

type MerkleTree struct {
	Hash   []byte
	Leaf   fmt.Stringer
	Parent *MerkleTree
	Left   *MerkleTree
	Right  *MerkleTree
	// contains filtered or unexported fields
}

func Build

func Build(hash hash.Hash, useHex bool, separator byte, leaves []fmt.Stringer) (*MerkleTree, error)

func (*MerkleTree) CheckProof

func (tree *MerkleTree) CheckProof(steps []MerkleStep) error

func (*MerkleTree) EnumerateLeaves

func (tree *MerkleTree) EnumerateLeaves(callback func(*MerkleTree) bool) bool

func (*MerkleTree) EnumerateWithProofs

func (tree *MerkleTree) EnumerateWithProofs(callback func(*MerkleTree, []MerkleStep) (bool, error)) (bool, error)

func (*MerkleTree) FindProofFor

func (tree *MerkleTree) FindProofFor(leaf fmt.Stringer) ([]MerkleStep, error)

func (*MerkleTree) Print

func (tree *MerkleTree) Print(indent int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL