AnonVote/contracts

GitHub: AnonVote/contracts

AnonVote/contracts 是一套基于 Stellar Soroban 的 Rust 智能合约,为匿名投票系统提供链上可审计的投票记录验证机制。

Stars: 0 | Forks: 5

# AnonVote 合约 **用于 AnonVote 投票记录链上审计与验证的 Soroban 智能合约。** 本仓库包含 AnonVote 生态系统的所有 Stellar/Soroban 合约代码。合约提供可链上查询的状态,以补充链下隐私引擎——让任何人都能直接在 Stellar 账本上验证投票记录的完整性,而无需信任 AnonVote 的服务器。 [![Rust](https://img.shields.io/badge/Rust-1.78+-orange)](https://www.rust-lang.org/) [![Soroban SDK](https://img.shields.io/badge/soroban--sdk-21.0.0-blueviolet)](https://github.com/stellar/rs-soroban-sdk) [![license: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) ## 在生态系统中的角色 | 仓库 | 关系 | | --------------------------------------------------------- | --------------------------------------------------------------- | | [AnonVote/core](https://github.com/AnonVote/core) | 后端调用 `sorobanService.ts`,由其调用这些合约 | | [AnonVote/js](https://github.com/AnonVote/js) | 无依赖——合约是 Rust 编写,而非 JS | | [AnonVote/protocol](https://github.com/AnonVote/protocol) | 白皮书引用了合约审计模型 | ## 本仓库包含什么 ### `contracts/anonvote/` — AnonVote 审计合约 主要的 Soroban 合约。以公开读取权限在链上记录不可篡改的审计事件。 | 函数 | 描述 | | -------------------------------------------- | -------------------------------------------------------------------- | | `initialize(admin)` | 部署后的一次性设置。设置管理员地址。 | | `record_ballot(ballot_id_hash)` | 在链上登记投票记录。输入为投票记录 UUID 的 SHA-256 十六进制值。 | | `record_token(ballot_id_hash)` | 增加一次投票记录的已发放 token 计数。 | | `record_vote(ballot_id_hash)` | 增加一次投票记录的已投票计数。 | | `record_result(ballot_id_hash, result_hash)` | 发布结果哈希(统计 JSON 的 SHA-256)。一经设置即不可篡改。 | | `get_tokens_issued(ballot_id_hash)` | 读取 token 计数(视图调用)。 | | `get_votes_cast(ballot_id_hash)` | 读取投票计数(视图调用)。 | | `get_result_hash(ballot_id_hash)` | 读取结果哈希(视图调用)。 | | `ballot_exists(ballot_id_hash)` | 检查投票记录是否已登记在链上。 | | `is_consistent(ballot_id_hash)` | 如果 `tokens_issued == votes_cast` 则返回 `true`。 | **隐私保证:** - 不存储任何投票者标识符 - 不存储任何 token 值 - 不存储任何投票内容 - 仅存储计数和哈希值——与链下系统相同的隐私模型 ### `service/sorobanService.ts` — TypeScript 服务存根 一个完全类型化的 TypeScript 包装器,围绕 Soroban 合约构建,随时可接入 [AnonVote/core](https://github.com/AnonVote/core)。使用 `stellar-sdk` v12,并包含正确的 RPC、模拟和组装 API。 ## 前置条件 ``` # 安装 Rust curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh # 添加 WASM target rustup target add wasm32-unknown-unknown # 安装 Stellar CLI cargo install --locked stellar-cli --features opt ``` ## 构建 ``` cd contracts/anonvote cargo build --target wasm32-unknown-unknown --release ``` 输出:`target/wasm32-unknown-unknown/release/anonvote.wasm` ## 测试 ``` cd contracts/anonvote cargo test ``` 所有测试均在 Soroban 原生测试环境中运行——无需网络连接。 ## 部署到 Testnet ``` # 部署合约 WASM stellar contract deploy \ --wasm contracts/anonvote/target/wasm32-unknown-unknown/release/anonvote.wasm \ --source \ --network testnet # 输出:CONTRACT_ID(例如 CABC123...) # 使用你的 admin public key 进行初始化 stellar contract invoke \ --id \ --source \ --network testnet \ -- initialize \ --admin ``` 然后将其添加到 `core` 的 `backend/.env` 中: ``` SOROBAN_CONTRACT_ID= ``` ## 接入 core 部署完成后,[AnonVote/core](https://github.com/AnonVote/core) 中的以下位置会调用 `sorobanService.ts` 辅助函数: | Core 文件 | 合约调用 | | ----------------------------- | ----------------------------------------------- | | `services/ballotEngine.ts` | `sorobanRecordBallot(ballotIdHash)` | | `services/identityManager.ts` | `sorobanRecordToken(ballotIdHash)` | | `services/privacyEngine.ts` | `sorobanRecordVote(ballotIdHash)` | | `services/resultEngine.ts` | `sorobanRecordResult(ballotIdHash, resultHash)` | `ballot_id_hash` 参数是来自 `@anonvote/crypto` 的 `hashIdentifier(ballotId)`。 ## 仓库结构 ``` contracts/ ├── contracts/ │ └── anonvote/ │ ├── src/ │ │ └── lib.rs # Soroban contract implementation │ ├── Cargo.toml │ └── README.md ├── service/ │ └── sorobanService.ts # TypeScript service stub for core └── README.md ``` ## 许可证 [MIT](LICENSE)
标签:Rust, Soroban, Stellar, 区块链, 匿名投票, 可视化界面, 智能合约, 网络流量审计, 通知系统, 链上审计