Expand description
§jiminy-anchor
Interoperability bridge between Anchor-framework accounts and Jiminy
programs. No dependency on anchor-lang itself - this crate operates
purely on raw byte layouts and deterministic hash conventions.
§Anchor Account Format
Anchor accounts use an 8-byte discriminator at bytes [0..8],
computed as sha256("account:<TypeName>")[..8]. The remaining bytes
are Borsh-serialized data. Anchor’s zero_copy attribute uses
#[repr(C)] overlays but still prefixes the same 8-byte discriminator.
Anchor instructions also carry an 8-byte discriminator, computed as
sha256("global:<function_name>")[..8], placed at the start of
instruction data.
§What This Crate Provides
§Account discriminators
anchor_disc- compute the 8-byte Anchor account discriminator at compile timecheck_anchor_disc- validate an Anchor discriminator on raw account dataAnchorHeader- zero-copy overlay for the 8-byte Anchor discriminator
§Instruction discriminators
anchor_ix_disc- compute the 8-byte Anchor instruction discriminator at compile timecheck_anchor_ix_disc- validate an instruction discriminator on instruction data
§Body access
anchor_body/anchor_body_mut- get the body slice (bytes[8..]) from Anchor account datacheck_and_body- discriminator check + body slice in one callcheck_and_overlay/check_and_overlay_mut- discriminator check + Pod overlay on the body
§Cross-framework verification
check_anchor_with_layout_id- verify both Anchor disc and Jiminylayout_idcheck_anchor_with_version- verify disc + Jiminy layout_id + version for versioned interop
§AccountView helpers
load_anchor_account- validate owner + Anchor disc + borrow from anAccountViewload_anchor_overlay- validate owner + Anchor disc, borrow, then Pod overlay the body
§Integration Pattern: Anchor + Jiminy
Use Anchor for orchestration (instruction routing, account
deserialization, constraint macros) and Jiminy for the performance-critical
hot path (zero-copy reads, math, CPI guards). Jiminy’s zero_copy_layout!
accounts can coexist with Anchor’s #[account(zero_copy)] by sharing
a common #[repr(C)] body layout.
A typical pattern:
- Anchor program creates accounts with its discriminator.
- Jiminy helper program reads those accounts via
check_and_overlay. - If the Anchor body is itself a Jiminy layout (with
AccountHeader), usecheck_anchor_with_layout_idfor full cross-framework verification.
§Example: Reading an Anchor account from a Jiminy program
use jiminy_anchor::{anchor_disc, check_anchor_disc, anchor_body};
use jiminy_core::account::{pod_from_bytes, Pod, FixedLayout};
// Compute Anchor's discriminator for "Vault" at compile time.
const VAULT_DISC: [u8; 8] = anchor_disc("Vault");
#[repr(C)]
#[derive(Clone, Copy)]
struct AnchorVaultBody {
balance: [u8; 8], // u64 LE
authority: [u8; 32],
}
unsafe impl Pod for AnchorVaultBody {}
impl FixedLayout for AnchorVaultBody { const SIZE: usize = 40; }
fn read_anchor_vault(data: &[u8]) -> Result<&AnchorVaultBody, jiminy_core::ProgramError> {
check_anchor_disc(data, &VAULT_DISC)?;
pod_from_bytes::<AnchorVaultBody>(&data[8..])
}§Example: Routing Anchor instructions from a Jiminy program
use jiminy_anchor::anchor_ix_disc;
const IX_DEPOSIT: [u8; 8] = anchor_ix_disc("deposit");
const IX_WITHDRAW: [u8; 8] = anchor_ix_disc("withdraw");
fn process_instruction(data: &[u8]) -> ProgramResult {
let (disc, body) = data.split_at(8);
match disc.try_into().unwrap_or(&[0u8; 8]) {
&IX_DEPOSIT => process_deposit(body),
&IX_WITHDRAW => process_withdraw(body),
_ => Err(ProgramError::InvalidInstructionData),
}
}Structs§
- Anchor
Header - Zero-copy overlay for the 8-byte Anchor discriminator.
Functions§
- anchor_
body - Get the body of an Anchor account (everything after the 8-byte discriminator).
- anchor_
body_ mut - Get the mutable body of an Anchor account.
- anchor_
disc - Compute the Anchor 8-byte discriminator for an account type name at compile time.
- anchor_
event_ disc - Compute the Anchor 8-byte event discriminator at compile time.
- anchor_
ix_ disc - Compute the Anchor 8-byte instruction discriminator at compile time.
- check_
anchor_ disc - Validate that the first 8 bytes of account data match the expected Anchor discriminator.
- check_
anchor_ ix_ disc - Validate that instruction data starts with the expected Anchor instruction discriminator.
- check_
anchor_ with_ layout_ id - Validate that an Anchor
zero_copyaccount body carries a Jiminylayout_idat the expected position. - check_
anchor_ with_ version - Validate Anchor discriminator, Jiminy
layout_id, and Jiminyversionon a cross-framework account. - check_
and_ body - Convenience: check discriminator and return the body slice in one call.
- check_
and_ overlay - Check the Anchor discriminator and overlay a
Podtype on the body. - check_
and_ overlay_ mut - Check the Anchor discriminator and overlay a mutable
Podtype on the body. - check_
ix_ and_ body - Check the instruction discriminator and return the remaining instruction body (bytes after the 8-byte discriminator).
- load_
anchor_ account - Load an Anchor account from an
AccountView: validate owner + discriminator, then borrow the data. - load_
anchor_ overlay - Load an Anchor account, validate owner + discriminator, then overlay
the body as a
Podtype.