use super::{CERTIFICATE_LIMIT, admit_reply, custody, root};
use crate::files;
use clap::Args;
use ic_platform::native::{PreparedUpdate, RequestStatus};
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::path::PathBuf;
#[derive(Args)]
#[group(id = "transfer")]
pub(crate) struct Input {
#[command(flatten)]
original: custody::Input,
#[arg(long)]
certificate: PathBuf,
#[arg(long, value_parser = root)]
certificate_sha256: String,
#[arg(long, value_parser = gateway)]
gateway: String,
#[arg(long, value_parser = selector)]
project: String,
#[arg(long, value_parser = selector)]
bucket: String,
}
fn gateway(value: &str) -> Result<String, String> {
let url = crate::endpoint(value)?;
if url.path() != "/" {
return Err("provider gateway must be an origin without a path".into());
}
Ok(url.origin().ascii_serialization())
}
fn selector(value: &str) -> Result<String, String> {
if value.is_empty() || value.len() > 128 || !value.bytes().all(|b| b.is_ascii_graphic()) {
return Err("provider selector must contain 1..128 printable ASCII bytes".into());
}
Ok(value.to_owned())
}
#[derive(Serialize)]
struct Claim<'a> {
version: u8,
original_request: &'a [u8],
gateway: &'a str,
project: &'a str,
bucket: &'a str,
certificate_sha256: [u8; 32],
}
pub(crate) fn claim(input: &Input) -> Result<String, String> {
let target = &input.original.target;
let agent = target.client()?;
let (held, packet, id) = custody::load(&input.original, &agent)?;
if files::read_at(&held, "dispatch.claim".as_ref(), 32)? != id.to_vec() {
return Err("original dispatch custody is incomplete; preserve it".into());
}
let bytes = crate::read(&input.certificate, CERTIFICATE_LIMIT)?;
let hash: [u8; 32] = Sha256::digest(&bytes).into();
if format!("sha256:{}", crate::overview::hex(&hash)) != input.certificate_sha256 {
return Err("certificate differs from the client's transfer plan".into());
}
let original = PreparedUpdate::from_bytes(&packet, target.canister, super::PACKET_LIMIT)
.map_err(|_| "invalid retained signed request")?;
let observed = agent
.transport
.authenticate_request(&original, &bytes, crate::network::policy(true)?)
.map_err(|_| "retained certificate authentication failed")?;
let RequestStatus::Replied(reply) = observed.status() else {
return Err("certificate has no reply".into());
};
admit_reply(reply, &target.root)?;
let intent = ic_blob_storage::storage_codec::encode(
&Claim {
version: 3,
original_request: id.as_ref(),
gateway: &input.gateway,
project: &input.project,
bucket: &input.bucket,
certificate_sha256: hash,
},
4096,
)
.map_err(|_| "cannot encode bounded transfer claim")?;
let claim = held
.create_directory("transfer".as_ref(), 0o700, 0o077)
.map_err(|_| "transfer already claimed or custody unavailable; preserve it, never retry")?;
for (name, data) in [
("intent.wire", intent.as_slice()),
("certificate.cbor", bytes.as_slice()),
] {
files::publish(&claim, name.as_ref(), data).map_err(|error| {
format!("transfer custody publication failed; preserve claim: {error}")
})?;
}
Ok(format!(
"Transfer claim retained for original request={}; no network call made.\nThis authenticates historical proof, not current application permission or provider acceptance.",
crate::overview::hex(&id)
))
}