Savitri SDK
savitri-sdk 是与 Savitri Network 交互的官方 Rust 客户端库,提供以下功能:
- RpcClient —— 覆盖所有节点 API 的 JSON-RPC 2.0 客户端
- LightClient —— 常用操作的简化封装
- Wallet —— Ed25519 密钥管理与交易签名
- ContractClient —— 合约、预言机和治理的高级辅助工具
- TransactionBuilder —— 链式构建交易的流式构建器
安装
在 Cargo.toml 中添加:
[dependencies]
savitri-sdk = { path = "../savitri-sdk" }
tokio = { version = "1", features = ["full"] }
anyhow = "1"
如需 HTTP 客户端支持:
savitri-sdk = { path = "../savitri-sdk", features = ["http-client"] }
快速入门
use savitri_sdk::{RpcClient, Wallet, TransactionBuilder};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// 连接到节点
let client = RpcClient::from_url("http://localhost:8545")?;
// 健康检查
let health = client.health().await?;
println!("Node: {} ({})", health.service, health.mode);
// 区块高度
let height = client.get_block_number().await?;
println!("Height: {}", height);
// 创建钱包
let wallet = Wallet::new();
println!("Address: {}", wallet.address());
// 检查余额
let account = client.get_account(wallet.address()).await?;
println!("Balance: {} (nonce: {})", account.balance, account.nonce);
Ok(())
}
模块
| 模块 | 描述 |
|---|---|
| RPC 客户端 | 完整的 JSON-RPC 2.0 客户端 |
| 钱包 | 密钥生成、签名、余额查询 |
| 交易 | 构建、签名并提交交易 |
| 合约 | 合约调用、预言机、治理 |
| 轻客户端 | 轻量级消费者的简化 API |
安全性
- HTTPS 强制执行:远程(非 localhost)端点默认要求 HTTPS。设置
allow_insecure: true可覆盖此行为(不推荐)。 - TLS 1.2 最低要求:HTTPS 连接强制使用 TLS 1.2+。
- 密钥清零:在
Wallet::drop()时私钥会被清零以防止内存泄漏。 - 超时:默认请求超时时间为 30 秒。
命令行工具
SDK 包含三个二进制工具:
# 生成 ed25519 密钥对
cargo run -p savitri-sdk --bin key_generator
# 离线签名交易
cargo run -p savitri-sdk --bin transaction_signer
# 监控节点健康状态
cargo run -p savitri-sdk --bin network_monitor