Skip to content

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

Open
MarkFeder wants to merge 7 commits into
solana-foundation:mainfrom
MarkFeder:tokens-token-2022-cpi-guard-pinocchio
Open

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

Conversation

@MarkFeder

@MarkFeder MarkFeder commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue

Fixes #786

Replaces #701, which the PR-hygiene bot closed before the issue was accepted; GitHub doesn't let the author reopen it.

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 checks that Token-2022 owns the mint account, then reads decimals from it (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.

Also: unblocks CI for block-list

Adding a workspace member changes the root Cargo.lock, which makes CI rebuild every Pinocchio project, including transfer-hook/block-list/pinocchio, which is currently red on main too. That example is its own workspace with a gitignored Cargo.lock, so its CLI's solana-* = "4.3.2" requirements now resolve to Agave 4.4.1, which needs rustc 1.98.1 while rust-toolchain.toml pins 1.97. One commit holds those four crates at ~4.3.2 (what #773 merged with); with it the example passes tsc, the SBF and CLI builds, its Rust tests and pnpm audit on rustc 1.97.1. The same commit is in #792 and #793, so whichever merges second has nothing left to apply.

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
  ✔ Rejects a mint that Token-2022 does not own, before any CPI
2 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.

The second test passes a byte-for-byte copy of a real mint owned by the System Program, and asserts the program fails before any Token-2022 CPI and no tokens move. With the owner check deleted, this test fails (the fake mint reaches TransferChecked).

pnpm run deploy names token_2022_cpi_guard_pinocchio_program.so, the file cargo build-sbf writes; checked in Linux with a real build (same check as #794). The name is explicit rather than a *.so glob because pnpm runs scripts through cmd.exe on Windows, which doesn't expand globs.

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.

@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

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

Summary

Adds a Pinocchio example showing how CpiGuard blocks a token transfer through another program.

  • The Pinocchio CPI Guard example transfers only when the source guard allows it.

Both earlier comments are addressed: deployment names the crate’s output file, and the documentation points readers to pinocchio-token-2022.

Reviews (7) · Last reviewed commit: "token-2022 cpi-guard pinocchio: assert t..." · Reviewed by Greptile

@MarkFeder
MarkFeder force-pushed the tokens-token-2022-cpi-guard-pinocchio branch from 7e29851 to 8bd3daf Compare November 18, 2026 20:42
dev-jodee pushed a commit that referenced this pull request Nov 20, 2026
What and why
Adds a Pinocchio version of tokens/token-2022/memo-transfer, which only had an Anchor one. It follows the same kit + LiteSVM + @solana-program template as the merged Token-2022 Pinocchio examples and builds on the account-extension shape from immutable-owner (#696).

The single instruction creates a Token-2022 token account with the MemoTransfer extension enabled, so every transfer into it must be preceded by a memo. The program hand-builds three CPIs:

CreateAccount, with space for the extension.
InitializeAccount3 (variant 18): [18] + owner(32).
EnableRequiredMemoTransfers: MemoTransferExtension (30), sub-instruction Enable (0) → [30, 0].
Unlike ImmutableOwner, this extension is enabled after the account is initialized, and the enable must be signed by the account owner (the payer here). The account is 171 bytes: 165 base + 1 account-type byte + a 5-byte MemoTransfer TLV.

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

Also: unblocks CI for block-list
Adding a workspace member changes the root Cargo.lock, which makes CI rebuild every Pinocchio project, including transfer-hook/block-list/pinocchio, which is currently red on main too. That example is its own workspace with a gitignored Cargo.lock, so its CLI's solana-* = "4.3.2" requirements now resolve to Agave 4.4.1, which needs rustc 1.98.1 while rust-toolchain.toml pins 1.97. One commit holds those four crates at ~4.3.2 (what #773 merged with); with it the example passes tsc, the SBF and CLI builds, its Rust tests and pnpm audit on rustc 1.97.1. The same commit is in #792 and #793, so whichever merges second has nothing left to apply.

@dev-jodee dev-jodee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one comment adn i think you need to rebase

m => appendTransactionMessageInstruction(transferIx, m),
);
const blockedResult = svm.sendTransaction(await signTransactionMessageWithSigners(blockedTx));
assert.instanceOf(blockedResult, FailedTransactionMetadata, 'expected the guarded CPI transfer to be rejected');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only checks that the tx failed, not why, so the blocked case passes on any error, the anchor version pins it to TokenError::CpiGuardTransferBlocked, can we assert on that custom error code from blockedResult.err() instaed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1071609: the blocked case now asserts InstructionError(0, Custom(42)), i.e. TokenError::CpiGuardTransferBlocked, like the anchor test. Also rebased onto main.

MarkFeder and others added 7 commits November 20, 2026 21:02
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.
Matches the versions solana-foundation#765 set for the other non-anchor examples (kit 8.5, mocha 12, typescript 7) and adds the same pnpm-workspace.yaml overrides, which clears pnpm-audit. The signer role in the hand-built instruction takes 'as const' so kit 8 keeps it as a signer role, as the sibling tests on main do.
cargo build-sbf names the artifact after the crate, so deploy the *.so in the output dir instead of program.so, and stop cicd.sh if the build fails. pinocchio-token-2022 now exists in the workspace, so the comment says the instruction is hand-built to show its layout rather than claiming there is no crate.
pnpm runs package.json scripts through cmd.exe on Windows, which does not expand the *.so glob. Name the file cargo build-sbf writes instead, matching solana-foundation#794.
cpi_transfer read decimals from whatever account was passed as the mint. Token-2022 would still reject a fake mint inside TransferChecked, but an example should show the check: only read a mint's bytes once Token-2022 owns it. The new test passes a byte-for-byte copy of a real mint owned by the System Program and asserts the program fails before any Token-2022 CPI and no tokens move.
solana-foundation#800 removed every example's cicd.sh and replaced em dashes in comments; drop this example's cicd.sh and do the same.
… failure

The blocked case only checked that the transaction failed, so it passed on any error. Assert Token-2022's TokenError::CpiGuardTransferBlocked (Custom(42)) on the example's instruction, as the anchor test does.
@MarkFeder
MarkFeder force-pushed the tokens-token-2022-cpi-guard-pinocchio branch from 8bd3daf to 1071609 Compare November 20, 2026 20:12
@MarkFeder
MarkFeder requested a review from dev-jodee November 20, 2026 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants