Skip to content

Commit

Permalink
feat: Allow passing an identity for an address (#604)
Browse files Browse the repository at this point in the history
* feat(CLI): allow using identities that are resolvable to an address

This allows using `--addr default` to look up the corresponding address.

* feat: add config.toml to warn about pedantic clippy and alias for md-gen

---------

Co-authored-by: Tsachi Herman <[email protected]>
Co-authored-by: Paul Bellamy <[email protected]>
  • Loading branch information
3 people authored Apr 26, 2023
1 parent ecee4dd commit f9d3312
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 10 deletions.
159 changes: 159 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# paths = ["/path/to/override"] # path dependency overrides

[alias] # command aliases
md-gen = "run --bin doc-gen --features clap-markdown"
# b = "build"
# c = "check"
# t = "test"
# r = "run"
# rr = "run --release"
# recursive_example = "rr --example recursions"
# space_example = ["run", "--release", "--", "\"command list\""]

[build]
# jobs = 1 # number of parallel jobs, defaults to # of CPUs
# rustc = "rustc" # the rust compiler tool
# rustc-wrapper = "…" # run this wrapper instead of `rustc`
# rustc-workspace-wrapper = "…" # run this wrapper instead of `rustc` for workspace members
# rustdoc = "rustdoc" # the doc generator tool
# target = "triple" # build for the target triple (ignored by `cargo install`)
# target-dir = "target" # path of where to place all generated artifacts
rustflags = [
"-Wclippy::pedantic",
"-Aclippy::needless_pass_by_value",
"-Aclippy::must_use_candidate",
"-Aclippy::missing_panics_doc",
"-Aclippy::missing_errors_doc",
# "-Aclippy::missing_safety_doc",
# "-Aclippy::inline_always",
# "-Aclippy::default_trait_access",
# "-Aclippy::module_name_repetitions",
# "-Aclippy::module_name_repetitions",
# "-Aclippy::too_many_lines",
# "-Aclippy::cast_possible_truncation",
# "-Aclippy::cast_sign_loss",
# "-Aclippy::cast_possible_wrap",
# "-Aclippy::similar_names",
# "-Aclippy::doc_markdown",
# "-Aclippy::struct_excessive_bools",
# "-Aclippy::cast_lossless",
# "-Aclippy::trivially_copy_pass_by_ref",
# "-Aclippy::wrong_self_convention",
# "-Aclippy::unused_self",
# "-Aclippy::enum_glob_use",
# "-Aclippy::return_self_not_must_use",
# "-Aclippy::map_entry",
# "-Aclippy::match_same_arms",
# "-Aclippy::iter_not_returning_iterator",
# "-Aclippy::unnecessary_wraps",
# "-Aclippy::type_complexity",
] # custom flags to pass to all compiler invocations
# rustdocflags = ["…", "…"] # custom flags to pass to rustdoc
# incremental = true # whether or not to enable incremental compilation
# dep-info-basedir = "…" # path for the base directory for targets in depfiles

# [doc]
# browser = "chromium" # browser to use with `cargo doc --open`,
# # overrides the `BROWSER` environment variable

# [env]
# # Set ENV_VAR_NAME=value for any process run by Cargo
# ENV_VAR_NAME = "value"
# # Set even if already present in environment
# ENV_VAR_NAME_2 = { value = "value", force = true }
# # Value is relative to .cargo directory containing `config.toml`, make absolute
# ENV_VAR_NAME_3 = { value = "relative/path", relative = true }

# [future-incompat-report]
# frequency = 'always' # when to display a notification about a future incompat report

# [cargo-new]
# vcs = "none" # VCS to use ('git', 'hg', 'pijul', 'fossil', 'none')

# [http]
# debug = false # HTTP debugging
# proxy = "host:port" # HTTP proxy in libcurl format
# ssl-version = "tlsv1.3" # TLS version to use
# ssl-version.max = "tlsv1.3" # maximum TLS version
# ssl-version.min = "tlsv1.1" # minimum TLS version
# timeout = 30 # timeout for each HTTP request, in seconds
# low-speed-limit = 10 # network timeout threshold (bytes/sec)
# cainfo = "cert.pem" # path to Certificate Authority (CA) bundle
# check-revoke = true # check for SSL certificate revocation
# multiplexing = true # HTTP/2 multiplexing
# user-agent = "…" # the user-agent header

# [install]
# root = "/some/path" # `cargo install` destination directory

# [net]
# retry = 2 # network retries
# git-fetch-with-cli = true # use the `git` executable for git operations
# offline = true # do not access the network

# [net.ssh]
# known-hosts = ["..."] # known SSH host keys

# [patch.<registry>]
# # Same keys as for [patch] in Cargo.toml

# [profile.<name>] # Modify profile settings via config.
# inherits = "dev" # Inherits settings from [profile.dev].
# opt-level = 0 # Optimization level.
# debug = true # Include debug info.
# split-debuginfo = '...' # Debug info splitting behavior.
# debug-assertions = true # Enables debug assertions.
# overflow-checks = true # Enables runtime integer overflow checks.
# lto = false # Sets link-time optimization.
# panic = 'unwind' # The panic strategy.
# incremental = true # Incremental compilation.
# codegen-units = 16 # Number of code generation units.
# rpath = false # Sets the rpath linking option.
# [profile.<name>.build-override] # Overrides build-script settings.
# # Same keys for a normal profile.
# [profile.<name>.package.<name>] # Override profile for a package.
# # Same keys for a normal profile (minus `panic`, `lto`, and `rpath`).

# [registries.<name>] # registries other than crates.io
# index = "…" # URL of the registry index
# token = "…" # authentication token for the registry

# [registry]
# default = "…" # name of the default registry
# token = "…" # authentication token for crates.io

# [source.<name>] # source definition and replacement
# replace-with = "…" # replace this source with the given named source
# directory = "…" # path to a directory source
# registry = "…" # URL to a registry source
# local-registry = "…" # path to a local registry source
# git = "…" # URL of a git repository source
# branch = "…" # branch name for the git repository
# tag = "…" # tag name for the git repository
# rev = "…" # revision for the git repository

# [target.<triple>]
# linker = "…" # linker to use
# runner = "…" # wrapper to run executables
# rustflags = ["…", "…"] # custom flags for `rustc`

# [target.<cfg>]
# runner = "…" # wrapper to run executables
# rustflags = ["…", "…"] # custom flags for `rustc`

# [target.<triple>.<links>] # `links` build script override
# rustc-link-lib = ["foo"]
# rustc-link-search = ["/path/to/foo"]
# rustc-flags = ["-L", "/some/path"]
# rustc-cfg = ['key="value"']
# rustc-env = {key = "value"}
# rustc-cdylib-link-arg = ["…"]
# metadata_key1 = "value"
# metadata_key2 = "value"

# [term]
# quiet = false # whether cargo output is quiet
# verbose = false # whether cargo provides verbose output
# color = 'auto' # whether cargo colorizes output
# progress.when = 'auto' # whether cargo shows progress bar
# progress.width = 80 # width of progress bar
4 changes: 2 additions & 2 deletions .github/workflows/full-help-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
# this looks goofy to get GITHUB_OUTPUT to work with multi-line return values;
# see https://stackoverflow.com/a/74266196/249801
run: |
cargo run --bin doc-gen --features clap-markdown
cargo md-gen
raw_diff=$(git diff docs/soroban-cli-full-docs.md)
if [ "$raw_diff" != "" ]; then echo ""; echo "Unexpected docs change:"; echo "======================="; echo ""; echo "$raw_diff"; echo ""; echo "======================="; echo ""; fi
echo diff=$raw_diff >> $GITHUB_OUTPUT
Expand All @@ -29,4 +29,4 @@ jobs:
uses: actions/github-script@v3
with:
script: |
core.setFailed('Expected `doc-gen` to generate no diffs, but got diff shown in previous step.\n\nUpdate the full help docs:\n\n cargo run --bin doc-gen --features clap-markdown\n\nDo this automatically on every commit by installing the pre-commit hook as explained in the README.')
core.setFailed('Expected `doc-gen` to generate no diffs, but got diff shown in previous step.\n\nUpdate the full help docs:\n\n cargo md-gen\n\nDo this automatically on every commit by installing the pre-commit hook as explained in the README.')
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ impl Contract {
vec![&env, !boolean]
}

pub fn auth(env: Env, addr: Address, world: Symbol) -> Vec<Symbol> {
pub fn auth(env: Env, addr: Address, world: Symbol) -> Address {
addr.require_auth();
// Emit test event
env.events().publish(("auth",), world.clone());
vec![&env, Symbol::short("Hello"), world]
env.events().publish(("auth",), world);
addr
}

#[allow(unused_variables)]
Expand Down
29 changes: 26 additions & 3 deletions cmd/crates/soroban-test/tests/it/invoke_sandbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use soroban_cli::commands::contract;
use soroban_cli::commands::{config::identity, contract};
use soroban_test::TestEnv;

use crate::util::{
Expand Down Expand Up @@ -137,7 +137,30 @@ fn invoke_auth() {
.arg(&format!("--addr={DEFAULT_PUB_KEY}"))
.arg("--world=world")
.assert()
.stdout("[\"Hello\",\"world\"]\n")
.stdout(format!("\"{DEFAULT_PUB_KEY}\"\n"))
.success();
}

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

sandbox
.cmd::<identity::generate::Cmd>("test -d ")
.run()
.unwrap();
sandbox
.new_assert_cmd("contract")
.arg("invoke")
.arg("--id=1")
.arg("--wasm")
.arg(HELLO_WORLD.path())
.arg("--")
.arg("auth")
.arg("--addr=test")
.arg("--world=world")
.assert()
.stdout(format!("\"{DEFAULT_PUB_KEY}\"\n"))
.success();
}

Expand All @@ -156,7 +179,7 @@ fn invoke_auth_with_different_test_account() {
.arg(&format!("--addr={DEFAULT_PUB_KEY_1}"))
.arg("--world=world")
.assert()
.stdout("[\"Hello\",\"world\"]\n")
.stdout(format!("\"{DEFAULT_PUB_KEY_1}\"\n"))
.success();
}

Expand Down
12 changes: 11 additions & 1 deletion cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ impl Cmd {
.map(|i| {
let name = i.name.to_string().unwrap();
if let Some(mut raw_val) = matches_.get_raw(&name) {
let s = raw_val.next().unwrap().to_string_lossy().to_string();
let mut s = raw_val.next().unwrap().to_string_lossy().to_string();
if matches!(i.type_, ScSpecTypeDef::Address) {
let cmd = crate::commands::config::identity::address::Cmd {
name: Some(s.clone()),
hd_path: Some(0),
locator: self.config.locator.clone(),
};
if let Ok(address) = cmd.public_key() {
s = address.to_string();
}
}
spec.from_string(&s, &i.type_)
.map_err(|error| Error::CannotParseArg { arg: name, error })
} else if matches!(i.type_, ScSpecTypeDef::Option(_)) {
Expand Down
4 changes: 3 additions & 1 deletion cmd/soroban-cli/src/strval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ impl Spec {
| ScType::Symbol
| ScType::Status
| ScType::Bytes
| ScType::Address
| ScType::Void
| ScType::Timepoint
| ScType::Duration
| ScType::U256
| ScType::I256
| ScType::String
| ScType::Bool => String::new(),
ScType::Address => String::from(
"Can be public key (G13..), a contract hash (6c45307) or an identity (alice), ",
),
ScType::Option(type_) => return self.doc(name, &type_.value_type),
ScType::Udt(ScSpecTypeUdt { name }) => {
let spec_type = self.find(&name.to_string_lossy())?;
Expand Down

0 comments on commit f9d3312

Please sign in to comment.