use super::{CERTIFICATE_LIMIT, admit_reply, custody, root};
use crate::files;
use clap::Args;
use ic_platform_legacy::{agent::lookup_value, certification::cbor::decode_certificate};
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.agent()?;
let (held, _, 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 certificate = decode_certificate(&bytes, CERTIFICATE_LIMIT)
.map_err(|_| "invalid retained certificate")?;
agent
.verify(&certificate, target.effective()?)
.map_err(|_| "retained certificate authentication failed")?;
let path = |leaf: &'static [u8]| [b"request_status".as_slice(), id.as_ref(), leaf];
if lookup_value(&certificate, path(b"status")).map_err(|_| "missing certified status")?
!= b"replied"
{
return Err("original certified status is not replied".into());
}
admit_reply(
lookup_value(&certificate, path(b"reply")).map_err(|_| "missing certified reply")?,
&target.root,
)?;
let intent = ic_wire::encode(
&Claim {
version: 1,
original_request: id.as_ref(),
gateway: &input.gateway,
project: &input.project,
bucket: &input.bucket,
certificate_sha256: hash,
},
ic_wire::Encoding::Payload,
ic_wire::Limits {
bytes: 4096,
allocation: 32_768,
nodes: 1024,
depth: 16,
},
)
.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={id}; no network call made.\nThis authenticates historical proof, not current application permission or provider acceptance."
))
}