CKB SDK Rust
The Rust SDK for Nervos CKB provides several essential features for developers:
- RPC access to CKB nodes
- Data structure definitions for various concepts within CKB
- Support for assembling CKB transactions
- Signature unlocking support for commonly used lock scripts.
These features allow for seamless interaction with CKB and facilitate the development of decentralized applications on the CKB network.
Install
# Cargo.toml
[]
= "5"
Build
Build:
Run unit tests:
make test
Please refer to the Makefile for more compilation commands.
Quick start
Setup
ckb-sdk-rust provides a convenient client that enables you to easily interact with CKB nodes.
use CkbRpcClient;
let mut ckb_client = new;
let block = ckb_client.get_block_by_number.unwrap;
println!;
For more details about CKB RPC APIs, please refer to the CKB RPC doc.
Build transaction by manual
The following code example demonstrates how to construct a transfer transaction on CKB. You can use it to transfer a specified amount of CKB from one address to another.
NOTE: The address and key are for demo purposes only and should not be used in a production environment.
use ;
use ;
use ;
// 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 = from_str.unwrap;
let sender_key = from_slice
.unwrap;
let receiver = from_str.unwrap;
let capacity = from_str.unwrap;
// Build ScriptUnlocker
let signer = new_with_secret_keys;
let sighash_unlocker = from;
let sighash_script_id = new_type;
let mut unlockers = default;
unlockers.insert;
// Build CapacityBalancer
let placeholder_witness = new_builder
.lock
.build;
let balancer = new_simple;
// Build:
// * CellDepResolver
// * HeaderDepResolver
// * CellCollector
// * TransactionDependencyProvider
let mut ckb_client = new;
let cell_dep_resolver = ;
let header_dep_resolver = new;
let mut cell_collector = new;
let tx_dep_provider = new;
// Build the transaction
let output = new_builder
.lock
.capacity
.build;
let builder = new;
let = builder
.build_unlocked
.unwrap;
Generate a new address
In CKB, a private key can be used to generate a public key, which is then hashed using the Blake2b hashing algorithm to produce a CKB address. The public key is derived from the private key using the secp256k1 elliptic curve cryptography algorithm. This process results in a unique CKB address that can be used to receive or send CKB tokens. It is important to keep the private key secure, as anyone with access to it can potentially access the associated CKB funds.
use ;
use Rng;
let mut rng = thread_rng;
let privkey_bytes: = rng.gen;
let secp_secret_key = from_slice.unwrap;
let pubkey =
from_secret_key;
let payload = from_pubkey;
let address = new;
println!;
Parse address
In the world of CKB, a lock script can be represented as an address. It is possible to parse an address from an encoded string and then obtain its network and script.
use Address;
use Script;
use FromStr;
let addr_str = "ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgvf0k9sc40s3azmpfvhyuudhahpsj72tsr8cx3d";
let addr = from_str.unwrap;
let _network = addr.network;
let _script: Script = addr.payload.into;
For more details please about CKB address refer to CKB rfc 0021.
More Examples
transfer_from_sighash.rsTransfer capacity from a sighash addresstransfer_from_multisig.rsTransfer capacity from a multisig address (main logic is less than 60 lines of code)
You can try compiling them by running the following command in your terminal:
For more use cases of building transactions with CKB node, please refer to these examples and unit tests.
Wasm
ckb-sdk-rust has limited support for wasm32-unknown-unknown. Some trait are not implemented on wasm32, such as impl CellDataProvider for &dyn TransactionDependencyProvider. To use on wasm, default features must be disabled
License
The SDK is available as open source under the terms of the MIT License.
ChangeLog
See CHANGELOG for more details.