From d3e22ccb2703b85657c42a4b0e66d44fe41654ee Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:42:30 +1000 Subject: [PATCH] move StellarSigner and eliminate trait --- cmd/soroban-cli/src/config/secret.rs | 32 +------------------ cmd/soroban-cli/src/signer.rs | 2 +- cmd/soroban-cli/src/signer/types.rs | 47 +++++++++++++++++----------- 3 files changed, 31 insertions(+), 50 deletions(-) diff --git a/cmd/soroban-cli/src/config/secret.rs b/cmd/soroban-cli/src/config/secret.rs index e2578c81b..6b76e7418 100644 --- a/cmd/soroban-cli/src/config/secret.rs +++ b/cmd/soroban-cli/src/config/secret.rs @@ -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")] @@ -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 { - 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 { std::io::stdout().flush().map_err(|_| Error::PasswordRead)?; rpassword::read_password().map_err(|_| Error::PasswordRead) diff --git a/cmd/soroban-cli/src/signer.rs b/cmd/soroban-cli/src/signer.rs index d38b8e29a..9097b4a5f 100644 --- a/cmd/soroban-cli/src/signer.rs +++ b/cmd/soroban-cli/src/signer.rs @@ -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 { diff --git a/cmd/soroban-cli/src/signer/types.rs b/cmd/soroban-cli/src/signer/types.rs index e44935b31..73bff030c 100644 --- a/cmd/soroban-cli/src/signer/types.rs +++ b/cmd/soroban-cli/src/signer/types.rs @@ -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, @@ -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 { + 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 { 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 { @@ -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; -} - pub struct LocalKey { key: ed25519_dalek::SigningKey, #[allow(dead_code)]