======================== CODE SNIPPETS ======================== TITLE: Setting Up CKB Ruby SDK (Shell) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_1 LANGUAGE: shell CODE: ``` git clone https://github.com/nervosnetwork/ckb-sdk-ruby cd ckb-sdk-ruby ./bin/setup # 安装插件 ./bin/console # 进入 ruby REPL环境 ``` ---------------------------------------- TITLE: Installing Project Dependencies with Yarn DESCRIPTION: This command installs all necessary project dependencies using Yarn. It should be executed once after cloning the repository to set up the development environment. SOURCE: https://github.com/nervoscommunity/docs/blob/master/README.md#_snippet_0 LANGUAGE: bash CODE: ``` $ yarn ``` ---------------------------------------- TITLE: Installing Ruby Environment with rbenv (Shell) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_0 LANGUAGE: shell CODE: ``` 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 ``` ---------------------------------------- TITLE: Example Start Withdrawal Epoch Object (JavaScript) DESCRIPTION: This constant represents an example parsed epoch object, similar to `depositEpoch`, but specifically for the epoch when a Nervos DAO withdrawal was initiated. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md#_snippet_9 LANGUAGE: JavaScript CODE: ``` const startWithdrawingEpoch = { "length": "0xa", "index": "0x0", "number": "0xbe" } ``` ---------------------------------------- TITLE: Starting Local Development Server with Yarn DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/README.md#_snippet_1 LANGUAGE: bash CODE: ``` $ yarn start ``` ---------------------------------------- TITLE: Example Nervos DAO Function Calls (JavaScript) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md#_snippet_13 LANGUAGE: JavaScript CODE: ``` // deposit() // logDepositEpoch() // starWithdrawing() // logStartWithdrawingEpoch() // logCurrentEpoch() // withdraw() // setInterval(logCurrentEpoch, 1000) ``` ---------------------------------------- TITLE: Initializing CKB API and Wallet - Ruby DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/script/script-issue-udt.md#_snippet_0 LANGUAGE: Ruby CODE: ``` pry(main)> api = CKB::API.new => # pry(main)> new_key = CKB::Key.random_private_key => "0xc5e72c391909d695fb02a9ee40e55f0ffcd5188591373f6fe0013ad4b89a5342" pry(main)> wallet = CKB::Wallet.from_hex(api, new_key) => #>, @address="ckt1qyq2t9nf8m54kwgp5kdg87nqkyj82ywj6nwsl67j63", @api=#, @blake160="0xa596693ee95b3901a59a83fa60b1247511d2d4dd", @hash_type="type", @key=#, @pubkey="0x02ce875d7dd28539e5d6d74a1ba2743082be36e5465df1181292436ca0dac1dae6", @skip_data_and_type=true> ``` ---------------------------------------- TITLE: Example Multisig Script for CKB DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0021-ckb-address-format/0021-ckb-address-format.md#_snippet_2 LANGUAGE: plaintext CODE: ``` 0 | 1 | 2 | 3 | Pk_Cipher_h | Pk_Alice_h | Pk_Bob_h ``` ---------------------------------------- TITLE: Compiling and Running Eaglesong C Implementation DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0010-eaglesong/0010-eaglesong.md#_snippet_7 LANGUAGE: Bash CODE: ``` $> gcc -o hash eaglesong.c hash.c $> ./hash > Hello, world! > [Ctrl-D] > 64867e2441d162615dc2430b6bcb4d3f4b95e4d0db529fca1eece73c077d72d6 ``` ---------------------------------------- TITLE: Example Start Withdrawal OutPoint (JavaScript) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md#_snippet_7 LANGUAGE: JavaScript CODE: ``` const startWithDrawOutPoint = { "txHash": "0xc8ad01deb8b25c56169992598398ad7d539314ada90c84bff12fa7fc69095076", "index": "0x0" } ``` ---------------------------------------- TITLE: Estimating Fee Rate using RPC (Bash) DESCRIPTION: 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). SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md#_snippet_19 LANGUAGE: bash CODE: ``` 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 ``` ---------------------------------------- TITLE: Setting Up CKB-VM Bench Scripts for Native C Benchmarking DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md#_snippet_5 LANGUAGE: shell CODE: ``` 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 ``` ---------------------------------------- TITLE: Querying Transaction Pool Information (Bash) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md#_snippet_45 LANGUAGE: bash CODE: ``` echo '{ "id": 2, "jsonrpc": "2.0", "method": "tx_pool_info", "params": [] }' \ | tr -d '\n' \ | curl -H 'content-type: application/json' -d @- \ http://localhost:8114 ``` ---------------------------------------- TITLE: Building Static Website Content with Yarn DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/README.md#_snippet_2 LANGUAGE: bash CODE: ``` $ yarn build ``` ---------------------------------------- TITLE: Benchmarking Secp256k1 Performance on CKB-VM DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md#_snippet_4 LANGUAGE: shell CODE: ``` 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 ``` ---------------------------------------- TITLE: Example Deposit Epoch Object (JavaScript) DESCRIPTION: 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`. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md#_snippet_5 LANGUAGE: JavaScript CODE: ``` const depositEpoch = { "length": "0xa", "index": "0x0", "number": "0x69" } ``` ---------------------------------------- TITLE: Deploying Website to GitHub Pages with Yarn DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/README.md#_snippet_3 LANGUAGE: bash CODE: ``` $ GIT_USER= USE_SSH=true yarn deploy ``` ---------------------------------------- TITLE: Example Final Withdrawal OutPoint (JavaScript) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md#_snippet_12 LANGUAGE: JavaScript CODE: ``` const withDrawOutPoint = { "txHash": "0xb1ee185a4e811247b1705a52df487c3ce839bfa2f72e4c7a74b6fc6b0ea4cfa7", "index": "0x0" } ``` ---------------------------------------- TITLE: Compiling and Patching Secp256k1 Binaries for CKB-VM DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md#_snippet_3 LANGUAGE: shell CODE: ``` 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 ``` ---------------------------------------- TITLE: Benchmarking Native C Secp256k1 on CKB-VM DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md#_snippet_6 LANGUAGE: shell CODE: ``` ./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 ``` ---------------------------------------- TITLE: Example Deposit OutPoint (JavaScript) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md#_snippet_3 LANGUAGE: JavaScript CODE: ``` const depositOutPoint = { "txHash": "0x40e1d58cf8576d5206d55d242284a28f64cb114d0b9a8292582e7596082e5bda", "index": "0x0" } ``` ---------------------------------------- TITLE: Building Simple UDT Script - Bash DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0025-simple-udt/0025-simple-udt.md#_snippet_3 LANGUAGE: Bash CODE: ``` $ git clone https://github.com/nervosnetwork/ckb-miscellaneous-scripts $ cd ckb-miscellaneous-scripts $ git checkout 175b8b0933340f9a7b41d34106869473d575b17a $ git submodule update --init $ make all-via-docker ``` ---------------------------------------- TITLE: CKB Transaction Structure Example DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-19-ckbscript-09.md#_snippet_0 LANGUAGE: JSON CODE: ``` { "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" ] } ``` ---------------------------------------- TITLE: C Main Function for WAVM-Precompiled Secp256k1 DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md#_snippet_2 LANGUAGE: c CODE: ``` #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; } ``` ---------------------------------------- TITLE: Querying Address and Balance with CKB Ruby Wallet DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/start-build-dev.md#_snippet_1 LANGUAGE: Ruby CODE: ``` _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 ``` ---------------------------------------- TITLE: Example Response for get_block_by_number RPC (JSON) DESCRIPTION: 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`. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md#_snippet_12 LANGUAGE: json CODE: ``` { "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": [] } } ``` ---------------------------------------- TITLE: CKB `since` Field Examples DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0017-tx-valid-since/0017-tx-valid-since.md#_snippet_3 LANGUAGE: txt CODE: ``` # 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 ``` ---------------------------------------- TITLE: Example Nervos CKB Block Structure in JSON DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md#_snippet_3 LANGUAGE: JSON CODE: ``` { "uncles": [ ``` ---------------------------------------- TITLE: Querying CKB Address Details and Balance with Ruby SDK DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_5 LANGUAGE: ruby CODE: ``` _priv_key = "fe59445edc3c30db6b0e1abcddc317137368f5604ce01cc1d279dfda001e8474" api = CKB::API.new => # 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 ``` ---------------------------------------- TITLE: Initializing CKB Rust Contract Project (Shell) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md#_snippet_0 LANGUAGE: Shell CODE: ``` cargo new --lib ckb-rust-demo cd ckb-rust-demo cargo new contract ``` ---------------------------------------- TITLE: Example CKB Transaction Structure (JSON) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md#_snippet_2 LANGUAGE: json CODE: ``` { "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"] } ``` ---------------------------------------- TITLE: Defining `no_std` Entry Point for CKB Rust Contract (Rust) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md#_snippet_3 LANGUAGE: Rust CODE: ``` #![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() {} ``` ---------------------------------------- TITLE: Initializing CKB SDK and Generating Addresses (JavaScript) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md#_snippet_0 LANGUAGE: JavaScript CODE: ``` 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) ``` ---------------------------------------- TITLE: Compiling and Running Eaglesong C Implementation DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0010-eaglesong/0010-eaglesong.zh(需要相关的技术背景才能翻译好,暂留不发布).md#_snippet_4 LANGUAGE: Shell CODE: ``` $> gcc -o hash eaglesong.c hash.c $> ./hash > Hello, world! > [Ctrl-D] > 64867e2441d162615dc2430b6bcb4d3f4b95e4d0db529fca1eece73c077d72d6 ``` ---------------------------------------- TITLE: Serializing multiple bytes in a fixed-size vector (Bytes) DESCRIPTION: Example of a `fixvec` containing 8 bytes (`0x1234567890abcdef`). The serialization includes a 4-byte length prefix (representing 8 items) followed by the 8 bytes. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md#_snippet_8 LANGUAGE: hex CODE: ``` 08 00 00 00 12 34 56 78 90 ab cd ef ``` ---------------------------------------- TITLE: Serializing a struct with a single byte field DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md#_snippet_4 LANGUAGE: hex CODE: ``` ab ``` ---------------------------------------- TITLE: C Main Function for WABT-Generated Secp256k1 DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md#_snippet_1 LANGUAGE: c CODE: ``` #include #include #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; } ``` ---------------------------------------- TITLE: Response for Indexing Lock Hash (JSON) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md#_snippet_24 LANGUAGE: json CODE: ``` { "id": 2, "jsonrpc": "2.0", "result": { "block_hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40", "block_number": "0x400", "lock_hash": "0x4ceaa32f692948413e213ce6f3a83337145bde6e11fd8cb94377ce2637dcc412" } } ``` ---------------------------------------- TITLE: Serializing multiple Uint32s in a fixed-size vector (Uint32Vec) DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md#_snippet_11 LANGUAGE: hex CODE: ``` 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 ``` ---------------------------------------- TITLE: Querying CKB Tip Header with Ruby SDK DESCRIPTION: 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. SOURCE: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md#_snippet_4 LANGUAGE: ruby CODE: ``` rpc = CKB::RPC.new => # 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"} ```