Repository navigation
Conversation
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.
1 of 2 tasks
Contributor
[Medium impact] The PR appears safe to merge; no new actionable issues were found. SummaryAdds a Pinocchio example showing how Token-2022’s
Reviews (4) · Last reviewed commit: "token-2022 cpi-guard pinocchio: check th..." · Reviewed by Greptile |
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.
1 of 2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.CpiGuardstops 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:cpi_transferinstruction: a Token-2022TransferCheckedCPI (variant 12) from source to destination, signed by the source's owner. It checks that Token-2022 owns the mint account, then readsdecimalsfrom it (offset 44) so the checked transfer matches the mint.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-testandpnpm auditin a Linux container, pluscargo fmt --check,cargo clippy -D warningsandprettier --check:The test creates a mint and a funded source account with CpiGuard enabled, asserts
cpi_transferfails (FailedTransactionMetadata), disables the guard, and asserts the same call succeeds and the destination balance is1. 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).bash cicd.shandpnpm run deployboth nametoken_2022_cpi_guard_pinocchio_program.so, the filecargo build-sbfwrites; checked in Linux with a real build (same check as #794). The name is explicit rather than a*.soglob because pnpm runs scripts throughcmd.exeon Windows, which doesn't expand globs.AI disclosure
Check exactly one. See CONTRIBUTING.md.