merkle

package
v0.0.0-...-1208081 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2022 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder represents a merkle tree builder.

func NewBuilder

func NewBuilder(algorithm Hash) *Builder

NewBuilder creates a new merkle tree builder.

func (*Builder) Add

func (b *Builder) Add(key string, value []byte) *Builder

Add adds data to the builder. Whatever data is passed here will be hashed with the algorithm specified in the builder.

func (*Builder) AddBatch

func (b *Builder) AddBatch(data []*struct {
	Key   string
	Value []byte
}) *Builder

AddBatch adds a batch of items to the tree.

func (*Builder) AddRaw

func (b *Builder) AddRaw(key string, value string) *Builder

AddRaw adds data to the builder but will not hash the provided data.

func (*Builder) Build

func (b *Builder) Build() *Tree

Build constructs the tree and returns the tree struct.

func (*Builder) Description

func (b *Builder) Description(d string) *Builder

Description sets the description to the final tree.

func (*Builder) Writer

func (b *Builder) Writer(key string) *Writer

Writer returns a writer object for writing streams of bytes.

type File

type File struct {
	Algorithm string                `json:"algorithm"` // algorithm used to construct tree
	Proofs    []*anchor.AnchorProof `json:"proofs"`    // any associated tree proofs
	Data      [][]string            `json:"data"`      // the tree data
}

File is a complete representation of a merkle tree and it's related data.

type Hash

type Hash string

Hash represents the hash functions.

const (
	SHA224   Hash = "sha-224"
	SHA256   Hash = "sha-256"
	SHA384   Hash = "sha-384"
	SHA512   Hash = "sha-512"
	SHA3_224 Hash = "sha3-224"
	SHA3_256 Hash = "sha3-256"
	SHA3_384 Hash = "sha3-384"
	SHA3_512 Hash = "sha3-512"
)

func (Hash) Hash

func (h Hash) Hash() crypto.Hash

Hash returns the crypto.Hash of the given hash.

type Leaf

type Leaf struct {
	Key   string
	Value string // hex encoded hash
}

Leaf represents a single leaf in a tree.

type Path

type Path struct {
	L string `json:"l,omitempty"` // the left leaf
	R string `json:"r,omitempty"` // the right leaf
}

Path represents a merkle path. Either L or R will be populated so the final hash can be recalculated.

type Proof

type Proof interface {
	// Metadata provides information about the anchoring.
	Metadata() map[string]interface{}
	// Data is the actual receipt.
	Data() interface{}
}

func FromAnchorProof

func FromAnchorProof(p *anchor.Proof) Proof

type Tree

type Tree struct {
	// Algorithm used to perform tree hashing functions.
	Algorithm Hash
	// An array of proofs submitted for this tree.
	Proofs []*anchor.AnchorProof
	// The two-dimensional array of tree data, starting from the leaves (tree[0]) all the way
	// to the root (tree[tree.length - 1])
	Layers [][]string
}

Tree represents a single Merkle tree.

func NewTree

func NewTree(algorithm Hash, proofs []*anchor.AnchorProof, layers [][]string) *Tree

NewTree creates a new Merkle Tree.

func NewTreeFromFile

func NewTreeFromFile(path string) (*Tree, error)

NewTreeFromFile creates a new tree from an existing file.

func (*Tree) AddPathToProof

func (t *Tree) AddPathToProof(proof *anchor.AnchorProof, key string, label string) (*anchor.AnchorProof, error)

func (*Tree) AddProof

func (t *Tree) AddProof(proof *anchor.AnchorProof)

AddProof adds a proof for this tree.

func (*Tree) Export

func (t *Tree) Export(path string) error

Export exports this tree to file.

func (*Tree) GetAlgorithm

func (t *Tree) GetAlgorithm() Hash

GetAlgorithm returns the tree's hashing algorithm.

func (*Tree) GetLeaf

func (t *Tree) GetLeaf(key string) *Leaf

GetLeaf returns a single leaf matching the given key.

func (*Tree) GetLeaves

func (t *Tree) GetLeaves() []*Leaf

GetLeaves returns the leaves of the tree.

func (*Tree) GetLevel

func (t *Tree) GetLevel(level int) []string

GetLevel returns a specific level in the tree.

func (*Tree) GetLevels

func (t *Tree) GetLevels() [][]string

GetLevels returns all the levels of this tree.

func (*Tree) GetPath

func (t *Tree) GetPath(key string) []*Path

GetPath returns the path from a specific leaf all the way to the root hash. leaf must be the matching leaf value (hashed).

func (*Tree) GetRoot

func (t *Tree) GetRoot() string

GetRoot returns the root hash of this tree.

func (*Tree) NDepth

func (t *Tree) NDepth() int

CountDepth returns the depth of the tree.

func (*Tree) NLeaves

func (t *Tree) NLeaves() int

CountLeaves returns the number of leaves in this tree.

func (*Tree) NLevels

func (t *Tree) NLevels() int

CountLevels returns the number of levels in this tree.

func (*Tree) NNodes

func (t *Tree) NNodes() int

CountNodes returns the number of nodes in this tree.

func (*Tree) Verify

func (t *Tree) Verify(expected string) bool

Verify recalculates the root hash of this tree and returns the whether the calculated root hash matches the expected

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer for writing streams of data to the tree.

func (*Writer) Close

func (w *Writer) Close() *Builder

Close completes the writing and adds the hashed sum to the tree.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write implements the write method of the io.Writer interface.

Jump to

Keyboard shortcuts

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