npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

stake-provably-fair

v1.0.0

Published

Node.js provably fair RNG primitives and Dice helpers.

Readme

stake-provably-fair 中文说明

这是一个 Node.js provably fair RNG npm 库,提供可验证随机数核心能力和 Dice 应用层 helper。

安装

npm install stake-provably-fair

要求 Node.js 18 或更新版本。

入口

完整入口:

import {
  generateServerSeed,
  hashServerSeed,
  verifyServerSeed,
  generateClientSeed,
  generateFloats,
  generateDiceRoll,
  simulateDice,
} from 'stake-provably-fair';

分层入口:

import { generateFloats } from 'stake-provably-fair/core';
import { generateDiceRoll } from 'stake-provably-fair/dice';

CommonJS 也支持:

const { generateFloats } = require('stake-provably-fair/core');
const { generateDiceRoll } = require('stake-provably-fair/dice');

包内置 TypeScript 类型,根入口和子路径入口都有 .d.ts 声明。

解决的问题

在线博彩的核心信任问题是:玩家如何确认结果不是平台在下注后篡改的?

Provably fair 的思路是把结果生成拆成两部分:

公平结果 = 平台输入(下注前公开 hash 承诺) + 玩家输入

在本项目中:

  • 平台输入是 serverSeed
  • 玩家输入是 clientSeed
  • 每次下注递增 nonce
  • 多结果游戏通过 cursor 从确定性字节流中继续取值。
  • 下注前公开 hashServerSeed(serverSeed),平台不能在之后无痕替换 seed。
  • seed 轮换/reveal 后,玩家用 verifyServerSeed(serverSeed, committedHash) 验证公开 hash 是否匹配原始 seed。

这不是"预测未来结果"的系统,而是"下注后可复算、可审计"的系统。

模块设计

stake-provably-fair/core
  ├─ generateServerSeed()
  ├─ hashServerSeed()
  ├─ verifyServerSeed()
  ├─ generateClientSeed()
  ├─ byteGenerator(...)
  ├─ generateFloats(...)
  ├─ floatToIndex(...)
  ├─ pickOutcome(...)
  └─ pickOutcomes(...)

stake-provably-fair/dice
  ├─ diceRollFromFloat(...)
  ├─ generateDiceRoll(...)
  ├─ isDiceWin(...)
  ├─ getDiceWinProbability(...)
  └─ simulateDice(...)

核心原则:

  • RNG 核心不依赖具体游戏规则。
  • 所有随机结果都由 HMAC-SHA256 确定性生成。
  • 相同的 serverSeed + clientSeed + nonce + cursor 永远生成相同结果。
  • 应用层只负责把通用 float 映射成具体事件,例如 Dice roll。

随机字节生成

核心字节流使用:

HMAC_SHA256(serverSeed, `${clientSeed}:${nonce}:${round}`)

每个 HMAC round 输出 32 bytes。cursor 用于指定从字节流的哪个位置开始读取:

  • round = Math.floor(cursor / 32)
  • roundCursor = cursor % 32

Dice 示例

Dice 使用 00.00100.00 的范围,共 10,001 个可能结果。

import {
  generateServerSeed,
  hashServerSeed,
  verifyServerSeed,
  generateClientSeed,
} from 'stake-provably-fair/core';
import { generateDiceRoll, isDiceWin } from 'stake-provably-fair/dice';

const serverSeed = generateServerSeed();
const committedHash = hashServerSeed(serverSeed);
const clientSeed = generateClientSeed();

const roll = generateDiceRoll({
  serverSeed,
  clientSeed,
  nonce: 0,
});

const win = isDiceWin(roll, {
  target: 50,
  condition: 'under',
});

console.log({ committedHash, roll, win });
console.log(verifyServerSeed(serverSeed, committedHash));

100 万次模拟

运行:

npm run simulate:dice

可通过环境变量覆盖:

BETS=1000000 TARGET=50 CONDITION=under npm run simulate:dice

输出包含 winslosseswinRatetheoreticalWinRatedifferenceexpectedWinsstandardDeviationzScoreminRollmaxRoll

这个模拟只能说明分布没有明显异常,不能替代逐笔 seed reveal 后的可验证复算。

边界

当前包只实现 RNG 核心和 Dice helper,不处理 HTTP API、数据库、用户账户、seed 会话持久化、余额、下注金额、赔率、house edge 或合规问题。

测试

npm test
npm run pack:dry-run