@swarm-tips/client 0.4.5 → 0.4.7

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.
@@ -1,8 +1,10 @@
1
1
  import { Buffer } from "buffer";
2
2
  import { ComputeBudgetProgram, PublicKey, Secp256k1Program } from "@solana/web3.js";
3
3
  import { SHILLBOT_PROGRAM_ID, shillbotInstructionDiscriminator } from "../contracts/index.js";
4
- import { SwarmClientError } from "../errors.js";
5
- import { inspectTransaction, } from "./index.js";
4
+ import { SwarmClientError, TransactionMismatchError } from "../errors.js";
5
+ import { EXTENSION_CREDIT_PROGRAM_ID, advancePda, inspectTransaction, routeAndRecoupDiscriminator, } from "./index.js";
6
+ /** Alias for `advancePda` (`seeds = [b"advance", sponsorPubkey, agentPubkey]`). */
7
+ export const deriveAdvancePda = advancePda;
6
8
  /** Default maximum allowed Solana priority fee price per compute unit (`100_000` micro-lamports/CU). */
7
9
  export const MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS = 100000n;
8
10
  /** Solana runtime ceiling for `ComputeBudget::SetComputeUnitLimit` (`1_400_000` CUs). */
@@ -218,8 +220,7 @@ const DISCRIMINATOR_TO_SPEC = (() => {
218
220
  return map;
219
221
  })();
220
222
  function txMismatch(operation, message) {
221
- return new SwarmClientError({
222
- code: "TRANSACTION_MISMATCH",
223
+ return new TransactionMismatchError({
223
224
  operation,
224
225
  message,
225
226
  });
@@ -327,6 +328,8 @@ function allowedInstructionNamesForAction(action) {
327
328
  return ["verify_task", "verify_task_attested"];
328
329
  case "finalize":
329
330
  return ["finalize_task"];
331
+ case "expire":
332
+ return ["expire_task"];
330
333
  default:
331
334
  return [];
332
335
  }
@@ -380,7 +383,7 @@ function verifyInstructionAuthorityAndShape(decoded, inspected, expected, operat
380
383
  if (spec.exactAccounts !== undefined && ix.accounts.length !== spec.exactAccounts) {
381
384
  throw txMismatch(operation, `Instruction ${spec.name} requires exact ${spec.exactAccounts} accounts, got ${ix.accounts.length}`);
382
385
  }
383
- const expectedFeePayer = expected.feePayer ?? expected.wallet;
386
+ const expectedFeePayer = expected.feePayer ?? expected.sponsor ?? expected.wallet;
384
387
  if (!spec.allowsSponsoredFeePayer && inspected.fee_payer !== expectedFeePayer) {
385
388
  throw txMismatch(operation, `Instruction ${spec.name} requires fee_payer ${expectedFeePayer}, got ${inspected.fee_payer}`);
386
389
  }
@@ -435,7 +438,8 @@ function verifyInstructionAuthorityAndShape(decoded, inspected, expected, operat
435
438
  * Strict fail-closed local validator for built or server-prepared Shillbot Solana transactions.
436
439
  *
437
440
  * Enforces:
438
- * - Allowed program IDs (Shillbot + ComputeBudget, plus Switchboard + Secp256k1 only on `verify`).
441
+ * - Allowed program IDs (Shillbot + ComputeBudget, Switchboard + Secp256k1 on `verify` / score-0 reopen,
442
+ * and `extension-credit` `route_and_recoup` strictly on `finalize`).
439
443
  * - Full loop validation of all `ComputeBudget` instructions (no early return on `RequestHeapFrame`),
440
444
  * capping `SetComputeUnitPrice <= MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS` (`100_000` micro-lamports/CU).
441
445
  * - Exact Anchor account-index positions for `task` PDA and authority signer slots per discriminator.
@@ -456,6 +460,15 @@ export function verifyBuiltTransaction(encoded, expected, operation = "shillbot.
456
460
  if (expected.feePayer !== undefined) {
457
461
  parseValidatedPubkey(expected.feePayer, "feePayer", operation);
458
462
  }
463
+ if (expected.sponsor !== undefined) {
464
+ parseValidatedPubkey(expected.sponsor, "sponsor", operation);
465
+ }
466
+ if (expected.feePayer !== undefined &&
467
+ expected.sponsor !== undefined &&
468
+ expected.feePayer !== expected.sponsor) {
469
+ throw invalidArg(operation, "expected.feePayer and expected.sponsor must match when both are provided");
470
+ }
471
+ const explicitFeePayer = expected.feePayer ?? expected.sponsor;
459
472
  if (expected.oracleAuthority !== undefined) {
460
473
  parseValidatedPubkey(expected.oracleAuthority, "oracleAuthority", operation);
461
474
  }
@@ -476,19 +489,22 @@ export function verifyBuiltTransaction(encoded, expected, operation = "shillbot.
476
489
  if (!inspected.signers.includes(expected.wallet)) {
477
490
  throw txMismatch(operation, "Wallet is not a required signer");
478
491
  }
479
- if (expected.feePayer && inspected.fee_payer !== expected.feePayer) {
492
+ if (explicitFeePayer && inspected.fee_payer !== explicitFeePayer) {
480
493
  throw txMismatch(operation, "Fee payer differs from expectation");
481
494
  }
482
495
  const isVerifyContext = expected.action === "verify" ||
483
496
  expected.instructionName === "verify_task" ||
484
497
  expected.instructionName === "verify_task_attested";
498
+ const isFinalizeContext = expected.action === "finalize" || expected.instructionName === "finalize_task";
485
499
  const shillbotId = SHILLBOT_PROGRAM_ID.toBase58();
500
+ const extensionCreditId = EXTENSION_CREDIT_PROGRAM_ID.toBase58();
486
501
  const alwaysAllowed = new Set([
487
502
  shillbotId,
488
503
  ComputeBudgetProgram.programId.toBase58(),
489
504
  ...(isVerifyContext
490
505
  ? [SWITCHBOARD_PROGRAM_IDS[expected.network], Secp256k1Program.programId.toBase58()]
491
506
  : []),
507
+ ...(isFinalizeContext ? [extensionCreditId] : []),
492
508
  ...(expected.allowedProgramIds ?? []),
493
509
  ]);
494
510
  const unrelated = inspected.instructions.find((instruction) => !alwaysAllowed.has(instruction.program_id));
@@ -529,6 +545,43 @@ export function verifyBuiltTransaction(encoded, expected, operation = "shillbot.
529
545
  }
530
546
  }
531
547
  verifyInstructionAuthorityAndShape(primary, inspected, expected, operation);
548
+ // Validate optional `extension-credit::route_and_recoup` companion on `finalize`
549
+ const creditIxs = inspected.instructions
550
+ .map((ix, txIndex) => ({ ix, txIndex }))
551
+ .filter((entry) => entry.ix.program_id === extensionCreditId);
552
+ if (creditIxs.length > 0) {
553
+ if (!isFinalizeContext || primary.spec.name !== "finalize_task") {
554
+ throw txMismatch(operation, "extension-credit instruction only permitted on finalize_task");
555
+ }
556
+ if (creditIxs.length !== 1) {
557
+ throw txMismatch(operation, "At most one extension-credit instruction is permitted");
558
+ }
559
+ const { ix: creditIx, txIndex: creditTxIndex } = creditIxs[0];
560
+ if (creditTxIndex <= primary.txIndex) {
561
+ throw txMismatch(operation, "route_and_recoup must execute after finalize_task");
562
+ }
563
+ const creditData = Buffer.from(creditIx.data_base64, "base64");
564
+ if (creditData.length !== 8 || !creditData.equals(routeAndRecoupDiscriminator())) {
565
+ throw txMismatch(operation, "Unsupported or malformed extension-credit instruction");
566
+ }
567
+ if (creditIx.accounts.length !== 3) {
568
+ throw txMismatch(operation, `route_and_recoup requires exact 3 accounts [advance, backer, recipient], got ${creditIx.accounts.length}`);
569
+ }
570
+ const [advanceAcct, backerAcct, recipientAcct] = creditIx.accounts;
571
+ if (advanceAcct !== primary.ix.accounts[2]) {
572
+ throw txMismatch(operation, `route_and_recoup advance account (${advanceAcct}) must match finalize_task payout slot [2] (${primary.ix.accounts[2]})`);
573
+ }
574
+ if (expectedPayoutPubkey && advanceAcct !== expectedPayoutPubkey.toBase58()) {
575
+ throw txMismatch(operation, `route_and_recoup advance account (${advanceAcct}) differs from expected.payoutTo (${expectedPayoutPubkey.toBase58()})`);
576
+ }
577
+ if (recipientAcct !== expected.wallet) {
578
+ throw txMismatch(operation, `route_and_recoup recipient (${recipientAcct}) must equal caller wallet (${expected.wallet})`);
579
+ }
580
+ const derivedAdvance = advancePda(backerAcct, recipientAcct).toBase58();
581
+ if (advanceAcct !== derivedAdvance) {
582
+ throw txMismatch(operation, `route_and_recoup advance account (${advanceAcct}) does not match canonical Advance PDA (${derivedAdvance})`);
583
+ }
584
+ }
532
585
  // Instruction-specific payload validation
533
586
  if (primary.spec.name === "create_task") {
534
587
  if (primary.data.length < 24) {
@@ -566,6 +619,31 @@ export function verifyBuiltTransaction(encoded, expected, operation = "shillbot.
566
619
  throw txMismatch(operation, `finalize_task payout recipient slot [2] (${primary.ix.accounts[2]}) differs from expected.payoutTo ${expectedPayoutPubkey.toBase58()}`);
567
620
  }
568
621
  }
622
+ else if (primary.spec.name === "expire_task") {
623
+ if (primary.data.length !== 8) {
624
+ throw txMismatch(operation, `expire_task instruction data must be exact 8-byte discriminator, got ${primary.data.length}`);
625
+ }
626
+ if (primary.ix.accounts.length !== 3 && primary.ix.accounts.length !== 4) {
627
+ throw txMismatch(operation, `expire_task requires 3 accounts (or 4 when expiring a Claimed task), got ${primary.ix.accounts.length}`);
628
+ }
629
+ if (expected.expireTaskState !== undefined) {
630
+ if (expected.expireTaskState === "open" ||
631
+ expected.expireTaskState === "submitted" ||
632
+ expected.expireTaskState === "approved") {
633
+ if (primary.ix.accounts.length !== 3) {
634
+ throw txMismatch(operation, `expire_task on ${expected.expireTaskState} task requires exact 3 accounts [task, global_state, client], got ${primary.ix.accounts.length}`);
635
+ }
636
+ }
637
+ else if (expected.expireTaskState === "claimed") {
638
+ if (primary.ix.accounts.length !== 4) {
639
+ throw txMismatch(operation, `expire_task on claimed task requires exact 4 accounts [task, global_state, client, agent_state], got ${primary.ix.accounts.length}`);
640
+ }
641
+ }
642
+ else {
643
+ throw invalidArg(operation, `Unsupported expireTaskState: ${String(expected.expireTaskState)}`);
644
+ }
645
+ }
646
+ }
569
647
  // Validate companion Shillbot instructions (strictly fail-closed)
570
648
  const validateClaimCompanion = (claimIx) => {
571
649
  if (primary.spec.name === "submit_work") {
@@ -590,6 +668,28 @@ export function verifyBuiltTransaction(encoded, expected, operation = "shillbot.
590
668
  const isSubmitLifecycle = expected.action === "submit" ||
591
669
  primary.spec.name === "submit_work" ||
592
670
  primary.spec.name === "submit_work_session";
671
+ const isVerifyLifecycle = expected.action === "verify" ||
672
+ primary.spec.name === "verify_task" ||
673
+ primary.spec.name === "verify_task_attested";
674
+ const isSponsoredClaimOrSubmit = (isClaimLifecycle || isSubmitLifecycle) && inspected.fee_payer !== expected.wallet;
675
+ const derivedAdvancePda = isSponsoredClaimOrSubmit
676
+ ? advancePda(inspected.fee_payer, expected.wallet)
677
+ : undefined;
678
+ const requiresAdvancePayout = isSponsoredClaimOrSubmit && explicitFeePayer === undefined;
679
+ if (inspected.fee_payer !== expected.wallet &&
680
+ explicitFeePayer === undefined &&
681
+ !isSponsoredClaimOrSubmit &&
682
+ !(primary.spec.name === "verify_task_attested" && primary.ix.accounts[2] === expected.wallet)) {
683
+ throw txMismatch(operation, `Fee payer ${inspected.fee_payer} differs from expected wallet ${expected.wallet}`);
684
+ }
685
+ if (requiresAdvancePayout &&
686
+ expectedPayoutPubkey &&
687
+ !expectedPayoutPubkey.equals(derivedAdvancePda)) {
688
+ throw txMismatch(operation, `set_payout_to destination pubkey ${expectedPayoutPubkey.toBase58()} differs from deterministic Advance PDA ${derivedAdvancePda.toBase58()}`);
689
+ }
690
+ const authorizedPayoutPubkey = requiresAdvancePayout
691
+ ? derivedAdvancePda
692
+ : (expectedPayoutPubkey ?? derivedAdvancePda);
593
693
  if (companions.length === 0) {
594
694
  if (expectedPayoutPubkey &&
595
695
  primary.spec.name !== "set_payout_to" &&
@@ -598,42 +698,91 @@ export function verifyBuiltTransaction(encoded, expected, operation = "shillbot.
598
698
  }
599
699
  return inspected;
600
700
  }
601
- if (isClaimLifecycle &&
701
+ if (isVerifyLifecycle &&
602
702
  companions.length === 1 &&
603
703
  companions[0].spec.name === "set_payout_to") {
604
- if (primary.spec.name !== "claim_task") {
704
+ if (companions[0].txIndex >= primary.txIndex) {
705
+ throw txMismatch(operation, "set_payout_to must precede verify instruction");
706
+ }
707
+ validateSetPayoutToInstruction(companions[0], expected.wallet, primary.ix.accounts[0], expectedPayoutPubkey, operation);
708
+ return inspected;
709
+ }
710
+ if ((isClaimLifecycle || isSubmitLifecycle) &&
711
+ companions.length === 1 &&
712
+ companions[0].spec.name === "set_payout_to") {
713
+ if (isClaimLifecycle && primary.spec.name !== "claim_task") {
605
714
  throw txMismatch(operation, "set_payout_to companion requires direct agent claim_task");
606
715
  }
607
- if (companions[0].txIndex <= primary.txIndex) {
716
+ if (isSubmitLifecycle && primary.spec.name !== "submit_work") {
717
+ throw txMismatch(operation, "set_payout_to companion requires direct agent submit_work");
718
+ }
719
+ if (isClaimLifecycle && companions[0].txIndex <= primary.txIndex) {
608
720
  throw txMismatch(operation, "payout route must follow claim");
609
721
  }
610
- validateSetPayoutToInstruction(companions[0], expected.wallet, primary.ix.accounts[0], expectedPayoutPubkey, operation);
722
+ validateSetPayoutToInstruction(companions[0], expected.wallet, primary.ix.accounts[0], authorizedPayoutPubkey, operation);
611
723
  return inspected;
612
724
  }
725
+ // Optional leading score-0 reopen (`verify_task` or `verify_task_attested` with score == 0)
726
+ // before `[claim_task, (set_payout_to?), submit_work]` (1d / Q4).
727
+ let submitCompanions = companions;
613
728
  if (isSubmitLifecycle &&
614
- companions.length === 1 &&
615
- (companions[0].spec.name === "claim_task" ||
616
- companions[0].spec.name === "claim_task_session")) {
729
+ (companions[0].spec.name === "verify_task" ||
730
+ companions[0].spec.name === "verify_task_attested")) {
731
+ const reopenIx = companions[0];
732
+ if (reopenIx.data.length !== 48 || reopenIx.ix.accounts.length < 3) {
733
+ throw txMismatch(operation, "Malformed score-0 reopen verify companion");
734
+ }
735
+ if (reopenIx.ix.accounts[0] !== primary.ix.accounts[0]) {
736
+ throw txMismatch(operation, "Score-0 reopen verify companion targets a different task PDA");
737
+ }
738
+ const scoreView = new DataView(reopenIx.data.buffer, reopenIx.data.byteOffset + 8, 8);
739
+ if (scoreView.getBigUint64(0, true) !== 0n) {
740
+ throw txMismatch(operation, "Reopen verify companion in submit bundle must have composite_score === 0");
741
+ }
742
+ if (reopenIx.spec.name === "verify_task_attested") {
743
+ const attester = reopenIx.ix.accounts[2];
744
+ if (!inspected.signers.includes(attester)) {
745
+ throw txMismatch(operation, "verify_task_attested reopen attester must be a signer");
746
+ }
747
+ if (expected.oracleAuthority && attester !== expected.oracleAuthority) {
748
+ throw txMismatch(operation, "verify_task_attested reopen attester mismatch");
749
+ }
750
+ }
751
+ else if (expected.oracleAuthority && reopenIx.ix.accounts[2] !== expected.oracleAuthority) {
752
+ throw txMismatch(operation, "verify_task reopen oracle mismatch");
753
+ }
754
+ submitCompanions = companions.slice(1);
755
+ if (submitCompanions.length > 0 && reopenIx.txIndex >= submitCompanions[0].txIndex) {
756
+ throw txMismatch(operation, "Score-0 reopen verify must precede claim_task");
757
+ }
758
+ }
759
+ if (isSubmitLifecycle &&
760
+ submitCompanions.length === 1 &&
761
+ (submitCompanions[0].spec.name === "claim_task" ||
762
+ submitCompanions[0].spec.name === "claim_task_session")) {
617
763
  if (expectedPayoutPubkey) {
618
764
  throw txMismatch(operation, "Expected set_payout_to instruction matching expected.payoutTo, but none was present");
619
765
  }
620
- if (primary.txIndex !== companions[0].txIndex + 1 ||
621
- !validateClaimCompanion(companions[0])) {
766
+ if (primary.txIndex !== submitCompanions[0].txIndex + 1 ||
767
+ !validateClaimCompanion(submitCompanions[0])) {
622
768
  throw txMismatch(operation, "Expected exactly one matching Shillbot lifecycle instruction");
623
769
  }
624
770
  return inspected;
625
771
  }
626
772
  if (isSubmitLifecycle &&
627
- companions.length === 2 &&
628
- companions[0].spec.name === "claim_task" &&
629
- companions[1].spec.name === "set_payout_to") {
773
+ submitCompanions.length === 2 &&
774
+ submitCompanions[0].spec.name === "claim_task" &&
775
+ submitCompanions[1].spec.name === "set_payout_to") {
776
+ const isClaimPayoutSubmit = submitCompanions[1].txIndex === submitCompanions[0].txIndex + 1 &&
777
+ primary.txIndex === submitCompanions[1].txIndex + 1;
778
+ const isClaimSubmitPayout = primary.txIndex === submitCompanions[0].txIndex + 1 &&
779
+ submitCompanions[1].txIndex === primary.txIndex + 1;
630
780
  if (primary.spec.name !== "submit_work" ||
631
- companions[1].txIndex !== companions[0].txIndex + 1 ||
632
- primary.txIndex !== companions[1].txIndex + 1 ||
633
- !validateClaimCompanion(companions[0])) {
781
+ (!isClaimPayoutSubmit && !isClaimSubmitPayout) ||
782
+ !validateClaimCompanion(submitCompanions[0])) {
634
783
  throw txMismatch(operation, "Expected exactly one matching Shillbot lifecycle instruction");
635
784
  }
636
- validateSetPayoutToInstruction(companions[1], expected.wallet, primary.ix.accounts[0], expectedPayoutPubkey, operation);
785
+ validateSetPayoutToInstruction(submitCompanions[1], expected.wallet, primary.ix.accounts[0], authorizedPayoutPubkey, operation);
637
786
  return inspected;
638
787
  }
639
788
  if (companions.some((c) => c.spec.name === "set_payout_to")) {
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const CLIENT_VERSION = "0.4.5";
1
+ export declare const CLIENT_VERSION = "0.4.7";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLIENT_VERSION = "0.4.5";
1
+ export const CLIENT_VERSION = "0.4.7";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swarm-tips/client",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "description": "Typed browser and Node.js clients for Swarm Tips, Shillbot, and Coordination Game",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",