DCID for cdk-rsx

The same canonical format as the Motoko and TypeScript implementations:

[ object ID ][ minimal 1–8 canister bytes ][ 3-bit byte-count-minus-one ]

Principal-only keys are the full unsigned 64-bit canister number. Object IDs and composite keys use cdk_rsx_wire::Nat, supporting arbitrary-size values. The library exports that Nat and ic_principal::Principal; both work with the bounded Wire public endpoint codec.

Local dependency

For an application directory directly beneath the repository root:

[dependencies]
cdk-rsx-dcid = { path = "../cdk-rsx/crates/cdk-rsx-dcid" }
use cdk_rsx_dcid::{encode_with_id, decode_with_id, to_decimal, to_hex, Nat, Principal};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let canister = Principal::from_text("mxzaz-hqaaa-aaaar-qaada-cai")?;
    let object_id = Nat::from(234242u64);
    let key = encode_with_id(&canister, &object_id)?;
    assert_eq!(to_decimal(&key), "8048494128398387");
    assert_eq!(to_hex(&key), "0x1c981011800033");
    assert_eq!(decode_with_id(&key)?, (canister, object_id));
    Ok(())
}

encode, decode, encode_with_id, decode_with_id, and is_canister match the Motoko API. Codec errors are InvalidPrincipal, Overflow, and NonCanonical; Error::as_str() gives the shared lower-camel-case error code. Error values support Wire serialization and generated interface metadata. A normal Rust Result uses Ok/Err labels in generated clients.

parse_nat accepts unsigned decimal strings and 0x hexadecimal strings. Use to_decimal for JSON fields: this produces a lossless decimal string that TypeScript's toNat accepts. Public endpoint values use Wire's unsigned magnitude representation; transport limits bound its encoded size and allocation.

Management, anonymous and self-authenticating principals are rejected. Only the ten-byte numbered-canister representation ending in 01 01 is supported. The principal-only decoder rejects numbers at or above 2^64. Composite decoding rejects redundant leading canister bytes and has no object-ID limit.

Tests

Use the workspace-pinned compiler. From the newstack repository root:

source tools/env.sh
cargo test --manifest-path cdk-rsx/Cargo.toml -p cdk-rsx-dcid
cargo run --manifest-path cdk-rsx/Cargo.toml -p cdk-rsx-dcid --example conformance -- \
  cdk-rsx/crates/cdk-rsx-dcid/fixtures/vectors.json

The Rust suite checks 65,536 exhaustive small pairs, 30,000 seeded property cases with shrinking, bounded Wire transport, invalid principals and canonical tags. The conformance example checks the copied independent shared corpus; it does not itself start a Motoko canister. Actual application interoperability is qualified separately by the RRT PocketIC suites. Source and license provenance are in UPSTREAM.json.

Maps and stable storage

The returned Nat implements Ord, Eq and Hash, so it can be used directly as a key in Rust's BTreeMap and HashMap. It is compared or hashed without decoding the principal. RSX persistent collections have their own codec/native layout contracts: Nat is not automatically a fixed persistent key. An owning module must choose and freeze a representation appropriate to its address range, while preserving the full public numeric value. Do not silently narrow a DCID to u128: a 64-bit object ID plus a full canister number and format tag can need 131 bits. A smaller stored key is appropriate only when the module proves its bound; the unrestricted public codec remains unchanged.