Dhatu is a Rust library that provides core functionality for interacting with Substrate-based blockchains, with a primary focus on Mandala-based blockchains. This project demonstrates how to create an account using the Dhatu library.
- Substrate Integration: Built on top of Substrate framework with support for Mandala-based blockchains
- Transaction Management: Comprehensive transaction handling and signing capabilities
- Identity Management: Robust keypair and identity management system
- Runtime Types: Auto-generated runtime types for Mandala node integration
- Error Handling: Comprehensive error handling system
- Rust and Cargo installed (version 1.56.0 or later)
- Basic knowledge of Rust programming
- Create a new Rust project:
cargo new dhatu-account
cd dhatu-account- Add the required dependencies to your
Cargo.toml:
[package]
name = "dhatu-account"
version = "0.1.0"
edition = "2021"
[dependencies]
dhatu = { version = "0.2.2", features = ["sp-keyring", "subxt", "unstable_sp_core"] }
tokio = { version = "1.0", features = ["full"] }
hex = "0.4.3"
sp-keyring = "24.0.0"
sp-core = "21.0.0"The following code demonstrates how to:
- Connect to a blockchain node (using a dummy WebSocket URL)
- Create an account using the Sr25519 keyring
- Get the public key and account ID
use dhatu::ext::sp_core::crypto::{AccountId32, Pair};
use dhatu::ext::sp_keyring::sr25519::Keyring as Sr25519Keyring;
use dhatu::ext::subxt::{OnlineClient, SubstrateConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to the blockchain node (replace with your actual WebSocket URL)
let api = OnlineClient::<SubstrateConfig>::from_url("wss://your-blockchain-node.com").await?;
println!("Connected to blockchain node.");
// Create a default Alice keypair using the keyring
let alice = Sr25519Keyring::Alice;
let keypair = alice.pair();
let public_key = keypair.public();
let account_id = AccountId32::from(public_key);
println!("Alice's public key: {:?}", public_key);
println!("Alice's account id: {:?}", account_id);
Ok(())
}To run the project:
cargo runExpected output:
Connected to blockchain node.
Alice's public key: d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d (5GrwvaEF...)
Alice's account id: d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d (5GrwvaEF...)
-
Connection: The code connects to a blockchain node using a WebSocket URL. Replace
wss://your-blockchain-node.comwith your actual node's WebSocket URL. -
Keypair Generation:
- Uses
Sr25519Keyring::Aliceto create a predefined keypair - The keyring provides several predefined accounts (Alice, Bob, Charlie, etc.)
- Uses
-
Account Information:
public_key: The raw public key in hex formataccount_id: The SS58-encoded account address (starts with "5GrwvaEF...")
The Dhatu library provides several feature flags:
default: Includes tokio and serdeunstable: Enables all features including substrate primitivessp_keyring: Enables substrate keyring functionalityasset_migration: Enables asset migration featurestokio: Async runtime supportserde: Serialization/deserialization supportunstable_sp_core: Unstable substrate core featuressubxt: Substrate extrinsic support
You can extend this implementation to:
- Check account balance
- Make transactions
- Create custom keypairs
- Sign and verify messages
subxt: Substrate extrinsic supportschnorrkel: Cryptographytokio: Async runtimeserde: Serializationparity-scale-codec: SCALE codecsp-keyring: Substrate keyring (optional)
This project is licensed under the Apache-2.0 License.
Contributions are welcome! Please feel free to submit a Pull Request.