Expand description
Binary serialization and deserialization for the XRPL protocol.
This crate implements the canonical binary encoding used by the XRP Ledger for transactions, ledger objects, and other protocol messages.
§Architecture
definitions— Field definitions loaded fromdefinitions.jsonfield_code— Field ID encoding (1-3 byte headers from type_code/field_code)error— Codec error typesserializer— Canonical binary serializationdeserializer— Binary to typed objectssigning— Signing-specific serialization with hash prefixes
§Examples
Serialize a transaction to binary and deserialize it back:
use serde_json::json;
use xrpl_mithril_codec::serializer::serialize_json_object;
use xrpl_mithril_codec::deserializer::deserialize_object;
// Build a minimal Payment transaction as JSON
let tx = json!({
"TransactionType": "Payment",
"Flags": 0u32,
"Sequence": 1u32,
"Fee": "12",
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"Destination": "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
"Amount": "1000000"
});
// Serialize to canonical binary
let map = tx.as_object().expect("json object");
let mut buf = Vec::new();
serialize_json_object(map, &mut buf, false)?;
assert!(!buf.is_empty());
// Deserialize back to JSON
let decoded = deserialize_object(&buf)?;
assert_eq!(decoded.get("TransactionType").and_then(|v| v.as_str()), Some("Payment"));
assert_eq!(decoded.get("Fee").and_then(|v| v.as_str()), Some("12"));
assert_eq!(decoded.get("Amount").and_then(|v| v.as_str()), Some("1000000"));
assert_eq!(
decoded.get("Account").and_then(|v| v.as_str()),
Some("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")
);Compute a transaction’s signing hash and transaction ID:
use serde_json::json;
use xrpl_mithril_codec::signing::{signing_hash, transaction_id_hex};
let tx = json!({
"TransactionType": "Payment",
"Sequence": 1u32,
"Fee": "12",
"Account": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
"Destination": "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
"Amount": "1000000",
"SigningPubKey": "ED5F5AC43F527AE97194E860E5B28E6751B0B3BBEAC0780826AAF6DB9B3EE001",
"TxnSignature": "DEADBEEFCAFE"
});
let map = tx.as_object().expect("json object");
// Signing hash (for the signer to sign over)
let hash = signing_hash(map)?;
assert_eq!(hash.len(), 32);
// Transaction ID (includes the signature in the hash)
let tx_id = transaction_id_hex(map)?;
assert_eq!(tx_id.len(), 64); // 32 bytes as uppercase hex
assert!(tx_id.chars().all(|c| c.is_ascii_hexdigit()));Modules§
- definitions
- Compile-time field definitions from
definitions.json. - deserializer
- Binary deserialization for XRPL objects.
- error
- Error types for the binary codec.
- field_
code - Field ID encoding and variable-length (VL) prefix encoding.
- serializer
- Canonical binary serialization for XRPL objects.
- signing
- Signing-specific serialization and transaction hashing.