| 1024 | } |
| 1025 | |
| 1026 | pub fn intent_attest(args: &super::intent::attest::IntentAttest) -> CliResult<bool> { |
| 1027 | let Some(session) = Service::open()? else { |
| 1028 | return Ok(false); |
| 1029 | }; |
| 1030 | let target = pb::AttestationTarget { |
| 1031 | kind: Some(TargetKind::IntentId(args.id.clone())), |
| 1032 | }; |
| 1033 | |
| 1034 | // Signing elsewhere, step 1: say what to sign — the document and the |
| 1035 | // bytes a key holder elsewhere (a browser, a hardware token, a signing |
| 1036 | // service) signs. No key on this machine is involved. |
| 1037 | if args.prepare { |
| 1038 | let request = pb::PrepareAttestationRequest { |
| 1039 | repository: Some(session.reference.clone()), |
| 1040 | meta: request_meta(), |
| 1041 | target: Some(target), |
| 1042 | identity_did: args.identity.clone().unwrap_or_default(), |
| 1043 | view: None, |
| 1044 | }; |
| 1045 | let response = session.prepare_attestation(request)?; |
| 1046 | let document: serde_json::Value = serde_json::from_str(&response.document) |
| 1047 | .map_err(|e| CliError::Internal(anyhow::anyhow!("prepared document: {e}")))?; |
| 1048 | println!( |
| 1049 | "{}", |
| 1050 | serde_json::to_string_pretty(&serde_json::json!({ |
| 1051 | "document": document, |
| 1052 | "signingBytes": data_encoding::BASE64.encode(&response.signing_bytes), |
| 1053 | })) |
| 1054 | .unwrap() |
| 1055 | ); |
| 1056 | return Ok(true); |
| 1057 | } |
| 1058 | |
| 1059 | // Signing elsewhere, step 2: record an attestation signed elsewhere. The |
| 1060 | // file holds the `--prepare` document with its proof attached; the service |
| 1061 | // re-derives the document from the intent AS IT IS NOW and verifies the |
| 1062 | // signature against it, so a stale or altered intent is refused. |
| 1063 | let caller_signature = if let Some(path) = &args.signed { |
| 1064 | let text = std::fs::read_to_string(path).map_err(CliError::Io)?; |
| 1065 | let signed: serde_json::Value = |
| 1066 | serde_json::from_str(&text).map_err(|e| CliError::InvalidArgument { |
| 1067 | message: format!("{} is not JSON: {e}", path.display()), |
| 1068 | })?; |
| 1069 | atomic_canonical::proof::proof_signature(&signed) |
| 1070 | .map_err(|e| CliError::InvalidArgument { |
| 1071 | message: format!("not an attested intent: {e}"), |
| 1072 | })? |
| 1073 | .as_bytes() |
| 1074 | .to_vec() |
| 1075 | } else { |
| 1076 | Vec::new() |
| 1077 | }; |
| 1078 | |
| 1079 | let request = pb::RecordAttestationRequest { |
| 1080 | repository: Some(session.reference.clone()), |
| 1081 | meta: request_meta(), |
| 1082 | target: Some(target), |
| 1083 | identity_did: args.identity.clone().unwrap_or_default(), |