Skip to content

Commit

Permalink
move StellarSigner and eliminate trait
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Sep 23, 2024
1 parent 9beed05 commit d3e22cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 50 deletions.
32 changes: 1 addition & 31 deletions cmd/soroban-cli/src/config/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ use std::{io::Write, str::FromStr};
use stellar_strkey::ed25519::{PrivateKey, PublicKey};

use crate::print::Print;
use crate::utils::transaction_hash;
use crate::xdr::{self, DecoratedSignature};
use crate::{
signer::{self, LocalKey},
signer::{self, LocalKey, SignerKind, StellarSigner},
utils,
};

use super::network::Network;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("invalid secret key")]
Expand Down Expand Up @@ -167,32 +163,6 @@ impl Secret {
}
}

pub struct StellarSigner {
kind: SignerKind,
printer: Print,
}

pub enum SignerKind {
Local(LocalKey),
}

#[async_trait::async_trait]
impl signer::SignTx for StellarSigner {
async fn sign_tx(
&self,
txn: &xdr::Transaction,
network: &Network,
) -> Result<DecoratedSignature, signer::types::Error> {
let tx_hash = transaction_hash(txn, &network.network_passphrase)?;
let hex_hash = hex::encode(tx_hash);
self.printer
.infoln(format!("Signing transaction with hash: {hex_hash}"));
match &self.kind {
SignerKind::Local(key) => key.sign_tx_hash(tx_hash),
}
}
}

fn read_password() -> Result<String, Error> {
std::io::stdout().flush().map_err(|_| Error::PasswordRead)?;
rpassword::read_password().map_err(|_| Error::PasswordRead)
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use soroban_env_host::xdr::{

pub mod types;
use crate::utils::transaction_hash;
pub use types::{LocalKey, SignTx};
pub use types::{LocalKey, SignerKind, StellarSigner};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down
47 changes: 29 additions & 18 deletions cmd/soroban-cli/src/signer/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use ed25519_dalek::ed25519::signature::Signer;

use crate::{
config::network::Network,
print::Print,
utils::transaction_hash,
xdr::{
self, DecoratedSignature, Signature, SignatureHint, TransactionEnvelope,
TransactionV1Envelope,
Expand All @@ -26,14 +28,39 @@ pub enum Error {
UnsupportedTransactionEnvelopeType,
}

pub struct StellarSigner {
pub kind: SignerKind,
pub printer: Print,
}

pub enum SignerKind {
Local(LocalKey),
}

impl StellarSigner {
pub fn sign_tx(
&self,
txn: &xdr::Transaction,
network: &Network,
) -> Result<DecoratedSignature, Error> {
let tx_hash = transaction_hash(txn, &network.network_passphrase)?;
let hex_hash = hex::encode(tx_hash);
self.printer
.infoln(format!("Signing transaction with hash: {hex_hash}"));
match &self.kind {
SignerKind::Local(key) => key.sign_tx_hash(tx_hash),
}
}
}

pub async fn sign_tx_env(
signer: &(impl SignTx + std::marker::Sync),
signer: &StellarSigner,
txn_env: TransactionEnvelope,
network: &Network,
) -> Result<TransactionEnvelope, Error> {
match txn_env {
TransactionEnvelope::Tx(TransactionV1Envelope { tx, signatures }) => {
let decorated_signature = signer.sign_tx(&tx, network).await?;
let decorated_signature = signer.sign_tx(&tx, network)?;
let mut sigs = signatures.to_vec();
sigs.push(decorated_signature);
Ok(TransactionEnvelope::Tx(TransactionV1Envelope {
Expand All @@ -45,22 +72,6 @@ pub async fn sign_tx_env(
}
}

/// A trait for signing Stellar transactions and Soroban authorization entries
#[async_trait::async_trait]
pub trait SignTx {
/// Sign a Stellar transaction with the given source account
/// This is a default implementation that signs the transaction hash and returns a decorated signature
///
/// Todo: support signing the transaction directly.
/// # Errors
/// Returns an error if the source account is not found
async fn sign_tx(
&self,
txn: &xdr::Transaction,
network: &Network,
) -> Result<DecoratedSignature, Error>;
}

pub struct LocalKey {
key: ed25519_dalek::SigningKey,
#[allow(dead_code)]
Expand Down

0 comments on commit d3e22cc

Please sign in to comment.