Check a pull
How anyone can redo the random pick of a pull from its proof.
Grail Network picks the cards of a pull at random. The proof of a pull lets you, your buyer or anyone else redo each pick, and see that Grail Network did not choose the cards after it knew your seed. The method is commit and reveal, as at Phygitals.
How it works
- Before the pull, Grail Network makes a secret server seed, and gives you only its SHA-256 hash: the commitment.
- Your server sends its own client seed with the pull. Grail Network cannot know it in advance.
- Grail Network picks the cards with both seeds.
- After the pull, Grail Network reveals the server seed. Its hash must be the hash that you got before.
Read the proof
GET /shop/pulls/{pullId}/proofOnly a completed pull has a proof. For another state, the route answers 404 PULL_PROOF_NOT_FOUND. A failed pull
never shows its server seed.
The proof holds:
| Field | Meaning |
|---|---|
pullId | The pull |
recipe | The name of the method: g1 |
serverSeed, serverSeedHash | The revealed seed and the hash that you got before the pull |
clientSeed | Your seed |
committedAt | When Grail Network made the commitment |
packCount | The number of packs |
refusedItemIds | Cards that the pick named, but that another pull took in the same moment |
version | The pack as it was at the pull: id, number, packPrice, the slots with their tiers (id, name, minPrice, maxPrice, chancePpb), and the featuredCards (position, itemId, oddsPpb) |
packs | For each pack: its index (from 0), its draws, and its featured roll (roll, itemId, outcome), or null when the pack has no featured cards |
Each draw has drawIndex, slotPosition, tierRoll, tierId, rowSeed, start, pool (the ids of the cards that
the slot could take) and itemId (the card that it took).
The types: chancePpb, oddsPpb, tierRoll and roll are text, such as "864045973", so that no number loses a
digit in JSON. Read them as big integers. drawIndex, slotPosition, start, index and position are numbers. An
amount is { amountAtomic, currency }, with amountAtomic as text.
A chance is in parts per billion: 1,000,000,000 is 100%. A slot's chances add up to exactly 1,000,000,000.
The recipe g1
HMAC(t)is HMAC-SHA256. Its key isserverSeedas UTF-8 text: the 64 hex characters, not decoded. Its message istas UTF-8. Its output is lowercase hex.tail(h, n)is the number of the last 15 hex characters ofh, modulon.SCALEis 1,000,000,000.- The index of a draw is
d = packIndex × slotCount + slotPosition − 1.
The steps:
- The seed.
sha256(serverSeed)equalsserverSeedHash. - The tier of draw d.
tierRoll = tail(HMAC(clientSeed + ":" + d), SCALE). Walk the slot's tiers in ascendingminPrice, and add up theirchancePpb. The tier is the first one whose running total is greater thantierRoll. - The pool of draw d. The cards of that tier at the pick, in ascending id text, without the first card of each
earlier draw of the pull. The first card of a draw is
pool[start]of step 4. So a later draw of the same price range has the earlier draw's pool without the first cards of the draws since then. - The card of draw d.
rowSeed = HMAC(clientSeed + ":row:" + d).start = tail(rowSeed, n), wherenis the length of the pool. Fromstart, and back to the beginning of the pool after its end, the card is the first id that is not inrefusedItemIds, not the card of another draw, and not a featured card that the pull won. - The featured roll of pack p, when the pack has featured cards.
roll = tail(HMAC(clientSeed + ":featured:" + p), SCALE). Walk the featured cards in ascendingposition, and add up theiroddsPpb. The named card is the first one whose running total is greater thanroll. Whenrollis past the sum, the roll names no card (none). - The order. Every draw first: pack 0 to the last pack, slot 1 to the last slot. Then the featured rolls, pack 0 to the last.
An example of step 2: a slot has Common at 800,000,000 and Uncommon at 150,000,000. A tierRoll of 512,004,113 is
under 800,000,000, so the tier is Common. 873,550,021 is under 950,000,000, so the tier is Uncommon.
A script
The script needs Node.js 22.12 or later, and no package. Save the two files below in one folder. Save the answer of
the proof route as proof.json, then run:
node run-check.js proof.jsonIt prints the pull is right, or each step that does not hold, one in each line.
import { createHash, createHmac } from 'node:crypto';
const SCALE = 1_000_000_000n;
const hmac = (seed, text) => createHmac('sha256', seed).update(text).digest('hex');
const tail = (hex, size) => BigInt(`0x${hex.slice(-15)}`) % BigInt(size);
// The proof is the answer of GET /shop/pulls/{pullId}/proof, or its `data`. The answer is "the pull is right", or
// each step that does not hold, one in each line.
export function checkPull(answer) {
const proof = answer.data ?? answer;
const problems = [];
const { serverSeed, clientSeed, version } = proof;
// 1. The seed: its SHA-256 is the hash of the commitment.
if (createHash('sha256').update(serverSeed).digest('hex') !== proof.serverSeedHash) {
problems.push('the server seed does not match the hash of the commitment');
}
const slotCount = version.slots.length;
const tiers = version.slots.flatMap((slot) => slot.tiers);
const draws = proof.packs.flatMap((pack) =>
pack.draws.map((draw) => ({ ...draw, packIndex: pack.index })),
);
const won = new Set(
proof.packs
.filter((pack) => pack.featured?.outcome === 'hit')
.map((pack) => pack.featured.itemId),
);
const refused = new Set(proof.refusedItemIds);
const firstCards = [];
const earlierOfRange = new Map();
draws.forEach((draw, place) => {
const d = draw.packIndex * slotCount + draw.slotPosition - 1;
if (draw.drawIndex !== d || place !== d) {
problems.push(`draw ${d}: the index or the order is wrong`);
}
// 2. The tier: walk the slot's tiers in ascending minPrice, adding chancePpb (a text: read it as a BigInt).
const tierRoll = tail(hmac(serverSeed, `${clientSeed}:${d}`), SCALE);
if (String(tierRoll) !== String(draw.tierRoll))
problems.push(`draw ${d}: the tier roll is wrong`);
const slot = version.slots.find((one) => one.position === draw.slotPosition);
const ordered = slot.tiers.toSorted((a, b) =>
BigInt(a.minPrice.amountAtomic) < BigInt(b.minPrice.amountAtomic) ? -1 : 1,
);
let total = 0n;
const tier = ordered.find((one) => (total += BigInt(one.chancePpb)) > tierRoll);
if (tier?.id !== draw.tierId) problems.push(`draw ${d}: the tier is wrong`);
// 3. The pool: in id order, without the first card of an earlier draw. A later draw of the same price range has
// the earlier pool without the first cards of the draws since then.
if (draw.pool.some((id, index) => index > 0 && draw.pool[index - 1] >= id)) {
problems.push(`draw ${d}: the pool is not in id order`);
}
if (firstCards.some((id) => draw.pool.includes(id))) {
problems.push(`draw ${d}: the pool holds the first card of an earlier draw`);
}
const tierOfDraw = tiers.find((one) => one.id === draw.tierId);
if (tierOfDraw) {
const range = `${tierOfDraw.minPrice.amountAtomic}:${tierOfDraw.maxPrice.amountAtomic}`;
const earlier = earlierOfRange.get(range);
if (earlier) {
const since = new Set(firstCards.slice(earlier.d));
if (earlier.pool.filter((id) => !since.has(id)).join() !== draw.pool.join()) {
problems.push(`draw ${d}: the pool does not follow the earlier pool of its range`);
}
}
earlierOfRange.set(range, { pool: draw.pool, d });
}
// 4. The card: from start, the first card that is not refused, not another draw's card, and not a won card.
const rowSeed = hmac(serverSeed, `${clientSeed}:row:${d}`);
if (rowSeed !== draw.rowSeed) problems.push(`draw ${d}: the row seed is wrong`);
const start = Number(tail(rowSeed, draw.pool.length));
if (start !== draw.start) problems.push(`draw ${d}: the start is wrong`);
firstCards.push(draw.pool[start]);
const others = new Set(draws.filter((other) => other !== draw).map((other) => other.itemId));
let card;
for (let step = 0; step < draw.pool.length && card === undefined; step += 1) {
const id = draw.pool[(start + step) % draw.pool.length];
if (!refused.has(id) && !others.has(id) && !won.has(id)) card = id;
}
if (card !== draw.itemId) problems.push(`draw ${d}: the card is wrong`);
});
// 5. The featured roll of each pack: walk the featured cards in ascending position, adding oddsPpb (a text).
for (const pack of proof.packs) {
if (version.featuredCards.length === 0) {
if (pack.featured !== null)
problems.push(`pack ${pack.index}: a featured roll with no featured cards`);
continue;
}
const roll = tail(hmac(serverSeed, `${clientSeed}:featured:${pack.index}`), SCALE);
if (String(roll) !== String(pack.featured?.roll)) {
problems.push(`pack ${pack.index}: the featured roll is wrong`);
}
let total = 0n;
const named = version.featuredCards
.toSorted((a, b) => a.position - b.position)
.find((one) => (total += BigInt(one.oddsPpb)) > roll);
if ((named?.itemId ?? null) !== pack.featured?.itemId) {
problems.push(`pack ${pack.index}: the featured card is wrong`);
}
if ((named === undefined) !== (pack.featured?.outcome === 'none')) {
problems.push(`pack ${pack.index}: the featured outcome is wrong`);
}
}
return problems.length === 0 ? 'the pull is right' : problems.join('\n');
}import { readFileSync } from 'node:fs';
import { checkPull } from './check-pull.js';
console.log(checkPull(JSON.parse(readFileSync(process.argv[2], 'utf8'))));What a proof cannot show
- That a pool held every card of its tier at the moment of the pick.
- That a refused card was really taken by another pull.
- Whether a named featured card was really not available (
unavailable).