Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
CKB
https://github.com/nervoscommunity/docs
Admin
docs for ckb.dev
Tokens:
70,652
Snippets:
285
Trust Score:
3.7
Update:
1 year ago
Context
Chat
Benchmark
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Setting Up CKB Ruby SDK (Shell) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md This snippet outlines the shell commands to clone the CKB Ruby SDK repository, navigate into its directory, run the setup script to install plugins, and finally enter the Ruby REPL environment for interactive use. ```shell git clone https://github.com/nervosnetwork/ckb-sdk-ruby cd ckb-sdk-ruby ./bin/setup # 安装插件 ./bin/console # 进入 ruby REPL环境 ``` -------------------------------- ### Installing Project Dependencies with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command installs all necessary project dependencies using Yarn. It should be executed once after cloning the repository to set up the development environment. ```bash $ yarn ``` -------------------------------- ### Installing Ruby Environment with rbenv (Shell) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md This snippet provides shell commands to clone rbenv and ruby-build repositories, set up a Ruby China mirror (recommended for domestic users), list available Ruby versions, install a specific version (2.5.5), and verify Ruby and Gem versions. It also includes a command to install bundler. ```shell git clone https://github.com/rbenv/rbenv.git ~/.rbenv git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build # 使用 Ruby China 的镜像安装 Ruby, 国内用户推荐 git clone git://github.com/AndorChen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror rbenv install --list # 列出所有 ruby 版本 rbenv install 2.5.5 # 安装 2.5.5 ruby -v # 查看ruby版本 gem -v # 查看 gem版本 gem i bundler -V # 安装 bundler ``` -------------------------------- ### Example Start Withdrawal Epoch Object (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant represents an example parsed epoch object, similar to `depositEpoch`, but specifically for the epoch when a Nervos DAO withdrawal was initiated. ```JavaScript const startWithdrawingEpoch = { "length": "0xa", "index": "0x0", "number": "0xbe" } ``` -------------------------------- ### Starting Local Development Server with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command initiates a local development server and automatically opens a browser window. Most changes made to the code are reflected live without requiring a server restart, facilitating rapid development. ```bash $ yarn start ``` -------------------------------- ### Example Nervos DAO Function Calls (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This snippet shows commented-out example calls to the various Nervos DAO interaction functions defined in the file, demonstrating how they would be invoked sequentially for a full deposit and withdrawal cycle. ```JavaScript // deposit() // logDepositEpoch() // starWithdrawing() // logStartWithdrawingEpoch() // logCurrentEpoch() // withdraw() // setInterval(logCurrentEpoch, 1000) ``` -------------------------------- ### Setting Up CKB-VM Bench Scripts for Native C Benchmarking Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This snippet provides commands to clone and build the `ckb-vm-bench-scripts` repository, specifically checking out a particular commit. This setup is necessary to compile and run the native C version of the secp256k1 benchmark within a Docker environment, allowing for performance comparison with WASM-based implementations. ```shell cd $TOP git clone --recursive https://github.com/nervosnetwork/ckb-vm-bench-scripts cd ckb-vm-bench-scripts git checkout f7ab37c055b1a59bbc4f931c732331642c728c1d cd $TOP sudo docker run --rm -it -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20191209 bash cd /code/ckb-vm-bench-scripts make (omitted ...) exit exit ``` -------------------------------- ### Example Start Withdrawal OutPoint (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant provides an example `OutPoint` object for a transaction that initiates a Nervos DAO withdrawal. It is used to identify the specific cell output that represents the start of the withdrawal period. ```JavaScript const startWithDrawOutPoint = { "txHash": "0xc8ad01deb8b25c56169992598398ad7d539314ada90c84bff12fa7fc69095076", "index": "0x0" } ``` -------------------------------- ### Initializing CKB API and Wallet - Ruby Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/script/script-issue-udt.md This snippet initializes a connection to the CKB node, generates a new random private key, and constructs a CKB wallet from this key. It demonstrates the initial setup required to interact with the CKB blockchain using the Ruby SDK. ```Ruby pry(main)> api = CKB::API.new => #<API@http://localhost:8114> pry(main)> new_key = CKB::Key.random_private_key => "0xc5e72c391909d695fb02a9ee40e55f0ffcd5188591373f6fe0013ad4b89a5342" pry(main)> wallet = CKB::Wallet.from_hex(api, new_key) => #<CKB::Wallet:0x00007fffe3ea57a0 @addr= #<CKB::Address:0x00007fffe3ea4f08 @mode="testnet", @prefix="ckt", @script=#<CKB::Types::Script:0x00007fffe3ea4fd0 @args="0xa596693ee95b3901a59a83fa60b1247511d2d4dd", @code_hash="0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", @hash_type="type">>, @address="ckt1qyq2t9nf8m54kwgp5kdg87nqkyj82ywj6nwsl67j63", @api=#<API@http://localhost:8114>, @blake160="0xa596693ee95b3901a59a83fa60b1247511d2d4dd", @hash_type="type", @key=#<CKB::Key:0x00007fffe3ea7b68 @privkey="0xc5e72c391909d695fb02a9ee40e55f0ffcd5188591373f6fe0013ad4b89a5342", @pubkey="0x02ce875d7dd28539e5d6d74a1ba2743082be36e5465df1181292436ca0dac1dae6">, @pubkey="0x02ce875d7dd28539e5d6d74a1ba2743082be36e5465df1181292436ca0dac1dae6", @skip_data_and_type=true> ``` -------------------------------- ### Querying Transaction Pool Information (Bash) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This `bash` example demonstrates how to retrieve current information about the CKB node's transaction pool using the `tx_pool_info` RPC method. It sends a JSON-RPC request with an empty `params` array to get the pool's status. ```bash echo '{ "id": 2, "jsonrpc": "2.0", "method": "tx_pool_info", "params": [] }' \ | tr -d '\n' \ | curl -H 'content-type: application/json' -d @- \ http://localhost:8114 ``` -------------------------------- ### CKB Transaction Structure Example Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-19-ckbscript-09.md An example of a CKB transaction (tx.json) structure, detailing `cell_deps`, `header_deps`, `inputs`, `outputs`, `witnesses`, and `outputs_data`. This JSON is used as input for the CKB debugger. ```JSON { "cell_deps": [ { "out_point": { "tx_hash": "0xfcd1b3ddcca92b1e49783769e9bf606112b3f8cf36b96cac05bf44edcf5377e6", "index": "0x0" }, "dep_type": "code" }, { "out_point": { "tx_hash": "0xfcd1b3ddcca92b1e49783769e9bf606112b3f8cf36b96cac05bf44edcf5377e6", "index": "0x1" }, "dep_type": "code" } ], "header_deps": [ "0x51d199c4060f703344eab3c9b8794e6c60195ae9093986c35dba7c3486224409" ], "inputs": [ { "previous_output": { "tx_hash": "0xa98c57135830e1b91345948df6c4b8870828199a786b26f09f7dec4bc27a73da", "index": "0x0" }, "since": "0x0" } ], "outputs": [ { "capacity": "0x0", "lock": { "args": "0x", "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "hash_type": "data" }, "type": null } ], "witnesses": [ "0x210000000c0000001d0000000d0000006920616d20612073656372657400000000" ], "outputs_data": [ "0x" ] } ``` -------------------------------- ### Example Multisig Script for CKB Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0021-ckb-address-format/0021-ckb-address-format.md This example demonstrates a specific multisig script configuration. It sets S=0 (format version), R=1 (Cipher must approve, as their key is first), M=2 (any two signatures required), and N=3 (total participants). The script then lists the Blake160 hashes of Cipher's, Alice's, and Bob's public keys in order. ```plaintext 0 | 1 | 2 | 3 | Pk_Cipher_h | Pk_Alice_h | Pk_Bob_h ``` -------------------------------- ### Compiling and Running Eaglesong C Implementation Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0010-eaglesong/0010-eaglesong.md This snippet provides instructions for compiling and running the C reference implementation of the Eaglesong hash function. It uses 'gcc' to compile 'eaglesong.c' and 'hash.c' into an executable named 'hash', then demonstrates how to run it and input a string from stdin to get the hash output. ```Bash $> gcc -o hash eaglesong.c hash.c $> ./hash > Hello, world! > [Ctrl-D] > 64867e2441d162615dc2430b6bcb4d3f4b95e4d0db529fca1eece73c077d72d6 ``` -------------------------------- ### Defining `no_std` Entry Point for CKB Rust Contract (Rust) Source: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md This Rust code snippet provides the essential boilerplate for a `no_std` application, which is required for CKB contracts due to the RISC-V target's limitations. It includes `no_std`, `no_main`, `start` feature, `lang_items`, a `start` function as the entry point, a `panic_handler`, and `eh_personality` for exception handling. ```Rust #![no_std] #![no_main] #![feature(start)] #![feature(lang_items)] #[no_mangle] #[start] pub fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 } #[panic_handler] fn panic_handler(_: &core::panic::PanicInfo) -> ! { loop {} } #[lang = "eh_personality"] extern "C" fn eh_personality() {} ``` -------------------------------- ### C Implementation of Load Script Hash Syscall Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md The `ckb_load_script_hash` function provides a C interface for the *Load Script Hash* syscall (ID 2062). It calculates the hash of the current running script and copies it to the VM memory space. The `addr`, `len`, and `offset` arguments are used for partial loading, specifying the destination address, data length, and starting offset. ```C int ckb_load_script_hash(void* addr, uint64_t* len, size_t offset) { return syscall(2062, addr, len, offset, 0, 0, 0); } ``` -------------------------------- ### Starting Duktape REPL for CKB Script Debugging in Shell Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-02-21-ckbscript-07.md This script navigates to the 'htlc-runner' directory and executes 'runner.js' with debug logging. Instead of running a transaction, it uses the 'repl0' Duktape binary to launch an interactive Duktape REPL, allowing developers to test and debug CKB scripts interactively. ```Shell $ cd $TOP/htlc-runner $ RUST_LOG=debug `./runner.js ../ckb-duktape/build/repl0 ../htlc-template/build/duktape.js` ``` -------------------------------- ### Generating CKB Addresses (YAML) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0021-ckb-address-format/0021-ckb-address-format.md This YAML snippet demonstrates the process of generating different CKB address types. It includes examples for short addresses (both standard and multi-signature) and full addresses, showing the input arguments required for encoding and the final generated address strings. ```YAML == short address (code_hash_index = 0x00) test == args to encode: b39bbc0b3673c7d36450bc14cfcdad2d559c6c64 address generated: ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v == short address (code_hash_index = 0x01) test == multi sign script: 00 | 01 | 02 | 03 | bd07d9f32bce34d27152a6a0391d324f79aab854 | 094ee28566dff02a012a66505822a2fd67d668fb | 4643c241e59e81b7876527ebff23dfb24cf16482 args to encode: 4fb2be2e5d0c1a3b8694f832350a33c1685d477a address generated: ckb1qyq5lv479ewscx3ms620sv34pgeuz6zagaaqklhtgg == full address test == code_hash to encode: 9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8 with args to encode: b39bbc0b3673c7d36450bc14cfcdad2d559c6c64 full address generated: ckb1qjda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xw3vumhs9nvu786dj9p0q5elx66t24n3kxgj53qks ``` -------------------------------- ### Redefining CKB Contract Entry Point to `_start` (Rust) Source: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md This Rust snippet redefines the contract's entry point from `#[start]` to a `_start` function. This change is necessary because `riscv64imac-unknown-none-elf` handles entry points differently, and returning from `_start` would result in an `InvalidPermission` error. The function is designed to never return. ```Rust #[no_mangle] pub fn _start() -> ! { loop{} } ``` -------------------------------- ### C Implementation of Load Transaction Hash Syscall Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md The `ckb_load_tx_hash` function provides a C interface for the *Load Transaction Hash* syscall (ID 2061). It calculates the hash of the current transaction and copies it to the VM memory space. The `addr`, `len`, and `offset` arguments are used for partial loading, specifying the destination address, data length, and starting offset. ```C int ckb_load_tx_hash(void* addr, uint64_t* len, size_t offset) { return syscall(2061, addr, len, offset, 0, 0, 0); } ``` -------------------------------- ### Estimating Fee Rate using RPC (Bash) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This snippet demonstrates how to call the `estimate_fee_rate` RPC method using `curl` to get an estimated transaction fee rate. It sends a JSON-RPC request with a specified `fee_rate` parameter (0xa in this example). ```bash echo '{ "id": 2, "jsonrpc": "2.0", "method": "estimate_fee_rate", "params": [ "0xa" ] }' \n| tr -d '\n' \n| curl -H 'content-type: application/json' -d @- \nhttp://localhost:8114 ``` -------------------------------- ### Building Static Website Content with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command generates static content for the website and places it into the `build` directory. The resulting static files can then be served using any static content hosting service. ```bash $ yarn build ``` -------------------------------- ### Benchmarking Secp256k1 Performance on CKB-VM Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This snippet executes the compiled secp256k1 binaries (`secp_wabt` and `secp_wavm_patched`) using a `runner.rb` script to measure their performance on the CKB-VM. It displays file sizes and detailed cycle consumption metrics (total, transfer, and running cycles) for different input values, demonstrating the performance differences between the WABT and WAVM approaches. ```shell ls -l secp_wabt secp_wavm_patched -rwxrwxr-x 1 ubuntu 1791744 Mar 3 05:27 secp_wabt* -rw-rw-r-- 1 ubuntu 1800440 Mar 3 05:29 secp_wavm_patched ./runner.rb secp_wabt 0x01010000 Run result: Ok(0) Total cycles consumed: 35702943 Transfer cycles: 438060, running cycles: 35264883 ./runner.rb secp_wabt 0x01050000 Run result: Ok(0) Total cycles consumed: 90164183 Transfer cycles: 438060, running cycles: 89726123 ./runner.rb secp_wavm_patched 0x01010000 Run result: Ok(0) Total cycles consumed: 10206568 Transfer cycles: 428764, running cycles: 9777804 ./runner.rb secp_wavm_patched 0x01050000 Run result: Ok(0) Total cycles consumed: 49307936 Transfer cycles: 428764, running cycles: 48879172 ``` -------------------------------- ### Deploying Website to GitHub Pages with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command provides a convenient way to build the website and push it to the `gh-pages` branch, specifically for hosting on GitHub Pages. It requires specifying a GitHub username and using SSH for authentication. ```bash $ GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy ``` -------------------------------- ### Example Deposit Epoch Object (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant represents an example parsed epoch object, containing the length, index, and number of the epoch. This structure is typically returned by `ckb.utils.parseEpoch`. ```JavaScript const depositEpoch = { "length": "0xa", "index": "0x0", "number": "0x69" } ``` -------------------------------- ### Example Final Withdrawal OutPoint (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant provides an example `OutPoint` object for a transaction that completes a Nervos DAO withdrawal. It identifies the cell output representing the final state of the withdrawn funds. ```JavaScript const withDrawOutPoint = { "txHash": "0xb1ee185a4e811247b1705a52df487c3ce839bfa2f72e4c7a74b6fc6b0ea4cfa7", "index": "0x0" } ``` -------------------------------- ### Compiling and Patching Secp256k1 Binaries for CKB-VM Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This snippet outlines the compilation process for both WABT and WAVM versions of the secp256k1 code using `riscv64-unknown-elf-gcc` within a Docker container. It specifies include paths and linker scripts. Finally, it uses `ckb-binary-patcher` to optimize the WAVM binary for CKB-VM execution. ```shell cd $TOP sudo docker run --rm -it -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20191209 bash cd /code/ riscv64-unknown-elf-gcc -O3 -I ckb-c-stdlib -I wavm-aot-generator -I wabt/wasm2c secp_wabt_main.c secp.c wabt/wasm2c/wasm-rt-impl.c -o secp_wabt riscv64-unknown-elf-gcc -O3 -I ckb-c-stdlib -I wavm-aot-generator -I wabt/wasm2c secp_wavm_main.c wavm-aot-generator/abi/riscv64_runtime.S secp_precompiled.o -o secp_wavm -Wl,-T wavm-aot-generator/abi/riscv64.lds exit exit ckb-binary-patcher/target/release/ckb-binary-patcher -i secp_wavm -o secp_wavm_patched ``` -------------------------------- ### Benchmarking Native C Secp256k1 on CKB-VM Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This snippet executes the native C version of the secp256k1 benchmark (`secp256k1_bench`) using the `runner.rb` script. It shows the total, transfer, and running cycles consumed for different input values, providing a direct performance comparison point against the WASM-compiled versions. ```shell ./runner.rb ckb-vm-bench-scripts/build/secp256k1_bench 0x01010000 Run result: Ok(0) Total cycles consumed: 1621594 Transfer cycles: 272630, running cycles: 1348964 ./runner.rb ckb-vm-bench-scripts/build/secp256k1_bench 0x01050000 Run result: Ok(0) Total cycles consumed: 7007598 Transfer cycles: 272630, running cycles: 6734968 ``` -------------------------------- ### Example Deposit OutPoint (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant provides an example `OutPoint` object, which identifies a specific cell output on the CKB blockchain. It is typically returned after a successful transaction and used as an input for subsequent transactions. ```JavaScript const depositOutPoint = { "txHash": "0x40e1d58cf8576d5206d55d242284a28f64cb114d0b9a8292582e7596082e5bda", "index": "0x0" } ``` -------------------------------- ### Building Simple UDT Script - Bash Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0025-simple-udt/0025-simple-udt.md These bash commands provide steps to reproducibly build the Simple UDT script from the Nervos CKB miscellaneous scripts repository. The process involves cloning the repository, navigating into it, checking out a specific commit, updating submodules, and then building all components using Docker, allowing verification against the deployed script. ```Bash $ git clone https://github.com/nervosnetwork/ckb-miscellaneous-scripts $ cd ckb-miscellaneous-scripts $ git checkout 175b8b0933340f9a7b41d34106869473d575b17a $ git submodule update --init $ make all-via-docker ``` -------------------------------- ### C Main Function for WAVM-Precompiled Secp256k1 Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This C code defines the `main` function for the WAVM-precompiled secp256k1 test. It loads a `value` from the CKB witness, then iteratively calls the `wavm_exported_function_run` from the precompiled WAVM module, accumulating the results. It also includes `ckb_debug` for tracing. ```c #include "secp_precompiled_glue.h" #include "abi/ckb_vm_wasi_abi.h" #include "ckb_syscalls.h" int main() { uint32_t value; uint64_t len = 4; int ret = ckb_load_witness((void*) &value, &len, 0, 0, CKB_SOURCE_GROUP_INPUT); if (ret != CKB_SUCCESS) { return ret; } if (len < 4) { return -1; } uint32_t times = value >> 8; value = value & 0xFF; uint8_t result = 0; for (int i = 0; i < times; i++) { ckb_debug("One run!"); wavm_ret_int32_t wavm_ret = wavm_exported_function_run(NULL, value); result += wavm_ret.value; } return result; } ``` -------------------------------- ### Querying Address and Balance with CKB Ruby Wallet Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/start-build-dev.md This snippet demonstrates how to initialize a CKB wallet from a private key and query its associated address, public key, private key, Blake160 hash (lock_arg), and current balance. It showcases essential wallet functionalities for managing accounts on the CKB network. ```Ruby _priv_key = "fe59445edc3c30db6b0e1abcddc317137368f5604ce01cc1d279dfda001e8474" api = CKB::API.new wallet = CKB::Wallet.from_hex(api, "0x"+_priv_key) wallet.address wallet.key.privkey wallet.key.pubkey wallet.blake160 wallet.get_balance ``` -------------------------------- ### Example Response for get_block_by_number RPC (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON snippet provides an example of the response returned by the `get_block_by_number` RPC method. It contains comprehensive block information including `header` details, `proposals`, `transactions`, and `uncles`. ```json { "id": 2, "jsonrpc": "2.0", "result": { "header": { "compact_target": "0x1e083126", "dao": "0xb5a3e047474401001bc476b9ee573000c0c387962a38000000febffacf030000", "epoch": "0x7080018000001", "hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40", "nonce": "0x0", "number": "0x400", "parent_hash": "0xae003585fa15309b30b31aed3dcf385e9472c3c3e93746a6c4540629a6a1ed2d", "proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp": "0x5cd2b117", "transactions_root": "0xc47d5b78b3c4c4c853e2a32810818940d0ee403423bea9ec7b8e566d9595206c", "uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "version": "0x0" }, "proposals": [], "transactions": [ { "cell_deps": [], "hash": "0x365698b50ca0da75dca2c87f9e7b563811d3b5813736b8cc62cc3b106faceb17", "header_deps": [], "inputs": [ { "previous_output": { "index": "0xffffffff", "tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000000" }, "since": "0x400" } ], "outputs": [ { "capacity": "0x18e64b61cf", "lock": { "args": "0x", "code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", "hash_type": "data" }, "type": null } ], "outputs_data": [ "0x" ], "version": "0x0", "witnesses": [ "0x450000000c000000410000003500000010000000300000003100000028e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5000000000000000000" ] } ], "uncles": [] } } ``` -------------------------------- ### CKB `since` Field Examples Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0017-tx-valid-since/0017-tx-valid-since.md This section provides examples of `since` field values for both absolute and relative time locks in CKB. It illustrates how different flag and value combinations correspond to block number, timestamp, and epoch-based lock conditions, demonstrating how transactions fail verification if the specified conditions are not met. ```txt # Absolute time lock 0x0000_0000_0000_3039 # The tx failed verification unless the block number is greater than #12345 0x4000_0000_5e83_d980 # The tx failed verification unless current blockchain date is later than 2020-04-01 0x2000_0000_0000_0400 # The tx failed verification unless the epoch number is greater than 1024 # Relative time lock 0x8000_0000_0000_0064 # The tx failed verification unless it is 100 blocks later since the input cell get confirmed on-chain 0xc000_0000_0012_7500 # The tx failed verification unless it is 14 days(blockchain time) later since the input cell get confirmed on-chain 0xa000_0000_0000_0018 # The tx failed verification unless it is 24 epochs later since the input cell get confirmed on-chain ``` -------------------------------- ### Example Nervos CKB Block Structure in JSON Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md This JSON snippet illustrates the complete structure of a Nervos CKB block, including its header, uncle blocks, proposals, and a detailed transaction. It showcases the nested nature of CKB data types like `CellInput` and `OutPoint` within a transaction, providing a concrete example of how these elements are represented. ```JSON { "uncles": [ ``` -------------------------------- ### Querying CKB Address Details and Balance with Ruby SDK Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md This Ruby snippet demonstrates how to derive a CKB wallet from a private key and then query its associated address, public key, private key, lock_arg (blake160 hash), and current balance. It initializes `CKB::API` and `CKB::Wallet` objects to perform these operations. ```ruby _priv_key = "fe59445edc3c30db6b0e1abcddc317137368f5604ce01cc1d279dfda001e8474" api = CKB::API.new => #<API@http://localhost:8114> wallet = CKB::Wallet.from_hex(api, "0x"+_priv_key) wallet.address => "ckt1qyqrc4wkvc95f2wxguxaafwtgavpuqnqkxzqs0375w" wallet.key.privkey #私钥 => "0xfe59445edc3c30db6b0e1abcddc317137368f5604ce01cc1d279dfda001e8474" wallet.key.pubkey #公钥 => "0x0351d114becc7f9ad1986b99951969c67bae6d00252c4901a6a3ef70ff1eb57dd4" wallet.blake160 #lock_arg => "0x3c55d6660b44a9c6470ddea5cb47581e0260b184" wallet.get_balance # 余额 => 103265972696 ``` -------------------------------- ### Initializing CKB Rust Contract Project (Shell) Source: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md This snippet demonstrates how to set up the basic project structure for CKB Rust contracts and their associated test code. It creates two new cargo projects: `ckb-rust-demo` for testing and `contract` for the actual contract code. ```Shell cargo new --lib ckb-rust-demo cd ckb-rust-demo cargo new contract ``` -------------------------------- ### Example CKB Transaction Structure (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md This JSON snippet illustrates a complete CKB transaction, showcasing its various fields such as version, cell dependencies, header dependencies, inputs, outputs, output data, and witnesses. It serves as a concrete example of how a transaction is structured on the CKB blockchain. It includes details like `out_point` for dependencies and `lock` scripts for outputs. ```json { "version": "0x0", "cell_deps": [ { "out_point": { "tx_hash": "0xbd864a269201d7052d4eb3f753f49f7c68b8edc386afc8bb6ef3e15a05facca2", "index": "0x0" }, "dep_type": "dep_group" } ], "header_deps": [ "0xaa1124da6a230435298d83a12dd6c13f7d58caf7853f39cea8aad992ef88a422" ], "inputs": [ { "previous_output": { "tx_hash": "0x8389eba3ae414fb6a3019aa47583e9be36d096c55ab2e00ec49bdb012c24844d", "index": "0x1" }, "since": "0x0" } ], "outputs": [ { "capacity": "0x746a528800", "lock": { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x56008385085341a6ed68decfabb3ba1f3eea7b68", "hash_type": "type" }, "type": null }, { "capacity": "0x1561d9307e88", "lock": { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x886d23a7858f12ebf924baaacd774a5e2cf81132", "hash_type": "type" }, "type": null } ], "outputs_data": [ "0x", "0x" ], "witnesses": ["0x55000000100000005500000055000000410000004a975e08ff99fa0001\n 42ff3b86a836b43884b5b46f91b149f7cc5300e8607e633b7a29c94dc01c6616a12f62e74a1\n 415f57fcc5a00e41ac2d7034e90edf4fdf800"] } ``` -------------------------------- ### Initializing CKB SDK and Generating Addresses (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This snippet initializes the CKB SDK, connects to a specified node URL, and derives public key, public key hash, and address from a given private key. It sets up the basic environment for subsequent CKB operations. ```JavaScript const CKB = require('../lib').default const nodeUrl = process.env.NODE_URL || 'http://localhost:8114' // example node url const ckb = new CKB(nodeUrl) const sk = process.env.PRIV_KEY || '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' // example private key const pk = ckb.utils.privateKeyToPublicKey(sk) const pkh = `0x${ckb.utils.blake160(pk, 'hex')}` const addr = ckb.utils.privateKeyToAddress(sk) ``` -------------------------------- ### Compiling and Running Eaglesong C Implementation Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0010-eaglesong/0010-eaglesong.zh(需要相关的技术背景才能翻译好,暂留不发布).md This snippet provides shell commands to compile and execute the C reference implementation of the Eaglesong hash function. It demonstrates using `gcc` to link `eaglesong.c` and `hash.c` into an executable named `hash`, then running it and providing input via standard input (stdin) to compute and display the hash. ```Shell $> gcc -o hash eaglesong.c hash.c $> ./hash > Hello, world! > [Ctrl-D] > 64867e2441d162615dc2430b6bcb4d3f4b95e4d0db529fca1eece73c077d72d6 ``` -------------------------------- ### Serializing multiple bytes in a fixed-size vector (Bytes) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md Example of a `fixvec` containing 8 bytes (`0x1234567890abcdef`). The serialization includes a 4-byte length prefix (representing 8 items) followed by the 8 bytes. ```hex 08 00 00 00 12 34 56 78 90 ab cd ef ``` -------------------------------- ### Serializing a struct with a single byte field Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md Example of a struct `OnlyAByte { f1: byte }` containing only one byte field. The serialized output is simply the byte value `ab` itself, as fixed-size structs have no overhead. ```hex ab ``` -------------------------------- ### C Main Function for WABT-Generated Secp256k1 Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This C code defines the `main` function for the WABT-generated secp256k1 test. It loads a `value` from the CKB witness, initializes the secp library, and then calls the `Z_runZ_ii` function multiple times based on the loaded `value` to perform secp256k1 operations, returning the accumulated result. ```c #include <stdio.h> #include <stdlib.h> #include "ckb_syscalls.h" #include "secp.h" int main() { uint32_t value; uint64_t len = 4; int ret = ckb_load_witness((void*) &value, &len, 0, 0, CKB_SOURCE_GROUP_INPUT); if (ret != CKB_SUCCESS) { return ret; } if (len < 4) { return -1; } init(); uint32_t times = value >> 8; value = value & 0xFF; uint8_t result = 0; for (int i = 0; i < times; i++) { result += Z_runZ_ii(value); } return result; } ``` -------------------------------- ### Response for Indexing Lock Hash (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON object is the response from the `index_lock_hash` RPC call, confirming the indexed `lock_hash` along with the `block_hash` and `block_number` where indexing started. ```json { "id": 2, "jsonrpc": "2.0", "result": { "block_hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40", "block_number": "0x400", "lock_hash": "0x4ceaa32f692948413e213ce6f3a83337145bde6e11fd8cb94377ce2637dcc412" } } ``` -------------------------------- ### Serializing multiple Uint32s in a fixed-size vector (Uint32Vec) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md Example of a `fixvec` containing six Uint32 values. The serialization includes a 4-byte length prefix (representing 6 items) followed by each Uint32 serialized in little-endian format. ```hex 06 00 00 00 23 01 00 00 56 04 00 00 90 78 00 00 0a 00 00 00 bc 00 00 00 ef 0d 00 00 ``` -------------------------------- ### Querying CKB Tip Header with Ruby SDK Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md This Ruby snippet demonstrates how to connect to the CKB RPC and retrieve the latest block header information. It initializes an `CKB::RPC` object and then calls `get_tip_header` to fetch details like block hash, number, timestamp, and DAO data. ```ruby rpc = CKB::RPC.new => #<RPC@http://localhost:8114> rpc.get_tip_header #获取最高块的数据 => {:compact_target=>"0x20038e38", :dao=>"0x2baf11ed5932a22ed36667662587230019ce2276e9380000006aae417a02ff06", :epoch=>"0x7080166000001", :hash=>"0x1d637d9f74c4191cda8d828be9f41143a7f5ce82b1d0beabba46df7de8fb5a67", :nonce=>"0xbf57b7ed827a117965da0db15d3d180f", :number=>"0x54e", :parent_hash=>"0xa12044c77bcde4d19f44ba12378d766b6bf75cd7269b8f5cce2abb237a32d236", :proposals_hash=>"0x0000000000000000000000000000000000000000000000000000000000000000", :timestamp=>"0x170c45a5be2", :transactions_root=>"0x0531880b6da6d8be724114815a04b955fc7a28b63400d9024cb1dc02b54410cc", :uncles_hash=>"0x0000000000000000000000000000000000000000000000000000000000000000", :version=>"0x0"} ``` -------------------------------- ### Preparing Duktape Script Data and Cell Dependency - Ruby Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/start-deploy-script.md This snippet prepares the necessary components for deploying a Duktape script on the CKB blockchain. It sends capacity to store the `duktape_data`, calculates its Blake2b hash, and creates a `CellDep` object referencing the deployed script, which is crucial for later transactions that depend on this script. ```Ruby duktape_tx_hash = wallet.send_capacity(wallet.address, CKB::Utils.byte_to_shannon(280000), CKB::Utils.bin_to_hex(duktape_data), fee: 5000) duktape_data_hash = CKB::Blake2b.hexdigest(duktape_data) duktape_cell_dep = CKB::Types::CellDep.new(out_point: CKB::Types::OutPoint.new(tx_hash: duktape_tx_hash, index: 0)) ``` -------------------------------- ### Serializing `dynvec` with Multiple Items Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md This example illustrates the serialization of a `BytesVec` containing five `Bytes` items of varying lengths. It demonstrates how the total size, multiple offsets, and individual item data are structured in the byte sequence. ```Hex Bytes # the full size is 52 (0x34) bytes 34 00 00 00 # five offsets (20 bytes in total) 18 00 00 00, 1e 00 00 00, 22 00 00 00, 28 00 00 00, 2d 00 00 00 # five items (28 bytes in total) 02 00 00 00, 12 34 00 00 00 00, 02 00 00 00, 05 67 01 00 00 00, 89 03 00 00 00, ab cd ef ``` -------------------------------- ### Generating BIP44 Addresses with ckb-cli Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/ckb-cli/ckb-cli-account.md This command generates BIP44 compliant addresses using `ckb-cli`. It allows specifying the length and starting index for both change and receiving addresses, along with a lock argument and output format. ```shell ckb-cli account bip44-addresses --change-length <change-length> --from-change-index <from-change-index> --from-receiving-index <from-receiving-index> --lock-arg <lock-arg> --output-format <output-format> --receiving-length <receiving-length> ``` -------------------------------- ### Loading Current Script (C) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md This syscall serializes the current running script into Molecule Encoding format and copies it to VM memory. It uses `addr`, `len`, and `offset` for partial loading, similar to other loading syscalls. The function `ckb_load_script` wraps `syscall(2052, ...)`. ```C int ckb_load_script(void* addr, uint64_t* len, size_t offset) { return syscall(2052, addr, len, offset, 0, 0, 0); } ```