Here i want to use account value in the verify_proof function in the hash_it(Account.account..as_bytes(), &mut leaf);

  Kiến thức lập trình

Here is the full code. Verify proof should use the account detail from the verify_account function. I have tried creating struct and static variable but it shows None. Please give solution to this problem. for example, user will call the function verify_account and the account has been passed then the value should be used in verify_proof.

**Note :- I do not want to pass any extra arguments any of the functions. It will change the whole purpose of the code.
**

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

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

pub struct Account {
    pub account: String,
}

const ADMIN_ACCOUNT: &str = "Cv66dQwcSJDdXNVHybchYqp73d75Y8XWj6pKcjN3ffAy";

pub fn whitelist_account(args: WhitelistArgs, admin_account: String) -> ProgramResult {
    if admin_account != ADMIN_ACCOUNT {
        return Err(ProgramError::IllegalOwner);
    }

    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 = verify_proof(whitelist_tree);
    msg!("result: {:?}", result);

    if root == result {
        msg!("Verified");
    }

    Ok(())
}

pub fn verify_proof(tree: MerkleTree) -> [u8; 32] {
    let mut leaf = [0u8; 32];
    hash_it("".as_bytes(), &mut leaf);
    let proof = tree.make_proof(leaf);
    msg!("Proof: {:?}", proof);

    tree.check_proof(proof, leaf)
}

pub fn verify_account(account: String) -> Account {
    Account {
        account
    }
}

New contributor

Aditya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT