CKB Pulse

All lessons · Rust scripts and ckb-std · Start here · Glossary

Partial load of a cell's type Script header to match code_hash and hash_typeAdvancedFrom one fix

From one fix · Check: right · Checked 2026-11-19

In a ckb-std script you can load a cell's type script into a fixed 49 byte buffer with syscalls::load_cell_by_field(..., CellField::Type), then compare code_hash at bytes 16..48 and hash_type at byte 48 of the molecule Script table, without allocating for args. A short buffer returns Err(SysError::LengthNotEnough) yet still fills the buffer, so treat that error like Ok, and treat ItemMissing as a cell with no type script.

Why it matters: Loading the full type Script of every input and output allocates for arbitrary, attacker-sized args, and treating LengthNotEnough as a failure would wrongly reject cells whose script header was in fact read correctly.

Pattern

Prefer
Ok(_) | Err(SysError::LengthNotEnough(_)) => { if b[16..48] == script.code_hash().as_slice()[..] && b[48] == script.hash_type().as_slice()[0]

Taken from the commit, shortened.

Concepts
Data encoding (molecule, hex, bigints), Cell deps and script lookup
Evidence
From one fix: from one project, checked against its commit
First seen
2026-10-07
Last changed
2026-10-07
Last checked
2026-11-19
Check against the commit
right: the lesson follows from its commit and still holds in the code today

Sources (1)

More about Rust scripts and ckb-std

All 35 in this topic

Do you maintain a project this lesson came from? Confirm or correct it

Cite this lesson
LusoCryptoLabs, "Partial load of a cell's type Script header to match code_hash and hash_type", CKB lessons, https://pulse.lusocryptolabs.com/learned/scripts/partial-load-of-a-cell-s-type-script-header-to-match-code-ha, CC BY 4.0, last changed 2026-10-07.

Drawn from public commits and checked against the commit each one came from and the project's code today. Read the source before you rely on one. Text under CC BY 4.0: credit LusoCryptoLabs, CKB lessons, with a link to the lesson. Want a lesson changed or removed? Open an issue.