prizerail 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ import { type Check, type EntrantRow } from "./verify-draw.mjs";
2
+ export type { Check, EntrantRow };
3
+ /**
4
+ * A draw's proof, `prizerail-draw-proof/3`. The fields the SDK reads are
5
+ * typed, and verifyDraw checks the rest, so treat a proof from elsewhere
6
+ * as untrusted until it has.
7
+ */
8
+ export type DrawProof = {
9
+ format: string;
10
+ campaign: {
11
+ id: string;
12
+ slug?: string;
13
+ name: string;
14
+ };
15
+ drawing: {
16
+ id: string;
17
+ number: number;
18
+ drawAt: string;
19
+ };
20
+ entrantList?: {
21
+ url: string;
22
+ root: string;
23
+ count: number;
24
+ totalWeight: number;
25
+ winnersRequested: number;
26
+ lockedAt: string | null;
27
+ };
28
+ [field: string]: unknown;
29
+ };
30
+ export type VerifyResult = {
31
+ /** True when no check failed. Checks with `ok: null` are notes. */
32
+ ok: boolean;
33
+ checks: Check[];
34
+ proof: DrawProof;
35
+ /** The locked entrant list, when the draw has one and it was read. */
36
+ entrants?: EntrantRow[];
37
+ };
38
+ export type VerifyOptions = {
39
+ /**
40
+ * The entrant list: rows already read, or a link or file to read them
41
+ * from. By default, the proof's own `entrantList.url`.
42
+ */
43
+ entrants?: EntrantRow[] | string;
44
+ /** Where a proof passed as an object came from, to find its list. */
45
+ proofUrl?: string;
46
+ /** Used for the proof and each page of the list. Defaults to the global fetch. */
47
+ fetch?: typeof fetch;
48
+ /** Per request. Defaults to 30 seconds. */
49
+ timeoutMs?: number;
50
+ };
51
+ /**
52
+ * Checks a draw from its proof alone: the entrant list's fingerprint, the
53
+ * random value, and each winner and prize. `source` is the proof, or a link
54
+ * or file path to it. This is the same code as the public verify script.
55
+ */
56
+ export declare function verifyDraw(source: string | DrawProof, { entrants, proofUrl, fetch: fetchImpl, timeoutMs, }?: VerifyOptions): Promise<VerifyResult>;
57
+ /**
58
+ * Where an entrant id is in a locked list, or null if it isn't there. The
59
+ * list holds keccak256 of each id, so this hashes the id and looks it up.
60
+ */
61
+ export declare function wasEntered(entrantId: string, rows: EntrantRow[]): {
62
+ index: number;
63
+ weight: number;
64
+ } | null;
package/dist/verify.js ADDED
@@ -0,0 +1,69 @@
1
+ import { readEntrants, verifyProof, winnerHash, } from "./verify-draw.mjs";
2
+ const isUrl = (s) => /^https?:\/\//.test(s);
3
+ async function readProof(source, fetchImpl, timeoutMs) {
4
+ if (isUrl(source)) {
5
+ const res = await fetchImpl(source, {
6
+ signal: AbortSignal.timeout(timeoutMs),
7
+ });
8
+ if (!res.ok)
9
+ throw new Error(`${source}: HTTP ${res.status}`);
10
+ return (await res.json());
11
+ }
12
+ const { readFile } = await import("node:fs/promises");
13
+ return JSON.parse(await readFile(source, "utf8"));
14
+ }
15
+ /**
16
+ * Checks a draw from its proof alone: the entrant list's fingerprint, the
17
+ * random value, and each winner and prize. `source` is the proof, or a link
18
+ * or file path to it. This is the same code as the public verify script.
19
+ */
20
+ export async function verifyDraw(source, { entrants, proofUrl, fetch: fetchImpl = globalThis.fetch, timeoutMs = 30_000, } = {}) {
21
+ const base = typeof source === "string" ? source : proofUrl;
22
+ const proof = typeof source === "string"
23
+ ? await readProof(source, fetchImpl, timeoutMs)
24
+ : source;
25
+ let rows = Array.isArray(entrants) ? entrants : undefined;
26
+ // A list the caller names wins. Otherwise the proof's own link, if any.
27
+ const listUrl = proof.entrantList?.url;
28
+ const listSource = typeof entrants === "string"
29
+ ? entrants
30
+ : rows || typeof listUrl !== "string"
31
+ ? undefined
32
+ : base && isUrl(base)
33
+ ? new URL(listUrl, base).href
34
+ : isUrl(listUrl)
35
+ ? listUrl
36
+ : undefined;
37
+ if (listSource) {
38
+ rows = await readEntrants(listSource, { fetch: fetchImpl, timeoutMs });
39
+ }
40
+ let checks;
41
+ try {
42
+ checks = verifyProof(proof, rows);
43
+ }
44
+ catch (error) {
45
+ // A proof missing a field the checks need is a failed draw, not a crash.
46
+ checks = [
47
+ {
48
+ ok: false,
49
+ label: "The proof is well formed",
50
+ detail: error instanceof Error ? error.message : String(error),
51
+ },
52
+ ];
53
+ }
54
+ return {
55
+ ok: !checks.some(c => c.ok === false),
56
+ checks,
57
+ proof,
58
+ entrants: rows,
59
+ };
60
+ }
61
+ /**
62
+ * Where an entrant id is in a locked list, or null if it isn't there. The
63
+ * list holds keccak256 of each id, so this hashes the id and looks it up.
64
+ */
65
+ export function wasEntered(entrantId, rows) {
66
+ const hash = winnerHash(entrantId);
67
+ const row = rows.find(r => r[1].toLowerCase() === hash);
68
+ return row ? { index: row[0], weight: row[2] } : null;
69
+ }
package/package.json CHANGED
@@ -1,10 +1,39 @@
1
1
  {
2
2
  "name": "prizerail",
3
- "version": "0.0.1",
4
- "description": "Prizerail command line tools. Coming soon: npx prizerail verify <proof url>.",
5
- "homepage": "https://www.prizerail.com",
3
+ "version": "0.1.0",
4
+ "description": "The Prizerail SDK: send entries, read draws and verify them.",
6
5
  "license": "MIT",
6
+ "homepage": "https://docs.prizerail.com",
7
7
  "type": "module",
8
- "bin": { "prizerail": "cli.mjs" },
9
- "files": ["cli.mjs", "README.md"]
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "bin": {
17
+ "prizerail": "dist/cli.js"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "sideEffects": false,
28
+ "scripts": {
29
+ "build": "node scripts/build.mjs",
30
+ "prepublishOnly": "node scripts/build.mjs"
31
+ },
32
+ "keywords": [
33
+ "prizerail",
34
+ "sweepstakes",
35
+ "prize draw",
36
+ "pyth",
37
+ "verifiable"
38
+ ]
10
39
  }
package/cli.mjs DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env node
2
- console.log("The Prizerail CLI is coming soon.")
3
- console.log("Until then, verify a draw with https://www.prizerail.com/verify/verify-draw.mjs")
4
- console.log("Docs: https://docs.prizerail.com")