upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/ngit/sub_commands/export_keys.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-11-27 07:41:25 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-11-27 07:41:25 +0000
commitc92e252adc9990fa4d147bad0a8bccafc19dfbb8 (patch)
tree4bea092f25cd8c06ca0ad11a691cf623a24ff9cb /src/bin/ngit/sub_commands/export_keys.rs
parentbdf71cb3d5a5ff8399c10c8d2492d3dd01c5fa33 (diff)
feat(export-keys): to use in other clients
as part of the easy on-boaridng flow
Diffstat (limited to 'src/bin/ngit/sub_commands/export_keys.rs')
-rw-r--r--src/bin/ngit/sub_commands/export_keys.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/bin/ngit/sub_commands/export_keys.rs b/src/bin/ngit/sub_commands/export_keys.rs
new file mode 100644
index 0000000..12fc380
--- /dev/null
+++ b/src/bin/ngit/sub_commands/export_keys.rs
@@ -0,0 +1,89 @@
1use anyhow::{Context, Result};
2use ngit::{
3 cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms},
4 login::{
5 existing::{get_signer_info, load_existing_login},
6 fresh::generate_qr,
7 SignerInfo, SignerInfoSource,
8 },
9};
10
11use crate::git::Repo;
12
13pub async fn launch() -> Result<()> {
14 let git_repo_result = Repo::discover().context("failed to find a git repository");
15 let git_repo = {
16 match git_repo_result {
17 Ok(git_repo) => Some(git_repo),
18 Err(_) => None,
19 }
20 };
21
22 if let Ok((signer_info, source)) = get_signer_info(&git_repo.as_ref(), &None, &None, &None) {
23 if let Ok((_, user_ref, source)) = load_existing_login(
24 &git_repo.as_ref(),
25 &None,
26 &None,
27 &Some(source),
28 None,
29 true,
30 false,
31 )
32 .await
33 {
34 let logged_in_msg = format!(
35 "logged in {}as {}",
36 if source == SignerInfoSource::GitLocal {
37 "to local git repository "
38 } else {
39 ""
40 },
41 user_ref.metadata.name
42 );
43 match signer_info {
44 SignerInfo::Bunker {
45 bunker_uri: _,
46 bunker_app_key: _,
47 npub: _,
48 } => {
49 eprintln!(
50 "failed: {logged_in_msg} using nostr connect so your keys are stored in a remote signer"
51 );
52 return Ok(());
53 }
54 SignerInfo::Nsec {
55 nsec,
56 password: _,
57 npub: _,
58 } => {
59 match Interactor::default().choice(
60 PromptChoiceParms::default()
61 .with_default(0)
62 .with_prompt(logged_in_msg)
63 .with_choices(vec![
64 "print nsec".to_string(),
65 "show QR code of nsec".to_string(),
66 "cancel".to_string(),
67 ]),
68 )? {
69 0 => {
70 println!("{nsec}");
71 return Ok(());
72 }
73 1 => {
74 for line in generate_qr(&nsec)? {
75 println!("{line}");
76 }
77 return Ok(());
78 }
79 _ => {
80 return Ok(());
81 }
82 }
83 }
84 }
85 }
86 }
87 eprintln!("not logged in so no keys are stored");
88 Ok(())
89}