-
Notifications
You must be signed in to change notification settings - Fork 34
feat: pinocchio ephemeral permission #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dodecahedr0x
wants to merge
17
commits into
main
Choose a base branch
from
dode/ephemeral-permission
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
683ee73
feat: pinocchio ephemeral permission
Dodecahedr0x 128a8e6
fix: import
Dodecahedr0x d0b4b08
fix: license
Dodecahedr0x ad2bf5c
feat: update vitest
Dodecahedr0x 39ea357
feat: undelegate
Dodecahedr0x d3f952f
feat: update permission
Dodecahedr0x a59a4ce
docs: update readme
Dodecahedr0x f4441d1
feat: safer ix data reads
Dodecahedr0x a195282
feat: rent for
Dodecahedr0x dc4bb71
feat: remove log
Dodecahedr0x 4d4e538
feat: check ownership
Dodecahedr0x c94d255
feat: reuse check
Dodecahedr0x 587bb77
style: lint
Dodecahedr0x 4c1a2af
docs: safety
Dodecahedr0x 7e0257e
chore: align names
Dodecahedr0x eb891bf
feat: confirmed get info
Dodecahedr0x 6d6caf0
feat: update rust
Dodecahedr0x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Private Key (optional - will be generated if not provided) | ||
| # PRIVATE_KEY=[...] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /target | ||
| **/*.rs.bk | ||
| .DS_Store | ||
| Cargo.lock | ||
| .env | ||
| node_modules/ | ||
| dist/ | ||
| build/ | ||
| *.log | ||
| .vscode | ||
| .idea | ||
| .pnp.cjs | ||
| .pnp.loader.mjs |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| nodeLinker: node-modules |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "pinocchio-ephemeral-permission-counter" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [features] | ||
| logging = [] | ||
|
|
||
| [dependencies] | ||
| pinocchio = { version = "0.10.1", features = ["cpi", "copy"] } | ||
| pinocchio-log = { version = "0.5.1" } | ||
| pinocchio-system = { version = "0.5" } | ||
| solana-address = { version = "2.6", features = ["syscalls"] } | ||
| ephemeral-rollups-pinocchio = { git = "https://github.com/magicblock-labs/ephemeral-rollups-sdk.git", rev = "a8ecea450d3bbe5bac119a3f4c55cd9f96b4837b" } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2025 MagicBlock Labs | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Pinocchio Ephemeral Permission Counter | ||
|
|
||
| A minimal Solana counter program built with [Pinocchio](https://github.com/anza-xyz/pinocchio) and MagicBlock Ephemeral Rollups. The example shows how to initialize a counter PDA, delegate it to an Ephemeral Rollup, protect it with ephemeral permissions, and commit the final state back to the base layer. | ||
|
|
||
| The program is `no_std`, does not use Borsh, and keeps account data in a fixed-size `Counter` struct. | ||
|
|
||
| ## Requirements | ||
|
|
||
| | Software | Version | Installation Guide | | ||
| | ---------- | ------- | ----------------------------------------------------------- | | ||
| | Solana CLI | 2.3.13 | [Install Solana](https://docs.anza.xyz/cli/install) | | ||
| | Rust | 1.87.0 | [Install Rust](https://www.rust-lang.org/tools/install) | | ||
| | Node.js | 24.10.0 | [Install Node](https://nodejs.org/en/download/current) | | ||
| | Yarn | 4.x | [Install Yarn](https://yarnpkg.com/getting-started/install) | | ||
|
|
||
| ## Setup | ||
|
|
||
| Install the TypeScript test dependencies: | ||
|
|
||
| ```bash | ||
| yarn install | ||
| ``` | ||
|
|
||
| The tests read `PRIVATE_KEY` from the environment, or fall back to `~/.config/solana/id.json`. | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| Optional RPC overrides: | ||
|
|
||
| - `PROVIDER_ENDPOINT` | ||
| - `WS_ENDPOINT` | ||
| - `EPHEMERAL_PROVIDER_ENDPOINT` | ||
| - `EPHEMERAL_WS_ENDPOINT` | ||
|
|
||
| ## Build | ||
|
|
||
| ```bash | ||
| yarn build | ||
| ``` | ||
|
|
||
| This runs: | ||
|
|
||
| ```bash | ||
| cargo build-sbf | ||
| ``` | ||
|
|
||
| ## Test | ||
|
|
||
| Run the Vitest integration flow: | ||
|
|
||
| ```bash | ||
| yarn test | ||
| ``` | ||
|
|
||
| The test initializes the counter on Solana devnet, delegates it to the Ephemeral Rollup, increments it on both layers, creates/updates/closes a permission account, and commits the delegated state back to Solana. | ||
|
|
||
| ## Program Model | ||
|
|
||
| The counter PDA is derived with: | ||
|
|
||
| ```text | ||
| ["counter", id] | ||
| ``` | ||
|
|
||
| where `id` is a 32-byte client-provided public key. The account stores: | ||
|
|
||
| | Field | Size | Description | | ||
| | ------- | -------- | --------------------------------- | | ||
| | `id` | 32 bytes | Identifier used in the PDA seeds | | ||
| | `count` | 8 bytes | Little-endian `u64` counter value | | ||
| | `bump` | 1 byte | PDA bump | | ||
| | `_pad` | 7 bytes | Alignment padding | | ||
|
|
||
| Total size: 48 bytes. | ||
|
|
||
| ## Instructions | ||
|
|
||
| Each instruction starts with an 8-byte little-endian discriminator. | ||
|
|
||
| | Discriminator | Instruction | Payload | Description | | ||
| | ------------- | --------------------- | --------------------- | ---------------------------------------------------------------------- | | ||
| | `0` | `InitializeCounter` | `id` (`[u8; 32]`) | Creates the counter PDA and initializes `count` to `0`. | | ||
| | `1` | `IncreaseCounter` | `increase_by` (`u64`) | Adds `increase_by` to the counter with overflow checking. | | ||
| | `2` | `Delegate` | None | Delegates the counter PDA to the Ephemeral Rollups delegation program. | | ||
| | `3` | `CommitAndUndelegate` | None | Commits the counter state and undelegates it back to the base layer. | | ||
| | `4` | `CreatePermission` | None | Creates a private ephemeral permission for the counter. | | ||
| | `5` | `UpdatePermission` | None | Updates the counter permission membership. | | ||
| | `6` | `ClosePermission` | None | Closes the counter permission account. | | ||
|
|
||
| The delegation program also invokes the undelegation callback discriminator: | ||
|
|
||
| ```text | ||
| [196, 28, 41, 206, 48, 37, 51, 167] | ||
| ``` |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| { | ||
| "name": "pinocchio-ephemeral-permission-counter", | ||
| "version": "0.1.0", | ||
| "description": "Tests for pinocchio-ephemeral-permission-counter program", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "vitest run tests/", | ||
| "test:watch": "vitest watch tests/", | ||
| "build": "cargo build-sbf", | ||
| "check": "cargo check" | ||
| }, | ||
| "keywords": [ | ||
| "solana", | ||
| "pinocchio", | ||
| "ephemeral", | ||
| "permission", | ||
| "counter", | ||
| "program" | ||
| ], | ||
| "author": "", | ||
| "license": "MIT", | ||
| "devDependencies": { | ||
| "@magicblock-labs/ephemeral-rollups-sdk": "^0.11.1", | ||
| "@solana-program/system": "^0.10.0", | ||
| "@solana/kit": "^5.4.0", | ||
| "@solana/web3.js": "^1.93.0", | ||
| "@types/node": "^20.0.0", | ||
| "borsh": "^0.7.0", | ||
| "dotenv": "^16.0.0", | ||
| "ts-node": "^10.9.0", | ||
| "tweetnacl": "^1.0.3", | ||
| "typescript": "^5.0.0", | ||
| "vitest": "^3.0.0" | ||
| } | ||
| } |
178 changes: 178 additions & 0 deletions
178
pinocchio-ephemeral-permission-counter/src/entrypoint.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| use crate::processor::{ | ||
| process_close_permission, process_commit_and_undelegate, process_create_permission, | ||
| process_delegate, process_increase_counter, process_initialize_counter, | ||
| process_undelegation_callback, process_update_permission, | ||
| }; | ||
| use core::{mem::MaybeUninit, slice::from_raw_parts}; | ||
| use pinocchio::{ | ||
| entrypoint::deserialize, error::ProgramError, no_allocator, nostd_panic_handler, AccountView, | ||
| Address, ProgramResult, MAX_TX_ACCOUNTS, SUCCESS, | ||
| }; | ||
|
|
||
| #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
| enum InstructionDiscriminator { | ||
| InitializeCounter, | ||
| IncreaseCounter, | ||
| Delegate, | ||
| CommitAndUndelegate, | ||
| CreatePermission, | ||
| UpdatePermission, | ||
| ClosePermission, | ||
| UndelegationCallback, | ||
| } | ||
|
|
||
| impl InstructionDiscriminator { | ||
| const INITIALIZE_COUNTER: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0]; | ||
| const INCREASE_COUNTER: [u8; 8] = [1, 0, 0, 0, 0, 0, 0, 0]; | ||
| const DELEGATE: [u8; 8] = [2, 0, 0, 0, 0, 0, 0, 0]; | ||
| const COMMIT_AND_UNDELEGATE: [u8; 8] = [3, 0, 0, 0, 0, 0, 0, 0]; | ||
| const CREATE_PERMISSION: [u8; 8] = [4, 0, 0, 0, 0, 0, 0, 0]; | ||
| const UPDATE_PERMISSION: [u8; 8] = [5, 0, 0, 0, 0, 0, 0, 0]; | ||
| const CLOSE_PERMISSION: [u8; 8] = [6, 0, 0, 0, 0, 0, 0, 0]; | ||
| // Undelegation callback called by the delegation program | ||
| const UNDELEGATION_CALLBACK: [u8; 8] = [196, 28, 41, 206, 48, 37, 51, 167]; | ||
|
|
||
| fn from_bytes(bytes: [u8; 8]) -> Result<Self, ProgramError> { | ||
| match bytes { | ||
| Self::INITIALIZE_COUNTER => Ok(Self::InitializeCounter), | ||
| Self::INCREASE_COUNTER => Ok(Self::IncreaseCounter), | ||
| Self::DELEGATE => Ok(Self::Delegate), | ||
| Self::COMMIT_AND_UNDELEGATE => Ok(Self::CommitAndUndelegate), | ||
| Self::CREATE_PERMISSION => Ok(Self::CreatePermission), | ||
| Self::UPDATE_PERMISSION => Ok(Self::UpdatePermission), | ||
| Self::CLOSE_PERMISSION => Ok(Self::ClosePermission), | ||
| Self::UNDELEGATION_CALLBACK => Ok(Self::UndelegationCallback), | ||
| _ => Err(ProgramError::InvalidInstructionData), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Do not allocate memory. | ||
| no_allocator!(); | ||
| // Use the no_std panic handler. | ||
| nostd_panic_handler!(); | ||
|
|
||
| #[no_mangle] | ||
| #[allow(clippy::arithmetic_side_effects)] | ||
| pub unsafe extern "C" fn entrypoint(input: *mut u8) -> u64 { | ||
| const UNINIT: MaybeUninit<AccountView> = MaybeUninit::<AccountView>::uninit(); | ||
| let mut accounts = [UNINIT; { MAX_TX_ACCOUNTS }]; | ||
|
|
||
| let (program_id, count, instruction_data) = | ||
| deserialize::<MAX_TX_ACCOUNTS>(input, &mut accounts); | ||
|
|
||
| match process_instruction( | ||
| program_id, | ||
| from_raw_parts(accounts.as_ptr() as _, count), | ||
| instruction_data, | ||
| ) { | ||
| Ok(()) => SUCCESS, | ||
| Err(error) => error.into(), | ||
| } | ||
| } | ||
|
|
||
| /// Log an error. | ||
| #[cold] | ||
| fn log_error(_error: &ProgramError) { | ||
| #[cfg(feature = "logging")] | ||
| pinocchio_log::log!("Program error"); | ||
| } | ||
|
|
||
| /// Process an instruction. | ||
| #[inline(always)] | ||
| pub fn process_instruction( | ||
| program_id: &Address, | ||
| accounts: &[AccountView], | ||
| instruction_data: &[u8], | ||
| ) -> ProgramResult { | ||
| let result = inner_process_instruction(program_id, accounts, instruction_data); | ||
| result.inspect_err(log_error) | ||
| } | ||
|
|
||
| /// Process an instruction. | ||
| #[inline(always)] | ||
| pub(crate) fn inner_process_instruction( | ||
| _program_id: &Address, | ||
| accounts: &[AccountView], | ||
| instruction_data: &[u8], | ||
| ) -> ProgramResult { | ||
| if instruction_data.len() < 8 { | ||
| return Err(ProgramError::InvalidInstructionData); | ||
| } | ||
|
|
||
| let discriminator: [u8; 8] = instruction_data[..8] | ||
| .try_into() | ||
| .map_err(|_| ProgramError::InvalidInstructionData)?; | ||
| let discriminator = InstructionDiscriminator::from_bytes(discriminator)?; | ||
| let payload = instruction_data | ||
| .get(8..) | ||
| .ok_or(ProgramError::InvalidInstructionData)?; | ||
|
|
||
| #[cfg(feature = "logging")] | ||
| log_instruction(discriminator); | ||
|
|
||
| match discriminator { | ||
| InstructionDiscriminator::InitializeCounter => { | ||
| let id = Address::new_from_array( | ||
| payload | ||
| .get(..32) | ||
| .ok_or(ProgramError::InvalidInstructionData)? | ||
| .try_into() | ||
| .map_err(|_| ProgramError::InvalidInstructionData)?, | ||
| ); | ||
| process_initialize_counter(accounts, &id) | ||
| } | ||
| InstructionDiscriminator::IncreaseCounter => { | ||
| let increase_by = read_u64(payload)?; | ||
| process_increase_counter(accounts, increase_by) | ||
| } | ||
| InstructionDiscriminator::Delegate => process_delegate(accounts), | ||
| InstructionDiscriminator::CommitAndUndelegate => process_commit_and_undelegate(accounts), | ||
| InstructionDiscriminator::CreatePermission => process_create_permission(accounts), | ||
| InstructionDiscriminator::UpdatePermission => process_update_permission(accounts), | ||
| InstructionDiscriminator::ClosePermission => process_close_permission(accounts), | ||
| InstructionDiscriminator::UndelegationCallback => { | ||
| process_undelegation_callback(accounts, payload) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn read_u64(input: &[u8]) -> Result<u64, ProgramError> { | ||
| if input.len() < 8 { | ||
| return Err(ProgramError::InvalidInstructionData); | ||
| } | ||
| let mut bytes = [0u8; 8]; | ||
| bytes.copy_from_slice(&input[..8]); | ||
| Ok(u64::from_le_bytes(bytes)) | ||
| } | ||
|
|
||
| #[allow(unused_variables)] | ||
| #[cfg(feature = "logging")] | ||
| fn log_instruction(discriminator: InstructionDiscriminator) { | ||
| match discriminator { | ||
| InstructionDiscriminator::InitializeCounter => { | ||
| pinocchio_log::log!("InitializeCounter"); | ||
| } | ||
| InstructionDiscriminator::IncreaseCounter => { | ||
| pinocchio_log::log!("IncreaseCounter"); | ||
| } | ||
| InstructionDiscriminator::Delegate => { | ||
| pinocchio_log::log!("Delegate"); | ||
| } | ||
| InstructionDiscriminator::CommitAndUndelegate => { | ||
| pinocchio_log::log!("CommitAndUndelegate"); | ||
| } | ||
| InstructionDiscriminator::CreatePermission => { | ||
| pinocchio_log::log!("CreatePermission"); | ||
| } | ||
| InstructionDiscriminator::UpdatePermission => { | ||
| pinocchio_log::log!("UpdatePermission"); | ||
| } | ||
| InstructionDiscriminator::ClosePermission => { | ||
| pinocchio_log::log!("ClosePermission"); | ||
| } | ||
| InstructionDiscriminator::UndelegationCallback => { | ||
| pinocchio_log::log!("UndelegationCallback"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.