Skip to content

Commit

Permalink
feat(CLI): config identity fund to fund accounts on networks (stell…
Browse files Browse the repository at this point in the history
…ar#828)

* feat: fund command

* feat: move `config identity`

Also use `network` on `config identity generate`

* fix: allow for https and provide better errors

* fix: docs and fmt

* fix: get url from rpc and add openssl

* fix: docs

* fix: skip https on windows for now

* fix: prevent windows in cargo.toml
  • Loading branch information
willemneal committed Aug 10, 2023
1 parent 295b8a5 commit a3eac81
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 53 deletions.
119 changes: 119 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions cmd/crates/soroban-test/tests/it/invoke_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,14 @@ fn invoke_auth() {
.success();
}

#[test]
fn invoke_auth_with_identity() {
#[tokio::test]
async fn invoke_auth_with_identity() {
let sandbox = TestEnv::default();

sandbox
.cmd::<identity::generate::Cmd>("test -d ")
.run()
.await
.unwrap();
sandbox
.new_assert_cmd("contract")
Expand Down
5 changes: 5 additions & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ csv = "1.1.6"
ed25519-dalek = "1.0.1"
jsonrpsee-http-client = "0.18.1"
jsonrpsee-core = "0.18.1"
hyper = "0.14.27"
hyper-tls = "0.5"
http = "0.2.9"
regex = "1.6.0"
wasm-opt = { version = "0.114.0", optional = true }
Expand All @@ -89,6 +91,9 @@ tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
cargo_metadata = "0.15.4"
pathdiff = "0.2.1"
# For hyper-tls
[target.'cfg(unix)'.dependencies]
openssl = { version = "0.10.55", features = ["vendored"] }

[build-dependencies]
crate-git-revision = "0.0.4"
Expand Down
11 changes: 4 additions & 7 deletions cmd/soroban-cli/src/commands/config/identity/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@ pub struct Cmd {

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
println!("{}", self.public_key()?.to_string());
println!("{}", self.public_key()?);
Ok(())
}

pub fn public_key(&self) -> Result<stellar_strkey::ed25519::PublicKey, Error> {
let res = if let Some(name) = &self.name {
Ok(if let Some(name) = &self.name {
self.locator.read_identity(name)?
} else {
Secret::test_seed_phrase()?
};
let key = res.key_pair(self.hd_path)?;
Ok(stellar_strkey::ed25519::PublicKey::from_payload(
key.public.as_bytes(),
)?)
}
.public_key(self.hd_path)?)
}
}
34 changes: 34 additions & 0 deletions cmd/soroban-cli/src/commands/config/identity/fund.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use clap::command;

use crate::commands::config::network;

use super::address;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Address(#[from] address::Error),
#[error(transparent)]
Network(#[from] network::Error),
}

#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub network: network::Args,
/// Address to fund
#[command(flatten)]
pub address: address::Cmd,
}

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let addr = self.address.public_key()?;
self.network
.get(&self.address.locator)?
.fund_address(&addr)
.await?;
Ok(())
}
}
19 changes: 16 additions & 3 deletions cmd/soroban-cli/src/commands/config/identity/generate.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use clap::{arg, command};

use super::super::{
locator,
locator, network,
secret::{self, Secret},
};
use clap::{arg, command};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),
#[error(transparent)]
Secret(#[from] secret::Error),
#[error(transparent)]
Network(#[from] network::Error),
}

#[derive(Debug, clap::Parser, Clone)]
Expand Down Expand Up @@ -38,10 +41,13 @@ pub struct Cmd {
/// Equivalent to --seed 0000000000000000
#[arg(long, short = 'd', conflicts_with = "seed")]
pub default_seed: bool,

#[command(flatten)]
pub network: network::Args,
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
pub async fn run(&self) -> Result<(), Error> {
let seed_phrase = if self.default_seed {
Secret::test_seed_phrase()
} else {
Expand All @@ -56,6 +62,13 @@ impl Cmd {
seed_phrase
};
self.config_locator.write_identity(&self.name, &secret)?;
if !self.network.is_no_network() {
let addr = secret.public_key(self.hd_path)?;
self.network
.get(&self.config_locator)?
.fund_address(&addr)
.await?;
}
Ok(())
}
}
16 changes: 11 additions & 5 deletions cmd/soroban-cli/src/commands/config/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;

pub mod add;
pub mod address;
pub mod fund;
pub mod generate;
pub mod ls;
pub mod rm;
Expand All @@ -13,6 +14,8 @@ pub enum Cmd {
Add(add::Cmd),
/// Given an identity return its address (public key)
Address(address::Cmd),
/// Fund an identity on a test network
Fund(fund::Cmd),
/// Generate a new identity with a seed phrase, currently 12 words
Generate(generate::Cmd),
/// List identities
Expand All @@ -30,26 +33,29 @@ pub enum Error {

#[error(transparent)]
Address(#[from] address::Error),
#[error(transparent)]
Fund(#[from] fund::Error),

#[error(transparent)]
Generate(#[from] generate::Error),
#[error(transparent)]
Rm(#[from] rm::Error),
#[error(transparent)]
Ls(#[from] ls::Error),

#[error(transparent)]
Rm(#[from] rm::Error),
#[error(transparent)]
Show(#[from] show::Error),
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
pub async fn run(&self) -> Result<(), Error> {
match self {
Cmd::Add(cmd) => cmd.run()?,
Cmd::Address(cmd) => cmd.run()?,
Cmd::Rm(cmd) => cmd.run()?,
Cmd::Fund(cmd) => cmd.run().await?,
Cmd::Generate(cmd) => cmd.run().await?,
Cmd::Ls(cmd) => cmd.run()?,
Cmd::Generate(cmd) => cmd.run()?,
Cmd::Rm(cmd) => cmd.run()?,
Cmd::Show(cmd) => cmd.run()?,
};
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub enum Error {
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
pub async fn run(&self) -> Result<(), Error> {
match &self {
Cmd::Identity(identity) => identity.run()?,
Cmd::Identity(identity) => identity.run().await?,
Cmd::Network(network) => network.run()?,
}
Ok(())
Expand Down
Loading

0 comments on commit a3eac81

Please sign in to comment.