Postchain Client Rust
A Rust client library for interacting with the Chromia blockchain deployed to a Postchain single node (manual mode) or multi-nodes managed by Directory Chain (managed mode).
This library provides functionality for executing queries, creating and signing transactions, and managing blockchain operations.
Installation
Add this to your Cargo.toml:
For only use the postchain_client::utils::operation::Params enum to construct data for queries and transactions.
[]
= "0.0.3"
= { = "1.42.0", = ["rt"] }
For the both use the postchain_client::utils::operation::Params enum and the Rust's struct to serialize and deserialize with serde:
[]
= "0.0.3"
= { = "1.42.0", = ["rt"] }
= { = "1.0.216", = ["derive"] }
= { = "1.0", = ["preserve_order"] }
Usage Guide
1. Setting Up the Client
use RestClient;
let client = RestClient ;
2. Executing Queries
Queries allow our to fetch data from the blockchain:
use Params;
async
async
3. Creating and Sending Transactions
3.1 Creating Operations
use ;
// Create operation with named parameters (dictionary)
let operation = from_dict;
// Or create operation with unnamed parameters (list)
let operation = from_list;
3.2 Creating and Signing Transactions
use Transaction;
// Create new transaction
let mut tx = new;
// Sign the transaction
let private_key1 = "C70D5A77CC10552019179B7390545C46647C9FCA1B6485850F2B913F87270300"; // Replace with actual private key
tx.sign.expect;
// Multi sign the transaction
let private_key2 = "17106092B72489B785615BD2ACB2DDE8D0EA05A2029DCA4054987494781F988C"; // Replace with actual private key
tx.sign.expect;
// Sign the transaction from raw private key
tx.sign_from_raw_priv_key;
// Multi sign the transaction from raw private keys
tx.multi_sign_from_raw_priv_keys;
3.3 Sending Transactions
async
4. Error and Response Handling
The response from client.query and client.send_transaction is a postchain_client::transport::client::RestResponse enum if success
or a postchain_client::transport::client::RestError enum if failed.
We can handle it as follows:
let result = client.query.await;
match result
let result = client.send_transaction.await;
match result
The response from client.get_transaction_status is a postchain_client::utils::transaction::TransactionStatus enum if success or a postchain_client::transport::client::RestError enum if failed.
We can handle it as follows:
let result = client.get_transaction_status.await;
match result
5. Use serde for serialize or deserialize a struct to Params::Dict or vice versa
Please look at all tests in operation.rs source here: https://github.com/cuonglb/postchain-client-rust/blob/dev/src/utils/operation.rs
6. Logging
postchain-client uses tracing crate for logging. You can use tracing-subscriber crate to enable all logs.
use tracing_subscriber;
async
7. Parameter Types
The library supports various parameter types through the Params enum and Rust struct :
| GTV(*) | Rust types or 3rd crates | Postchain Client Params enums | Note |
|---|---|---|---|
| null | Option<T> = None |
Params::Null | |
| integer | bool | Params::Boolean(bool) | |
| integer | i64 | Params::Integer(i64) | |
| bigInteger | num_bigint::BigInt | Params::BigInteger(num_bigint::BigInt) | (**) |
| decimal | bigdecimal::BigDecimal | Params::Decimal(bigdecimal::BigDecimal) | (***) |
| string | String | Params::Text(String) | |
| array | Vec<T> |
Params::Array(Vec<Params>) |
|
| dict | BTreeMap<K, V> |
Params::Dict(BTreeMap<String, Params>) |
|
| byteArray | Vec<u8> |
Params:: ByteArray(Vec<u8>) |
(*) GTV gets converted to ASN.1 DER when it's sent. See more : https://docs.chromia.com/intro/architecture/generic-transaction-protocol#generic-transfer-value-gtv
(**) We can use serde custom derive macros in some of cases to:
Handle arbitrary-precision integers:
operation::deserialize_bigintfor deserialization.operation::serialize_bigintfor serialization.
use ;
...
...
Handle arbitrary-precision decimal:
operation::deserialize_bigintfor deserialization.operation::serialize_bigintfor serialization.
use ;
...
...
Examples
Book Review Application Example
Here's a real-world example from a book review application that demonstrates querying, creating transactions, and handling structured data: https://github.com/cuonglb/postchain-client-rust/tree/dev/examples/book-review
This example demonstrates:
- Defining and using structured data with serde
- Querying the blockchain and handling responses
- Creating and sending transactions
- Signing transactions with a private key
- Transaction error handling and status checking
How To Run
Install Rust (https://www.rust-lang.org/tools/install) and Docker with compose.
Start a Postchain single node with the book-review Rell dapp:
$ cd examples/book-review/rell-dapp/
$ sudo docker compose up -d
Start a simple Rust application to interact with the book-review blockchain:
$ cd examples/book-review/client
$ cargo run
Documentation for postchain_client latest crate
https://docs.rs/postchain-client/latest/postchain_client/
Other
https://github.com/cuonglb/postchain-client-rust/tree/dev/examples/for-docs
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the terms specified in the LICENSE file.