TITLE: Installing CKB SDK Rust Dependency (Cargo.toml) DESCRIPTION: This snippet shows how to add the `ckb-sdk` crate as a dependency in your Rust project's `Cargo.toml` file, specifying version 3.7.0. This is the first step to integrate the SDK into your application. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_0 LANGUAGE: TOML CODE: ``` [dependencies] ckb-sdk = "3.7.0" ``` ---------------------------------------- TITLE: Building CKB Capacity Transfer Transaction Manually (Rust) DESCRIPTION: This comprehensive Rust example illustrates the manual construction of a CKB transfer transaction. It covers preparing sender/receiver addresses and keys, initializing RPC clients and resolvers, building unlockers and balancers, and finally constructing the transaction to transfer a specified CKB capacity. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_4 LANGUAGE: Rust CODE: ``` use ckb_sdk::{ constants::SIGHASH_TYPE_HASH, rpc::CkbRpcClient, traits::{ DefaultCellCollector, DefaultCellDepResolver, DefaultHeaderDepResolver, DefaultTransactionDependencyProvider, SecpCkbRawKeySigner, }, tx_builder::{transfer::CapacityTransferBuilder, CapacityBalancer, TxBuilder}, unlock::{ScriptUnlocker, SecpSighashUnlocker}, Address, HumanCapacity, ScriptId, }; use ckb_types::{ bytes::Bytes, core::BlockView, h256, packed::{CellOutput, Script, WitnessArgs}, prelude::*, }; use std::{collections::HashMap, str::FromStr}; // Prepare the necessary data for a CKB transaction: // * set the RPC endpoint for the testnet // * define the sender's address and secret key // * define the recipient's address // * specify the capacity to transfer let ckb_rpc = "https://testnet.ckb.dev:8114"; let sender = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqf7v2xsyj0p8szesqrwqapvvygpc8hzg9sku954v").unwrap(); let sender_key = secp256k1::SecretKey::from_slice( h256!("0xef4dfe655b3df20838bdd16e20afc70dfc1b9c3e87c54c276820315a570e6555").as_bytes(), ) .unwrap(); let receiver = Address::from_str("ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqvglkprurm00l7hrs3rfqmmzyy3ll7djdsujdm6z").unwrap(); let capacity = HumanCapacity::from_str("100.0").unwrap(); // Build ScriptUnlocker let signer = SecpCkbRawKeySigner::new_with_secret_keys(vec![sender_key]); let sighash_unlocker = SecpSighashUnlocker::from(Box::new(signer) as Box<_>); let sighash_script_id = ScriptId::new_type(SIGHASH_TYPE_HASH.clone()); let mut unlockers = HashMap::default(); unlockers.insert( sighash_script_id, Box::new(sighash_unlocker) as Box, ); // Build CapacityBalancer let placeholder_witness = WitnessArgs::new_builder() .lock(Some(Bytes::from(vec![0u8; 65])).pack()) .build(); let balancer = CapacityBalancer::new_simple(sender.payload().into(), placeholder_witness, 1000); // Build: // * CellDepResolver // * HeaderDepResolver // * CellCollector // * TransactionDependencyProvider let mut ckb_client = CkbRpcClient::new(ckb_rpc); let cell_dep_resolver = { let genesis_block = ckb_client.get_block_by_number(0.into()).unwrap().unwrap(); DefaultCellDepResolver::from_genesis(&BlockView::from(genesis_block)).unwrap() }; let header_dep_resolver = DefaultHeaderDepResolver::new(ckb_rpc); let mut cell_collector = DefaultCellCollector::new(ckb_rpc); let tx_dep_provider = DefaultTransactionDependencyProvider::new(ckb_rpc, 10); // Build the transaction let output = CellOutput::new_builder() .lock(Script::from(&receiver)) .capacity(capacity.0.pack()) .build(); let builder = CapacityTransferBuilder::new(vec![(output, Bytes::default())]); let (_tx, _) = builder .build_unlocked( &mut cell_collector, &cell_dep_resolver, &header_dep_resolver, &tx_dep_provider, &balancer, &unlockers, ) .unwrap(); ``` ---------------------------------------- TITLE: Initializing CKB RPC Client and Fetching Block (Rust) DESCRIPTION: This Rust snippet demonstrates how to create a new `CkbRpcClient` instance to connect to a CKB node, specifically the testnet. It then uses the client to fetch the genesis block (block number 0) and prints its pretty-formatted JSON representation. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_3 LANGUAGE: Rust CODE: ``` use ckb_sdk::rpc::CkbRpcClient; let mut ckb_client = CkbRpcClient::new("https://testnet.ckb.dev"); let block = ckb_client.get_block_by_number(0.into()).unwrap(); println!("block: {}", serde_json::to_string_pretty(&block).unwrap()); ``` ---------------------------------------- TITLE: Generating CKB Address (Rust) DESCRIPTION: This snippet demonstrates how to generate a new CKB address in Rust. It uses the `ckb-sdk` and `secp256k1` crates to derive a public key from a randomly generated private key, then constructs an `AddressPayload` and a `Mainnet` address. The generated address is printed to the console. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_5 LANGUAGE: Rust CODE: ``` use ckb_sdk::types::{Address, AddressPayload, NetworkType}; use rand::Rng; let mut rng = rand::thread_rng(); let privkey_bytes: [u8; 32] = rng.gen(); let secp_secret_key = secp256k1::SecretKey::from_slice(&privkey_bytes).unwrap(); let pubkey = secp256k1::PublicKey::from_secret_key(&ckb_crypto::secp::SECP256K1, &secp_secret_key); let payload = AddressPayload::from_pubkey(&pubkey); let address = Address::new(NetworkType::Mainnet, payload, true); println!("address: {}", address.to_string()); ``` ---------------------------------------- TITLE: Parsing CKB Address (Rust) DESCRIPTION: This snippet illustrates how to parse an existing CKB address string in Rust. It uses the `Address::from_str` method from `ckb-sdk` to convert the string into an `Address` object, from which the network type and the underlying lock `Script` can be extracted. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_6 LANGUAGE: Rust CODE: ``` use ckb_sdk::types::Address; use ckb_types::packed::Script; use std::str::FromStr; let addr_str = "ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgvf0k9sc40s3azmpfvhyuudhahpsj72tsr8cx3d"; let addr = Address::from_str(addr_str).unwrap(); let _network = addr.network(); let _script: Script = addr.payload().into(); ``` ---------------------------------------- TITLE: Compiling Molecule Schema to Rust DESCRIPTION: This Bash command compiles a Molecule schema file (`omni_lock.mol`) into Rust code using `moleculec`, then pipes the output to `rustfmt` for formatting, and finally saves the result as `omni_lock.rs`. This is a crucial step for generating Rust types from schema definitions. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/src/types/schemas/README.md#_snippet_0 LANGUAGE: Bash CODE: ``` moleculec --language rust --schema-file omni_lock.mol | rustfmt > omni_lock.rs ``` ---------------------------------------- TITLE: Building CKB SDK Rust Project (Bash) DESCRIPTION: This command compiles the Rust project that includes the CKB SDK. It uses `cargo build`, the standard Rust build tool, to compile all dependencies and the project's own code. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_1 LANGUAGE: Bash CODE: ``` cargo build ``` ---------------------------------------- TITLE: Compiling CKB SDK Examples (Shell) DESCRIPTION: This command demonstrates how to compile all examples provided within the CKB SDK Rust project. Running `cargo build --examples` in the project's root directory will build all executable examples, making them ready for execution. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_7 LANGUAGE: Shell CODE: ``` cargo build --examples ``` ---------------------------------------- TITLE: Adding Generated Module to Rust Project DESCRIPTION: This Rust snippet adds the newly generated `omni_lock` module to the `mod.rs` file. The `#[allow(clippy::all)]` attribute is used to suppress Clippy warnings for the generated code, which might not adhere to all linting rules. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/src/types/schemas/README.md#_snippet_2 LANGUAGE: Rust CODE: ``` #[allow(clippy::all)] pub mod omni_lock; ``` ---------------------------------------- TITLE: Replacing Molecule Imports in Rust DESCRIPTION: These Rust lines demonstrate the replacement of older Molecule-related import statements with new ones. The updated imports leverage `ckb_types` for `molecule`, `packed`, and `prelude` functionalities, ensuring compatibility with the CKB SDK's type system. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/src/types/schemas/README.md#_snippet_1 LANGUAGE: Rust CODE: ``` #![allow(unused_imports)] use ckb_types::molecule; use ckb_types::packed::*; use ckb_types::prelude::*; ``` LANGUAGE: Rust CODE: ``` use super::blockchain::*; use molecule::prelude::*; ``` ---------------------------------------- TITLE: Running Unit Tests for CKB SDK Rust (Makefile) DESCRIPTION: This command executes the unit tests defined in the project's Makefile. It's a common way to run tests in projects that use Make for build automation, ensuring the code functions as expected. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/README.md#_snippet_2 LANGUAGE: Bash CODE: ``` make test ``` ---------------------------------------- TITLE: Generating All Cycle Test Data via Docker (Bash) DESCRIPTION: This command, executed from the 'test-data' directory, triggers the 'all-via-docker' target defined in the project's Makefile. It utilizes Docker to create a standardized environment for generating all required cycle test data, streamlining the setup and execution for testing purposes. SOURCE: https://github.com/nervosnetwork/ckb-sdk-rust/blob/master/src/test-data/cycle.md#_snippet_0 LANGUAGE: bash CODE: ``` make all-via-docker ```