Skip to content

Add Token-2022 cpi-guard Pinocchio example - #701

Closed
MarkFeder wants to merge 1 commit into
solana-foundation:mainfrom
MarkFeder:tokens-token-2022-cpi-guard-pinocchio
Closed

MarkFeder wants to merge 1 commit into
solana-foundation:mainfrom
MarkFeder:tokens-token-2022-cpi-guard-pinocchio

Conversation

@MarkFeder

@MarkFeder MarkFeder commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue

Fixes #786

What and why

Adds a Pinocchio version of tokens/token-2022/cpi-guard, which only had an Anchor one, following the same kit + LiteSVM template as the other Token-2022 Pinocchio examples.

CpiGuard stops privileged token operations (transfer, burn, approve, close) authorized by the account owner from running through a CPI. Token-2022 doesn't allow enabling or disabling the guard through a CPI either, so unlike the other extension examples the program can't initialize it. Instead:

  • The program exposes one cpi_transfer instruction: a Token-2022 TransferChecked CPI (variant 12) from source to destination, signed by the source's owner. It reads decimals from the mint account (offset 44) so the checked transfer matches the mint.
  • The test enables and disables the guard client-side and shows its effect on that CPI.

The JS dependencies match the versions #765 set for the other non-anchor examples.

Testing

pnpm install --frozen-lockfile, tsc --noEmit, pnpm build, pnpm build-and-test and pnpm audit in a Linux container, plus cargo fmt --check, cargo clippy -D warnings and prettier --check:

Token-2022 CPI Guard (Pinocchio)
  ✔ Blocks a CPI transfer while CpiGuard is enabled and allows it once disabled
1 passing
No known vulnerabilities found

The test creates a mint and a funded source account with CpiGuard enabled, asserts cpi_transfer fails (FailedTransactionMetadata), disables the guard, and asserts the same call succeeds and the destination balance is 1. The success after disabling shows the guard caused the rejection.

AI disclosure

Check exactly one. See CONTRIBUTING.md.

  • No AI tooling was used beyond editor autocomplete.
  • AI tooling was used. Tool and extent: Claude Code helped write the program, the tests and this description; I reviewed the changes and the build and test results.

@MarkFeder
MarkFeder requested a review from dev-jodee as a code owner August 26, 2026 20:44
@greptile-apps

greptile-apps Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no new actionable issues found.

Summary

Adds a Pinocchio example that demonstrates Token-2022 CpiGuard.

  • A Pinocchio example shows when CpiGuard blocks an owner-signed transfer.

Reviews (2) · Last reviewed commit: "token-2022 cpi-guard: add pinocchio exam..." · Reviewed by Greptile

@MarkFeder

Copy link
Copy Markdown
Contributor Author

@amilz could you take a look at this one when you get a chance?

No open review threads left on it, so it is ready for maintainer review. It is one of 23 open Pinocchio ports I have up — they are independent and self-contained, so they can be reviewed and merged in any order: https://github.com/solana-developers/program-examples/pulls/MarkFeder

@MarkFeder

Copy link
Copy Markdown
Contributor Author

@dev-jodee friendly ping, could you review this when you get a chance? CI is green and it's mergeable. Thanks!

@MarkFeder

Copy link
Copy Markdown
Contributor Author

@dev-jodee gentle ping: this has been open since Aug 26 with no review. It's still green and mergeable, and I'm happy to rebase or change anything. These 3 are also what's blocking me from reopening the closed ports under the 3-PR limit. Thanks!

Ports the cpi-guard Token-2022 example to Pinocchio (the anchor example has
no native sibling). Unlike the other extension examples, CpiGuard cannot be
enabled or disabled through a CPI, so the program cannot initialize it;
instead the program exposes a cpi_transfer instruction (a Token-2022
TransferChecked CPI, reading the mint's decimals from account data) and the
example demonstrates the guard's effect.

The litesvm test creates a source account with CpiGuard enabled (client-side)
and funded, then shows the program's CPI transfer is rejected while the guard
is on and succeeds once it is disabled, asserting the destination balance.
Matches the kit + litesvm template of the other token-2022 pinocchio examples.
@MarkFeder
MarkFeder force-pushed the tokens-token-2022-cpi-guard-pinocchio branch from dc05d82 to f11b186 Compare November 4, 2026 18:21
@github-actions github-actions Bot added the needs-issue PR does not reference an issue a maintainer has accepted label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Hi @MarkFeder,

Thanks for the pull request. We ask that every change references an issue a maintainer has accepted, and there isn't one linked here.

These examples are a teaching resource and each one has to stay small enough to read in one sitting, so whether a new example or framework flavor belongs here is worth settling before anyone writes it. Agreeing on the approach in an issue first means nobody writes code that can't be merged. See CONTRIBUTING.md for the full workflow.

Please open an issue describing the change, or comment on the existing one. Once a maintainer has labelled it accepted, put Fixes #<issue> in this description and reopen this pull request; the check runs again and clears itself. Typos, broken links and comment-only fixes are exempt: write Linked issue: trivial instead.

@MarkFeder

Copy link
Copy Markdown
Contributor Author

Superseded by #793 (the bot closed this one and GitHub won't let me reopen it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-issue PR does not reference an issue a maintainer has accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

token-2022/cpi-guard: add a Pinocchio version

1 participant