Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Private DAO Voting with NEAR OutLayer

Anonymous, verifiable voting for NEAR DAOs using cryptography and trusted execution environments.

This example demonstrates how to build a cryptographically secure anonymous voting system that showcases NEAR OutLayer's capability to execute heavy cryptographic operations off-chain while maintaining on-chain verifiability.


🎯 What This Demonstrates

OutLayer Capabilities Used

  1. Heavy Cryptographic Operations Off-Chain

    • Key derivation (HKDF-SHA256) - impossible to do on-chain due to gas limits
    • ECIES encryption/decryption - would cost 100+ NEAR for 100 votes on-chain
    • Vote tallying with privacy guarantees - O(N) complexity infeasible in smart contract
  2. TEE Integration for Secret Management

    • Master secret accessed only in trusted environment (never exposed to workers or contract)
    • Private keys derived on-demand (never stored anywhere)
    • Individual votes decrypted in memory, never logged or persisted
  3. Secrets Management via Keymaster

    • Master secret stored encrypted in OutLayer contract
    • Automatic injection into WASI environment variables
    • Single secret enables unlimited user keys (deterministic derivation)
  4. Merkle Proofs for Vote Verification

    • Binary merkle tree built from vote hashes
    • Each voter gets inclusion proof
    • Frontend can verify vote was counted without revealing content

Why This Cannot Be Done Without OutLayer

On-chain limitations:

  • ❌ Gas costs: Deriving 100 user keys would cost ~300 NEAR (vs <0.1 NEAR with OutLayer)
  • ❌ Privacy: Smart contracts are public - votes would be visible to everyone
  • ❌ Computational limits: NEAR has 300 TGas limit - ECIES decryption exceeds this for >10 votes
  • ❌ Secret storage: Cannot store master secret on-chain (even encrypted, it's readable)

With OutLayer:

  • ✅ Affordable: 100 votes tallied for ~0.01 NEAR (1000x cheaper)
  • ✅ Private: Votes decrypted in TEE, only aggregate results published
  • ✅ Scalable: Can process 10,000+ votes in <5 seconds
  • ✅ Secure: Master secret never leaves TEE, access controlled by keymaster

Cryptographic Security

  • Vote Privacy: ECIES encryption with per-user secp256k1 keys
  • No Double Voting: Last vote per user counts (timestamp-based)
  • Verifiable Results: Merkle tree proofs + TEE attestation
  • Plausible Deniability: Users can send dummy encrypted messages

📋 Architecture Overview

┌─────────────────────────────────────────────────────────────────────┐
│                         PRIVATE DAO VOTING                          │
└─────────────────────────────────────────────────────────────────────┘

Phase 1: User Joins DAO
═══════════════════════

Client                    DAO Contract              OutLayer (TEE)
  │                            │                          │
  │ 1. join_dao()              │                          │
  │ ──────────────────────────>│                          │
  │                            │                          │
  │                            │ 2. Request key derivation│
  │                            │ ────────────────────────>│
  │                            │    input: {              │
  │                            │      action: derive_key  │
  │                            │      user: alice.near    │
  │                            │    }                     │
  │                            │    secrets: {            │
  │                            │      PROTECTED_DAO_MASTER_SECRET   │
  │                            │    }                     │
  │                            │                          │
  │                            │                          │ 3. HKDF-SHA256
  │                            │                          │    derive user key
  │                            │                          │    from master secret
  │                            │                          │
  │                            │ 4. Return pubkey         │
  │                            │ <────────────────────────│
  │                            │    {pubkey: "02abc..."}  │
  │                            │                          │
  │  5. Store pubkey           │                          │
  │ <──────────────────────────│                          │
  │     user_pubkeys[alice]    │                          │


Phase 2: Vote on Proposal
═══════════════════════════

Client                    DAO Contract
  │                            │
  │ 1. Get user pubkey         │
  │ ──────────────────────────>│
  │ <──────────────────────────│
  │                            │
  │ 2. Encrypt vote locally    │
  │    vote = "yes"            │
  │    encrypted = ECIES(      │
  │      vote, user_pubkey     │
  │    )                       │
  │                            │
  │ 3. cast_vote()             │
  │ ──────────────────────────>│
  │    {                       │
  │      proposal_id: 1,       │ 4. Store encrypted vote │
  │      encrypted: "02f1a..." │    votes[1].push(...)   │
  │    }                       │    Return timestamp     │
  │                            │                          │
  │  5. Receive timestamp      │
  │ <──────────────────────────│
  │                            │
  │ 6. Compute vote hash       │
  │    hash = SHA256(          │
  │      user + timestamp +    │
  │      encrypted             │
  │    )                       │
  │    Display to user         │


Phase 3: Finalize Proposal
════════════════════════════

Anyone                   DAO Contract              OutLayer (TEE)
  │                            │                          │
  │ 1. finalize_proposal()     │                          │
  │ ──────────────────────────>│                          │
  │                            │                          │
  │                            │ 2. Request tally         │
  │                            │ ────────────────────────>│
  │                            │    input: {              │
  │                            │      action: tally_votes │
  │                            │      proposal_id: 1      │
  │                            │      votes: [...]        │
  │                            │      quorum: {           │
  │                            │        Absolute: {       │
  │                            │          min_votes: 10   │
  │                            │        }                 │
  │                            │      }                   │
  │                            │    }                     │
  │                            │    secrets: {            │
  │                            │      PROTECTED_DAO_MASTER_SECRET   │
  │                            │    }                     │
  │                            │                          │
  │                            │                          │ 3. For each vote:
  │                            │                          │    - Derive user privkey
  │                            │                          │    - Decrypt vote (ECIES)
  │                            │                          │    - Check "yes"/"no"/"dummy"
  │                            │                          │    - Track last vote per user
  │                            │                          │
  │                            │                          │ 4. Count votes:
  │                            │                          │    yes_count = 7
  │                            │                          │    no_count = 3
  │                            │                          │
  │                            │                          │ 5. Build merkle tree:
  │                            │                          │    - Hash each vote
  │                            │                          │    - Build binary tree
  │                            │                          │    - Generate proofs
  │                            │                          │
  │                            │ 6. Return result         │
  │                            │ <────────────────────────│
  │                            │    {                     │
  │                            │      yes_count: 7,       │
  │                            │      no_count: 3,        │
  │                            │      total_votes: 10,    │
  │                            │      merkle_root: "...", │
  │                            │      merkle_proofs: [..],│
  │                            │      tee_attestation     │
  │                            │    }                     │
  │                            │                          │
  │  7. Update proposal        │                          │
  │     status: Passed         │
  │     Store tally result     │
  │                            │


Phase 4: Verify Vote
══════════════════════

Voter                    DAO Contract
  │                            │
  │ 1. get_vote_proofs()       │
  │ ──────────────────────────>│
  │ <──────────────────────────│
  │    {                       │
  │      vote_hash: "abc...",  │
  │      proof_path: [         │
  │        "sibling1",         │
  │        "sibling2"          │
  │      ]                     │
  │    }                       │
  │                            │
  │ 2. Verify locally:         │
  │    hash = vote_hash        │
  │    for sibling in path:    │
  │      hash = SHA256(        │
  │        hash + sibling      │
  │      )                     │
  │    assert hash == root ✓   │

🔐 Cryptographic Primitives

1. Key Derivation (HKDF-SHA256)

Each user gets a unique secp256k1 keypair derived deterministically from the master secret:

// In TEE (OutLayer worker)
let master_secret = std::env::var("PROTECTED_DAO_MASTER_SECRET")?;
let info = format!("user:{}:{}", dao_account, user_account);

let user_privkey = HKDF-SHA256(
    ikm: hex::decode(master_secret),
    salt: None,
    info: info.as_bytes()
);

let user_pubkey = secp256k1::derive_public_key(user_privkey);

Properties:

  • ✅ Deterministic: Same inputs always produce same key
  • ✅ Isolated: Different users get cryptographically independent keys
  • ✅ One-way: Cannot reverse pubkey → master_secret
  • ✅ Efficient: O(1) computation, keys derived on-demand (never stored)

Example:

Master secret:  a1b2c3d4e5f6...
DAO:            privatedao.testnet
User:           alice.testnet

→ alice privkey: 7c8f9e1a2b3c... (32 bytes)
→ alice pubkey:  02f1a2b3c4d5... (33 bytes compressed)

2. ECIES Encryption

Votes are encrypted using ECIES (Elliptic Curve Integrated Encryption Scheme):

Encryption (client-side, JavaScript):

import { encrypt } from 'eciesjs';

const vote = "yes";  // or "no" or "DUMMY_12345" for noise
const userPubkey = "02f1a2b3c4d5...";  // from DAO contract

const encrypted = encrypt(
  Buffer.from(userPubkey, 'hex'),
  Buffer.from(vote, 'utf-8')
);

// Result: ephemeral_pubkey || tag || ciphertext
// Size: 33 + 32 + len(vote) bytes ≈ 68 bytes for "yes"

Decryption (TEE, Rust):

// Derive user's private key from master secret
let user_privkey = derive_key(master_secret, dao, user);

// Decrypt ECIES ciphertext
let plaintext = ecies::decrypt(
    user_privkey.as_bytes(),
    encrypted_bytes
)?;

// Filter real votes from dummies
match plaintext.as_str() {
    "yes" => yes_count += 1,
    "no" => no_count += 1,
    _ => {} // ignore dummy
}

Security properties:

  • ✅ IND-CCA2 secure: Indistinguishable under chosen-ciphertext attack
  • ✅ Forward secrecy: Ephemeral keys used, old messages safe if key compromised
  • ✅ Authenticated: HMAC prevents tampering
  • ✅ Non-deterministic: Same vote encrypts differently each time (random ephemeral key)

3. Merkle Tree Proofs

After tallying, a binary merkle tree is built for vote verification:

Building the tree (in TEE):

// Step 1: Hash each vote (leaf nodes)
for vote in votes {
    let mut hasher = Sha256::new();
    hasher.update(vote.user.as_bytes());
    hasher.update(&vote.timestamp.to_le_bytes());  // 8 bytes, little-endian
    hasher.update(vote.encrypted_vote.as_bytes());
    let vote_hash = hex::encode(hasher.finalize());
    leaf_hashes.push(vote_hash);
}

// Step 2: Build tree bottom-up
while level.len() > 1 {
    for i in (0..level.len()).step_by(2) {
        let left = level[i];
        let right = level.get(i+1).unwrap_or(left);  // duplicate if odd

        let parent = SHA256(left + right);  // fixed order: left, right
        next_level.push(parent);
    }
    level = next_level;
}

merkle_root = level[0];

// Step 3: Generate proof for each vote
// Proof = list of sibling hashes from leaf to root

Verifying a proof (frontend, JavaScript):

async function verifyProof(voteHash, proofPath, merkleRoot) {
  // Try all possible orderings (2^depth paths)
  // Because we don't encode left/right position in proof

  async function tryAllPaths(hash, remainingPath) {
    if (remainingPath.length === 0) {
      return hash === merkleRoot;  // reached root?
    }

    const [sibling, ...rest] = remainingPath;

    // Try hash on left
    const leftFirst = await sha256(hash + sibling);
    if (await tryAllPaths(leftFirst, rest)) return true;

    // Try hash on right
    const rightFirst = await sha256(sibling + hash);
    if (await tryAllPaths(rightFirst, rest)) return true;

    return false;
  }

  return await tryAllPaths(voteHash, proofPath);
}

Why this design:

  • ✅ Simple proof format: Just array of sibling hashes (no left/right flags needed)
  • ✅ Flexible verification: Frontend tries all possible paths (O(2^depth) but depth is small)
  • ✅ TEE authoritative: TEE builds tree with fixed order, frontend adapts to match
  • ⚠️ Trade-off: Slightly more frontend compute, but simpler proof structure

Example tree for 3 votes:

        root
       /    \
     h01    h22
    /  \   /  \
   h0  h1 h2  h2  (h2 duplicated because odd count)

Proof for h0: [h1, h22]
Proof for h1: [h0, h22]
Proof for h2: [h2, h01]

4. Vote Hash Computation

Critical implementation detail: Vote hash must be computed identically on client and TEE.

Client (TypeScript):

// After casting vote, contract returns timestamp (u64)
const timestamp: string = result.timestamp;  // KEEP AS STRING!

// Convert to BigInt to preserve full precision (JavaScript Number loses precision on u64)
const timestampBigInt = BigInt(timestamp);

// Convert to 8-byte little-endian (matches Rust to_le_bytes())
const timestampBuffer = new ArrayBuffer(8);
const timestampView = new DataView(timestampBuffer);
timestampView.setBigUint64(0, timestampBigInt, true);  // true = little-endian

// Concatenate: user_bytes + timestamp_bytes + encrypted_bytes
const userBytes = new TextEncoder().encode(accountId);
const timestampBytes = new Uint8Array(timestampBuffer);
const encryptedBytes = new TextEncoder().encode(encrypted);

const combined = new Uint8Array(
  userBytes.length + timestampBytes.length + encryptedBytes.length
);
combined.set(userBytes, 0);
combined.set(timestampBytes, userBytes.length);
combined.set(encryptedBytes, userBytes.length + timestampBytes.length);

// SHA-256 hash
const hashBytes = await crypto.subtle.digest('SHA-256', combined);
const voteHash = Array.from(new Uint8Array(hashBytes))
  .map(b => b.toString(16).padStart(2, '0'))
  .join('');

TEE (Rust):

use sha2::{Digest, Sha256};

let mut hasher = Sha256::new();
hasher.update(vote.user.as_bytes());           // UTF-8 bytes
hasher.update(&vote.timestamp.to_le_bytes());  // 8 bytes little-endian
hasher.update(vote.encrypted_vote.as_bytes()); // hex string as bytes
let vote_hash = hex::encode(hasher.finalize());

Common pitfalls (FIXED in current implementation):

  • ❌ Using JSON.parse() on timestamp → loses precision on u64 values
  • ❌ Using parseInt() → loses last digits due to JavaScript Number limits
  • ❌ Concatenating timestamp as string instead of binary bytes
  • ❌ Wrong endianness (big-endian vs little-endian)

Correct approach:

  • ✅ Contract returns timestamp as u64
  • ✅ Client parses as string (no precision loss)
  • ✅ Convert to BigInt then to 8-byte little-endian buffer
  • ✅ Hash matches TEE exactly

🚀 Quick Start

Prerequisites

# Rust toolchain with WASI target
rustup target add wasm32-wasip1

# NEAR CLI
cargo install near-cli-rs

# Node.js for frontend
node --version  # v18+ recommended

Build WASI Module

cd private-dao-example

# Build for WASI Preview 1
RUSTFLAGS="--cfg wasmedge --cfg tokio_unstable" \
  cargo build --target wasm32-wasip1 --release

# Output: target/wasm32-wasip1/release/private-dao-example.wasm (~280 KB)

# Publish it and deploy as a project (as privatedao.testnet)
outlayer upload target/wasm32-wasip1/release/private-dao-example.wasm
outlayer deploy private-dao-example <FastFS URL printed by upload>

Build DAO Contract

cd dao-contract

# Build contract
cargo build --target wasm32-unknown-unknown --release

# Output: target/wasm32-unknown-unknown/release/private_dao_contract.wasm

Deploy Contract

# Create account for DAO
near account create-account fund-myself privatedao.testnet \
  '1 NEAR' autogenerate-new-keypair save-to-keychain \
  network-config testnet create

# Deploy and initialize
near contract deploy privatedao.testnet \
  use-file dao-contract/target/wasm32-unknown-unknown/release/private_dao_contract.wasm \
  with-init-call new json-args '{
    "name": "Private DAO Example",
    "owner": "privatedao.testnet",
    "membership_mode": "Public"
  }' \
  prepaid-gas '100 Tgas' \
  attached-deposit '0 NEAR' \
  network-config testnet sign-with-keychain send

Setup Master Secret

The master secret must be generated inside the TEE, not by you. The PROTECTED_ prefix is reserved for exactly this: the keystore generates the value, encrypts it, and injects it into the module at execution time. Nobody — including you — ever sees the plaintext, which is what makes the "the DAO operator cannot decrypt votes" claim true rather than a promise. The keystore rejects manually supplied secrets whose name carries the PROTECTED_ prefix, so there is no way to do this by hand even if you wanted to.

Via the dashboard: open /secrets, connect the DAO wallet, use Generate Secrets with project privatedao.testnet/private-dao-example, profile production, secret name PROTECTED_DAO_MASTER_SECRET and generation type Hex 32 bytes. You get back the key name for verification and nothing else.

The dashboard then stores the encrypted blob on-chain for you. The equivalent contract call, if you are scripting it, is:

near call outlayer.testnet store_secrets '{
  "accessor": { "Project": { "project_id": "privatedao.testnet/private-dao-example" } },
  "profile": "production",
  "encrypted_secrets_base64": "<blob returned by the keystore>",
  "access": "AllowAll",
  "vault_id": null
}' --accountId privatedao.testnet --deposit 0.1

vault_id must be present even when null — near-sdk rejects JSON that omits a required Option field.

CRITICAL: The WASI module reads std::env::var("PROTECTED_DAO_MASTER_SECRET") - must be exactly this name!

PROTECTED_ secrets are immutable: the keystore refuses to regenerate one that already exists, because rotating it would silently invalidate every key derived from it and every vote encrypted to those keys. If you move the module to a different repository, you generate a new secret against the new accessor — the old one does not follow it.

Run Frontend

cd dao-frontend

# Install dependencies
npm install

# Start development server
npm start

# Build for production
npm run build

Open http://localhost:3000


📖 Complete Usage Flow

1. Create DAO

near call privatedao.testnet new '{
  "name": "Climate Action DAO",
  "owner": "admin.testnet",
  "membership_mode": "Public"
}' --accountId admin.testnet

2. Join DAO

User calls:

near call privatedao.testnet join_dao '{}' \
  --accountId alice.testnet \
  --deposit 0.01

Contract internally:

  1. Adds alice to members
  2. Calls OutLayer to derive alice's pubkey:
    {
      "action": "derive_pubkey",
      "dao_account": "privatedao.testnet",
      "user_account": "alice.testnet"
    }
  3. OutLayer returns: {"pubkey": "02f1a2b3..."}
  4. Contract stores: user_pubkeys.insert("alice.testnet", "02f1a2b3...")

3. Create Proposal

near call privatedao.testnet create_proposal '{
  "title": "Fund Solar Panel Installation",
  "description": "Allocate 1000 NEAR to solar panels in community center",
  "deadline_seconds": 604800,
  "quorum": {
    "Absolute": {
      "min_votes": 10
    }
  }
}' --accountId alice.testnet --deposit 0.001

Quorum types:

  • Absolute { min_votes: N } - Requires at least N votes total

4. Cast Vote

Frontend flow:

// 1. Fetch user's public key from contract
const pubkey = await contract.get_user_pubkey({
  user: accountId
});

// 2. Encrypt vote using ECIES
import { encrypt } from 'eciesjs';
const vote = "yes";  // or "no"
const encrypted = encrypt(
  Buffer.from(pubkey, 'hex'),
  Buffer.from(vote, 'utf-8')
).toString('hex');

// 3. Submit to contract
const result = await wallet.signAndSendTransaction({
  receiverId: contractId,
  actions: [{
    type: 'FunctionCall',
    params: {
      methodName: 'cast_vote',
      args: {
        proposal_id: 1,
        encrypted_vote: encrypted
      },
      gas: '30000000000000',
      deposit: '2000000000000000000000' // 0.002 NEAR
    }
  }]
});

// 4. Extract timestamp from transaction result
const successValue = result.receipts_outcome[0].outcome.status.SuccessValue;
const timestamp = atob(successValue).trim();  // Keep as string!

// 5. Compute vote hash (to verify later)
const timestampBigInt = BigInt(timestamp);
const timestampBuffer = new ArrayBuffer(8);
const timestampView = new DataView(timestampBuffer);
timestampView.setBigUint64(0, timestampBigInt, true);

const userBytes = new TextEncoder().encode(accountId);
const timestampBytes = new Uint8Array(timestampBuffer);
const encryptedBytes = new TextEncoder().encode(encrypted);

const combined = new Uint8Array(
  userBytes.length + timestampBytes.length + encryptedBytes.length
);
combined.set(userBytes, 0);
combined.set(timestampBytes, userBytes.length);
combined.set(encryptedBytes, userBytes.length + timestampBytes.length);

const hashBytes = await crypto.subtle.digest('SHA-256', combined);
const voteHash = Array.from(new Uint8Array(hashBytes))
  .map(b => b.toString(16).padStart(2, '0'))
  .join('');

console.log('Your vote hash:', voteHash);
// User saves this to verify their vote was counted later

5. Send Dummy Messages (Optional)

For plausible deniability:

// Real vote
await castVote(proposalId, "yes");

// Dummy messages (add noise)
await castVote(proposalId, "DUMMY_random_12345");
await castVote(proposalId, "");
await castVote(proposalId, crypto.randomBytes(16).toString('hex'));

// On-chain: 4 encrypted messages visible
// In TEE: Only "yes" is counted, others ignored

Cost: ~0.002 NEAR per message Benefit: Observers can't tell which message contains real vote

6. Finalize Proposal

After deadline, anyone can trigger finalization:

near call privatedao.testnet finalize_proposal '{
  "proposal_id": 1
}' --accountId anyone.testnet --deposit 0.05

What happens:

  1. Contract checks deadline passed
  2. Contract fetches all votes for proposal
  3. Contract calls OutLayer:
    {
      "action": "tally_votes",
      "dao_account": "privatedao.testnet",
      "proposal_id": 1,
      "votes": [
        {"user": "alice.testnet", "encrypted_vote": "...", "timestamp": 123},
        {"user": "bob.testnet", "encrypted_vote": "...", "timestamp": 124},
        ...
      ],
      "quorum": {"Absolute": {"min_votes": 10}}
    }
  4. OutLayer worker:
    • Gets PROTECTED_DAO_MASTER_SECRET from keymaster
    • Derives each user's privkey
    • Decrypts each vote using ECIES
    • Filters "yes"/"no" (ignores dummies)
    • Tracks last vote per user (allows revoting)
    • Counts yes/no votes
    • Checks quorum
    • Builds merkle tree with vote hashes
    • Generates proof for each vote
  5. Returns result:
    {
      "proposal_id": 1,
      "yes_count": 12,
      "no_count": 3,
      "total_votes": 15,
      "tee_attestation": "mvp-attestation:abc123...",
      "votes_merkle_root": "e5f6g7h8...",
      "merkle_proofs": [
        {
          "voter": "alice.testnet",
          "vote_index": 0,
          "vote_hash": "abc...",
          "proof_path": ["def...", "ghi..."],
          "timestamp": 123
        },
        ...
      ]
    }
  6. Contract stores tally result and updates proposal status

7. Verify Your Vote

Frontend verification:

// 1. Get your merkle proof from contract
const proofs = await contract.get_vote_proofs({
  proposal_id: 1,
  account_id: accountId
});

// 2. Find your vote (you saved voteHash earlier)
const myProof = proofs.find(p => p.voter === accountId);

// 3. Verify proof
const isValid = await verifyProof(
  myProof.vote_hash,
  myProof.proof_path,
  proposal.tally_result.votes_merkle_root
);

if (isValid) {
  console.log('✓ Your vote was included in the tally!');
} else {
  console.error('✗ Proof verification failed - possible tampering!');
}

The verification proves:

  • ✓ Your encrypted vote was included in the merkle tree
  • ✓ The merkle root matches what contract stored
  • ✓ TEE signed this root with attestation

It does NOT reveal:

  • ✗ What you voted (yes/no)
  • ✗ What others voted
  • ✗ Which votes were dummies

🔍 Privacy Features

Multiple Votes (Last One Counts)

Users can change their vote before deadline:

// Monday
await castVote(1, "yes");  // timestamp: 100

// Wednesday (changed mind)
await castVote(1, "no");   // timestamp: 200

// Friday (final decision)
await castVote(1, "yes");  // timestamp: 300

// Result after tallying: "yes" is counted (timestamp 300 is latest)

Implementation:

// In TEE
let mut user_votes: HashMap<String, (String, u64)> = HashMap::new();

for vote in all_votes {
    let decrypted = decrypt_vote(&vote);

    if decrypted == "yes" || decrypted == "no" {
        if let Some((_, existing_ts)) = user_votes.get(&vote.user) {
            if vote.timestamp > *existing_ts {
                // This vote is newer, update
                user_votes.insert(vote.user, (decrypted, vote.timestamp));
            }
        } else {
            // First vote from this user
            user_votes.insert(vote.user, (decrypted, vote.timestamp));
        }
    }
}

Dummy Messages

Users can send encrypted noise to hide voting patterns:

// Strategy 1: Random text
await castVote(proposalId, crypto.randomBytes(32).toString('hex'));

// Strategy 2: Empty string
await castVote(proposalId, "");

// Strategy 3: Predictable dummy prefix
await castVote(proposalId, "DUMMY_" + Date.now());

// Real vote mixed in
await castVote(proposalId, "yes");

Why this works:

  • All messages look identical on-chain (encrypted blobs)
  • Only TEE can decrypt and see "yes"/"no" vs dummy
  • Observer sees N transactions, can't tell which is real
  • Even timing analysis doesn't help (user controls timing)

Cost-privacy trade-off:

  • 1 message: ~0.002 NEAR, no privacy
  • 5 messages: ~0.01 NEAR, good privacy (20% chance each is real)
  • 20 messages: ~0.04 NEAR, excellent privacy (5% chance each)

Retroactive Voting Prevention

Members can only vote on proposals created AFTER they joined:

// In contract
pub fn cast_vote(&mut self, proposal_id: u64, encrypted_vote: String) {
    let voter = env::predecessor_account_id();
    let member_info = self.members.get(&voter).expect("Only members can vote");
    let proposal = self.proposals.get(&proposal_id).expect("Proposal not found");

    // Check member joined BEFORE proposal was created
    assert!(
        member_info.joined_at < proposal.created_at,
        "Cannot vote on proposals created before you joined"
    );

    // ... store vote
}

Why this matters:

  • Prevents vote buying: Can't join just to vote, then leave
  • Prevents Sybil attacks: Can't create accounts after seeing proposal
  • Ensures fair participation: Only active members vote

🧪 Testing

Unit Tests

cd private-dao-example
cargo test

# Output:
# running 6 tests
# test crypto::tests::test_derive_key_deterministic ... ok
# test crypto::tests::test_derive_key_different_users ... ok
# test crypto::tests::test_encrypt_decrypt ... ok
# test crypto::tests::test_decrypt_wrong_user_fails ... ok
# test tally::tests::test_merkle_tree_single_vote ... ok
# test tally::tests::test_merkle_tree_multiple_votes ... ok

Integration Tests

Test full voting cycle:

./test_full_cycle.sh

# Output:
# ✅ Building WASM...
# ✅ Deriving keys for alice, bob, carol...
# ✅ Encrypting votes...
# ✅ Tallying votes...
# 📊 Results:
#    YES: 2 votes (alice, bob)
#    NO: 1 vote (carol)
#    Dummies ignored: 1 (dave)
# ✅ All tests passed!

Frontend Tests

cd dao-frontend
npm test

📊 Performance

Gas Costs

On-chain (contract calls):

  • Join DAO: ~5 TGas + 0.01 NEAR (OutLayer execution)
  • Create proposal: ~2 TGas
  • Cast vote: ~3 TGas + 0.002 NEAR storage
  • Finalize: ~10 TGas + 0.05 NEAR (OutLayer execution)

OutLayer execution:

  • Derive key: ~1M instructions (~0.01 NEAR)
  • Tally 100 votes: ~50M instructions (~0.05 NEAR)

Comparison with on-chain tallying:

  • 100 votes on-chain: ~30,000 TGas = ~300 NEAR (if it were possible)
  • 100 votes OutLayer: ~0.05 NEAR (6000x cheaper!)

Computation Time

Measured on M1 MacBook Pro:

Operation Time Notes
Derive pubkey ~1 ms HKDF + secp256k1 point
Encrypt vote (client) ~5 ms ECIES with random ephemeral key
Decrypt vote (TEE) ~2 ms ECIES decrypt
Tally 100 votes ~300 ms Decrypt all + count
Build merkle tree (100 votes) ~50 ms SHA-256 hashing
Verify merkle proof ~10 ms O(depth) = O(log N)

Storage Costs

Item Size Cost (NEAR)
Member pubkey 33 bytes ~0.0003
Encrypted vote ~200 bytes ~0.002
Proposal ~500 bytes ~0.005
Tally result ~1 KB ~0.01

Example DAO:

  • 100 members: 0.03 NEAR
  • 10 proposals: 0.05 NEAR
  • 500 total votes: 1.0 NEAR
  • Total: ~1.1 NEAR for full lifecycle

🏗️ Project Structure

private-dao-example/
├── src/
│   ├── main.rs           # Entry point, handles actions
│   ├── crypto.rs         # HKDF + ECIES encryption
│   └── tally.rs          # Vote tallying + merkle tree
├── dao-contract/
│   ├── src/
│   │   ├── lib.rs        # Main contract logic
│   │   ├── types.rs      # Data structures
│   │   ├── execution.rs  # Proposal + voting
│   │   ├── events.rs     # NEAR events
│   │   ├── views.rs      # View methods
│   │   └── admin.rs      # Owner functions
│   └── Cargo.toml
├── dao-frontend/
│   ├── src/
│   │   ├── App.tsx                      # Main app
│   │   ├── components/
│   │   │   ├── JoinDAO.tsx             # Join interface
│   │   │   ├── CreateProposal.tsx      # Create proposals
│   │   │   ├── VoteOnProposal.tsx      # Voting UI (vote hash computation)
│   │   │   ├── ProposalList.tsx        # List all proposals
│   │   │   └── VoteProofs.tsx          # Merkle proof verification
│   │   ├── types.ts                     # TypeScript types
│   │   └── near-wallet.ts               # Wallet integration
│   └── package.json
├── tests/
│   ├── test_full_cycle.sh    # End-to-end test
│   └── encrypt_test_vote.py  # Helper script
├── Cargo.toml
├── build.sh
└── README.md

📚 References

Cryptography:

Merkle Trees:

Privacy:

NEAR:

OutLayer:


🤝 Contributing

Contributions welcome! Areas to improve:

  1. Frontend

    • Better UX for vote verification
    • Batch voting UI
    • Mobile support
  2. Cryptography

    • ZK proof integration (Semaphore)
    • Weighted voting algorithms
    • Quadratic voting math
  3. Contract

    • Proposal templates
    • Voting power calculation
    • Delegation logic
  4. Testing

    • More integration tests
    • Load testing (1000+ votes)
    • Security audits

📄 License

MIT OR Apache-2.0, at your option — see LICENSE-MIT and LICENSE-APACHE.


🙏 Acknowledgments

  • NEAR Protocol - Blockchain platform
  • OutLayer Team - Off-chain computation infrastructure
  • Privacy & Scaling Explorations - ZK research

Built with ❤️ using NEAR OutLayer

This example showcases the power of combining:

  • ✅ Trusted Execution Environments (TEE) - Secure computation
  • ✅ Heavy Cryptography Off-Chain - ECIES, HKDF, merkle trees
  • ✅ On-Chain Verification - Merkle proofs + attestations
  • ✅ Affordable Privacy - 1000x cheaper than on-chain

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages