Skip to content

supersorbet/onchain-merkle-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

solidity/On Chain Merkle Proof Generator 🔗

A zero-deps solidity utility for generating Merkle roots + proofs & testing w/ forge or directly in your contracts. Foundry-ready and optimized

@supersorbet

Start

  1. Grab package:
forge install supersorbet/onchainmerkle-generator
  1. Basic usage:
import "merkle-generator/src/MerkleGen.sol";

contract myAirdropOrSumthinLikeThat {
    using MerkleGenerator for MerkleGenerator;
    
    bytes32 public merkleRoot;
    
    function setRoot(address[] calldata users, uint256[] calldata amounts) public {
        (merkleRoot, ) = MerkleGenerator.generateClaimsRootAndProofs(users, amounts);
    }

    function claim(address user, uint256 amount, bytes32[] calldata proof) public {
        bytes32 leaf = keccak256(abi.encodePacked(
            keccak256(abi.encodePacked(user, amount)) // Double-hash!
        ));
        
        require(verifyProof(merkleRoot, proof, leaf), "Bad proof");
        // Your claim logic here
    }
}

Why?

because rewriting merkle logic for every project is tedious and node packages are a pain in the ass sometimes


Features 🛠️

  • Generate roots + proofs in one call
  • Prevents hash collisions with double-hashing
  • Gas-optimized tree construction
  • Works with any address/amount combos

Test & Get Proofs

# Run the proof generation test with verbose logs
forge test --match-test testGenerateProofs -vv
#OR
forge test -vvv


# Example output:
# MERKLE_ROOT: 0x1234...abcd
# 
# PROOFS:
# Proof for address 0xBbD9...3849f:
# 0x5678...def1
# 0x9abc...2345

CLI ✨

# Generate proofs from command line
./scripts/generate-merkle.sh \
    0xYourContractAddress \
    "[0x111...,0x222...]" \
    "[100,200...]"

Safety Stuff ⚠️

  • Encoding matters - Keep your leaf format consistent
  • Test your hashes - See verification tests
  • Gas limits - Works best for <1000 leaves

Foundry Toolkit Overview 🔨

Foundry is a blazing fast Ethereum development toolkit written in Rust. It includes:

  • Forge: Testing & deployment framework
  • Cast: CLI for contract interactions
  • Anvil: Local testnet node
  • Chisel: Solidity REPL

Basic Commands

# Build project
forge build

# Run tests
forge test

# Format code
forge fmt

# Gas snapshots
forge snapshot

# Start local node
anvil

# Deploy contracts
forge script script/Deploy.s.sol --rpc-url <RPC_URL> --private-key <PK>

Need Help?

forge --help
anvil --help
cast --help

Full docs: book.getfoundry.sh


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published