Skip to content
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

docs: add how-tos on hash calculation #408

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions website/docs/how-tos/how-to-calculate-code-hash.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
id: how-to-culculate-code-hash
title: How to Calculate Code Hash
---

import Tooltip from "@components/Tooltip";

# How to Calculate Code Hash

Code hash is the hash of the code binary stored as the data in the code Cell. It is calculated as follows:

```
ckbhash(data)
```

Where:

- <Tooltip>ckbhash</Tooltip> is the BLAKE2b hash algorithm specifically used in CKB
RetricSu marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 30 additions & 0 deletions website/docs/how-tos/how-to-culculate-script-hash.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
id: how-to-culculate-script-hash
title: How to Calculate Script Hash
---

import Tooltip from "@components/Tooltip";

# How to Calculate Script Hash

Script hash is calculated as follows:

```
ckbhash(molecule_encode(script))
```

Where:

- <Tooltip>ckbhash</Tooltip> is the BLAKE2b hash algorithm specifically used in CKB
RetricSu marked this conversation as resolved.
Show resolved Hide resolved

- `molecule_encode` [serializes](https://docs.nervos.org/docs/serialization/serialization-molecule-in-ckb) a structure into a binary format with the Molecule serialization method
RetricSu marked this conversation as resolved.
Show resolved Hide resolved

The definition of Script is as follows:

```
table Script {
code_hash: Byte32,
hash_type: byte,
args: Bytes,
}
```
32 changes: 32 additions & 0 deletions website/docs/how-tos/how-to-culculate-tx-hash.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
id: how-to-culculate-tx-hash
title: How to Calculate Transaction Hash
---

import Tooltip from "@components/Tooltip";

# How to Calculate Transaction Hash

Transaction hash is calculated as follows:

```
ckbhash(molecule_encode(tx_excluding_witness))
```

Where:

- <Tooltip>ckbhash</Tooltip> is the BLAKE2b hash algorithm specifically used in CKB

- `molecule_encode` [serializes](https://docs.nervos.org/docs/serialization/serialization-molecule-in-ckb) a structure into a binary format with the Molecule serialization method

- `tx_excluding_wit` refers to the transaction structure excluding the witness field, i.e., a raw transaction
RetricSu marked this conversation as resolved.
Show resolved Hide resolved

The definition of `RawTransaction` is as follows:

```
table Script {
RetricSu marked this conversation as resolved.
Show resolved Hide resolved
code_hash: Byte32,
hash_type: byte,
args: Bytes,
}
```
9 changes: 9 additions & 0 deletions website/docs/tech-explanation/ckbhash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
id: ckbhash
title: ckbhash
---

CKB uses [BLAKE2b](<https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2b_algorithm>) as the default hash algorithm. The specific configuration of BLAKE2b used in CKB is referred as ckbhash, characterized by:

- A 32-byte output digest size
- A personalization of `ckb-default-hash`
8 changes: 7 additions & 1 deletion website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ export default {
label: "How-Tos",
className: "category-howto",
collapsible: false,
items: ["how-tos/how-to-sign-a-tx"],
items: [
"how-tos/how-to-sign-a-tx",
"how-tos/how-to-culculate-tx-hash",
"how-tos/how-to-culculate-script-hash",
"how-tos/how-to-culculate-code-hash",
RetricSu marked this conversation as resolved.
Show resolved Hide resolved
],
},
{
type: "category",
Expand All @@ -132,6 +137,7 @@ export default {
"tech-explanation/lock-type-diff",
"tech-explanation/script-group-exe",
"tech-explanation/witness",
"tech-explanation/ckbhash",
"tech-explanation/transaction",
"tech-explanation/cell-deps",
"tech-explanation/out-point",
Expand Down
22 changes: 11 additions & 11 deletions website/src/components/Tooltip/key-terms.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"block": {
"content": "A grouping of transactions, marked with a timestamp, and a fingerprint of the previous block.",
"link": "/docs/tech-explanation/glossary#block"
"link": "/docs/tech-explanation/block"
},
"block height": {
"content": "Block height is the total number of blocks that have been confirmed on the blockchain, also used to identify a unique block when specifying a particular block height, as there is always an exact block at any block height.",
Expand Down Expand Up @@ -397,7 +397,7 @@
},
"transaction": {
"content": "Transaction is the basic object created and signed by users to interact with distributed ledger.",
"link": "/docs/tech-explanation/glossary#transaction"
"link": "/docs/tech-explanation/transaction"
},
"transaction fee": {
"content": "A fee which is paid in the native token to miners in exchange for processing a transaction.",
Expand Down Expand Up @@ -561,11 +561,11 @@
},
"cell_deps": {
"content": "Pointers to Live Cells on the chain that allow Scripts in the transaction to access (read-only) referenced Live Cells.",
"link": "/docs/tech-explanation/glossary#cell-deps"
"link": "/docs/tech-explanation/cell-deps"
},
"ckbhash": {
"content": "CKB uses blake2b as the default hash algorithm with the following configurations:.",
"link": "/docs/tech-explanation/glossary#ckbhash"
"link": "/docs/tech-explanation/ckbhash"
},
"ckb merkle tree": {
"content": "CKB Merkle Tree is a CBMT( ) using following merge function:.",
Expand All @@ -589,11 +589,11 @@
},
"dep group": {
"content": "A method for referencing multiple dependencies which are commonly used together using a single dependency field.",
"link": "/docs/tech-explanation/glossary#dep-group"
"link": "/docs/tech-explanation/dep-group"
},
"dep type": {
"content": "A field that specifies the type of the dependency.",
"link": "/docs/tech-explanation/glossary#dep-type"
"link": "/docs/tech-explanation/dep-type"
},
"deps": {
"content": "A shorthand name for dependencies.",
Expand Down Expand Up @@ -717,7 +717,7 @@
},
"lock script": {
"content": "A Script that enforces access and ownership of a Cell.",
"link": "/docs/tech-explanation/glossary#lock-script"
"link": "/docs/tech-explanation/lock-script"
},
"lock script hash": {
"content": "A hash of a Lock Script which is used as an identifier for the Script when referenced by a Cell.",
Expand Down Expand Up @@ -825,7 +825,7 @@
},
"outpoint": {
"content": "A particular output Cell in a transaction.",
"link": "/docs/tech-explanation/glossary#outpoint"
"link": "/docs/tech-explanation/outpoint"
},
"output": {
"content": "A Live Cell that is created in a transaction.",
Expand Down Expand Up @@ -909,7 +909,7 @@
},
"since": {
"content": "since is the u64 (unsigned 64-bit integer) field in transaction input for preventing inclusion before a certain block timestamp or a block number.",
"link": "/docs/tech-explanation/glossary#since"
"link": "/docs/tech-explanation/since"
},
"spv": {
"content": "An abbreviation for Simplified Payment Verification.",
Expand Down Expand Up @@ -961,7 +961,7 @@
},
"type script": {
"content": "A Script that enforces the rules that must be followed in a transaction for a Cell to be consumed as an input or for a Cell to be created as an output.",
"link": "/docs/tech-explanation/glossary#type-script"
"link": "/docs/tech-explanation/type-script"
},
"type script hash": {
"content": "Or type_hash, a Blake2b hash of a Type Script which is used as an identifier for the Script when referenced by a Cell.",
Expand All @@ -981,7 +981,7 @@
},
"witness": {
"content": "A set of cryptographic proof containing the data required to prove authorization of the resources used in the transaction.",
"link": "/docs/tech-explanation/glossary#witness"
"link": "/docs/tech-explanation/witness"
},
"xudt": {
"content": "An extended UDT standard upon sUDT (Simple UDT) to accommodate additional functionalities, including regulatory compliance.",
Expand Down
Loading