Skip to content

Commit 572dc28

Browse files
fix: change current_root_index type from u32 to u8 and improve scripts
1 parent 8fb3fcd commit 572dc28

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

scripts/get_merkle_root.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const schema = new Map([
3030
['height', 'u8'],
3131
['current_index', 'u32'],
3232
['next_index', 'u32'],
33-
['current_root_index', 'u32'],
33+
['current_root_index', 'u8'], // Changed from 'u32' to 'u8' to match the Solana program
3434
['roots', [['u8', 32], 30]], // Array of 30 roots, each 32 bytes
3535
['filled_subtrees', [['u8', 32]]], // Variable length array of 32-byte arrays
3636
['nullifier_hashes', [['u8', 32]]], // Variable length array of 32-byte arrays

scripts/run_tornado_transaction.sh

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -e
44
# Colors for output
55
GREEN='\033[0;32m'
66
YELLOW='\033[1;33m'
7+
RED='\033[0;31m' # Add this line
78
NC='\033[0m' # No Color
89

910
echo -e "${GREEN}Starting Tornado Cash Privacy Solution Transaction Script${NC}"
@@ -69,7 +70,12 @@ cd ../scripts
6970
echo -e "${YELLOW}Step 3: Building and deploying the program...${NC}"
7071
cd ..
7172
echo "Building the program..."
72-
cargo build-bpf || { echo -e "${RED}Error: Failed to build the program.${NC}"; exit 1; }
73+
# Try the newer cargo build-sbf command first, fall back to cargo build-bpf if not available
74+
if command -v cargo build-sbf &> /dev/null; then
75+
cargo build-sbf || { echo -e "${RED}Error: Failed to build the program.${NC}"; exit 1; }
76+
else
77+
cargo build-bpf || { echo -e "${RED}Error: Failed to build the program.${NC}"; exit 1; }
78+
fi
7379

7480
echo "Deploying the program..."
7581
DEPLOY_OUTPUT=$(solana program deploy target/deploy/tornado_svm.so)
@@ -89,7 +95,7 @@ sed -i "s/YourProgramIdHere/$PROGRAM_ID/g" client/tornado-cli.js
8995
# Step 4: Initialize a tornado instance
9096
echo -e "${YELLOW}Step 4: Initializing tornado instance...${NC}"
9197
cd client
92-
INIT_OUTPUT=$(node tornado-cli.js initialize --keypair "$WALLET_PATH" --denomination $DENOMINATION --height $MERKLE_TREE_HEIGHT)
98+
INIT_OUTPUT=$(npx ./tornado-cli.js initialize --keypair "$WALLET_PATH" --denomination $DENOMINATION --height $MERKLE_TREE_HEIGHT)
9399
TORNADO_INSTANCE=$(echo "$INIT_OUTPUT" | grep "Tornado instance created:" | awk '{print $4}')
94100

95101
if [ -z "$TORNADO_INSTANCE" ]; then
@@ -105,7 +111,7 @@ sleep 5
105111

106112
# Step 5: Generate a commitment
107113
echo -e "${YELLOW}Step 5: Generating commitment...${NC}"
108-
COMMITMENT_OUTPUT=$(node tornado-cli.js generate-commitment)
114+
COMMITMENT_OUTPUT=$(npx ./tornado-cli.js generate-commitment)
109115
NOTE_PATH=$(echo "$COMMITMENT_OUTPUT" | grep "Note saved to" | awk '{print $4}')
110116
COMMITMENT=$(echo "$COMMITMENT_OUTPUT" | grep "Commitment:" | awk '{print $2}')
111117

@@ -120,7 +126,7 @@ echo "Commitment: $COMMITMENT"
120126

121127
# Step 6: Deposit funds
122128
echo -e "${YELLOW}Step 6: Depositing funds...${NC}"
123-
DEPOSIT_OUTPUT=$(node tornado-cli.js deposit --keypair "$WALLET_PATH" --instance "$TORNADO_INSTANCE" --commitment "$COMMITMENT" --amount $DENOMINATION)
129+
DEPOSIT_OUTPUT=$(npx ./tornado-cli.js deposit --keypair "$WALLET_PATH" --instance "$TORNADO_INSTANCE" --commitment "$COMMITMENT" --amount $DENOMINATION)
124130
DEPOSIT_SIGNATURE=$(echo "$DEPOSIT_OUTPUT" | grep "Transaction signature:" | awk '{print $3}')
125131

126132
if [ -z "$DEPOSIT_SIGNATURE" ]; then
@@ -137,13 +143,39 @@ sleep 10
137143

138144
# Step 7: Get the Merkle tree account
139145
echo -e "${YELLOW}Step 7: Getting Merkle tree account...${NC}"
140-
# Get the Merkle tree account using the seed
141-
MERKLE_TREE_SEED="merkle_tree${TORNADO_INSTANCE}0"
142-
MERKLE_TREE=$(solana address --keypair "$WALLET_PATH" --seed "$MERKLE_TREE_SEED" $PROGRAM_ID)
146+
# Get the Merkle tree account using find-program-address
147+
MERKLE_TREE=$(solana address find-program-address \
148+
--input "merkle_tree" \
149+
--input "$TORNADO_INSTANCE" \
150+
--input "0" \
151+
--program-id "$PROGRAM_ID" | head -1)
143152

144153
if [ -z "$MERKLE_TREE" ]; then
145154
echo -e "${RED}Error: Failed to get Merkle tree account.${NC}"
146-
exit 1
155+
# Try alternative method for older Solana CLI versions
156+
echo "Trying alternative method..."
157+
# In older versions, we need to use a different approach
158+
# We'll use the tornado-cli.js to get the Merkle tree account
159+
cd ../client
160+
MERKLE_TREE_OUTPUT=$(node -e "
161+
const { PublicKey } = require('@solana/web3.js');
162+
const programId = new PublicKey('$PROGRAM_ID');
163+
const tornadoInstance = new PublicKey('$TORNADO_INSTANCE');
164+
const seeds = [
165+
Buffer.from('merkle_tree', 'utf8'),
166+
tornadoInstance.toBuffer(),
167+
Buffer.from([0])
168+
];
169+
const [merkleTreePubkey] = PublicKey.findProgramAddressSync(seeds, programId);
170+
console.log(merkleTreePubkey.toString());
171+
")
172+
MERKLE_TREE=$MERKLE_TREE_OUTPUT
173+
cd ../scripts
174+
175+
if [ -z "$MERKLE_TREE" ]; then
176+
echo -e "${RED}Error: Failed to get Merkle tree account using alternative method.${NC}"
177+
exit 1
178+
fi
147179
fi
148180

149181
echo "Merkle tree account: $MERKLE_TREE"
@@ -166,7 +198,7 @@ fi
166198
echo -e "${YELLOW}Step 9: Generating proof for withdrawal...${NC}"
167199
cd ../client
168200
RECIPIENT=$(solana address)
169-
PROOF_OUTPUT=$(node tornado-cli.js generate-proof --note "$NOTE_PATH" --root "$ROOT" --recipient "$RECIPIENT")
201+
PROOF_OUTPUT=$(npx ./tornado-cli.js generate-proof --note "$NOTE_PATH" --root "$ROOT" --recipient "$RECIPIENT")
170202
PROOF=$(echo "$PROOF_OUTPUT" | grep "Proof:" | awk '{print $2}')
171203
NULLIFIER_HASH=$(echo "$PROOF_OUTPUT" | grep "Nullifier hash:" | awk '{print $3}')
172204

@@ -181,7 +213,7 @@ echo "Nullifier hash: $NULLIFIER_HASH"
181213

182214
# Step 10: Withdraw funds
183215
echo -e "${YELLOW}Step 10: Withdrawing funds...${NC}"
184-
WITHDRAW_OUTPUT=$(node tornado-cli.js withdraw --keypair "$WALLET_PATH" --instance "$TORNADO_INSTANCE" --proof "$PROOF" --root "$ROOT" --nullifier-hash "$NULLIFIER_HASH" --recipient "$RECIPIENT")
216+
WITHDRAW_OUTPUT=$(npx ./tornado-cli.js withdraw --keypair "$WALLET_PATH" --instance "$TORNADO_INSTANCE" --proof "$PROOF" --root "$ROOT" --nullifier-hash "$NULLIFIER_HASH" --recipient "$RECIPIENT")
185217
WITHDRAW_SIGNATURE=$(echo "$WITHDRAW_OUTPUT" | grep "Transaction signature:" | awk '{print $3}')
186218

187219
if [ -z "$WITHDRAW_SIGNATURE" ]; then

0 commit comments

Comments
 (0)