diff --git a/src/curr/mod.rs b/src/curr/mod.rs index 288c1e2e..ed8feaf0 100644 --- a/src/curr/mod.rs +++ b/src/curr/mod.rs @@ -5,6 +5,8 @@ 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..884089cb --- /dev/null +++ b/src/curr/transaction_conversions.rs @@ -0,0 +1,14 @@ +impl From for super::TransactionEnvelope { + fn from(tx: super::Transaction) -> Self { + super::TransactionEnvelope::Tx(super::TransactionV1Envelope { + tx, + signatures: super::VecM::default(), + }) + } +} + +impl From<&super::Transaction> for super::TransactionEnvelope { + fn from(tx: &super::Transaction) -> Self { + tx.clone().into() + } +} diff --git a/src/next/transaction_conversions.rs b/src/next/transaction_conversions.rs new file mode 100644 index 00000000..71f2ad01 --- /dev/null +++ b/src/next/transaction_conversions.rs @@ -0,0 +1,17 @@ +impl From for super::TransactionEnvelope { + + fn from(tx: super::Transaction) -> Self { + super::TransactionEnvelope::Tx( + super::TransactionV1Envelope { + tx, + signatures: super::VecM::default(), }, + ) + } +} + +impl From<&super::Transaction> for super::TransactionEnvelope { + + fn from(tx: &super::Transaction) -> Self { + tx.clone().into() + } +} diff --git a/tests/serde_tx.rs b/tests/serde_tx.rs index 1db87f1a..7e05622a 100644 --- a/tests/serde_tx.rs +++ b/tests/serde_tx.rs @@ -12,41 +12,39 @@ use stellar_xdr::{ #[cfg(feature = "curr")] #[test] fn test_serde_tx() -> Result<(), Box> { - let te = TransactionEnvelope::Tx(TransactionV1Envelope { - tx: Transaction { - source_account: MuxedAccount::Ed25519(Uint256([ - 0x3c, 0xb3, 0x61, 0xab, 0x62, 0x4b, 0x10, 0x70, 0x4c, 0x6c, 0xcf, 0x4f, 0xdb, 0x1e, - 0x40, 0x79, 0xd2, 0x3d, 0x68, 0xec, 0x2c, 0xd3, 0x22, 0xc2, 0x28, 0x34, 0xc4, 0x1a, - 0xe1, 0xe6, 0x4b, 0xd3, - ])), - fee: 0, - seq_num: SequenceNumber(1), - cond: Preconditions::None, - memo: Memo::Text("Stellar".as_bytes().try_into()?), - operations: [Operation { - source_account: Some(MuxedAccount::Ed25519(Uint256([ - 0x9b, 0x9f, 0xfa, 0xba, 0xcf, 0x46, 0x65, 0xb3, 0x57, 0x29, 0x76, 0xfb, 0x85, - 0x09, 0x79, 0xcb, 0xc7, 0x6b, 0x9d, 0x67, 0x9c, 0x6b, 0xca, 0xeb, 0xd5, 0x9b, - 0xbf, 0xb3, 0x43, 0xe8, 0xe9, 0x46, - ]))), - body: OperationBody::ChangeTrust(ChangeTrustOp { - line: ChangeTrustAsset::CreditAlphanum4(AlphaNum4 { - asset_code: AssetCode4(*b"ABCD"), - issuer: AccountId(stellar_xdr::PublicKey::PublicKeyTypeEd25519(Uint256([ - 0x43, 0xd0, 0x9f, 0x49, 0x2a, 0x2a, 0xe3, 0xaa, 0x0a, 0xed, 0x8e, 0xce, - 0xdc, 0xb2, 0x26, 0xa4, 0xf7, 0x50, 0xa9, 0x0e, 0xcb, 0x4e, 0x09, 0xf9, - 0xac, 0x76, 0x4a, 0x55, 0x37, 0xca, 0xd8, 0x77, - ]))), - }), - limit: i64::MAX, + let te: TransactionEnvelope = Transaction { + source_account: MuxedAccount::Ed25519(Uint256([ + 0x3c, 0xb3, 0x61, 0xab, 0x62, 0x4b, 0x10, 0x70, 0x4c, 0x6c, 0xcf, 0x4f, 0xdb, 0x1e, + 0x40, 0x79, 0xd2, 0x3d, 0x68, 0xec, 0x2c, 0xd3, 0x22, 0xc2, 0x28, 0x34, 0xc4, 0x1a, + 0xe1, 0xe6, 0x4b, 0xd3, + ])), + fee: 0, + seq_num: SequenceNumber(1), + cond: Preconditions::None, + memo: Memo::Text("Stellar".as_bytes().try_into()?), + operations: [Operation { + source_account: Some(MuxedAccount::Ed25519(Uint256([ + 0x9b, 0x9f, 0xfa, 0xba, 0xcf, 0x46, 0x65, 0xb3, 0x57, 0x29, 0x76, 0xfb, 0x85, 0x09, + 0x79, 0xcb, 0xc7, 0x6b, 0x9d, 0x67, 0x9c, 0x6b, 0xca, 0xeb, 0xd5, 0x9b, 0xbf, 0xb3, + 0x43, 0xe8, 0xe9, 0x46, + ]))), + body: OperationBody::ChangeTrust(ChangeTrustOp { + line: ChangeTrustAsset::CreditAlphanum4(AlphaNum4 { + asset_code: AssetCode4(*b"ABCD"), + issuer: AccountId(stellar_xdr::PublicKey::PublicKeyTypeEd25519(Uint256([ + 0x43, 0xd0, 0x9f, 0x49, 0x2a, 0x2a, 0xe3, 0xaa, 0x0a, 0xed, 0x8e, 0xce, + 0xdc, 0xb2, 0x26, 0xa4, 0xf7, 0x50, 0xa9, 0x0e, 0xcb, 0x4e, 0x09, 0xf9, + 0xac, 0x76, 0x4a, 0x55, 0x37, 0xca, 0xd8, 0x77, + ]))), }), - }] - .to_vec() - .try_into()?, - ext: TransactionExt::V0, - }, - signatures: [].try_into()?, - }); + limit: i64::MAX, + }), + }] + .to_vec() + .try_into()?, + ext: TransactionExt::V0, + } + .into(); let s = serde_json::to_string_pretty(&te)?; println!("{s}"); assert_eq!( diff --git a/tests/tx_small.rs b/tests/tx_small.rs index 9ef91bc0..0300b269 100644 --- a/tests/tx_small.rs +++ b/tests/tx_small.rs @@ -10,7 +10,7 @@ use stellar_xdr::next as stellar_xdr; use stellar_xdr::{ Error, Memo, MuxedAccount, Preconditions, SequenceNumber, Transaction, TransactionEnvelope, - TransactionExt, TransactionV1Envelope, Uint256, + TransactionExt, Uint256, }; #[cfg(all(feature = "std", feature = "base64"))] use stellar_xdr::{Limits, Type, TypeVariant}; @@ -22,18 +22,16 @@ fn test_build_small_tx_with_std() -> Result<(), Error> { use stellar_xdr::WriteXdr; - let te = TransactionEnvelope::Tx(TransactionV1Envelope { - 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, - }, - signatures: [].try_into()?, - }); + let te: TransactionEnvelope = 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, + } + .into(); let xdr = te.to_xdr_base64(Limits::none())?; assert_eq!(xdr, "AAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAABAAAAB1N0ZWxsYXIAAAAAAAAAAAAAAAAA"); @@ -64,35 +62,47 @@ fn test_build_small_tx_with_std() -> Result<(), Error> { #[cfg(feature = "alloc")] #[test] fn test_build_small_tx_with_alloc() -> Result<(), Error> { - let _tx = TransactionEnvelope::Tx(TransactionV1Envelope { - 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, - }, - signatures: [].try_into()?, - }); + let _: TransactionEnvelope = 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, + } + .into(); + Ok(()) +} + +#[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(()) } #[cfg(not(feature = "alloc"))] #[test] fn test_build_small_tx_with_alloc() -> Result<(), Error> { - let _tx = TransactionEnvelope::Tx(TransactionV1Envelope { - 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: (&[]).try_into()?, - ext: TransactionExt::V0, - }, - signatures: (&[]).try_into()?, - }); + let _: TransactionEnvelope = 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: (&[]).try_into()?, + ext: TransactionExt::V0, + } + .into(); Ok(()) }