Code Monkey home page Code Monkey logo

soroban-merkleproof's Introduction

Soroban Merkleproof

In this crate I'm aiming to create a Soroban version of the Merkleproof contract, and also provide a whitelist smart contract template later. Right now the main focus are on to make something similar than this on EVM chains: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/cryptography/MerkleProof.sol

Environment Setup

The best and easiest would be if you following this: https://soroban.stellar.org/docs/getting-started/setup

Merkle Tree Generator

At the path src/bin/merkletree_generator.rs you can find a simple example of a Markle Tree generation. The root is important we going to need that later in the whitelist contract. It's important to note the wallet_addresses_to_whitelist should have a vector of 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024...etc long so numbers power of 2 works. In order to run this file type:

cargo run --bin merkletree_generator

This output gives us a lot of results what we can utilize later on, like the Root in bytes and Proof in bytes are the 2 most important. The first is hard coded into the whitelist example, the second we have to send during the contract call.

Building the library

To build the library into Soroban smart contract use the command:

cargo build --target wasm32-unknown-unknown --release

After building can find the wasm file at:

./target/wasm32-unknown-unknown/release/soroban_merkleproof.wasm

Example whitelist

To see a simple example smart contract which utilizing the soroban_merkleproof change directory to:

cd whitelist

We can also build like the previous:

cargo build --target wasm32-unknown-unknown --release

After building can find the wasm file at:

./target/wasm32-unknown-unknown/release/whitelist.wasm

In Soroban CLI

After built both contracts we can deploy it. First we have to deploy the merkkleproof contract:

soroban deploy \
--wasm src/soroban_merkleproof.wasm \
--id a

Then we can deploy the whitelist contract also:

soroban deploy \
--wasm target/wasm32-unknown-unknown/release/whitelist.wasm \
--id b

So we can invoke the contract function to test:

soroban invoke \
        --id b \
        --account GBADIKRAYTCM5TL34XIFBY6JN5JDDU23OC6TCG2HJIYVNJMCY5WQARPI \
        --fn is_wled \
        --arg a \
        --arg [[227,20,180,34,202,220,222,70,162,168,25,24,156,109,64,246,59,199,142,119,215,6,144,242,90,128,109,189,145,169,198,18],[85,16,51,17,218,113,23,115,85,17,78,73,214,193,105,141,236,173,135,120,141,109,170,217,131,186,159,161,87,246,163,219]]

We should get true because this address is whitelisted and we sent the proof route for the contract to check with the merkletree :) Let try 2 ways to fail. First is to change the proof argument:

soroban invoke \
        --id b \
        --account GBADIKRAYTCM5TL34XIFBY6JN5JDDU23OC6TCG2HJIYVNJMCY5WQARPI \
        --fn is_wled \
        --arg a \
        --arg [[228,19,181,35,203,220,222,70,162,168,25,24,156,109,64,246,59,199,142,119,215,6,144,242,90,128,109,189,145,169,198,18],[85,16,51,17,218,113,23,115,85,17,78,73,214,193,105,141,236,173,135,120,141,109,170,217,131,186,159,161,87,246,163,219]]

We can try to change the address also:

soroban invoke \
        --id b \
        --account GDQHNBKFCO666SPX4RS62VTDY7H5W2QXHVVVQCDTADTOI3IYZGEOZL6V \
        --fn is_wled \
        --arg a \
        --arg [[227,20,180,34,202,220,222,70,162,168,25,24,156,109,64,246,59,199,142,119,215,6,144,242,90,128,109,189,145,169,198,18],[85,16,51,17,218,113,23,115,85,17,78,73,214,193,105,141,236,173,135,120,141,109,170,217,131,186,159,161,87,246,163,219]]

Both should return is false which means we are not whitelisted. The correct proof should be generated by the website the contract call coming from.

soroban-merkleproof's People

Contributors

palinko91 avatar

Watchers

 avatar

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.