#[cfg(test)]
mod tests;
use clap::Args;
use ic_blob_storage::{
caffeine::ManifestLimits,
local::Limits,
protocol::{Installation as BlobInstallation, RPC_LIMITS},
};
use ic_blob_storage_reference_app::protocol::Installation;
use ic_platform_types::Principal;
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::{
num::{NonZeroU32, NonZeroU64},
path::PathBuf,
};
use crate::{
files,
overview::{hex, principal},
};
#[derive(Args)]
#[command(
after_help = "All limits are immutable local admission limits, not provider-enforced storage or cost caps. Both canister IDs must already be selected. The output is not a signed install request or proof of an empty target; installation and controller checks remain separate."
)]
pub(crate) struct Input {
#[arg(long, value_parser = principal)]
canister: Principal,
#[arg(long, value_parser = principal)]
blob_canister: Principal,
#[arg(long, value_parser = principal)]
manager: Principal,
#[arg(long, required = true, value_parser = principal)]
user: Vec<Principal>,
#[arg(long)]
max_content_bytes: NonZeroU64,
#[arg(long)]
max_chunks: NonZeroU32,
#[arg(long)]
max_headers: NonZeroU32,
#[arg(long)]
max_header_bytes: NonZeroU32,
#[arg(long, value_parser = clap::value_parser!(u8).range(1..=64))]
uploads: u8,
#[arg(long, value_parser = clap::value_parser!(u8).range(1..=64))]
references: u8,
#[arg(long)]
logical_bytes: NonZeroU64,
#[arg(long)]
exposure_bytes: NonZeroU64,
#[arg(long)]
output: PathBuf,
}
fn frame<T: Serialize>(method: &str, input: &T) -> Result<Vec<u8>, String> {
ic_wire::encode(
input,
ic_wire::Encoding::Frame {
method: ic_wire::method_id(method),
direction: ic_wire::Direction::Request,
},
RPC_LIMITS,
)
.map_err(|_| "cannot encode installation input".into())
}
pub(super) fn validate_roles(
canister: Principal,
blob_canister: Principal,
manager: Principal,
users: &[Principal],
) -> Result<(), String> {
let authorities = [canister, blob_canister, manager];
if authorities
.iter()
.enumerate()
.any(|(i, p)| principal(&p.to_text()).is_err() || authorities[..i].contains(p))
|| users.is_empty()
|| users.len() > 8
|| users.iter().enumerate().any(|(i, p)| {
principal(&p.to_text()).is_err() || authorities.contains(p) || users[..i].contains(p)
})
{
return Err("canisters, manager and one to eight users must be distinct".into());
}
Ok(())
}
pub(crate) fn run(input: Input) -> Result<String, String> {
validate_roles(
input.canister,
input.blob_canister,
input.manager,
&input.user,
)?;
let blob = BlobInstallation {
application: input.canister,
manager: input.manager,
limits: Limits {
manifest: ManifestLimits {
max_content_bytes: input.max_content_bytes,
max_chunks: input.max_chunks.try_into().unwrap(),
max_headers: input.max_headers.try_into().unwrap(),
max_header_bytes: input.max_header_bytes.try_into().unwrap(),
},
uploads: usize::from(input.uploads).try_into().unwrap(),
references: usize::from(input.references).try_into().unwrap(),
logical_bytes: input.logical_bytes,
exposure_bytes: input.exposure_bytes,
},
};
let app = Installation {
blob: input.blob_canister,
users: input.user,
};
let blob_bytes = frame("blob_init", &blob)?;
let app_bytes = frame("app_init", &app)?;
let report = format!(
"Offline fresh-install inputs (Blob {})\nApplication canister: {}\nBlob canister: {}\nManager: {}\nUsers: {:?}\nBlob limits: {:?}\nblob-init.wire SHA-256: {}\napp-init.wire SHA-256: {}\n\nNo network or signing. Targets, controllers, emptiness, Wasm and deployment are not authenticated. These are init arguments, not signed requests or replay protection. Never use reinstall to apply them to existing state. Provider acceptance and charge limits remain unqualified.\n",
env!("CARGO_PKG_VERSION"),
input.canister,
input.blob_canister,
input.manager,
app.users.iter().map(Principal::to_text).collect::<Vec<_>>(),
blob.limits,
hex(&Sha256::digest(&blob_bytes)),
hex(&Sha256::digest(&app_bytes)),
);
let output = files::claim(&input.output)?;
files::publish(&output, "blob-init.wire".as_ref(), &blob_bytes)?;
files::publish(&output, "app-init.wire".as_ref(), &app_bytes)?;
files::publish(&output, "inputs.txt".as_ref(), report.as_bytes())?;
Ok(report)
}