From a0cc592d1bbabd5f42728d9c41b65b94be1efaa8 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Thu, 12 Sep 2024 10:41:39 -0400 Subject: [PATCH] fix: simplify sign_with --- FULL_HELP_DOCS.md | 4 +-- cmd/soroban-cli/src/commands/tx/sign.rs | 15 ++++++-- cmd/soroban-cli/src/config/sign_with.rs | 48 ++++++++++--------------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/FULL_HELP_DOCS.md b/FULL_HELP_DOCS.md index 4a2821d59..ecd5c97cf 100644 --- a/FULL_HELP_DOCS.md +++ b/FULL_HELP_DOCS.md @@ -1331,19 +1331,19 @@ Calculate the hash of a transaction envelope from stdin Sign a transaction envolope appending the signature to the envelope -**Usage:** `stellar tx sign [OPTIONS]` +**Usage:** `stellar tx sign [OPTIONS] --source-account ` ###### **Options:** * `--sign-with-key ` — Sign with a local key. Can be an identity (--sign-with-key alice), a secret key (--sign-with-key SC36…), or a seed phrase (--sign-with-key "kite urban…"). If using seed phrase, `--hd-path` defaults to the `0` path * `--hd-path ` — If using a seed phrase to sign, sets which hierarchical deterministic path to use, e.g. `m/44'/148'/{hd_path}`. Example: `--hd-path 1`. Default: `0` * `--yes` — If one of `--sign-with-*` flags is provided, don't ask to confirm to sign a transaction +* `--source-account ` — Account that signs the transaction. Alias `source`. Can be an identity (--source alice), a secret key (--source SC36…), or a seed phrase (--source "kite urban…") * `--rpc-url ` — RPC server endpoint * `--network-passphrase ` — Network passphrase to sign the transaction sent to the rpc server * `--network ` — Name of network to use from config * `--global` — Use global config * `--config-dir ` — Location of config directory, default is "." -* `--source-account ` — Source account of the transaction. By default will be the account that signs the transaction diff --git a/cmd/soroban-cli/src/commands/tx/sign.rs b/cmd/soroban-cli/src/commands/tx/sign.rs index 3f0b90139..1047a91f7 100644 --- a/cmd/soroban-cli/src/commands/tx/sign.rs +++ b/cmd/soroban-cli/src/commands/tx/sign.rs @@ -1,5 +1,5 @@ use crate::{ - config::sign_with, + config::{locator, network, sign_with}, xdr::{self, Limits, TransactionEnvelope, WriteXdr}, }; @@ -8,6 +8,10 @@ pub enum Error { #[error(transparent)] XdrArgs(#[from] super::xdr::Error), #[error(transparent)] + Network(#[from] network::Error), + #[error(transparent)] + Locator(#[from] locator::Error), + #[error(transparent)] SignWith(#[from] sign_with::Error), #[error(transparent)] Xdr(#[from] xdr::Error), @@ -18,6 +22,10 @@ pub enum Error { pub struct Cmd { #[command(flatten)] pub sign_with: sign_with::Args, + #[command(flatten)] + pub network: network::Args, + #[command(flatten)] + pub locator: locator::Args, } impl Cmd { @@ -30,6 +38,9 @@ impl Cmd { } pub async fn sign_tx_env(&self, tx: TransactionEnvelope) -> Result { - Ok(self.sign_with.sign_txn_env(tx).await?) + Ok(self + .sign_with + .sign_txn_env(tx, &self.locator, &self.network.get(&self.locator)?) + .await?) } } diff --git a/cmd/soroban-cli/src/config/sign_with.rs b/cmd/soroban-cli/src/config/sign_with.rs index 355739c92..98338d493 100644 --- a/cmd/soroban-cli/src/config/sign_with.rs +++ b/cmd/soroban-cli/src/config/sign_with.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use crate::{ signer::{ self, @@ -61,36 +59,30 @@ pub struct Args { #[arg(long)] pub yes: bool, - #[command(flatten)] - pub network: network::Args, - - #[command(flatten)] - pub locator: locator::Args, - - /// Source account of the transaction. By default will be the account that signs the transaction. - #[arg(long, visible_alias = "source")] - pub source_account: Option, + /// Account that signs the transaction. Alias `source`. Can be an identity (--source alice), a secret key (--source SC36…), or a seed phrase (--source "kite urban…"). + #[arg(long, visible_alias = "source", env = "STELLAR_ACCOUNT")] + pub source_account: String, } impl Args { - pub fn secret(&self) -> Result { - let account = self.sign_with_key.as_deref().ok_or(Error::NoSignWithKey)?; - Ok(self.locator.account(account)?) + pub fn secret(&self, locator: &locator::Args) -> Result { + let account = self + .sign_with_key + .as_deref() + .unwrap_or(&self.source_account); + Ok(locator.account(account)?) } pub async fn sign_txn_env( &self, tx: TransactionEnvelope, + locator: &locator::Args, + network: &Network, ) -> Result { - let secret = self.secret()?; + let secret = self.secret(locator)?; let signer = secret.signer(self.hd_path, !self.yes)?; - let source_account = if let Some(source_account) = self.source_account.as_deref() { - stellar_strkey::ed25519::PublicKey::from_string(source_account)? - } else { - secret.public_key(self.hd_path)? - }; - - self.sign_tx_env_with_signer(&signer, &source_account, tx) + let source_account = self.source_account(locator)?; + self.sign_tx_env_with_signer(&signer, &source_account, tx, network) .await } @@ -99,16 +91,14 @@ impl Args { signer: &(impl Transaction + std::marker::Sync), source_account: &PublicKey, tx_env: TransactionEnvelope, + network: &Network, ) -> Result { - let network = self.get_network()?; Ok(sign_txn_env(signer, source_account, tx_env, &network).await?) } - pub fn get_network(&self) -> Result { - Ok(self.network.get(&self.locator)?) - } - - pub fn config_dir(&self) -> Result { - Ok(self.locator.config_dir()?) + pub fn source_account(&self, locator: &locator::Args) -> Result { + Ok(locator + .account(&self.source_account)? + .public_key(self.hd_path)?) } }