Skip to content

Commit

Permalink
fix: snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtiti committed May 10, 2024
1 parent 3ab6d8c commit d08a1a5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
16 changes: 7 additions & 9 deletions src/components/Voting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ export const Voting = () => {
const onSuccess = useCallback(
async (result: ISuccessResult) => {
// Get the proof data
const { merkle_root, nullifier_hash, proof } = result;
const { merkle_root: merkleRoot, nullifier_hash: nullifierHash, proof } = result;

try {
console.log('Voting proof:', result);

if (address) {
sendLog({
id: address,
proof: proof,
merkle_root: merkle_root,
nullifier_hash: nullifier_hash,
merkle_root: merkleRoot,
nullifier_hash: nullifierHash,
});
}

const proofData = formatProofData(result);
console.log(proofData);
const proofData = formatProofData({ merkleRoot, nullifierHash, proof });

//PRODUCTION
const isValid = await simulateCheckValidity(BigInt(PROPOSAL_ID), vote, proofData);

Expand Down Expand Up @@ -103,8 +101,8 @@ export const Voting = () => {
sendLog({
id: address,
proof: proof,
merkle_root: merkle_root,
nullifier_hash: nullifier_hash,
merkle_root: merkleRoot,
nullifier_hash: nullifierHash,
error: String(error),
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/tests/proofData.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { formatProofData, to32Bytes } from '~/utils/misc';
import { ISuccessResult } from '~/types';
import { ProofData } from '~/types';

describe('Proof Data Formatting', () => {
it('should correctly format proof data including handling of truncated merkle_root', () => {
const input: ISuccessResult = {
merkle_root: '0x' + '1'.repeat(63), // 31 bytes hex string, missing one character to make it 32 bytes
nullifier_hash: '0x' + '2'.repeat(64), // 32 bytes
const input: ProofData = {
merkleRoot: '0x' + '1'.repeat(63), // 31 bytes hex string, missing one character to make it 32 bytes
nullifierHash: '0x' + '2'.repeat(64), // 32 bytes
proof: '0x' + '3'.repeat(64).repeat(8), //uint256[8]
};

// Testing the merkle_root formatting to 32 bytes
const formattedMerkleRoot = to32Bytes(input.merkle_root);
const formattedMerkleRoot = to32Bytes(input.merkleRoot);
expect(formattedMerkleRoot.length).toBe(66); // 64 hex chars + '0x'

const output = formatProofData(input);
Expand Down
6 changes: 6 additions & 0 deletions src/types/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export interface ISuccessResult {
proof: string;
}

export interface ProofData {
merkleRoot: string;
nullifierHash: string;
proof: string;
}

export enum VerificationLevel {
Orb = 'orb',
Device = 'device',
Expand Down
16 changes: 8 additions & 8 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hexToBigInt, numberToHex, decodeAbiParameters, parseAbiParameters, encodePacked, Hex, Address } from 'viem';

import { ISuccessResult } from '~/types';
import { ProofData } from '~/types';

export const truncateValue = (value: string) => {
return `${value?.slice(0, 6)}...${value?.slice(-4)}`;
Expand Down Expand Up @@ -33,19 +33,19 @@ export function to32Bytes(hexString: string): string {
return bytes32;
}

export function formatProofData({ merkle_root, nullifier_hash, proof }: ISuccessResult) {
export function formatProofData({ merkleRoot, nullifierHash, proof }: ProofData) {
// Format merkle root to bytes32
const merkle_root_bytes32 = to32Bytes(merkle_root);
const merkleRootBytes32 = to32Bytes(merkleRoot);

const [decodedMerkleRoot] = decodeAbiParameters(parseAbiParameters('uint256 merkle_root'), merkleRootBytes32 as Hex);

const [decodedMerkleRoot] = decodeAbiParameters(
parseAbiParameters('uint256 merkle_root'),
merkle_root_bytes32 as Hex,
);
const [decodedNullifierHash] = decodeAbiParameters(
parseAbiParameters('uint256 nullifier_hash'),
nullifier_hash as Hex,
nullifierHash as Hex,
);

const [decodedProof] = decodeAbiParameters(parseAbiParameters('uint256[8] proof'), proof as Hex);

const proofData = encodePacked(
['uint256', 'uint256', 'uint256[8]'],
[decodedMerkleRoot, decodedNullifierHash, decodedProof],
Expand Down

0 comments on commit d08a1a5

Please sign in to comment.