Skip to content

Commit

Permalink
feat(CLI)!: rename subcommand 'identity' to 'keys'
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Nov 27, 2023
1 parent c136fc7 commit 58f64e1
Show file tree
Hide file tree
Showing 19 changed files with 86 additions and 72 deletions.
17 changes: 12 additions & 5 deletions cmd/crates/soroban-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir};
use fs_extra::dir::CopyOptions;

use soroban_cli::{
commands::{config, contract, contract::invoke, global, identity},
commands::{config, contract, contract::invoke, global, keys},
CommandParser, Pwd,
};

Expand Down Expand Up @@ -82,6 +82,13 @@ impl TestEnv {
pub fn new() -> Result<TestEnv, Error> {
let this = TempDir::new().map(|temp_dir| TestEnv { temp_dir })?;
std::env::set_var("XDG_CONFIG_HOME", this.temp_dir.as_os_str());
this.new_assert_cmd("keys")
.arg("generate")
.arg("test")
.arg("-d")
.arg("--no-fund")
.assert();
std::env::set_var("SOROBAN_ACCOUNT", "test");
Ok(this)
}

Expand Down Expand Up @@ -156,17 +163,17 @@ impl TestEnv {
&self.temp_dir
}

/// Returns the public key corresponding to the test identity's `hd_path`
/// Returns the public key corresponding to the test keys's `hd_path`
pub fn test_address(&self, hd_path: usize) -> String {
self.cmd::<identity::address::Cmd>(&format!("--hd-path={hd_path}"))
self.cmd::<keys::address::Cmd>(&format!("--hd-path={hd_path}"))
.public_key()
.unwrap()
.to_string()
}

/// Returns the private key corresponding to the test identity's `hd_path`
/// Returns the private key corresponding to the test keys's `hd_path`
pub fn test_show(&self, hd_path: usize) -> String {
self.cmd::<identity::show::Cmd>(&format!("--hd-path={hd_path}"))
self.cmd::<keys::show::Cmd>(&format!("--hd-path={hd_path}"))
.private_key()
.unwrap()
.to_string()
Expand Down
18 changes: 9 additions & 9 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use assert_fs::TempDir;
use soroban_test::TestEnv;
use std::{fs, path::Path};

use crate::util::{add_identity, add_test_id, SecretKind, DEFAULT_SEED_PHRASE};
use crate::util::{add_key, add_test_id, SecretKind, DEFAULT_SEED_PHRASE};
use soroban_cli::commands::network;

const NETWORK_PASSPHRASE: &str = "Local Sandbox Stellar Network ; September 2022";
Expand Down Expand Up @@ -137,24 +137,24 @@ fn multiple_networks() {
}

#[test]
fn read_identity() {
fn read_key() {
let sandbox = TestEnv::default();
let dir = sandbox.dir().as_ref();
add_test_id(dir);
let ident_dir = dir.join(".soroban/identity");
assert!(ident_dir.exists());
sandbox
.new_assert_cmd("identity")
.new_assert_cmd("keys")
.arg("ls")
.assert()
.stdout("test_id\n");
}

#[test]
fn generate_identity() {
fn generate_key() {
let sandbox = TestEnv::default();
sandbox
.new_assert_cmd("identity")
.new_assert_cmd("keys")
.arg("generate")
.arg("--network=futurenet")
.arg("--no-fund")
Expand All @@ -166,7 +166,7 @@ fn generate_identity() {
.success();

sandbox
.new_assert_cmd("identity")
.new_assert_cmd("keys")
.arg("ls")
.assert()
.stdout("test\n");
Expand All @@ -182,7 +182,7 @@ fn generate_identity() {
fn seed_phrase() {
let sandbox = TestEnv::default();
let dir = sandbox.dir();
add_identity(
add_key(
dir,
"test_seed",
SecretKind::Seed,
Expand Down Expand Up @@ -210,7 +210,7 @@ fn use_env() {
let sandbox = TestEnv::default();

sandbox
.new_assert_cmd("identity")
.new_assert_cmd("keys")
.env(
"SOROBAN_SECRET_KEY",
"SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD",
Expand All @@ -222,7 +222,7 @@ fn use_env() {
.success();

sandbox
.new_assert_cmd("identity")
.new_assert_cmd("keys")
.arg("show")
.arg("bob")
.assert()
Expand Down
12 changes: 1 addition & 11 deletions cmd/crates/soroban-test/tests/it/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use soroban_cli::commands::{
config::identity,
contract::{self, fetch},
};
use soroban_cli::commands::contract::{self, fetch};
use soroban_test::TestEnv;
use std::path::PathBuf;

Expand All @@ -11,13 +8,6 @@ use crate::util::{
TEST_SALT,
};








#[tokio::test]
async fn fetch() {
if !is_rpc() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use soroban_cli::commands::{
config::identity,
contract::{self, fetch},
keys,
};
use soroban_test::TestEnv;

Expand Down Expand Up @@ -111,7 +111,7 @@ fn invoke_auth(sandbox: &TestEnv, id: &str) {

async fn invoke_auth_with_identity(sandbox: &TestEnv, id: &str) {
sandbox
.cmd::<identity::generate::Cmd>("test -d ")
.cmd::<keys::generate::Cmd>("test -d ")
.run()
.await
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/integration/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use soroban_cli::commands::contract;
use soroban_test::{TestEnv, Wasm};
use std::{fmt::Display, path::Path};

use crate::util::{add_identity, SecretKind};
use crate::util::{add_key, SecretKind};

pub const HELLO_WORLD: &Wasm = &Wasm::Custom("test-wasms", "test_hello_world");
pub const CUSTOM_TYPES: &Wasm = &Wasm::Custom("test-wasms", "test_custom_types");

pub fn add_test_seed(dir: &Path) -> String {
let name = "test_seed";
add_identity(
add_key(
dir,
name,
SecretKind::Seed,
Expand Down
36 changes: 27 additions & 9 deletions cmd/crates/soroban-test/tests/it/integration/wrap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use soroban_cli::CommandParser;
use soroban_cli::{
commands::{
config::{self},
lab::token::wrap,
},
commands::{keys, lab::token::wrap},
utils::contract_id_hash_from_asset,
};
use soroban_test::TestEnv;
Expand All @@ -14,14 +11,16 @@ use super::util::network_passphrase;
#[ignore]
async fn burn() {
let sandbox = &TestEnv::default();
let address = config::identity::address::Cmd::parse("--hd-path=0")
let network_passphrase = network_passphrase().unwrap();
println!("NETWORK_PASSPHRASE: {network_passphrase:?}");
let address = keys::address::Cmd::parse("test")
.unwrap()
.public_key()
.unwrap();
let asset = format!("native:{address}");
wrap_cmd(&asset).run().await.unwrap();
let asset = soroban_cli::utils::parsing::parse_asset(&asset).unwrap();
let hash = contract_id_hash_from_asset(&asset, &network_passphrase().unwrap()).unwrap();
let hash = contract_id_hash_from_asset(&asset, &network_passphrase).unwrap();
let id = stellar_strkey::Contract(hash.0).to_string();
assert_eq!(
"CAMTHSPKXZJIRTUXQP5QWJIFH3XIDMKLFAWVQOFOXPTKAW5GKV37ZC4N",
Expand All @@ -33,6 +32,7 @@ async fn burn() {
.invoke(&[
"--id",
&id,
"--source=test",
"--",
"authorized",
"--id",
Expand All @@ -44,7 +44,16 @@ async fn burn() {
assert_eq!(
"\"9223372036854775807\"",
sandbox
.invoke(&["--id", &id, "--", "balance", "--id", &address.to_string()])
.invoke(&[
"--id",
&id,
"--source",
"test",
"--",
"balance",
"--id",
&address.to_string()
])
.await
.unwrap(),
);
Expand All @@ -55,6 +64,7 @@ async fn burn() {
.invoke(&[
"--id",
&id,
"--source=test",
"--",
"burn",
"--id",
Expand All @@ -68,12 +78,20 @@ async fn burn() {
assert_eq!(
"\"9223372036854775707\"",
sandbox
.invoke(&["--id", &id, "--", "balance", "--id", &address.to_string()])
.invoke(&[
"--id",
&id,
"--source=test",
"--",
"balance",
"--id",
&address.to_string()
])
.await
.unwrap(),
);
}

fn wrap_cmd(asset: &str) -> wrap::Cmd {
wrap::Cmd::parse_arg_vec(&[&format!("--asset={asset}")]).unwrap()
wrap::Cmd::parse_arg_vec(&["--source=test", &format!("--asset={asset}")]).unwrap()
}
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum SecretKind {
}

#[allow(clippy::needless_pass_by_value)]
pub fn add_identity(dir: &Path, name: &str, kind: SecretKind, data: &str) {
pub fn add_key(dir: &Path, name: &str, kind: SecretKind, data: &str) {
let secret = match kind {
SecretKind::Seed => Secret::SeedPhrase {
seed_phrase: data.to_string(),
Expand All @@ -32,7 +32,7 @@ pub fn add_identity(dir: &Path, name: &str, kind: SecretKind, data: &str) {

pub fn add_test_id(dir: &Path) -> String {
let name = "test_id";
add_identity(
add_key(
dir,
name,
SecretKind::Key,
Expand Down
2 changes: 2 additions & 0 deletions cmd/soroban-cli/src/commands/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub enum Error {
ConfigMissing(String, String),
#[error(transparent)]
String(#[from] std::string::FromUtf8Error),
#[error(transparent)]
Secret(#[from] crate::commands::config::secret::Error),
}

#[derive(Debug, clap::Args, Default, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ impl Cmd {
if let Some(mut val) = matches_.get_raw(&name) {
let mut s = val.next().unwrap().to_string_lossy().to_string();
if matches!(i.type_, ScSpecTypeDef::Address) {
let cmd = crate::commands::identity::address::Cmd {
name: Some(s.clone()),
let cmd = crate::commands::keys::address::Cmd {
name: s.clone(),
hd_path: Some(0),
locator: self.config.locator.clone(),
};
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::super::config::{
locator,
secret::{self, Secret},
};
use crate::commands::config::secret;

use super::super::config::locator;
use clap::arg;

#[derive(thiserror::Error, Debug)]
Expand All @@ -20,7 +19,7 @@ pub enum Error {
#[group(skip)]
pub struct Cmd {
/// Name of identity to lookup, default test identity used if not provided
pub name: Option<String>,
pub name: String,

/// If identity is a seed phrase use this hd path, default is 0
#[arg(long)]
Expand All @@ -37,20 +36,14 @@ impl Cmd {
}

pub fn private_key(&self) -> Result<ed25519_dalek::SigningKey, Error> {
Ok(if let Some(name) = &self.name {
self.locator.read_identity(name)?
} else {
Secret::test_seed_phrase()?
}
.key_pair(self.hd_path)?)
Ok(self
.locator
.read_identity(&self.name)?
.key_pair(self.hd_path)?)
}

pub fn public_key(&self) -> Result<stellar_strkey::ed25519::PublicKey, Error> {
if let Some(Ok(key)) = self
.name
.as_deref()
.map(stellar_strkey::ed25519::PublicKey::from_string)
{
if let Ok(key) = stellar_strkey::ed25519::PublicKey::from_string(&self.name) {
Ok(key)
} else {
Ok(stellar_strkey::ed25519::PublicKey::from_payload(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ impl Cmd {
}

pub fn ls(&self) -> Result<Vec<String>, Error> {
Ok(self.config_locator.list_identities()?)
let mut list = self.config_locator.list_identities()?;
let test = "test".to_string();
if !list.contains(&test) {
list.push(test);
}
Ok(list)
}

pub fn ls_l(&self) -> Result<Vec<String>, Error> {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum Error {
#[group(skip)]
pub struct Cmd {
/// Name of identity to lookup, default is test identity
pub name: Option<String>,
pub name: String,

/// If identity is a seed phrase use this hd path, default is 0
#[arg(long)]
Expand All @@ -35,11 +35,9 @@ impl Cmd {
}

pub fn private_key(&self) -> Result<stellar_strkey::ed25519::PrivateKey, Error> {
Ok(if let Some(name) = &self.name {
self.locator.read_identity(name)?
} else {
secret::Secret::test_seed_phrase()?
}
.private_key(self.hd_path)?)
Ok(self
.locator
.read_identity(&self.name)?
.private_key(self.hd_path)?)
}
}
Loading

0 comments on commit 58f64e1

Please sign in to comment.