From d023e37d7902d9b275f2a6c933bce838b5799683 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Wed, 22 May 2024 14:54:01 -0400 Subject: [PATCH] feat: add From and --- src/curr/mod.rs | 1 + src/curr/transaction_conversions.rs | 31 +++++++++++++++++++++++++++++ src/next/mod.rs | 1 + src/next/transaction_conversions.rs | 31 +++++++++++++++++++++++++++++ tests/tx_small.rs | 14 +++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 src/curr/transaction_conversions.rs create mode 100644 src/next/transaction_conversions.rs diff --git a/src/curr/mod.rs b/src/curr/mod.rs index ec2f1e37..cf84e11d 100644 --- a/src/curr/mod.rs +++ b/src/curr/mod.rs @@ -6,6 +6,7 @@ mod str; mod scval_conversions; pub use scval_conversions::*; +mod transaction_conversions; mod scval_validations; pub use scval_validations::*; diff --git a/src/curr/transaction_conversions.rs b/src/curr/transaction_conversions.rs new file mode 100644 index 00000000..5d014f17 --- /dev/null +++ b/src/curr/transaction_conversions.rs @@ -0,0 +1,31 @@ +use super::{FeeBumpTransaction, FeeBumpTransactionEnvelope, Transaction, TransactionEnvelope, TransactionV1Envelope, VecM}; + +impl From for TransactionEnvelope { + fn from(tx: Transaction) -> Self { + TransactionEnvelope::Tx(TransactionV1Envelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From for TransactionEnvelope { + fn from(tx: FeeBumpTransaction) -> Self { + TransactionEnvelope::TxFeeBump(FeeBumpTransactionEnvelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From<&FeeBumpTransaction> for TransactionEnvelope { + fn from(tx: &FeeBumpTransaction) -> Self { + tx.clone().into() + } +} + +impl From<&Transaction> for TransactionEnvelope { + fn from(tx: &Transaction) -> Self { + tx.clone().into() + } +} diff --git a/src/next/mod.rs b/src/next/mod.rs index ec2f1e37..cf84e11d 100644 --- a/src/next/mod.rs +++ b/src/next/mod.rs @@ -6,6 +6,7 @@ mod str; mod scval_conversions; pub use scval_conversions::*; +mod transaction_conversions; mod scval_validations; pub use scval_validations::*; diff --git a/src/next/transaction_conversions.rs b/src/next/transaction_conversions.rs new file mode 100644 index 00000000..5d014f17 --- /dev/null +++ b/src/next/transaction_conversions.rs @@ -0,0 +1,31 @@ +use super::{FeeBumpTransaction, FeeBumpTransactionEnvelope, Transaction, TransactionEnvelope, TransactionV1Envelope, VecM}; + +impl From for TransactionEnvelope { + fn from(tx: Transaction) -> Self { + TransactionEnvelope::Tx(TransactionV1Envelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From for TransactionEnvelope { + fn from(tx: FeeBumpTransaction) -> Self { + TransactionEnvelope::TxFeeBump(FeeBumpTransactionEnvelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From<&FeeBumpTransaction> for TransactionEnvelope { + fn from(tx: &FeeBumpTransaction) -> Self { + tx.clone().into() + } +} + +impl From<&Transaction> for TransactionEnvelope { + fn from(tx: &Transaction) -> Self { + tx.clone().into() + } +} diff --git a/tests/tx_small.rs b/tests/tx_small.rs index 9ef91bc0..cfdebd0a 100644 --- a/tests/tx_small.rs +++ b/tests/tx_small.rs @@ -76,6 +76,20 @@ fn test_build_small_tx_with_alloc() -> Result<(), Error> { }, signatures: [].try_into()?, }); + +#[cfg(feature = "alloc")] +#[test] +fn convert_reference_of_tx_to_unsigned_transaction_envelope() -> Result<(), Error> { + let tx = &Transaction { + source_account: MuxedAccount::Ed25519(Uint256([0; 32])), + fee: 0, + seq_num: SequenceNumber(1), + cond: Preconditions::None, + memo: Memo::Text("Stellar".as_bytes().try_into()?), + operations: [].to_vec().try_into()?, + ext: TransactionExt::V0, + }; + let _: TransactionEnvelope = tx.into(); Ok(()) }