Skip to main content

Crate jiminy_anchor

Crate jiminy_anchor 

Source
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 time
  • check_anchor_disc - validate an Anchor discriminator on raw account data
  • AnchorHeader - zero-copy overlay for the 8-byte Anchor discriminator

§Instruction discriminators

§Body access

§Cross-framework verification

§AccountView helpers

§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:

  1. Anchor program creates accounts with its discriminator.
  2. Jiminy helper program reads those accounts via check_and_overlay.
  3. If the Anchor body is itself a Jiminy layout (with AccountHeader), use check_anchor_with_layout_id for 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§

AnchorHeader
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_copy account body carries a Jiminy layout_id at the expected position.
check_anchor_with_version
Validate Anchor discriminator, Jiminy layout_id, and Jiminy version on 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 Pod type on the body.
check_and_overlay_mut
Check the Anchor discriminator and overlay a mutable Pod type 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 Pod type.