prizerail 1.3.0 → 1.3.1

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.
package/dist/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Http } from "./http.js";
2
2
  import type { Campaign, Drawing, DrawingRef, DrawingStatus, DrawingsPage, Entrant, EntrantEvent, EntrantLookup, EntriesPage, Guarantee, SendEntriesResponse, SentEntrant } from "./types.js";
3
3
  import { type DrawProof, type VerifyResult } from "./verify.js";
4
- export declare const VERSION = "1.3.0";
4
+ export declare const VERSION = "1.3.1";
5
5
  /** The API takes up to this many entries per request. */
6
6
  export declare const ENTRIES_PER_REQUEST = 1000;
7
7
  export type PrizerailOptions = {
package/dist/client.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { PrizerailCutoffError, PrizerailDrawingChangedError, PrizerailError, } from "./errors.js";
2
2
  import { Http } from "./http.js";
3
3
  import { verifyDraw } from "./verify.js";
4
- export const VERSION = "1.3.0";
4
+ export const VERSION = "1.3.1";
5
5
  const badPage = (detail) => new PrizerailError(0, "invalid_response", `Paging stopped: ${detail}.`);
6
6
  /** The API takes up to this many entries per request. */
7
7
  export const ENTRIES_PER_REQUEST = 1000;
@@ -397,6 +397,21 @@ const usd = cents =>
397
397
  minimumFractionDigits: cents % 100 ? 2 : 0,
398
398
  })}`;
399
399
 
400
+ /**
401
+ * Why a proof value can't be checked, naming its field, or "" if it can.
402
+ * `want` is "string" or "whole number".
403
+ */
404
+ function fieldProblem(value, field, want) {
405
+ if (want === "string" ? typeof value === "string" : Number.isInteger(value)) {
406
+ return "";
407
+ }
408
+ if (value === undefined || value === null) return `${field} is missing`;
409
+ const got = Array.isArray(value) ? "a list" : `a ${typeof value}`;
410
+ return `${field} is ${got}, not a ${want}`;
411
+ }
412
+
413
+ const isObject = value => typeof value === "object" && value !== null;
414
+
400
415
  /**
401
416
  * Every check on a proof, as { ok, label, detail? }. `ok` is true, false, or
402
417
  * null for something worth knowing that the math can't settle.
@@ -416,21 +431,43 @@ export function verifyProof(proof, entrants) {
416
431
  }
417
432
  }
418
433
 
419
- if (
420
- typeof proof.campaign?.id !== "string" ||
421
- !Number.isInteger(proof.drawing?.number)
422
- ) {
434
+ if (!isObject(proof)) {
435
+ check(false, "The proof is a JSON object");
436
+ return checks;
437
+ }
438
+ const idProblem =
439
+ fieldProblem(proof.campaign?.id, "campaign.id", "string") ||
440
+ (/^[\x20-\x7e]{1,32}$/.test(proof.campaign.id)
441
+ ? ""
442
+ : "campaign.id isn't 1 to 32 ASCII characters");
443
+ const keyProblems = [
444
+ idProblem,
445
+ fieldProblem(proof.drawing?.number, "drawing.number", "whole number"),
446
+ ].filter(Boolean);
447
+ if (keyProblems.length > 0) {
423
448
  check(
424
449
  false,
425
450
  "The proof names its campaign ID and drawing number",
426
- `this script reads ${proof.format ?? "an unknown format"} proofs only if they have both`
451
+ `${keyProblems.join(", ")}. This script reads ${proof.format ?? "an unknown format"} proofs only if they have both`
427
452
  );
428
453
  return checks;
429
454
  }
430
455
 
431
456
  const { denominator, tiers } = proof.prizeTable ?? {};
432
- if (!Array.isArray(tiers) || !Array.isArray(proof.results)) {
433
- check(false, "The proof has a prize table and results");
457
+ if (!Array.isArray(tiers) || !tiers.every(isObject)) {
458
+ check(
459
+ false,
460
+ "The proof has a prize table",
461
+ "prizeTable.tiers is missing or isn't a list of tiers"
462
+ );
463
+ return checks;
464
+ }
465
+ if (!Array.isArray(proof.results) || !proof.results.every(isObject)) {
466
+ check(
467
+ false,
468
+ "The proof has results",
469
+ "results is missing or isn't a list of results"
470
+ );
434
471
  return checks;
435
472
  }
436
473
  const total = tiers.reduce((n, t) => n + t.weight, 0);
@@ -441,6 +478,7 @@ export function verifyProof(proof, entrants) {
441
478
  tiers.length >= 2 &&
442
479
  tiers.length <= MAX_TIERS &&
443
480
  tiers.every(t => Number.isInteger(t.weight) && t.weight > 0) &&
481
+ tiers.every(t => Number.isInteger(t.amountCents) && t.amountCents >= 0) &&
444
482
  total === denominator &&
445
483
  tiers.every(
446
484
  (t, i) => i === 0 || tiers[i - 1].amountCents >= t.amountCents
@@ -464,9 +502,8 @@ export function verifyProof(proof, entrants) {
464
502
  if (!withinLimit) return checks;
465
503
  const resultAt = new Map(proof.results.map(r => [r.position, r]));
466
504
 
467
- const entropy = proof.entropy.value;
468
505
  const entrantList = proof.entrantList;
469
- if (!entrantList) {
506
+ if (!isObject(entrantList)) {
470
507
  check(
471
508
  false,
472
509
  "The proof has an entrant list",
@@ -474,15 +511,67 @@ export function verifyProof(proof, entrants) {
474
511
  );
475
512
  return checks;
476
513
  }
514
+ // Every pick and roll comes from the random value, but the list's root
515
+ // doesn't, so a missing one still leaves the root to check.
516
+ const entropy = proof.entropy?.value;
517
+ const entropyProblem =
518
+ fieldProblem(entropy, "entropy.value", "string") ||
519
+ (HASH.test(entropy) ? "" : "entropy.value isn't 32 bytes of hex");
520
+ if (entropyProblem) {
521
+ check(
522
+ false,
523
+ "The proof has its random value",
524
+ `${entropyProblem}, so no pick or roll was checked`
525
+ );
526
+ }
527
+ const badPositions = proof.results
528
+ .map((r, i) =>
529
+ fieldProblem(r.position, `results[${i}].position`, "whole number")
530
+ )
531
+ .filter(Boolean);
532
+ if (badPositions.length > 0) {
533
+ check(false, "Every result has a position", badPositions.join(", "));
534
+ }
477
535
  // The winners are the results in slot order, picked from the list.
478
536
  const winners = [...proof.results]
479
537
  .sort((a, b) => a.position - b.position)
480
538
  .map(r => r.winner);
481
- checkEntrants(proof, entrants, check, resultAt);
539
+ checkEntrants(proof, entrants, check, resultAt, !entropyProblem);
482
540
  check(
483
541
  proof.results.length === winners.length,
484
542
  `One result per winner (${proof.results.length})`
485
543
  );
544
+ if (!entropyProblem) checkRolls(proof, check, resultAt, winners);
545
+
546
+ const { lockedAt } = entrantList;
547
+ const { revealedAt, provider, requestId } = proof.entropy ?? {};
548
+ if (lockedAt && revealedAt) {
549
+ check(
550
+ new Date(lockedAt) < new Date(revealedAt),
551
+ "The list was locked before the random value was revealed",
552
+ "by Prizerail's own timestamps"
553
+ );
554
+ }
555
+ if (provider === "mock") {
556
+ check(
557
+ null,
558
+ "Random value source: test randomness",
559
+ "made by Prizerail in a test environment, so it can't be checked independently"
560
+ );
561
+ } else {
562
+ check(
563
+ null,
564
+ `Random value source: ${provider}${requestId ? ` request ${requestId}` : ""}`,
565
+ "this script doesn't look up the request onchain yet; check it on the chain's explorer"
566
+ );
567
+ }
568
+ return checks;
569
+ }
570
+
571
+ /** Each winner's random value, roll and prize. */
572
+ function checkRolls(proof, check, resultAt, winners) {
573
+ const entropy = proof.entropy.value;
574
+ const { denominator, tiers } = proof.prizeTable;
486
575
  // Every winner's roll first, since the top-tier rule looks across them.
487
576
  const rolled = winners.map((_, position) => {
488
577
  const randomValue = winnerRandomValue(
@@ -508,64 +597,59 @@ export function verifyProof(proof, entrants) {
508
597
  const amountCents = tier === -1 ? null : tiers[tier].amountCents;
509
598
  const demoted = rolled[position].tier === 0 && tier === 1;
510
599
  const problems = [
511
- randomValue !== result.randomValue.toLowerCase() &&
512
- `random value should be ${randomValue}`,
513
- roll !== result.roll && `roll should be ${roll}`,
514
- amountCents !== result.amountCents &&
515
- `prize should be ${amountCents === null ? "none" : usd(amountCents)}`,
600
+ fieldProblem(winner, "winner", "string"),
601
+ fieldProblem(result.randomValue, "randomValue", "string") ||
602
+ (randomValue !== result.randomValue.toLowerCase() &&
603
+ `random value should be ${randomValue}`),
604
+ fieldProblem(result.roll, "roll", "whole number") ||
605
+ (roll !== result.roll && `roll should be ${roll}`),
606
+ fieldProblem(result.amountCents, "amountCents", "whole number") ||
607
+ (amountCents !== result.amountCents &&
608
+ `prize should be ${amountCents === null ? "none" : usd(amountCents)}`),
516
609
  ].filter(Boolean);
610
+ const wins = Number.isInteger(result.amountCents)
611
+ ? usd(result.amountCents)
612
+ : "an unstated prize";
517
613
  check(
518
614
  problems.length === 0,
519
- `${label}: roll ${roll.toLocaleString("en-US")} wins ${usd(result.amountCents)}${demoted ? ", the second prize, since another winner with a lower random value kept the top one" : ""}`,
615
+ `${label}: roll ${roll.toLocaleString("en-US")} wins ${wins}${demoted ? ", the second prize, since another winner with a lower random value kept the top one" : ""}`,
520
616
  problems.join(", ") || undefined
521
617
  );
522
618
  }
523
-
524
- const { lockedAt } = entrantList;
525
- const { revealedAt, provider, requestId } = proof.entropy;
526
- if (lockedAt && revealedAt) {
527
- check(
528
- new Date(lockedAt) < new Date(revealedAt),
529
- "The list was locked before the random value was revealed",
530
- "by Prizerail's own timestamps"
531
- );
532
- }
533
- if (provider === "mock") {
534
- check(
535
- null,
536
- "Random value source: test randomness",
537
- "made by Prizerail in a test environment, so it can't be checked independently"
538
- );
539
- } else {
540
- check(
541
- null,
542
- `Random value source: ${provider}${requestId ? ` request ${requestId}` : ""}`,
543
- "this script doesn't look up the request onchain yet; check it on the chain's explorer"
544
- );
545
- }
546
- return checks;
547
619
  }
548
620
 
549
- /** The entrant list's checks: its root, then every pick. */
550
- function checkEntrants(proof, entrants, check, results) {
621
+ /**
622
+ * The entrant list's checks: its root, then every pick, if the proof has a
623
+ * usable random value (`picksCheckable`).
624
+ */
625
+ function checkEntrants(proof, entrants, check, results, picksCheckable) {
551
626
  const { root, count, totalWeight, winnersRequested } = proof.entrantList;
552
627
  // Proofs from before the setting have no field, and those draws took
553
628
  // each winner out.
554
629
  const repeat = proof.entrantList.repeatWinners === true;
555
- if (
556
- typeof root !== "string" ||
557
- !HASH.test(root) ||
558
- !Number.isInteger(count) ||
559
- count < 1 ||
560
- !Number.isSafeInteger(totalWeight) ||
561
- !Number.isInteger(winnersRequested) ||
562
- winnersRequested < 1 ||
563
- winnersRequested > MAX_WINNERS
564
- ) {
630
+ const fieldProblems = [
631
+ fieldProblem(root, "entrantList.root", "string") ||
632
+ (HASH.test(root) ? "" : "entrantList.root isn't 32 bytes of hex"),
633
+ fieldProblem(count, "entrantList.count", "whole number") ||
634
+ (count < 1 ? "entrantList.count is below 1" : ""),
635
+ fieldProblem(totalWeight, "entrantList.totalWeight", "whole number") ||
636
+ (Number.isSafeInteger(totalWeight)
637
+ ? ""
638
+ : "entrantList.totalWeight is too big"),
639
+ fieldProblem(
640
+ winnersRequested,
641
+ "entrantList.winnersRequested",
642
+ "whole number"
643
+ ) ||
644
+ (winnersRequested < 1 || winnersRequested > MAX_WINNERS
645
+ ? `entrantList.winnersRequested isn't from 1 to ${MAX_WINNERS}`
646
+ : ""),
647
+ ].filter(Boolean);
648
+ if (fieldProblems.length > 0) {
565
649
  check(
566
650
  false,
567
651
  "The proof's entrant list fields are well formed",
568
- "root must be 32 bytes of hex, count and totalWeight whole numbers, and winnersRequested from 1 to 500"
652
+ fieldProblems.join(", ")
569
653
  );
570
654
  return;
571
655
  }
@@ -641,6 +725,7 @@ function checkEntrants(proof, entrants, check, results) {
641
725
  "Entrant list root matches the list",
642
726
  computed === root.toLowerCase() ? undefined : `computed ${computed}`
643
727
  );
728
+ if (!picksCheckable) return;
644
729
  const picks = selectWinners(
645
730
  proof.entropy.value,
646
731
  proof.campaign.id,
@@ -658,13 +743,17 @@ function checkEntrants(proof, entrants, check, results) {
658
743
  }
659
744
  const entry = list.at(pick.entrantIndex);
660
745
  const problems = [
661
- pick.selectionValue !== result.selectionValue?.toLowerCase() &&
662
- `selection value should be ${pick.selectionValue}`,
663
- pick.pick !== result.pick && `pick should be ${pick.pick}`,
664
- pick.entrantIndex !== result.entrantPosition &&
665
- `winner should be entrant ${pick.entrantIndex}`,
666
- winnerHash(result.winner) !== entry[0].toLowerCase() &&
667
- `${result.winner} doesn't hash to entrant ${pick.entrantIndex}`,
746
+ fieldProblem(result.selectionValue, "selectionValue", "string") ||
747
+ (pick.selectionValue !== result.selectionValue.toLowerCase() &&
748
+ `selection value should be ${pick.selectionValue}`),
749
+ fieldProblem(result.pick, "pick", "whole number") ||
750
+ (pick.pick !== result.pick && `pick should be ${pick.pick}`),
751
+ fieldProblem(result.entrantPosition, "entrantPosition", "whole number") ||
752
+ (pick.entrantIndex !== result.entrantPosition &&
753
+ `winner should be entrant ${pick.entrantIndex}`),
754
+ fieldProblem(result.winner, "winner", "string") ||
755
+ (winnerHash(result.winner) !== entry[0].toLowerCase() &&
756
+ `${result.winner} doesn't hash to entrant ${pick.entrantIndex}`),
668
757
  ].filter(Boolean);
669
758
  check(
670
759
  problems.length === 0,
@@ -763,16 +852,17 @@ async function main() {
763
852
  }
764
853
  const proof = await readJson(source);
765
854
  let entrants;
766
- if (proof.entrantList) {
855
+ if (proof?.entrantList) {
856
+ const listUrl = proof.entrantList.url;
767
857
  const listSource =
768
858
  process.argv[3] ??
769
- (/^https?:\/\//.test(source)
770
- ? new URL(proof.entrantList.url, source).href
859
+ (/^https?:\/\//.test(source) && typeof listUrl === "string"
860
+ ? new URL(listUrl, source).href
771
861
  : undefined);
772
862
  if (listSource) entrants = await readEntrants(listSource);
773
863
  }
774
864
  console.log(
775
- `\n${proof.campaign.name}, draw of ${proof.drawing.drawAt} (${proof.drawing.id})\n`
865
+ `\n${proof?.campaign?.name ?? "Unnamed campaign"}, draw of ${proof?.drawing?.drawAt ?? "an unknown time"} (${proof?.drawing?.id ?? "no draw ID"})\n`
776
866
  );
777
867
  const checks = verifyProof(proof, entrants);
778
868
  for (const { ok, label, detail } of checks) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prizerail",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "The Prizerail SDK: send entries, read draws and verify them.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://docs.prizerail.com",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prizerail",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Integrate Prizerail prize draws into an app, check an integration end to end, and look up or verify draws.",
5
5
  "author": {
6
6
  "name": "Prizerail",