Skip to main content

krust/
main.rs

1use clap::{Parser, Subcommand};
2use anyhow::Result;
3
4mod commands;
5mod error;
6mod passcode;
7
8use commands::*;
9
10#[derive(Parser)]
11#[command(name = "krust")]
12#[command(about = "A CLI for libkeri - KERI protocol implementation", long_about = None)]
13#[command(version)]
14struct Cli {
15    #[command(subcommand)]
16    command: Commands,
17}
18
19#[derive(Subcommand)]
20enum Commands {
21    /// Initialize a keystore and database environment
22    Init {
23        /// Name of the keystore
24        #[arg(short, long)]
25        name: String,
26
27        /// Optional passcode for the keystore
28        #[arg(short, long)]
29        passcode: Option<String>,
30
31        /// Use no passcode (alternative to --passcode)
32        #[arg(long)]
33        nopasscode: bool,
34
35        /// Salt for key derivation (hex string)
36        #[arg(short, long)]
37        salt: Option<String>,
38
39        /// Configuration directory path
40        #[arg(long, default_value = ".")]
41        config_dir: String,
42
43        /// Configuration file name
44        #[arg(long)]
45        config_file: Option<String>,
46    },
47
48    /// Passcode management commands
49    Passcode {
50        #[command(subcommand)]
51        command: PasscodeCommands,
52    },
53
54    /// Incept a new KERI identifier
55    Incept {
56        /// Name of the keystore to use
57        #[arg(short, long)]
58        name: String,
59
60        /// Alias for the identifier
61        #[arg(short, long)]
62        alias: String,
63
64        /// Optional passcode for the keystore
65        #[arg(short, long)]
66        passcode: Option<String>,
67
68        /// Use no passcode
69        #[arg(long)]
70        nopasscode: bool,
71
72        /// Configuration file for inception parameters
73        #[arg(short, long)]
74        file: Option<String>,
75
76        /// Whether the prefix is transferable
77        #[arg(short = 't', long)]
78        transferable: Option<bool>,
79
80        /// Witnesses (can be specified multiple times)
81        #[arg(short, long)]
82        wits: Vec<String>,
83
84        /// Witness threshold (threshold of acceptable duplicity)
85        #[arg(long)]
86        toad: Option<u32>,
87
88        /// Incepting key count
89        #[arg(long)]
90        icount: Option<String>,
91
92        /// Signing threshold for inception event
93        #[arg(long)]
94        isith: Option<String>,
95
96        /// Next key count
97        #[arg(long)]
98        ncount: Option<String>,
99
100        /// Signing threshold for next rotation
101        #[arg(long)]
102        nsith: Option<String>,
103
104        /// Only allow establishment events
105        #[arg(short, long)]
106        est_only: bool,
107
108        /// Anchor data: JSON seal or list of seals, or @file (as kli --data)
109        #[arg(short, long)]
110        data: Option<String>,
111
112        /// Delegator prefix: incept a delegated identifier (dip)
113        #[arg(long)]
114        delpre: Option<String>,
115
116        /// Local identifier that sends the delegated inception to the delegator (as kli --proxy)
117        #[arg(long)]
118        proxy: Option<String>,
119
120        /// Seconds to wait for the delegator's approval
121        #[arg(long, default_value_t = 120)]
122        timeout: u64,
123    },
124
125    /// Display status of a KERI identifier
126    Status {
127        /// Name of the keystore
128        #[arg(short, long)]
129        name: String,
130
131        /// Alias of the identifier
132        #[arg(short, long)]
133        alias: String,
134    },
135
136    /// Rotate keys for an existing identifier
137    Rotate {
138        /// Name of the keystore to use
139        #[arg(short, long)]
140        name: String,
141
142        /// Alias of the identifier to rotate
143        #[arg(short, long)]
144        alias: String,
145
146        /// Optional passcode for the keystore
147        #[arg(short, long)]
148        passcode: Option<String>,
149
150        /// Use no passcode
151        #[arg(long)]
152        nopasscode: bool,
153
154        /// Signing threshold for rotation event
155        #[arg(long)]
156        isith: Option<String>,
157
158        /// Signing threshold for next rotation
159        #[arg(long)]
160        nsith: Option<String>,
161
162        /// Next key count
163        #[arg(long)]
164        ncount: Option<String>,
165
166        /// Witness threshold (threshold of acceptable duplicity)
167        #[arg(long)]
168        toad: Option<u32>,
169
170        /// Witnesses to remove (can be specified multiple times)
171        #[arg(long)]
172        cuts: Vec<String>,
173
174        /// Witnesses to add (can be specified multiple times)
175        #[arg(long)]
176        adds: Vec<String>,
177
178        /// Anchor data: JSON seal or list of seals, or @file (as kli --data)
179        #[arg(short, long)]
180        data: Option<String>,
181
182        /// Local identifier that sends a delegated rotation to the delegator (default: the identifier itself)
183        #[arg(long)]
184        proxy: Option<String>,
185
186        /// Seconds to wait for the delegator's approval of a delegated rotation
187        #[arg(long, default_value_t = 120)]
188        timeout: u64,
189    },
190
191    /// Out-of-band introductions
192    Oobi {
193        #[command(subcommand)]
194        command: OobiCommands,
195    },
196
197    /// Group (multisig) identifiers
198    Multisig {
199        #[command(subcommand)]
200        command: MultisigCommands,
201    },
202
203    /// Delegation, delegator side
204    Delegate {
205        #[command(subcommand)]
206        command: DelegateCommands,
207    },
208}
209
210#[derive(Subcommand)]
211enum DelegateCommands {
212    /// Approve delegated events waiting for a local delegator (as kli delegate confirm)
213    Confirm {
214        /// Name of the keystore
215        #[arg(short, long)]
216        name: String,
217
218        /// Alias of the local delegator
219        #[arg(short, long)]
220        alias: String,
221
222        /// Anchor the approval in a rotation instead of an interaction event
223        #[arg(long)]
224        rotate: bool,
225
226        /// Delegate's witness OOBI to watch for its delegated event (repeatable)
227        #[arg(long)]
228        oobi: Vec<String>,
229
230        /// Seconds to wait for a delegated event to approve
231        #[arg(long, default_value_t = 120)]
232        timeout: u64,
233    },
234}
235
236#[derive(Subcommand)]
237enum MultisigCommands {
238    /// Propose a new group identifier, written to a file for the other members to join
239    Incept {
240        /// Name of the keystore
241        #[arg(short, long)]
242        name: String,
243
244        /// Alias of the local member identifier that signs for the group
245        #[arg(short, long)]
246        alias: String,
247
248        /// Alias for the group identifier
249        #[arg(short, long)]
250        group: String,
251
252        /// Signing member AIDs, in group key order (repeat for each member)
253        #[arg(long = "smids", required = true)]
254        smids: Vec<String>,
255
256        /// Rotating member AIDs, in next-key order (default: same as --smids)
257        #[arg(long = "rmids")]
258        rmids: Vec<String>,
259
260        /// Signing threshold (default: majority of members)
261        #[arg(long)]
262        isith: Option<String>,
263
264        /// Next signing threshold (default: same as --isith)
265        #[arg(long)]
266        nsith: Option<String>,
267
268        /// File to write the proposed inception with this member's signature
269        #[arg(short, long)]
270        out: String,
271    },
272
273    /// Add other members' signatures on a group event, read from a file
274    Ingest {
275        /// Name of the keystore
276        #[arg(short, long)]
277        name: String,
278
279        /// Alias of the group identifier
280        #[arg(short, long)]
281        group: String,
282
283        /// File holding the signed event
284        #[arg(short, long)]
285        file: String,
286    },
287
288    /// Join a group whose inception another member proposed (exchanged as a file)
289    Join {
290        /// Name of the keystore
291        #[arg(short, long)]
292        name: String,
293
294        /// Alias of the local member identifier that signs for the group
295        #[arg(short, long)]
296        alias: String,
297
298        /// Alias for the group identifier
299        #[arg(short, long)]
300        group: String,
301
302        /// Signing member AIDs, in group key order (repeat for each member)
303        #[arg(long = "smids", required = true)]
304        smids: Vec<String>,
305
306        /// Rotating member AIDs, in next-key order (default: same as --smids)
307        #[arg(long = "rmids")]
308        rmids: Vec<String>,
309
310        /// File holding the proposed group inception with the proposer's signatures
311        #[arg(short, long)]
312        file: String,
313
314        /// File to write the inception with every signature collected so far
315        #[arg(short, long)]
316        out: String,
317    },
318}
319
320#[derive(Subcommand)]
321enum OobiCommands {
322    /// Resolve an OOBI: fetch the KEL and endpoint replies it serves
323    Resolve {
324        /// Name of the keystore
325        #[arg(short, long)]
326        name: String,
327
328        /// Alias for the OOBI
329        #[arg(long)]
330        oobi_alias: String,
331
332        /// OOBI URL
333        #[arg(long)]
334        oobi: String,
335    },
336}
337
338#[derive(Subcommand)]
339enum PasscodeCommands {
340    /// Generate a cryptographically secure random passcode
341    Generate,
342}
343
344#[tokio::main]
345async fn main() -> Result<()> {
346    let cli = Cli::parse();
347
348    match cli.command {
349        Commands::Init { name, passcode, nopasscode, salt, config_dir, config_file } => {
350            init::handle_init(&name, passcode.as_deref(), nopasscode, salt.as_deref(), &config_dir, config_file.as_deref()).await?;
351        }
352        Commands::Passcode { command } => {
353            match command {
354                PasscodeCommands::Generate => {
355                    commands::passcode::handle_generate().await?;
356                }
357            }
358        }
359        Commands::Incept { name, alias, passcode, nopasscode, file, transferable, wits, toad, icount, isith, ncount, nsith, est_only, data, delpre, proxy, timeout } => {
360            incept::handle_incept(&name, &alias, passcode.as_deref(), nopasscode, file.as_deref(), transferable, &wits, toad, icount.as_deref(), isith.as_deref(), ncount.as_deref(), nsith.as_deref(), est_only, data.as_deref(), delpre.as_deref(), proxy.as_deref(), timeout).await?;
361        }
362        Commands::Oobi { command } => match command {
363            OobiCommands::Resolve { name, oobi_alias, oobi } => {
364                commands::oobi::handle_resolve(&name, &oobi_alias, &oobi).await?;
365            }
366        },
367        Commands::Multisig { command } => match command {
368            MultisigCommands::Incept { name, alias, group, smids, rmids, isith, nsith, out } => {
369                commands::multisig::handle_incept(&name, &alias, &group, &smids, &rmids, isith.as_deref(), nsith.as_deref(), &out).await?;
370            }
371            MultisigCommands::Ingest { name, group, file } => {
372                commands::multisig::handle_ingest(&name, &group, &file).await?;
373            }
374            MultisigCommands::Join { name, alias, group, smids, rmids, file, out } => {
375                commands::multisig::handle_join(&name, &alias, &group, &smids, &rmids, &file, &out).await?;
376            }
377        },
378        Commands::Delegate { command } => match command {
379            DelegateCommands::Confirm { name, alias, rotate, oobi, timeout } => {
380                commands::delegate::handle_confirm(&name, &alias, rotate, &oobi, timeout).await?;
381            }
382        },
383        Commands::Status { name, alias } => {
384            status::handle_status(&name, &alias).await?;
385        }
386        Commands::Rotate { name, alias, passcode, nopasscode, isith, nsith, ncount, toad, cuts, adds, data, proxy, timeout } => {
387            rotate::handle_rotate(&name, &alias, passcode.as_deref(), nopasscode, isith.as_deref(), nsith.as_deref(), ncount.as_deref(), toad, &cuts, &adds, data.as_deref(), proxy.as_deref(), timeout).await?;
388        }
389    }
390
391    Ok(())
392}