Skip to main content

Crate chia_wallet_sdk

Crate chia_wallet_sdk 

Source
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:

ModuleCratePurpose
driverchia-sdk-driverBuilding and parsing spends of puzzles, with SpendContext, layers such as StandardLayer, and primitives such as Cat, Nft, Did, and Offer.
typeschia-sdk-typesConditions, puzzle argument and solution types, and consensus constants.
signerchia-sdk-signerCalculating the signatures a transaction needs with RequiredSignature.
testchia-sdk-testAn in-memory Simulator for testing spends, and test key pairs.
clientchia-sdk-clientConnecting to full nodes with the wallet protocol through a Peer.
coinsetchia-sdk-coinsetCalling the full node RPC API, such as coinset.org, through ChiaRpcClient.
daemonchia-sdk-daemonConnecting to the Chia daemon over its websocket.
utilschia-sdk-utilsAddresses, coin selection, and hex parsing.

The lower level crates from Chia that the SDK is built on are re-exported as well:

ModuleCratePurpose
clvmrclvmrThe CLVM runtime, including the Allocator.
clvm_traitsclvm-traitsConverting Rust types to and from CLVM with ToClvm and FromClvm.
clvm_utilsclvm-utilsTree hashing and currying.
puzzleschia-puzzlesThe compiled bytecode and hashes of Chia’s standard puzzles.
chia::protocolchia-protocolCore types such as Coin, CoinSpend, and SpendBundle, and the network protocol messages.
chia::blschia-blsBLS keys and signatures.
chia::secpchia-secpsecp256k1 and secp256r1 keys and signatures.
chia::puzzle_typeschia-puzzle-typesThe curried arguments and solutions of Chia’s standard puzzles.
chia::consensuschia-consensusConsensus constants and spend validation.
chia::traitschia-traitsThe Streamable serialization format.
chia::sha2chia-sha2SHA-256 hashing.
chia::sslchia-sslTLS certificates for connecting to Chia services.
chia::serdechia-serdeSerde 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 as native-tls, but uses rustls instead.
  • chip-0008: Enables the SplitterLayer from CHIP-0008, which divides a coin between the recipients of a fixed payout scheme.
  • chip-0021: Enables NFT fusion from CHIP-0021, with the Fusion primitive, 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 as Datastore.
  • chip-0037: Enables the P2ControllerPuzzleLayer and P2Eip712MessageLayer puzzle layers from CHIP-0037.
  • chip-0052: Enables partial offers from CHIP-0052, such as PartialOfferBundle, which can be filled in parts at a fixed price.
  • pooling: Enables plot NFTs for pooling, with the PlotNft primitive, 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 as RewardDistributor and CatalogRegistry. This also enables chip-0035.
  • auction: Enables English auctions for NFTs, with the Auction primitive. This also enables action-layer.
  • offer-compression: Enables compressing and encoding offers as offer1... strings, with functions such as encode_offer and decode_offer. With chip-0052, this also encodes partial offers as partial1... strings.
  • peer-simulator: Enables PeerSimulator, a local full node that a Peer can 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;

Modules§

chia
Re-exports of the crates from Chia’s chia_rs repository that the SDK is built on.
prelude
The most commonly used types, traits, and functions from across the SDK.