Skip to content

Commit

Permalink
feat: add From<Transaction> and From<&Transaction>
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed May 14, 2024
1 parent 204ccb8 commit a4796b4
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 71 deletions.
2 changes: 2 additions & 0 deletions src/curr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ mod str;

mod scval_conversions;
pub use scval_conversions::*;
mod transaction_conversions;


mod scval_validations;
pub use scval_validations::*;
Expand Down
14 changes: 14 additions & 0 deletions src/curr/transaction_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
impl From<super::Transaction> 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()
}
}
17 changes: 17 additions & 0 deletions src/next/transaction_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl From<super::Transaction> 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()
}
}
66 changes: 32 additions & 34 deletions tests/serde_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,39 @@ use stellar_xdr::{
#[cfg(feature = "curr")]
#[test]
fn test_serde_tx() -> Result<(), Box<dyn std::error::Error>> {
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!(
Expand Down
84 changes: 47 additions & 37 deletions tests/tx_small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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");
Expand Down Expand Up @@ -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(())
}

0 comments on commit a4796b4

Please sign in to comment.