Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generateMerkleProof and generateMerkleUpdate functions for dictionaries #8

Closed
wants to merge 0 commits into from

Conversation

Gusarich
Copy link

@Gusarich Gusarich commented Jul 15, 2023

Easiest way to use merkle proofs / updates from FunC is through dictionaries. I thought it will be nice to have some functionality to generate those exotic cells from this library.

I've tested these methods on some FunC code and it worked correctly. I've also added some tests that utilise exoticMerkleProof functions which check correctness too.

Here's an example on how to use it.
TS:

let d = Dictionary.empty(Dictionary.Keys.Uint(8), Dictionary.Values.Uint(32));
d.set(1, 11);
d.set(2, 22);
d.set(3, 33);

// we want to prove that there is a key `3` with corresponding value `33` in the dictionary to a smart contract that only stores the hash of the whole dictionary
const proof: Cell = d.generateMerkleProof(3);

FunC:

(slice, int) begin_parse_exotic (cell c) asm "XCTOS";

int check_proof (int merkle_root, cell merkle_proof, int index, slice expected_data) {
    (slice cs, int special?) = merkle_proof.begin_parse_exotic();

    if (~ special?) {
        return false;
    }
    if (cs~load_uint(8) != 3) { ;; Merkle proof
        return false;
    }
    if (cs~load_uint(256) != merkle_root) {
        return false;
    }

    cell dict = cs~load_ref();
    (slice data, int found?) = dict.udict_get?(8, index);

    if (~ found?) {
        return false;
    }

    return equal_slices(data, expected_data);
}

@Gusarich
Copy link
Author

I opened this PR to ton-core org at first, but looks like it was moved here.
ton-core#29

@Gusarich Gusarich marked this pull request as draft July 15, 2023 13:25
@Gusarich
Copy link
Author

I just found a bug in the merkle proof generation. Going to fix it and optimise as well.

@Gusarich Gusarich marked this pull request as ready for review September 28, 2023 08:56
@Gusarich
Copy link
Author

@dvlkv I fixed the generation of Merkle proofs! Also made the code more straight-forward.

I have also additionally tested it in my Airdrop project and it works as intended:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants