Skip to main content

krust/commands/
multisig.rs

1use anyhow::{Context, Result};
2
3use super::hab;
4use super::incept::parse_tholder;
5
6/// `krust multisig join`: join a group (multisig) identifier whose inception
7/// another member proposed, exchanged out of band as a file.
8///
9/// Reads the proposed group `icp` with the proposer's signatures from `file`,
10/// checks it against the members' KELs, adds the signature of local member
11/// `alias`, and records the group as `group`. Writes the inception with all
12/// signatures collected so far to `out`, for the other members to ingest.
13///
14/// Unlike `kli multisig join`, nothing is exchanged through mailboxes yet.
15pub async fn handle_join(
16    name: &str,
17    alias: &str,
18    group: &str,
19    smids: &[String],
20    rmids: &[String],
21    file: &str,
22    out: &str,
23) -> Result<()> {
24    let mut hby = hab::open(name, None)?;
25    let proposal = std::fs::read(file)
26        .with_context(|| format!("Failed to read proposed inception {}", file))?;
27    let rmids = (!rmids.is_empty()).then_some(rmids);
28
29    let signed = hby
30        .join_group_inception(group, alias, smids, rmids, &proposal)
31        .context("Failed to join group")?;
32    std::fs::write(out, &signed)
33        .with_context(|| format!("Failed to write signed inception to {}", out))?;
34
35    let hab = hby.hab_by_name(group)?;
36    let pre = hab.pre.clone().unwrap_or_default();
37    println!("✓ Joined group '{}' as member '{}'", group, alias);
38    println!("  Prefix: {}", pre);
39    println!("  Signed inception written to {}", out);
40    Ok(())
41}
42
43/// `krust multisig incept`: propose a new group identifier with local member
44/// `alias` (keripy `makeGroupHab`; kli `multisig incept` without mailboxes).
45///
46/// Builds the group inception from the members' KELs, signs it as `alias`
47/// and writes it to `out` for the other members to join. Until enough of
48/// them have signed, the event waits in the partial-signature escrow; their
49/// signatures are added with `krust multisig ingest`.
50#[allow(clippy::too_many_arguments)]
51pub async fn handle_incept(
52    name: &str,
53    alias: &str,
54    group: &str,
55    smids: &[String],
56    rmids: &[String],
57    isith: Option<&str>,
58    nsith: Option<&str>,
59    out: &str,
60) -> Result<()> {
61    let mut hby = hab::open(name, None)?;
62    let rmids = (!rmids.is_empty()).then_some(rmids);
63    let isith = parse_tholder(isith)?.map(|t| t.sith());
64    let nsith = parse_tholder(nsith)?.map(|t| t.sith());
65
66    let proposal = hby
67        .make_group_hab(group, alias, smids, rmids, isith, nsith)
68        .context("Failed to propose group")?;
69    std::fs::write(out, &proposal)
70        .with_context(|| format!("Failed to write proposed inception to {}", out))?;
71
72    let pre = hby.hab_by_name(group)?.pre.clone().unwrap_or_default();
73    println!("✓ Proposed group '{}' with member '{}'", group, alias);
74    println!("  Prefix: {}", pre);
75    println!("  Accepted: {}", hby.kevers().contains_key(&pre));
76    println!("  Proposed inception written to {}", out);
77    Ok(())
78}
79
80/// `krust multisig ingest`: add other members' signatures on a group event,
81/// read from `file`. Partially signed events complete from the escrow once
82/// enough signatures have arrived.
83pub async fn handle_ingest(name: &str, group: &str, file: &str) -> Result<()> {
84    let mut hby = hab::open(name, None)?;
85    let pre = hby
86        .hab_by_name(group)
87        .with_context(|| format!("No local group '{}'", group))?
88        .pre
89        .clone()
90        .unwrap_or_default();
91    let msgs = std::fs::read(file).with_context(|| format!("Failed to read {}", file))?;
92    // The file was handed over out of band by the operator: a local source.
93    let n = hby.ingest(&msgs, true).context("Failed to ingest signatures")?;
94
95    let accepted = hby.kevers().contains_key(&pre);
96    println!("✓ Ingested {} message(s) for group '{}'", n, group);
97    println!("  Prefix: {}", pre);
98    println!("  Accepted: {}", accepted);
99    if !accepted {
100        println!("  Still waiting for signatures from other members");
101    }
102    Ok(())
103}