gomerkle

package
v0.0.0-...-0599406 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: GPL-3.0 Imports: 3 Imported by: 0

README

Gomerkle

Build Status Coverage Status GoDoc Go Report Card

Golang Merkle tree implementation

Usage

package main

import (
    "crypto/sha256"

    "github.com/onrik/gomerkle"
)

func main() {
    data := [][]byte{
        []byte("Buzz"),
        []byte("Lenny"),
        []byte("Squeeze"),
        []byte("Wheezy"),
        []byte("Jessie"),
        []byte("Stretch"),
        []byte("Buster"),
    }
    tree := gomerkle.NewTree(sha256.New())
    tree.AddData(data...)

    err := tree.Generate()
    if err != nil {
        panic(err)
    }

    // Proof for Jessie
    proof := tree.GetProof(4)
    leaf := tree.GetLeaf(4)
    println(tree.VerifyProof(proof, tree.Root(), leaf))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MerkleProof

type MerkleProof struct {
	Proof []Proofnode
}

Proof presents merkle tree proof

type Node

type Node struct {
	Hash  []byte
	Left  *Node
	Right *Node
}

Node presents merkle tree node

func NewNode

func NewNode(h hash.Hash, block []byte) (Node, error)

NewNode creates a node given a hash function and data to hash

type Proofnode

type Proofnode struct {
	Pos  bool // true is left, false is right
	Hash []byte
}

type Tree

type Tree struct {

	// All nodes, linear
	Nodes []Node
	// Points to each level in the node. The first level contains the root node
	Levels [][]Node
	// contains filtered or unexported fields
}

Tree presents merkle tree

func NewTree

func NewTree(hasher hash.Hash) Tree

NewTree creates tree

func (*Tree) AddData

func (tree *Tree) AddData(data ...[]byte)

AddData add data to the tree

func (*Tree) AddHash

func (tree *Tree) AddHash(data ...[]byte)

AddHash add hashed data to the tree

func (*Tree) Generate

func (tree *Tree) Generate() error

Generate generates the tree nodes

func (*Tree) GetLeaf

func (tree *Tree) GetLeaf(index int) []byte

GetLeaf returns leaf hash

func (*Tree) GetNodesAtHeight

func (tree *Tree) GetNodesAtHeight(h uint64) []Node

GetNodesAtHeight returns all nodes at a given height, where height 1 returns a 1-element slice containing the root node, and a height of tree.Height() returns the leaves

func (*Tree) GetProof

func (tree *Tree) GetProof(index int) MerkleProof

GetProof generates proof

func (*Tree) Height

func (tree *Tree) Height() uint64

Height returns height of the tree

func (*Tree) Root

func (tree *Tree) Root() []byte

Root returns the root node of the tree, if available, else nil

func (*Tree) VerifyProof

func (tree *Tree) VerifyProof(proof MerkleProof, root, value []byte) bool

VerifyProof verify proof for value

Jump to

Keyboard shortcuts

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