1use anyhow::{Context, Result};
2
3use super::hab;
4
5#[allow(clippy::too_many_arguments)]
6pub async fn handle_incept(
7 name: &str,
8 alias: &str,
9 passcode: Option<&str>,
10 nopasscode: bool,
11 _file: Option<&str>,
12 transferable: Option<bool>,
13 wits: &[String],
14 toad: Option<u32>,
15 icount: Option<&str>,
16 isith: Option<&str>,
17 ncount: Option<&str>,
18 nsith: Option<&str>,
19 est_only: bool,
20 data: Option<&str>,
21 delpre: Option<&str>,
22 proxy: Option<&str>,
23 timeout: u64,
24) -> Result<()> {
25 if delpre.is_some() && proxy.is_none() {
26 anyhow::bail!("a delegated inception needs --proxy: a local identifier the delegator's witnesses know");
30 }
31 let _bran = crate::passcode::get_passcode(
32 passcode,
33 nopasscode,
34 "Enter passcode for keystore"
35 )?;
36
37 let mut hby = hab::open(name, None)?;
38
39 let count = parse_count(icount)?;
40 let nc = parse_count(ncount)?;
41
42 let hab = hby.make_hab(alias, libkeri::MakeHabArgs {
43 transferable,
44 isith: parse_tholder(isith)?,
45 icount: Some(count as usize),
46 nsith: parse_tholder(nsith)?,
47 ncount: Some(nc as usize),
48 toad,
49 wits: if !wits.is_empty() { Some(wits.to_vec()) } else { None },
50 est_only: if est_only { Some(true) } else { None },
51 data: parse_data(data)?,
52 delpre: delpre.map(str::to_string),
53 ..Default::default()
54 }).context("Failed to incept identifier")?;
55
56 let pre = hab.pre.clone().unwrap_or_default();
57 drop(hab);
58
59 if !wits.is_empty() {
60 println!("Waiting for witness receipts...");
61 let n = hab::collect_receipts(&mut hby, alias, 0).await?;
62 if (n as u32) < toad.unwrap_or(wits.len() as u32) {
63 anyhow::bail!("Only {} of {} witnesses receipted {}", n, wits.len(), pre);
64 }
65 }
66
67 if let Some(proxy) = proxy.filter(|_| delpre.is_some()) {
68 super::delegate::send_to_delegator(&mut hby, alias, proxy, 0).await?;
69 super::delegate::wait_and_complete(&mut hby, alias, proxy, 0, std::time::Duration::from_secs(timeout)).await?;
70 }
71
72 println!("✓ Identifier '{}' incepted", alias);
73 println!(" Prefix: {}", pre);
74
75 Ok(())
76}
77
78pub(crate) fn parse_count(count_str: Option<&str>) -> Result<u32> {
79 match count_str {
80 Some(s) => s.parse::<u32>()
81 .context(format!("Failed to parse count value: {}", s)),
82 None => Ok(1),
83 }
84}
85
86pub(crate) fn parse_data(data: Option<&str>) -> Result<Option<Vec<libkeri::SadValue>>> {
89 let Some(data) = data else { return Ok(None) };
90 let json = match data.strip_prefix('@') {
91 Some(path) => std::fs::read_to_string(path)
92 .with_context(|| format!("Failed to read anchor data file {}", path))?,
93 None => data.to_string(),
94 };
95 let value: libkeri::SadValue = serde_json::from_str(&json)
98 .context("data supplied to anchor in a seal must be valid JSON")?;
99 Ok(Some(match value {
100 libkeri::SadValue::Array(items) => items,
101 other => vec![other],
102 }))
103}
104
105pub(crate) fn parse_tholder(sith: Option<&str>) -> Result<Option<libkeri::Tholder>> {
106 match sith {
107 Some(s) => Ok(Some(libkeri::Tholder::new(
108 None, None,
109 Some(libkeri::TholderSith::HexString(s.to_string())),
110 ).context("Invalid threshold value")?)),
111 None => Ok(None),
112 }
113}