GithubHelp home page GithubHelp logo

oa / merkletree Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 12 KB

Lightweight library that lets you create Merkle trees with custom branching factors.

License: MIT License

V 100.00%
merkletree vlang

merkletree's Introduction

merkletree

Lightweight library that lets you create Merkle trees with custom branching factors.

The library supports all hashing algorithms native to V out of the box.

You can also use custom hashing algorithms that are implemented according to the interface.

pub type HashFunction = fn (data []u8) []u8

๐Ÿ’ก Interface

pub struct MerkleTree {
	branching_factor int = 2
	hash_function    HashFunction [required]
mut:
	root Node
}

build

Builds Merkle tree structure and pre-computes hashes with given data blocks.

pub fn (mut m MerkleTree) build(blocks [][]u8)

get_root

Returns Merkle root of tree as byte array.

pub fn (mut m MerkleTree) get_root() []u8

HashFunction

In case you want to implement a custom hashing algorithm, please do so according to this blueprint.

pub type HashFunction = fn (data []u8) []u8

๐Ÿš€ Simple example

import merkletree { MerkleTree }
import crypto.sha256

fn main() {
	mut tree := &MerkleTree{
		hash_function: sha256.sum
	}

	tree.build([
		'1'.bytes(),
		'2'.bytes(),
		'3'.bytes(),
		'4'.bytes(),
	])

	print(tree.get_root().hex())
}

Feel like contributing?

Create an issue or a pull request.

License

This project is licensed under the MIT license. Feel free to do whatever you want with the code!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.