Skip to content

Commit

Permalink
fix: simplify sign_with
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Sep 12, 2024
1 parent 58630c3 commit a0cc592
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SOURCE_ACCOUNT>`

###### **Options:**

* `--sign-with-key <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 <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 <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_URL>` — RPC server endpoint
* `--network-passphrase <NETWORK_PASSPHRASE>` — Network passphrase to sign the transaction sent to the rpc server
* `--network <NETWORK>` — Name of network to use from config
* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."
* `--source-account <SOURCE_ACCOUNT>` — Source account of the transaction. By default will be the account that signs the transaction



Expand Down
15 changes: 13 additions & 2 deletions cmd/soroban-cli/src/commands/tx/sign.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
config::sign_with,
config::{locator, network, sign_with},
xdr::{self, Limits, TransactionEnvelope, WriteXdr},
};

Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -30,6 +38,9 @@ impl Cmd {
}

pub async fn sign_tx_env(&self, tx: TransactionEnvelope) -> Result<TransactionEnvelope, Error> {
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?)
}
}
48 changes: 19 additions & 29 deletions cmd/soroban-cli/src/config/sign_with.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::PathBuf;

use crate::{
signer::{
self,
Expand Down Expand Up @@ -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<String>,
/// 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<Secret, Error> {
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<Secret, Error> {
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<TransactionEnvelope, Error> {
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
}

Expand All @@ -99,16 +91,14 @@ impl Args {
signer: &(impl Transaction + std::marker::Sync),
source_account: &PublicKey,
tx_env: TransactionEnvelope,
network: &Network,
) -> Result<TransactionEnvelope, Error> {
let network = self.get_network()?;
Ok(sign_txn_env(signer, source_account, tx_env, &network).await?)
}

pub fn get_network(&self) -> Result<Network, Error> {
Ok(self.network.get(&self.locator)?)
}

pub fn config_dir(&self) -> Result<PathBuf, Error> {
Ok(self.locator.config_dir()?)
pub fn source_account(&self, locator: &locator::Args) -> Result<PublicKey, Error> {
Ok(locator
.account(&self.source_account)?
.public_key(self.hd_path)?)
}
}

0 comments on commit a0cc592

Please sign in to comment.