基于以太坊区块哈希的公开透明抽奖系统
任何人可独立验证结果,无需信任本网站。
- 选定一个未来以太坊区块号,公告给所有参与者
- 等待该区块被网络打包确认
- 取出该区块的 256 位哈希(由全网验证者共同决定,无人可预测)
- 以哈希为种子,迭代 SHA-256 生成中奖号码,跳过重复
- 任何人可用附带的 Python 代码独立还原结果
OpenLuck/
├── index.html # 主页:发起抽奖
├── history.html # 历史记录查询
├── functions/
│ └── api/
│ └── lottery.js # Cloudflare Pages Function(KV 存储)
├── wrangler.toml # Cloudflare 配置
└── README.md
git init
git add .
git commit -m "init OpenLuck"
gh repo create OpenLuck --public --source=. --pushnpm install -g wrangler
wrangler login# 生产环境
wrangler kv:namespace create OPENLUCK_KV
# 本地预览(可选)
wrangler kv:namespace create OPENLUCK_KV --preview将输出的 id 和 preview_id 填入 wrangler.toml:
[[kv_namespaces]]
binding = "OPENLUCK_KV"
id = "你的KV命名空间ID"
preview_id = "你的预览KV命名空间ID"- 登录 Cloudflare Dashboard
- Workers & Pages → Create application → Pages → Connect to Git
- 选择
OpenLuck仓库 - Framework preset: 选 None(纯静态站)
- Build command: 留空
- Build output directory:
/(或.) - 在 Settings → Functions → KV namespace bindings 中绑定:
- Variable name:
OPENLUCK_KV - KV namespace: 选择刚创建的命名空间
- Variable name:
wrangler pages dev . --kv OPENLUCK_KVimport hashlib, requests
BLOCK_NUMBER = 21000000 # 替换为实际区块号
TOTAL = 100 # 总参与人数
COUNT = 3 # 中奖人数
r = requests.post("https://cloudflare-eth.com", json={
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": [hex(BLOCK_NUMBER), False],
"id": 1
})
block_hash = r.json()["result"]["hash"][2:]
winners, selected, rnd = [], set(), 0
while len(winners) < COUNT:
seed = f"{block_hash}{rnd:04x}".encode()
h = hashlib.sha256(seed).hexdigest()
num = int(h[:16], 16) % TOTAL + 1
if num not in selected:
selected.add(num)
winners.append(num)
rnd += 1
print("区块哈希:", block_hash)
print("中奖号码:", sorted(winners))可在以下平台直接运行:Google Colab · Replit · OnlinePython
| 节点 | 地址 |
|---|---|
| Cloudflare ETH | https://cloudflare-eth.com |
| Llama RPC | https://eth.llamarpc.com |
| Ankr | https://rpc.ankr.com/eth |
MIT