Expand description
An unofficial SDK for building wallets and other applications on the Chia blockchain.
The SDK covers the full lifecycle of a transaction: building coin spends for the standard puzzle, CATs, NFTs, DIDs, vaults, offers, and more, calculating and making the signatures they need, testing them against a simulator, and submitting them to the network through a full node peer or an RPC API. It is not a prebuilt wallet. For that, see Sage Wallet, which is built with this SDK.
To learn more about developing on Chia and its coin set model, see xch.dev.
§Crates
This crate re-exports the crates that make up the SDK, so that they can be used through a single dependency:
| Module | Crate | Purpose |
|---|---|---|
driver | chia-sdk-driver | Building and parsing spends of puzzles, with SpendContext, layers such as StandardLayer, and primitives such as Cat, Nft, Did, and Offer. |
types | chia-sdk-types | Conditions, puzzle argument and solution types, and consensus constants. |
signer | chia-sdk-signer | Calculating the signatures a transaction needs with RequiredSignature. |
test | chia-sdk-test | An in-memory Simulator for testing spends, and test key pairs. |
client | chia-sdk-client | Connecting to full nodes with the wallet protocol through a Peer. |
coinset | chia-sdk-coinset | Calling the full node RPC API, such as coinset.org, through ChiaRpcClient. |
daemon | chia-sdk-daemon | Connecting to the Chia daemon over its websocket. |
utils | chia-sdk-utils | Addresses, coin selection, and hex parsing. |
The lower level crates from Chia that the SDK is built on are re-exported as well:
| Module | Crate | Purpose |
|---|---|---|
clvmr | clvmr | The CLVM runtime, including the Allocator. |
clvm_traits | clvm-traits | Converting Rust types to and from CLVM with ToClvm and FromClvm. |
clvm_utils | clvm-utils | Tree hashing and currying. |
puzzles | chia-puzzles | The compiled bytecode and hashes of Chia’s standard puzzles. |
chia::protocol | chia-protocol | Core types such as Coin, CoinSpend, and SpendBundle, and the network protocol messages. |
chia::bls | chia-bls | BLS keys and signatures. |
chia::secp | chia-secp | secp256k1 and secp256r1 keys and signatures. |
chia::puzzle_types | chia-puzzle-types | The curried arguments and solutions of Chia’s standard puzzles. |
chia::consensus | chia-consensus | Consensus constants and spend validation. |
chia::traits | chia-traits | The Streamable serialization format. |
chia::sha2 | chia-sha2 | SHA-256 hashing. |
chia::ssl | chia-ssl | TLS certificates for connecting to Chia services. |
chia::serde | chia-serde | Serde helpers for Chia types. |
§Prelude
The prelude re-exports the most commonly used types and functions from all of these,
so most applications can start with:
use chia_wallet_sdk::prelude::*;§Feature flags
None of these are enabled by default.
native-tls: Enables connecting to peers, full node RPC servers, and the daemon using the platform’s TLS library.rustls: The same asnative-tls, but usesrustlsinstead.chip-0008: Enables theSplitterLayerfrom CHIP-0008, which divides a coin between the recipients of a fixed payout scheme.chip-0021: Enables NFT fusion from CHIP-0021, with theFusionprimitive, which swaps the NFTs on one side for the NFTs on the other.chip-0035: Enables the data layer store primitives from CHIP-0035, such asDatastore.chip-0037: Enables theP2ControllerPuzzleLayerandP2Eip712MessageLayerpuzzle layers from CHIP-0037.chip-0052: Enables partial offers from CHIP-0052, such asPartialOfferBundle, which can be filled in parts at a fixed price.pooling: Enables plot NFTs for pooling, with thePlotNftprimitive, which can self pool, join and leave pools, and claim pool rewards.action-layer: Enables the action layer and the primitives built on it, such asRewardDistributorandCatalogRegistry. This also enableschip-0035.auction: Enables English auctions for NFTs, with theAuctionprimitive. This also enablesaction-layer.offer-compression: Enables compressing and encoding offers asoffer1...strings, with functions such asencode_offeranddecode_offer. Withchip-0052, this also encodes partial offers aspartial1...strings.peer-simulator: EnablesPeerSimulator, a local full node that aPeercan connect to in tests.
§Quick start
Send XCH from one key to another in the Simulator, signing the
transaction the same way you would for a real network:
use chia_wallet_sdk::chia::bls::sign;
use chia_wallet_sdk::prelude::*;
let mut sim = Simulator::new();
let mut ctx = SpendContext::new();
// Alice has a key with a coin worth 1,000 mojos, locked with the standard puzzle.
let alice = sim.bls(1_000);
let alice_p2 = StandardLayer::new(alice.pk);
// Bob gave Alice his address.
let bob_address = Address::new(Bytes32::new([1; 32]), "txch".to_string()).encode()?;
let bob_puzzle_hash = Address::decode(&bob_address)?.expect_prefix("txch")?;
// Send 300 mojos to Bob, pay a 50 mojo fee, and send the change back to Alice.
// The hints let wallets find the coins by puzzle hash.
let bob_hint = ctx.hint(bob_puzzle_hash)?;
let alice_hint = ctx.hint(alice.puzzle_hash)?;
alice_p2.spend(
&mut ctx,
alice.coin,
Conditions::new()
.create_coin(bob_puzzle_hash, 300, bob_hint)
.create_coin(alice.puzzle_hash, 650, alice_hint)
.reserve_fee(50),
)?;
let coin_spends = ctx.take();
// Calculate the required signatures, and sign each of them with Alice's key.
let mut allocator = Allocator::new();
let constants = AggSigConstants::from(&*TESTNET11_CONSTANTS);
let required = RequiredSignature::from_coin_spends(&mut allocator, &coin_spends, &constants)?;
let mut signature = Signature::default();
for required in required {
let RequiredSignature::Bls(required) = required else {
unreachable!("the standard puzzle only uses BLS signatures");
};
assert_eq!(required.public_key, alice.pk);
signature += &sign(&alice.sk, required.message());
}
// On a real network, this spend bundle would be submitted to a peer or an RPC API.
sim.new_transaction(SpendBundle::new(coin_spends, signature))?;
let bob_coins = sim.unspent_coins(bob_puzzle_hash, false);
assert_eq!(bob_coins.len(), 1);
assert_eq!(bob_coins[0].amount, 300);Issue a CAT, and let the simulator sign the transaction:
use chia_wallet_sdk::prelude::*;
let mut sim = Simulator::new();
let mut ctx = SpendContext::new();
let alice = sim.bls(1_000);
let alice_p2 = StandardLayer::new(alice.pk);
// Issue 1,000 mojos of a new CAT to Alice. Its asset id is derived from Alice's coin id,
// so no more of it can ever be issued.
let hint = ctx.hint(alice.puzzle_hash)?;
let (issue_cat, cats) = Cat::single_issuance(
&mut ctx,
alice.coin.coin_id(),
None,
1_000,
Conditions::new().create_coin(alice.puzzle_hash, 1_000, hint),
)?;
alice_p2.spend(&mut ctx, alice.coin, issue_cat)?;
sim.spend_coins(ctx.take(), &[alice.sk])?;
let cat = cats[0];
assert_eq!(cat.coin.amount, 1_000);
assert_eq!(cat.info.p2_puzzle_hash, alice.puzzle_hash);
assert!(sim.coin_state(cat.coin.coin_id()).is_some_and(|state| state.spent_height.is_none()));Re-exports§
pub use chia_sdk_client as client;pub use chia_sdk_coinset as coinset;pub use chia_sdk_daemon as daemon;pub use chia_sdk_driver as driver;pub use chia_sdk_signer as signer;pub use chia_sdk_test as test;pub use chia_sdk_types as types;pub use chia_sdk_utils as utils;pub use chia_puzzles as puzzles;pub use clvm_traits;pub use clvm_utils;pub use clvmr;