Skip to content

Commit 24bfc54

Browse files
authored
Add stellar keys unset to remove default identity. (#2337)
1 parent 58c5c81 commit 24bfc54

File tree

6 files changed

+184
-1
lines changed

6 files changed

+184
-1
lines changed

FULL_HELP_DOCS.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Anything after the `--` double dash (the "slop") is parsed as arguments to the c
5353
- `snapshot` — Download a snapshot of a ledger from an archive
5454
- `tx` — Sign, Simulate, and Send transactions
5555
- `xdr` — Decode and encode XDR
56+
- `strkey` — Decode and encode strkey
5657
- `completion` — Print shell completion code for the specified shell
5758
- `cache` — Cache for transactions and contract specs
5859
- `version` — Print version information
@@ -1125,6 +1126,7 @@ Create and manage identities including keys and addresses
11251126
- `rm` — Remove an identity
11261127
- `secret` — Output an identity's secret key
11271128
- `use` — Set the default identity that will be used on all commands. This allows you to skip `--source-account` or setting a environment variable, while reusing this value in all commands that require it
1129+
- `unset` — Unset the default key identity defined previously with `keys use <identity>`
11281130

11291131
## `stellar keys add`
11301132

@@ -1303,6 +1305,17 @@ Set the default identity that will be used on all commands. This allows you to s
13031305
- `--global` — ⚠️ Deprecated: global config is always on
13041306
- `--config-dir <CONFIG_DIR>` — Location of config directory. By default, it uses `$XDG_CONFIG_HOME/stellar` if set, falling back to `~/.config/stellar` otherwise. Contains configuration files, aliases, and other persistent settings
13051307

1308+
## `stellar keys unset`
1309+
1310+
Unset the default key identity defined previously with `keys use <identity>`
1311+
1312+
**Usage:** `stellar keys unset [OPTIONS]`
1313+
1314+
###### **Options (Global):**
1315+
1316+
- `--global` — ⚠️ Deprecated: global config is always on
1317+
- `--config-dir <CONFIG_DIR>` — Location of config directory. By default, it uses `$XDG_CONFIG_HOME/stellar` if set, falling back to `~/.config/stellar` otherwise. Contains configuration files, aliases, and other persistent settings
1318+
13061319
## `stellar network`
13071320

13081321
Configure connection to networks
@@ -4141,6 +4154,65 @@ Print version information
41414154

41424155
**Usage:** `stellar xdr version`
41434156

4157+
## `stellar strkey`
4158+
4159+
Decode and encode strkey
4160+
4161+
**Usage:** `stellar strkey <COMMAND>`
4162+
4163+
###### **Subcommands:**
4164+
4165+
- `decode` — Decode strkey
4166+
- `encode` — Encode strkey
4167+
- `zero` — Generate the zero strkey
4168+
- `version` — Print version information
4169+
4170+
## `stellar strkey decode`
4171+
4172+
Decode strkey
4173+
4174+
**Usage:** `stellar strkey decode <STRKEY>`
4175+
4176+
###### **Arguments:**
4177+
4178+
- `<STRKEY>` — Strkey to decode
4179+
4180+
## `stellar strkey encode`
4181+
4182+
Encode strkey
4183+
4184+
**Usage:** `stellar strkey encode <JSON>`
4185+
4186+
###### **Arguments:**
4187+
4188+
- `<JSON>` — JSON for Strkey to encode
4189+
4190+
## `stellar strkey zero`
4191+
4192+
Generate the zero strkey
4193+
4194+
**Usage:** `stellar strkey zero [OPTIONS] <STRKEY>`
4195+
4196+
###### **Arguments:**
4197+
4198+
- `<STRKEY>` — Strkey type to generate the zero value for
4199+
4200+
Possible values: `public_key_ed25519`, `pre_auth_tx`, `hash_x`, `muxed_account_ed25519`, `signed_payload_ed25519`, `contract`, `liquidity_pool`, `claimable_balance_v0`
4201+
4202+
###### **Options:**
4203+
4204+
- `--output <OUTPUT>` — Output format
4205+
4206+
Default value: `strkey`
4207+
4208+
Possible values: `strkey`, `json`
4209+
4210+
## `stellar strkey version`
4211+
4212+
Print version information
4213+
4214+
**Usage:** `stellar strkey version`
4215+
41444216
## `stellar completion`
41454217

41464218
Print shell completion code for the specified shell

cmd/crates/soroban-test/tests/it/integration/keys.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use predicates::prelude::predicate;
1+
use predicates::prelude::{predicate, PredicateBooleanExt};
22
use soroban_test::AssertExt;
33
use soroban_test::TestEnv;
44

@@ -143,3 +143,70 @@ async fn overwrite_identity_with_add() {
143143
pubkey_for_identity(sandbox, "test3").trim()
144144
);
145145
}
146+
147+
#[tokio::test]
148+
#[allow(clippy::too_many_lines)]
149+
async fn set_default_identity() {
150+
let sandbox = &TestEnv::new();
151+
sandbox
152+
.new_assert_cmd("keys")
153+
.arg("generate")
154+
.arg("test4")
155+
.assert()
156+
.success();
157+
158+
sandbox
159+
.new_assert_cmd("keys")
160+
.arg("use")
161+
.arg("test4")
162+
.assert()
163+
.stderr(predicate::str::contains(
164+
"The default source account is set to `test4`",
165+
))
166+
.success();
167+
}
168+
169+
#[tokio::test]
170+
#[allow(clippy::too_many_lines)]
171+
async fn unset_default_identity() {
172+
let sandbox = &TestEnv::new();
173+
sandbox
174+
.new_assert_cmd("keys")
175+
.arg("generate")
176+
.arg("test5")
177+
.assert()
178+
.success();
179+
180+
sandbox
181+
.new_assert_cmd("keys")
182+
.arg("use")
183+
.arg("test5")
184+
.assert()
185+
.stderr(predicate::str::contains(
186+
"The default source account is set to `test5`",
187+
))
188+
.success();
189+
190+
sandbox
191+
.new_assert_cmd("env")
192+
.env_remove("STELLAR_ACCOUNT")
193+
.assert()
194+
.stdout(predicate::str::contains("STELLAR_ACCOUNT=test5"))
195+
.success();
196+
197+
sandbox
198+
.new_assert_cmd("keys")
199+
.arg("unset")
200+
.assert()
201+
.stderr(predicate::str::contains(
202+
"The default source account has been unset",
203+
))
204+
.success();
205+
206+
sandbox
207+
.new_assert_cmd("env")
208+
.env_remove("STELLAR_ACCOUNT")
209+
.assert()
210+
.stdout(predicate::str::contains("STELLAR_ACCOUNT=").not())
211+
.success();
212+
}

cmd/soroban-cli/src/commands/keys/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod ls;
99
pub mod public_key;
1010
pub mod rm;
1111
pub mod secret;
12+
pub mod unset;
1213

1314
#[derive(Debug, Parser)]
1415
pub enum Cmd {
@@ -40,6 +41,9 @@ pub enum Cmd {
4041
/// variable, while reusing this value in all commands that require it.
4142
#[command(name = "use")]
4243
Default(default::Cmd),
44+
45+
/// Unset the default key identity defined previously with `keys use <identity>`
46+
Unset(unset::Cmd),
4347
}
4448

4549
#[derive(thiserror::Error, Debug)]
@@ -67,6 +71,9 @@ pub enum Error {
6771

6872
#[error(transparent)]
6973
Default(#[from] default::Error),
74+
75+
#[error(transparent)]
76+
Unset(#[from] unset::Error),
7077
}
7178

7279
impl Cmd {
@@ -80,6 +87,7 @@ impl Cmd {
8087
Cmd::Rm(cmd) => cmd.run(global_args)?,
8188
Cmd::Secret(cmd) => cmd.run()?,
8289
Cmd::Default(cmd) => cmd.run(global_args)?,
90+
Cmd::Unset(cmd) => cmd.run(global_args)?,
8391
}
8492
Ok(())
8593
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::{commands::global, config::locator, print::Print};
2+
3+
#[derive(thiserror::Error, Debug)]
4+
pub enum Error {
5+
#[error(transparent)]
6+
Config(#[from] locator::Error),
7+
}
8+
9+
#[derive(Debug, clap::Parser, Clone)]
10+
#[group(skip)]
11+
pub struct Cmd {
12+
#[command(flatten)]
13+
pub config_locator: locator::Args,
14+
}
15+
16+
impl Cmd {
17+
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
18+
let printer = Print::new(global_args.quiet);
19+
20+
self.config_locator.unset_identity()?;
21+
22+
printer.infoln("The default source account has been unset".to_string());
23+
24+
Ok(())
25+
}
26+
}

cmd/soroban-cli/src/config/locator.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ impl Args {
215215
Config::new()?.set_identity(name).save()
216216
}
217217

218+
pub fn unset_identity(&self) -> Result<(), Error> {
219+
Config::new()?.unset_identity().save()
220+
}
221+
218222
pub fn list_identities(&self) -> Result<Vec<String>, Error> {
219223
Ok(KeyType::Identity
220224
.list_paths(&self.local_and_global()?)?

cmd/soroban-cli/src/config/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ impl Config {
207207
self
208208
}
209209

210+
#[must_use]
211+
pub fn unset_identity(mut self) -> Self {
212+
self.defaults.identity = None;
213+
self
214+
}
215+
210216
pub fn save(&self) -> Result<(), locator::Error> {
211217
let toml_string = toml::to_string(&self)?;
212218
let path = cli_config_file()?;

0 commit comments

Comments
 (0)