Skip to main content

graphrt_bundle/
lib.rs

1//! # graphrt-bundle
2//!
3//! A bundle (`.grt`) is one portable file holding everything a workflow needs to
4//! run on any graphrt host — the CLI, `graphrt serve`, or a phone through
5//! `graphrt-ffi`: the workflow document(s), WebAssembly plugins, knowledge-base
6//! configuration and data, a providers file without secrets, and assets.
7//!
8//! A bundle is a zip archive with a `bundle.json` manifest listing the SHA-256 of
9//! every file. Hosts check every hash when opening a bundle, and can require an
10//! Ed25519 signature (`bundle.sig`) from a trusted key, so a bundle exported by a
11//! builder platform is verifiable wherever it lands.
12//!
13//! ```no_run
14//! use graphrt_bundle::{Bundle, OpenOptions};
15//!
16//! let bytes = std::fs::read("app.grt")?;
17//! let bundle = Bundle::open(&bytes, &OpenOptions::default())?;
18//! let workflow = bundle.workflow(None)?;
19//! # Ok::<(), Box<dyn std::error::Error>>(())
20//! ```
21//!
22//! Secrets never travel in a bundle: providers files with inline keys are
23//! rejected, and environment variables are only *declared* (names), never
24//! stored.
25
26mod archive;
27mod builder;
28mod bundle;
29mod error;
30mod manifest;
31mod secrets;
32mod sign;
33#[cfg(feature = "wasm")]
34mod wasm;
35
36pub use builder::BundleBuilder;
37pub use bundle::{Bundle, BundledPlugin, CompatReport, OpenOptions};
38pub use error::BundleError;
39#[cfg(feature = "wasm")]
40pub use graphrt_wasm::WasmLimits;
41pub use manifest::{BUNDLE_FORMAT_VERSION, BundleManifest, MANIFEST_PATH, PluginRef, Requires, SIGNATURE_PATH};
42pub use secrets::check_no_inline_secrets;
43pub use sign::{BundleSignature, KeyPair, generate_keypair, parse_public_key, sign_bundle};
44
45/// Hex-encoded SHA-256 of `bytes`.
46pub fn sha256_hex(bytes: &[u8]) -> String {
47    use sha2::Digest;
48    let digest = sha2::Sha256::digest(bytes);
49    hex(&digest)
50}
51
52pub(crate) fn hex(bytes: &[u8]) -> String {
53    const DIGITS: &[u8; 16] = b"0123456789abcdef";
54    let mut out = String::with_capacity(bytes.len() * 2);
55    for b in bytes {
56        out.push(char::from(DIGITS.get(usize::from(b >> 4)).copied().unwrap_or(b'0')));
57        out.push(char::from(DIGITS.get(usize::from(b & 0x0f)).copied().unwrap_or(b'0')));
58    }
59    out
60}
61
62pub(crate) fn unhex(text: &str) -> Option<Vec<u8>> {
63    let text = text.trim();
64    if text.len() % 2 != 0 {
65        return None;
66    }
67    let mut out = Vec::with_capacity(text.len() / 2);
68    let bytes = text.as_bytes();
69    for pair in bytes.chunks(2) {
70        let s = std::str::from_utf8(pair).ok()?;
71        out.push(u8::from_str_radix(s, 16).ok()?);
72    }
73    Some(out)
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn hex_roundtrip() {
82        let bytes = [0u8, 1, 0xab, 0xff];
83        assert_eq!(hex(&bytes), "0001abff");
84        assert_eq!(unhex("0001abff").unwrap(), bytes);
85        assert!(unhex("abc").is_none());
86        assert!(unhex("zz").is_none());
87        assert_eq!(sha256_hex(b"abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
88    }
89}