From b57a4606f656a24d7682b5826016b264dd2df2cd Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Thu, 16 Feb 2023 12:38:37 +0000 Subject: [PATCH] cli: add did function for args parsing Adds a standard for parsing the Did type for the CLI commands. It attempts to parse the Did type. If it fails then it checks if the operator passed a PublicKey and suggests using `did:key` instead. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-cli/src/terminal/args.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/radicle-cli/src/terminal/args.rs b/radicle-cli/src/terminal/args.rs index ece18463..c6b56ea4 100644 --- a/radicle-cli/src/terminal/args.rs +++ b/radicle-cli/src/terminal/args.rs @@ -2,6 +2,8 @@ use std::ffi::OsString; use std::str::FromStr; use anyhow::anyhow; +use radicle::crypto; +use radicle::prelude::Did; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -71,3 +73,15 @@ pub fn finish(unparsed: Vec) -> anyhow::Result<()> { } Ok(()) } + +pub fn did(val: OsString) -> anyhow::Result { + let val = val.to_string_lossy(); + let Ok(peer) = Did::from_str(&val) else { + if crypto::PublicKey::from_str(&val).is_ok() { + return Err(anyhow!("expected DID, did you mean 'did:key:{val}'?")); + } else { + return Err(anyhow!("invalid DID '{}', expected 'did:key'", val)); + } + }; + Ok(peer) +}