MCPcopy Create free account
hub / github.com/bitcoindevkit/bdk-cli / handle_key_subcommand

Function handle_key_subcommand

src/handlers.rs:897–1004  ·  view source on GitHub ↗

Handle a key sub-command Key sub-commands are described in [`KeySubCommand`].

(
    network: Network,
    subcommand: KeySubCommand,
    pretty: bool,
)

Source from the content-addressed store, hash-verified

895///
896/// Key sub-commands are described in [`KeySubCommand`].
897pub(crate) fn handle_key_subcommand(
898 network: Network,
899 subcommand: KeySubCommand,
900 pretty: bool,
901) -> Result<String, Error> {
902 let secp = Secp256k1::new();
903
904 match subcommand {
905 KeySubCommand::Generate {
906 word_count,
907 password,
908 } => {
909 let mnemonic_type = match word_count {
910 12 => WordCount::Words12,
911 _ => WordCount::Words24,
912 };
913 let mnemonic: GeneratedKey<_, miniscript::BareCtx> =
914 Mnemonic::generate((mnemonic_type, Language::English))
915 .map_err(|_| Error::Generic("Mnemonic generation error".to_string()))?;
916 let mnemonic = mnemonic.into_key();
917 let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
918 let xprv = xkey.into_xprv(network).ok_or_else(|| {
919 Error::Generic("Privatekey info not found (should not happen)".to_string())
920 })?;
921 let fingerprint = xprv.fingerprint(&secp);
922 let phrase = mnemonic
923 .words()
924 .fold("".to_string(), |phrase, w| phrase + w + " ")
925 .trim()
926 .to_string();
927 if pretty {
928 let table = vec![
929 vec![
930 "Fingerprint".cell().bold(true),
931 fingerprint.to_string().cell(),
932 ],
933 vec!["Mnemonic".cell().bold(true), mnemonic.to_string().cell()],
934 vec!["Xprv".cell().bold(true), xprv.to_string().cell()],
935 ]
936 .table()
937 .display()
938 .map_err(|e| Error::Generic(e.to_string()))?;
939 Ok(format!("{table}"))
940 } else {
941 Ok(serde_json::to_string_pretty(
942 &json!({ "mnemonic": phrase, "xprv": xprv.to_string(), "fingerprint": fingerprint.to_string() }),
943 )?)
944 }
945 }
946 KeySubCommand::Restore { mnemonic, password } => {
947 let mnemonic = Mnemonic::parse_in(Language::English, mnemonic)?;
948 let xkey: ExtendedKey = (mnemonic.clone(), password).into_extended_key()?;
949 let xprv = xkey.into_xprv(network).ok_or_else(|| {
950 Error::Generic("Privatekey info not found (should not happen)".to_string())
951 })?;
952 let fingerprint = xprv.fingerprint(&secp);
953 if pretty {
954 let table = vec![

Callers 2

handle_commandFunction · 0.85
respondFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected