Constellation Metagraph SDK - Rust
Rust SDK for signing data and currency transactions on Constellation Network metagraphs built with the metakit framework.
Scope: This SDK supports both data transactions (state updates) and metagraph token transactions (value transfers). It implements the standardized serialization, hashing, and signing routines defined by metakit and may not be compatible with metagraphs using custom serialization.
Installation
Add to your Cargo.toml:
[]
= "0.2"
Or use cargo:
Quick Start
Data Transactions
use ;
use json;
Currency Transactions
use ;
API Reference
Data Transactions
High-Level API
create_signed_object(value, private_key, is_data_update) -> Result<Signed<T>>
Create a signed object with a single signature.
let signed = create_signed_object?;
// For L1 submission (DataUpdate)
let signed = create_signed_object?;
add_signature(signed, private_key, is_data_update) -> Result<Signed<T>>
Add an additional signature to an existing signed object.
let mut signed = create_signed_object?;
signed = add_signature?;
// signed.proofs.len() == 2
batch_sign(value, private_keys, is_data_update) -> Result<Signed<T>>
Create a signed object with multiple signatures at once.
let signed = batch_sign?;
// signed.proofs.len() == 3
verify(signed, is_data_update) -> VerificationResult
Verify all signatures on a signed object.
let result = verify;
if result.is_valid else
Low-Level Primitives
canonicalize(data) -> Result<String>
Canonicalize JSON data according to RFC 8785.
let canonical = canonicalize?;
// "{\"a\":1,\"b\":2}"
to_bytes(data, is_data_update) -> Result<Vec<u8>>
Convert data to binary bytes for signing.
// Regular encoding
let bytes = to_bytes?;
// DataUpdate encoding (with Constellation prefix)
let bytes = to_bytes?;
hash_data(data) -> Result<Hash> / hash_bytes(bytes) -> Hash
Compute SHA-256 hash.
let hash = hash_data?;
println!; // 64-char hex
println!; // [u8; 32]
sign(data, private_key) / sign_data_update(data, private_key)
Sign data and return a proof.
let proof = sign?;
// SignatureProof { id: "...", signature: "..." }
sign_hash(hash_hex, private_key) -> Result<String>
Sign a pre-computed hash.
let hash = hash_data?;
let signature = sign_hash?;
Wallet Utilities
generate_key_pair() -> KeyPair
Generate a new random key pair.
let key_pair = generate_key_pair;
// KeyPair { private_key, public_key, address }
key_pair_from_private_key(private_key) -> Result<KeyPair>
Derive a key pair from an existing private key.
let key_pair = key_pair_from_private_key?;
get_public_key_id(private_key) -> Result<String>
Get the public key ID (128 chars, no 04 prefix) for use in proofs.
let id = get_public_key_id?;
Currency Transactions
create_currency_transaction(params, private_key, last_ref) -> Result<CurrencyTransaction>
Create a metagraph token transaction.
use ;
let tx = create_currency_transaction?;
create_currency_transaction_batch(transfers, private_key, last_ref) -> Result<Vec<CurrencyTransaction>>
Create multiple token transactions in a batch.
let transfers = vec!;
let txns = create_currency_transaction_batch?;
sign_currency_transaction(transaction, private_key) -> Result<CurrencyTransaction>
Add an additional signature to a currency transaction (for multi-sig).
let mut tx = create_currency_transaction?;
tx = sign_currency_transaction?;
// tx.proofs.len() == 2
verify_currency_transaction(transaction) -> VerificationResult
Verify all signatures on a currency transaction.
let result = verify_currency_transaction;
println!;
hash_currency_transaction(transaction) -> Hash
Hash a currency transaction.
let hash = hash_currency_transaction;
println!;
get_transaction_reference(transaction, ordinal) -> TransactionReference
Get a transaction reference for chaining transactions.
let tx_ref = get_transaction_reference;
// Use tx_ref as last_ref for next transaction
Utility Functions
// Validate DAG address
is_valid_dag_address; // true/false
// Convert between token units and smallest units
token_to_units; // 10050000000
units_to_token; // 100.5
// Token decimals constant
TOKEN_DECIMALS; // 1e-8
Network Operations
Enable the network feature in your Cargo.toml:
[]
= { = "0.2", = ["network"] }
CurrencyL1Client
Client for interacting with Currency L1 nodes.
use ;
let config = NetworkConfig ;
let client = new?;
// Get last transaction reference for an address
let last_ref = client.get_last_reference.await?;
// Submit a signed transaction
let result = client.post_transaction.await?;
println!;
// Check pending transaction status
if let Some = client.get_pending_transaction.await?
// Check node health
let is_healthy = client.check_health.await;
DataL1Client
Client for interacting with Data L1 nodes (metagraphs).
use ;
let config = NetworkConfig ;
let client = new?;
// Estimate fee for data submission
let fee_info = client.estimate_fee.await?;
println!;
// Submit signed data
let result = client.post_data.await?;
println!;
// Check node health
let is_healthy = client.check_health.await;
Combined Configuration
let config = NetworkConfig ;
let l1_client = new?;
let data_client = new?;
Network Types
Types
// Currency transaction types
pub type CurrencyTransaction = ;
Usage Examples
Submit DataUpdate to L1
use ;
use json;
let data_update = json!;
// Sign as DataUpdate
let signed = create_signed_object?;
// Submit to data-l1 (using your HTTP client)
// POST http://l1-node:9300/data with signed as JSON body
Multi-Signature Workflow
use ;
// Party 1 creates and signs
let mut signed = create_signed_object?;
// Party 2 adds signature
signed = add_signature?;
// Party 3 adds signature
signed = add_signature?;
// Verify all signatures
let result = verify;
println!;
Currency Transactions
Create and Verify Token Transaction
use ;
// Generate keys
let sender_key = generate_key_pair;
let recipient_key = generate_key_pair;
// Get last transaction reference (from network or previous transaction)
let last_ref = TransactionReference ;
// Create transaction
let tx = create_currency_transaction?;
// Verify
let result = verify_currency_transaction;
println!;
// Note: Network submission not yet implemented in this SDK
// You can submit the transaction using dag4.js or custom network code
Batch Token Transactions
use ;
let last_ref = TransactionReference ;
let transfers = vec!;
// Create batch (transactions are automatically chained)
let txns = create_currency_transaction_batch?;
// txns[0].value.parent.ordinal == 10
// txns[1].value.parent.ordinal == 11
// txns[2].value.parent.ordinal == 12
Multi-Signature Token Transaction
use ;
let key1 = generate_key_pair;
let key2 = generate_key_pair;
let recipient = generate_key_pair;
let last_ref = TransactionReference ;
// Create transaction with first signature
let mut tx = create_currency_transaction?;
// Add second signature
tx = sign_currency_transaction?;
// Verify both signatures
let result = verify_currency_transaction;
println!;
Development
# Run tests
# Run tests with output
# Check for issues
# Format code
# Build release
License
Apache-2.0