Categorygithub.com/MetaDataLab/go-merkletree
modulepackage
1.0.3
Repository: https://github.com/metadatalab/go-merkletree.git
Documentation: pkg.go.dev

# README

go-merkletree

Tag License GoDoc Travis CI codecov.io Go Report Card

Go implementation of a Merkle tree.

Table of Contents

Install

go-merkletree is a standard Go module which can be installed with:

go get github.com/MetaDataLab/go-merkletree

Usage

go-merkletree generates Merkle trees from an array of []byte values and uses them to generate proofs. Proofs can be verified, and graphs generated

This package uses pollards and sparese multiproofs for efficient generation of multiple proofs against the same tree; see the articles Understanding Merkle pollards and Understanding sparse Merkle multiproofs for details.

This package can generate visualisations (in DOT format) for trees and proofs. Below is a tree visualisation:

Merkle tree

and below is a proof visualisation with the value being proved in red, the intermediate branches in green and the root in blue:

Merkle proof

Example

package main

import (
	merkletree "github.com/MetaDataLab/go-merkletree"
)

// Example using the Merkle tree to generate and verify proofs.
func main() {
	// Data for the tree
	data := [][]byte{
		[]byte("Foo"),
		[]byte("Bar"),
		[]byte("Baz"),
	}

	// Create the tree
	tree, err := merkletree.NewTree(merkletree.WithData(data))
	if err != nil {
		panic(err)
	}

	// Fetch the root hash of the tree
	root := tree.Root()

	baz := data[2]
	// Generate a proof for 'Baz'
	proof, err := tree.GenerateProof(baz, 0)
	if err != nil {
		panic(err)
	}

	// Verify the proof for 'Baz'
	verified, err := merkletree.VerifyProof(baz, false, proof, [][]byte{root})
	if err != nil {
		panic(err)
	}
	if !verified {
		panic("failed to verify proof for Baz")
	}
}

Maintainers

Jim McDonald: @mcdee.

Contribute

Contributions welcome. Please check out the issues.

License

Apache-2.0 © 2019 Weald Technology Trading Ltd

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
New creates a new Merkle tree using the provided raw data and default hash type.
NewMultiProof creates a new multiproof using the provided information.
NewTree creates a new merkle tree using the provided information.
NewTreeWithLeavesHashes creates a new merkle tree using leaves hashes, generating a tree without data.
NewUsing creates a new Merkle tree using the provided raw data and supplied hash type.
VerifyMultiProof verifies multiple Merkle tree proofs for pieces of data using the default hash type.
VerifyMultiProofUsing verifies multiple Merkle tree proofs for pieces of data using the provided hash type.
VerifyPollard ensures that the branches in the pollard match up with the root using the default hash type.
VerifyPollardUsing ensures that the branches in the pollard match up with the root using the supplied hash type.
VerifyProof verifies a Merkle tree proof for a piece of data using the default hash type.
VerifyProofUsing verifies a Merkle tree proof for a piece of data using the provided hash type.
WithData sets the data for the merkle tree.
WithHashes sets the indexed hashes of values that cannot be calculated from the proof.
WithHashType sets the hash type for the merkle tree or proof.
WithIndices sets the indices that can be calculated from the proof.
WithSalt sets the salt for the merkle tree or proof.
WithSorted sets the sorted for the merkle tree.
WithValues sets the values for the merkle proof.

# Constants

# Structs

Export is the structure for exporting the MerkleTree, the hashtype needs to be specified on load.
HexFormatter shows the entire value.
MerkleTree is the structure for the Merkle tree.
MultiProof is a single structure containing multiple proofs of a Merkle tree.
Proof is a proof of a Merkle tree.
StringFormatter shows the entire value as a string.
TruncatedHexFormatter shows only the first and last two bytes of the value.

# Interfaces

Formatter formats a []byte in to a string.
HashType defines the interface that must be supplied by hash functions.
Parameter is the interface for service parameters.

# Type aliases

No description provided by the author
HashFunc is a hashing function.