Skip to content

Commit

Permalink
feat: add signer.rs and add sign method to config::Args
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Jun 6, 2024
1 parent 6ce6259 commit c15a197
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 3 deletions.
38 changes: 37 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ rust-embed = { version = "8.2.0", features = ["debug-embed"] }
bollard = { workspace=true }
futures-util = "0.3.30"
home = "0.5.9"
termion = "4.0.0"
# For hyper-tls
[target.'cfg(unix)'.dependencies]
openssl = { version = "=0.10.55", features = ["vendored"] }
Expand Down
37 changes: 36 additions & 1 deletion cmd/soroban-cli/src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ use std::path::PathBuf;
use clap::{arg, command};
use serde::{Deserialize, Serialize};

use crate::Pwd;
use soroban_rpc::Client;
use stellar_strkey::Strkey;

use crate::xdr::{MuxedAccount, SequenceNumber, Transaction, TransactionEnvelope, Uint256};
use crate::{
signer::{LocalKey, Stellar},
Pwd,
};

use self::{network::Network, secret::Secret};

Expand All @@ -23,6 +30,8 @@ pub enum Error {
Secret(#[from] secret::Error),
#[error(transparent)]
Config(#[from] locator::Error),
#[error(transparent)]
Rpc(#[from] soroban_rpc::Error),
}

#[derive(Debug, clap::Args, Clone, Default)]
Expand All @@ -49,6 +58,32 @@ impl Args {
Ok(key.key_pair(self.hd_path)?)
}


pub async fn sign_with_local_key(
&self,
tx: Transaction,
) -> Result<TransactionEnvelope, Error> {
let signer = LocalKey::new(self.key_pair()?, false);
self.sign(&signer, tx).await
}

pub async fn sign(
&self,
signer: &impl Stellar,
mut tx: Transaction,
) -> Result<TransactionEnvelope, Error> {
let key = signer.get_public_key().await.unwrap();
let account = Strkey::PublicKeyEd25519(key);
let network = self.get_network()?;
let client = Client::new(&network.rpc_url)?;
tx.seq_num = SequenceNumber(client.get_account(&account.to_string()).await?.seq_num.0 + 1);
tx.source_account = MuxedAccount::Ed25519(Uint256(key.0));
Ok(signer
.sign_txn(tx, &network.network_passphrase)
.await
.unwrap())
}

pub fn account(&self, account_str: &str) -> Result<Secret, Error> {
if let Ok(secret) = self.locator.read_identity(account_str) {
Ok(secret)
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl NetworkRunnable for Cmd {
) -> Result<TxnResult<String>, Error> {
let config = config.unwrap_or(&self.config);
let wasm_hash = if let Some(wasm) = &self.wasm {
let hash = if self.fee.build_only {
let hash = if self.fee.build_only || self.fee.sim_only {
wasm::Args { wasm: wasm.clone() }.hash()?
} else {
install::Cmd {
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod fee;
pub mod get_spec;
pub mod key;
pub mod log;
pub mod signer;
pub mod toid;
pub mod utils;
pub mod wasm;
Expand Down
Loading

0 comments on commit c15a197

Please sign in to comment.