3

I did this code But have doubt that it will not work as desired:-

use borsh::{BorshDeserialize, BorshSerialize};
use merkletreers::tree::MerkleTree;
use merkletreers::utils::hash_it;
use solana_program::{entrypoint::ProgramResult, msg};

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct WhitelistArgs {
    pub accounts: Vec<String>, 
}


pub fn whitelist_account(args: &mut WhitelistArgs) -> ProgramResult {
    let leaves: Vec<[u8; 32]> = args
        .accounts
        .iter()
        .map(|account_str| {
            let account_bytes = account_str.as_bytes();
            let mut buffer = [0u8; 32];
            hash_it(account_bytes, &mut buffer);
            buffer
        })
        .collect();

    let whitelist_tree = MerkleTree::new(leaves);


    let root = whitelist_tree.root;

    msg!("Accounts have been added to the whitelist");
    msg!("{:?}", root);

    let mut leaf = [0u8; 32];
    hash_it(args.accounts[0].as_bytes(), &mut leaf);
    let proof = whitelist_tree.make_proof(leaf);
    msg!("Proof: {:?}", proof);


    let result = whitelist_tree.check_proof(proof, leaf);
    msg!("result: {:?}", result);

    Ok(())
}
1
  • It certainly is possible, but you might need to provide some more information. you'll need to store the updated merkle tree in an account too, along with providing a function to validate a leaf
    – Jon C
    Commented May 4 at 15:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.