Skip to content

Instantly share code, notes, and snippets.

@Flouse
Last active April 28, 2021 15:50
Show Gist options
  • Save Flouse/a5cab9f8fde8eb8e0939df151f3e96cb to your computer and use it in GitHub Desktop.
Save Flouse/a5cab9f8fde8eb8e0939df151f3e96cb to your computer and use it in GitHub Desktop.
Common Base Knowledge about CKB script programing

What is CKB?

An abbreviation which can have different meanings depending on the context:

Cells are the primary state units in CKB and assets owned by users. They must follow associated validation rules specified by scripts.

pub struct CellOutput {
    pub capacity: Capacity,
    pub lock: Script,
    #[serde(rename = "type")]
    pub type_: Option<Script>,
}

Example:

{
  "capacity": "0x19995d0ccf",
  "lock": {
    "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
    "args": "0x0a486fb8f6fe60f76f001d6372da41be91172259",
    "hash_type": "type"
  },
  "type": null
}

CKB scripts, running on CKB VM, actually perform series of validation rules on the input cells and output cells of the transaction. A transaction in CKB contains state transitions directly in the form of cells. To alter the state of an existing cell, one just destroys the original cell, then create a new one in a single, atomic transaction.

lock script

A script used to guard the cell: when the specified cell is used as an input cell in a transaction, the lock script included in a cell will be executed. The transaction will be rejected when the lock script fails in execution. One typical use case for lock script, is to represent the ownership of a cell, meaning a signature verification phase is usually included in the cell.

type script

An optional script used to validate cell structure. The type script of a cell will be executed both when the cell is included as an input cell, as well as when the cell is created as an output cell. Due to this nature, type script is typically used to validate dapp logic, such as creating UDTs.

Both lock script and type script use the same data structure:

pub struct Script {
    pub code_hash: H256,
    pub hash_type: ScriptHashType,
    pub args: JsonBytes,
}

Transaction

A CKB transaction A CKB transaction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment